use of com.amazonaws.services.comprehend.model.DetectDominantLanguageResult in project amplify-android by aws-amplify.
the class AWSComprehendService method fetchPredominantLanguage.
private Language fetchPredominantLanguage(String text) throws PredictionsException {
// Language is a required field for other detections.
// Always fetch language regardless of what configuration says.
isResourceConfigured(InterpretTextConfiguration.InterpretType.LANGUAGE);
DetectDominantLanguageRequest request = new DetectDominantLanguageRequest().withText(text);
// Detect dominant language from given text via AWS Comprehend
final DetectDominantLanguageResult result;
try {
result = comprehend.detectDominantLanguage(request);
} catch (AmazonClientException serviceException) {
throw new PredictionsException("AWS Comprehend encountered an error while detecting dominant language.", serviceException, "See attached service exception for more details.");
}
// Find the most dominant language from the list
DominantLanguage dominantLanguage = null;
for (DominantLanguage language : result.getLanguages()) {
if (dominantLanguage == null || language.getScore() > dominantLanguage.getScore()) {
dominantLanguage = language;
}
}
// Confirm that there was at least one detected language
if (dominantLanguage == null) {
throw new PredictionsException("AWS Comprehend did not detect any dominant language.", "Please verify the integrity of text being analyzed.");
}
String languageCode = dominantLanguage.getLanguageCode();
LanguageType language = LanguageType.from(languageCode);
Float score = dominantLanguage.getScore();
return Language.builder().value(language).confidence(score * PERCENT).build();
}
Aggregations