Search in sources :

Example 1 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSComprehendService method fetchSentiment.

private Sentiment fetchSentiment(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT sentiment
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.SENTIMENT)) {
        return null;
    }
    DetectSentimentRequest request = new DetectSentimentRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect sentiment from given text via AWS Comprehend
    final DetectSentimentResult result;
    try {
        result = comprehend.detectSentiment(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting sentiment.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    String comprehendSentiment = result.getSentiment();
    SentimentScore sentimentScore = result.getSentimentScore();
    SentimentType predominantSentiment = SentimentTypeAdapter.fromComprehend(comprehendSentiment);
    final float score;
    switch(predominantSentiment) {
        case POSITIVE:
            score = sentimentScore.getPositive();
            break;
        case NEGATIVE:
            score = sentimentScore.getNegative();
            break;
        case NEUTRAL:
            score = sentimentScore.getNeutral();
            break;
        case MIXED:
            score = sentimentScore.getMixed();
            break;
        default:
            score = 0f;
    }
    return Sentiment.builder().value(predominantSentiment).confidence(score * PERCENT).build();
}
Also used : DetectSentimentResult(com.amazonaws.services.comprehend.model.DetectSentimentResult) DetectSentimentRequest(com.amazonaws.services.comprehend.model.DetectSentimentRequest) AmazonClientException(com.amazonaws.AmazonClientException) PredictionsException(com.amplifyframework.predictions.PredictionsException) SentimentScore(com.amazonaws.services.comprehend.model.SentimentScore) SentimentType(com.amplifyframework.predictions.models.SentimentType)

Example 2 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSComprehendService method fetchEntities.

private List<Entity> fetchEntities(String text, LanguageType language) throws PredictionsException {
    // Skip if configuration specifies NOT entities
    if (!isResourceConfigured(InterpretTextConfiguration.InterpretType.ENTITIES)) {
        return null;
    }
    DetectEntitiesRequest request = new DetectEntitiesRequest().withText(text).withLanguageCode(language.getLanguageCode());
    // Detect entities from given text via AWS Comprehend
    final DetectEntitiesResult result;
    try {
        result = comprehend.detectEntities(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("AWS Comprehend encountered an error while detecting entities.", serviceException, "See attached service exception for more details.");
    }
    // Convert AWS Comprehend's detection result to Amplify-compatible format
    List<Entity> entities = new ArrayList<>();
    for (com.amazonaws.services.comprehend.model.Entity comprehendEntity : result.getEntities()) {
        EntityType entityType = EntityTypeAdapter.fromComprehend(comprehendEntity.getType());
        Entity amplifyEntity = Entity.builder().value(entityType).confidence(comprehendEntity.getScore() * PERCENT).targetText(comprehendEntity.getText()).startIndex(comprehendEntity.getBeginOffset()).build();
        entities.add(amplifyEntity);
    }
    return entities;
}
Also used : EntityType(com.amplifyframework.predictions.models.EntityType) Entity(com.amplifyframework.predictions.models.Entity) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) DetectEntitiesResult(com.amazonaws.services.comprehend.model.DetectEntitiesResult) DetectEntitiesRequest(com.amazonaws.services.comprehend.model.DetectEntitiesRequest) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 3 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException 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)

Example 4 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSPollyService method synthesizeSpeech.

void synthesizeSpeech(@NonNull String text, @NonNull AWSVoiceType voiceType, @NonNull Consumer<TextToSpeechResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
    try {
        InputStream data = synthesizeSpeech(text, voiceType);
        onSuccess.accept(TextToSpeechResult.fromAudioData(data));
    } catch (PredictionsException exception) {
        onError.accept(exception);
    }
}
Also used : InputStream(java.io.InputStream) PredictionsException(com.amplifyframework.predictions.PredictionsException)

Example 5 with PredictionsException

use of com.amplifyframework.predictions.PredictionsException in project amplify-android by aws-amplify.

the class AWSRekognitionService method detectModerationLabels.

private List<Label> detectModerationLabels(ByteBuffer imageData) throws PredictionsException {
    DetectModerationLabelsRequest request = new DetectModerationLabelsRequest().withImage(new Image().withBytes(imageData));
    // Detect moderation labels in the given image via Amazon Rekognition
    final DetectModerationLabelsResult result;
    try {
        result = rekognition.detectModerationLabels(request);
    } catch (AmazonClientException serviceException) {
        throw new PredictionsException("Amazon Rekognition encountered an error while detecting moderation labels.", serviceException, "See attached service exception for more details.");
    }
    List<Label> labels = new ArrayList<>();
    for (ModerationLabel moderationLabel : result.getModerationLabels()) {
        Label label = Label.builder().value(moderationLabel.getName()).confidence(moderationLabel.getConfidence()).parentLabels(Collections.singletonList(moderationLabel.getParentName())).build();
        labels.add(label);
    }
    return labels;
}
Also used : ModerationLabel(com.amazonaws.services.rekognition.model.ModerationLabel) AmazonClientException(com.amazonaws.AmazonClientException) Label(com.amplifyframework.predictions.models.Label) ModerationLabel(com.amazonaws.services.rekognition.model.ModerationLabel) ArrayList(java.util.ArrayList) PredictionsException(com.amplifyframework.predictions.PredictionsException) Image(com.amazonaws.services.rekognition.model.Image) DetectModerationLabelsResult(com.amazonaws.services.rekognition.model.DetectModerationLabelsResult) DetectModerationLabelsRequest(com.amazonaws.services.rekognition.model.DetectModerationLabelsRequest)

Aggregations

PredictionsException (com.amplifyframework.predictions.PredictionsException)32 AmazonClientException (com.amazonaws.AmazonClientException)15 ArrayList (java.util.ArrayList)9 Image (com.amazonaws.services.rekognition.model.Image)6 Test (org.junit.Test)5 RectF (android.graphics.RectF)4 IdentifyEntitiesConfiguration (com.amplifyframework.predictions.aws.configuration.IdentifyEntitiesConfiguration)3 Sentiment (com.amplifyframework.predictions.models.Sentiment)3 InputStream (java.io.InputStream)3 DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)2 ComparedFace (com.amazonaws.services.rekognition.model.ComparedFace)2 ModerationLabel (com.amazonaws.services.rekognition.model.ModerationLabel)2 Document (com.amazonaws.services.textract.model.Document)2 SpeechGeneratorConfiguration (com.amplifyframework.predictions.aws.configuration.SpeechGeneratorConfiguration)2 CelebrityDetails (com.amplifyframework.predictions.models.CelebrityDetails)2 KeyPhrase (com.amplifyframework.predictions.models.KeyPhrase)2 Landmark (com.amplifyframework.predictions.models.Landmark)2 LanguageType (com.amplifyframework.predictions.models.LanguageType)2 Pose (com.amplifyframework.predictions.models.Pose)2 Syntax (com.amplifyframework.predictions.models.Syntax)2