@Test public void processSample() throws Exception { LanguageDetectorModel model = LanguageDetectorMETest.trainModel(); LanguageDetectorME langdetector = new LanguageDetectorME(model); final AtomicInteger correctCount = new AtomicInteger(); final AtomicInteger incorrectCount = new AtomicInteger(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); LanguageDetectorEvaluator evaluator = new LanguageDetectorEvaluator(langdetector, new LanguageDetectorEvaluationMonitor() { @Override public void correctlyClassified(LanguageSample reference, LanguageSample prediction) { correctCount.incrementAndGet(); } @Override public void missclassified(LanguageSample reference, LanguageSample prediction) { incorrectCount.incrementAndGet(); } }, new LanguageDetectorEvaluationErrorListener(outputStream)); evaluator.evaluateSample(new LanguageSample(new Language("pob"), "escreve e faz palestras pelo mundo inteiro sobre anjos")); evaluator.evaluateSample(new LanguageSample(new Language("fra"), "escreve e faz palestras pelo mundo inteiro sobre anjos")); evaluator.evaluateSample(new LanguageSample(new Language("fra"), "escreve e faz palestras pelo mundo inteiro sobre anjos")); Assert.assertEquals(1, correctCount.get()); Assert.assertEquals(2, incorrectCount.get()); Assert.assertEquals(3, evaluator.getDocumentCount()); Assert.assertEquals(0.33, evaluator.getAccuracy(), 0.01); String report = outputStream.toString(StandardCharsets.UTF_8.name()); Assert.assertEquals("Expected\tPredicted\tContext" + System.lineSeparator() + "fra\tpob\tescreve e faz palestras pelo mundo inteiro sobre anjos" + System.lineSeparator() + "fra\tpob\tescreve e faz palestras pelo mundo inteiro sobre anjos" + System.lineSeparator(), report); }
public LanguageSample processSample(LanguageSample sample) { CharSequence document = sample.getContext(); Language predicted = languageDetector.predictLanguage(document); if (sample.getLanguage().getLang().equals(predicted.getLang())) { accuracy.add(1); } else { accuracy.add(0); } return new LanguageSample(predicted, sample.getContext()); }
LanguageDetectorEvaluator extends Evaluator<LanguageSample> { public LanguageSample processSample(LanguageSample sample) { CharSequence document = sample.getContext(); Language predicted = languageDetector.predictLanguage(document); if (sample.getLanguage().getLang().equals(predicted.getLang())) { accuracy.add(1); } else { accuracy.add(0); } return new LanguageSample(predicted, sample.getContext()); } }
LanguageDetectorEvaluator extends Evaluator<LanguageSample> { public LanguageSample processSample(LanguageSample sample) { CharSequence document = sample.getContext(); Language predicted = languageDetector.predictLanguage(document); if (sample.getLanguage().getLang().equals(predicted.getLang())) { accuracy.add(1); } else { accuracy.add(0); } return new LanguageSample(predicted, sample.getContext()); } LanguageDetectorEvaluator(LanguageDetector langDetect,
                                   LanguageDetectorEvaluationMonitor ... listeners); }
LanguageDetectorEvaluator extends Evaluator<LanguageSample> { public LanguageSample processSample(LanguageSample sample) { CharSequence document = sample.getContext(); Language predicted = languageDetector.predictLanguage(document); if (sample.getLanguage().getLang().equals(predicted.getLang())) { accuracy.add(1); } else { accuracy.add(0); } return new LanguageSample(predicted, sample.getContext()); } LanguageDetectorEvaluator(LanguageDetector langDetect,
                                   LanguageDetectorEvaluationMonitor ... listeners); LanguageSample processSample(LanguageSample sample); double getAccuracy(); long getDocumentCount(); @Override String toString(); }
LanguageDetectorEvaluator extends Evaluator<LanguageSample> { public LanguageSample processSample(LanguageSample sample) { CharSequence document = sample.getContext(); Language predicted = languageDetector.predictLanguage(document); if (sample.getLanguage().getLang().equals(predicted.getLang())) { accuracy.add(1); } else { accuracy.add(0); } return new LanguageSample(predicted, sample.getContext()); } LanguageDetectorEvaluator(LanguageDetector langDetect,
                                   LanguageDetectorEvaluationMonitor ... listeners); LanguageSample processSample(LanguageSample sample); double getAccuracy(); long getDocumentCount(); @Override String toString();  }