Search in sources :

Example 1 with DetectDominantLanguageResult

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();
}
Also used : DominantLanguage(com.amazonaws.services.comprehend.model.DominantLanguage) DetectDominantLanguageResult(com.amazonaws.services.comprehend.model.DetectDominantLanguageResult) AmazonClientException(com.amazonaws.AmazonClientException) DetectDominantLanguageRequest(com.amazonaws.services.comprehend.model.DetectDominantLanguageRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException) LanguageType(com.amplifyframework.predictions.models.LanguageType)

Aggregations

AmazonClientException (com.amazonaws.AmazonClientException)1 DetectDominantLanguageRequest (com.amazonaws.services.comprehend.model.DetectDominantLanguageRequest)1 DetectDominantLanguageResult (com.amazonaws.services.comprehend.model.DetectDominantLanguageResult)1 DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)1 PredictionsException (com.amplifyframework.predictions.PredictionsException)1 LanguageType (com.amplifyframework.predictions.models.LanguageType)1