How it works…

The detect method returned a list of Detection instances, each representing information about each entry in the text list:

List<Detection> detectionList = translate.detect(textList);

We used the listSupportedLanguages method, described in the Discovering supported languages using the Google API recipe, to make it possible to display the language name:

List<Language> supportedLanguages = translate.listSupportedLanguages();

The nested for loops were used in conjunction with the count variable to display the results. The Language class's getCode method and the Detection class's getLanguage both returned a two-character code for a language. When the if statement is true, we used the Language class's getName method to return a more friendly form of the language code. The count variable was used to obtain the original text. The Detection class's getConfidence method returned the likelihood that the correct language was identified.

For these two text samples, we obtained good results, as shown:

int count = 0;
for (Detection detection : detectionList) {
for (Language language : supportedLanguages) {
if (language.getCode().equals(detection.getLanguage())) {
System.out.printf(
"Text: "%-16s" Code: %2s Language: %-12s Confidence: %5.3f ",
textList.get(count).substring(0, Math.min(16,
textList.get(count).length())),
language.getCode(), language.getName(), detection.getConfidence());
count++;
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.159.164