-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubcommandClassify.java
More file actions
62 lines (53 loc) · 1.86 KB
/
Copy pathSubcommandClassify.java
File metadata and controls
62 lines (53 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package de.uni_passau.fim.se2.sa.readability.subcommands;
import de.uni_passau.fim.se2.sa.readability.utils.Classify;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.Spec;
import weka.classifiers.Evaluation;
import weka.core.Instances;
import java.io.File;
import java.util.concurrent.Callable;
@Command(
name = "classify",
description = "Classify readability of .jsnp Java snippets"
)
public class SubcommandClassify implements Callable<Integer> {
@Spec
CommandSpec spec;
private File data;
@Option(
names = {"-d", "--data"},
description = "The data .csv file to train the model on.",
required = true
)
public void setDataFile(File dataFile) {
if (!dataFile.exists() || !dataFile.isFile()) {
throw new ParameterException(spec.commandLine(), "The data file does not exist or is not a file.");
}
data = dataFile;
}
public Integer call() {
try {
Instances dataset = Classify.loadDataset(data);
Evaluation eval = Classify.trainAndEvaluate(dataset);
printResults(eval);
return 0;
} catch (Exception e) {
System.out.println(e.getMessage());
return 1;
}
}
/**
* Prints the results of the classification evaluation.
*
* @param eval hosts the classification results.
*/
private static void printResults(final Evaluation eval) {
System.out.println(eval.toSummaryString());
System.out.printf("%-20s%.2f%n", "Accuracy", eval.pctCorrect());
System.out.printf("%-20s%.2f%n", "Area Under ROC", eval.areaUnderROC(0));
System.out.printf("%-20s%.2f%n", "F-Score", eval.fMeasure(0));
}
}