in opennlp-tools/src/main/java/opennlp/tools/cmdline/langdetect/LanguageDetectorCrossValidatorTool.java [62:127]
public void run(String format, String[] args) {
super.run(format, args);
mlParams = CmdLineUtil.loadTrainingParameters(params.getParams(), false);
if (mlParams == null) {
mlParams = ModelUtil.createDefaultTrainingParameters();
}
List<EvaluationMonitor<LanguageSample>> listeners = new LinkedList<>();
if (params.getMisclassified()) {
listeners.add(new LanguageDetectorEvaluationErrorListener());
}
LanguageDetectorFineGrainedReportListener reportListener = null;
File reportFile = params.getReportOutputFile();
OutputStream reportOutputStream = null;
if (reportFile != null) {
CmdLineUtil.checkOutputFile("Report Output File", reportFile);
try {
reportOutputStream = new FileOutputStream(reportFile);
reportListener = new LanguageDetectorFineGrainedReportListener(reportOutputStream);
listeners.add(reportListener);
} catch (FileNotFoundException e) {
throw createTerminationIOException(e);
}
}
LanguageDetectorEvaluationMonitor[] listenersArr = listeners
.toArray(new LanguageDetectorEvaluationMonitor[0]);
LanguageDetectorCrossValidator validator;
try {
LanguageDetectorFactory factory = LanguageDetectorFactory.create(params.getFactory());
validator = new LanguageDetectorCrossValidator(mlParams,
factory, listenersArr);
validator.evaluate(sampleStream, params.getFolds());
} catch (IOException e) {
throw new TerminateToolException(-1,
"IO error while reading training data or indexing data: " + e.getMessage(), e);
} finally {
try {
sampleStream.close();
} catch (IOException e) {
// sorry that this can fail
}
}
logger.info("done");
if (reportListener != null) {
logger.info("Writing fine-grained report to {}",
params.getReportOutputFile().getAbsolutePath());
reportListener.writeReport();
try {
reportOutputStream.flush();
reportOutputStream.close();
} catch (IOException e) {
// nothing to do
}
}
logger.info("Accuracy: {} Number of documents: {}",
validator.getDocumentAccuracy(), validator.getDocumentCount());
}