throw new IllegalArgumentException("Unrecognized dataSource type: "
+ params.get("dataSource"));
}
ClassifierContext classifier = new ClassifierContext(algorithm, datastore);
classifier.initialize();
ResultAnalyzer resultAnalyzer = new ResultAnalyzer(classifier.getLabels(),
params.get("defaultCat"));
final TimingStatistics totalStatistics = new TimingStatistics();
if (subdirs != null) {
for (File file : subdirs) {
log.info("--------------");
log.info("Testing: " + file);
String correctLabel = file.getName().split(".txt")[0];
final TimingStatistics operationStats = new TimingStatistics();
long lineNum = 0;
for (String line : new FileLineIterable(new File(file.getPath()),
Charset.forName(params.get("encoding")), false)) {
Map<String, List<String>> document = new NGrams(line, Integer
.parseInt(params.get("gramSize"))).generateNGrams();
for (Map.Entry<String, List<String>> stringListEntry : document
.entrySet()) {
List<String> strings = stringListEntry.getValue();
TimingStatistics.Call call = operationStats.newCall();
TimingStatistics.Call outercall = totalStatistics.newCall();
ClassifierResult classifiedLabel = classifier.classifyDocument(
strings.toArray(new String[strings.size()]), params
.get("defaultCat"));
call.end();
outercall.end();
boolean correct = resultAnalyzer.addInstance(correctLabel,
classifiedLabel);
if (verbose) {
// We have one document per line
log.info("Line Number: " + lineNum + " Line(30): "
+ (line.length() > 30 ? line.substring(0, 30) : line)
+ " Expected Label: " + correctLabel + " Classified Label: "
+ classifiedLabel.getLabel() + " Correct: " + correct);
}
// log.info("{} {}", correctLabel, classifiedLabel);
}
lineNum++;
}
log.info("{}\t{}\t{}/{}", new Object[] { correctLabel,
resultAnalyzer.getConfusionMatrix().getAccuracy(correctLabel),
resultAnalyzer.getConfusionMatrix().getCorrect(correctLabel),
resultAnalyzer.getConfusionMatrix().getTotal(correctLabel) });
log.info("{}", operationStats.toString());
}
}
log.info("{}", totalStatistics.toString());
log.info(resultAnalyzer.summarize());
}