LanguageDetectionService { public LanguageDetectionResult detect(String text, String expectedBcp47Tag) throws UnsupportedLanguageException { LanguageDetectionResult languageDetectionResult = new LanguageDetectionResult(); try { String detectorLanguageForBcp47Tag = getDetectorLanguageForBcp47Tag(expectedBcp47Tag); Detector detector = getDetectorForLanguage(detectorLanguageForBcp47Tag); detector.append(text); String detect = detector.detect(); ArrayList<Language> probabilities = detector.getProbabilities(); for (Language probability : probabilities) { if (probability.lang.equals(detectorLanguageForBcp47Tag)) { languageDetectionResult.setProbabilityExpected(probability.prob); } } languageDetectionResult.setProbability(detector.getProbabilities().get(0).prob); languageDetectionResult.setDetected(detect); languageDetectionResult.setExpected(detectorLanguageForBcp47Tag); languageDetectionResult.setDetector(detector); } catch (LangDetectException lde) { logger.error("language detection failed\ntext: {}", text); languageDetectionResult.setLangDetectException(lde); } return languageDetectionResult; } LanguageDetectionService(); List<String> getSupportedLanguages(); boolean isSupportedBcp47Tag(String bcp47Tag); LanguageDetectionResult detect(String text, String expectedBcp47Tag);  }
@Test public void testDetection() throws LangDetectException, IOException,UnsupportedLanguageException { int nbFailed = 0; int nbBad = 0; double probabilitiesSum = 0; int sourceEqualsTarget = 0; int nbGood = 0; List<TextUnitDTO> translationsForLanguage = getTranslationsForLanguage("ko-KR", null); for (TextUnitDTO transaltionForLanguage : translationsForLanguage) { if(transaltionForLanguage.getTarget().equals(transaltionForLanguage.getSource())) { logger.debug("Skip source = target: {}", transaltionForLanguage.getSource()); sourceEqualsTarget++; continue; } LanguageDetectionResult ldr = languageDetectionService.detect(transaltionForLanguage.getTarget(), transaltionForLanguage.getTargetLocale()); probabilitiesSum += ldr.getProbability(); if (ldr.getLangDetectException() != null) { nbFailed++; logger.info("Language detection failed for: {}", transaltionForLanguage.getTarget()); logger.info("Error was", ldr.getLangDetectException()); continue; } if (!ldr.isExpectedLanguage()) { nbBad++; logger.info("Not proper language, found: {} probability: {}, should be {}/{} ({}), text: {}, {}", ldr.getDetected(), ldr.getProbability(), ldr.getExpected(), transaltionForLanguage.getTargetLocale(), ldr.getProbabilityExpected(), transaltionForLanguage.getTarget(), ldr.getDetector() != null ? ldr.getDetector().getProbabilities() : "-"); } else { nbGood++; } } int nbDetection = translationsForLanguage.size(); logger.info("detection: {}, source = target: {}, good: {}, bad: {}, failed: {}, average quality: {}, % good: {}", nbDetection, sourceEqualsTarget, nbGood, nbBad, nbFailed, probabilitiesSum / (double) nbDetection, nbGood / (double) nbDetection); }