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();
}
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;
}
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();
}
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);
}
}
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;
}
Aggregations