use of com.amplifyframework.predictions.models.Sentiment in project amplify-android by aws-amplify.
the class AWSPredictionsInterpretTest method testNegativeSentimentDetection.
/**
* Assert that unhappy review is detected as negative.
* @throws Exception if prediction fails
*/
@Test
public void testNegativeSentimentDetection() throws Exception {
// Interpret negative text and assert non-null result
final String negativeReview = Assets.readAsString("negative-review.txt");
InterpretResult result = predictions.interpret(negativeReview);
assertNotNull(result);
// Assert detected sentiment is negative
Sentiment sentiment = result.getSentiment();
FeatureAssert.assertMatches(SentimentType.NEGATIVE, sentiment);
}
use of com.amplifyframework.predictions.models.Sentiment in project amplify-android by aws-amplify.
the class AWSComprehendService method comprehend.
void comprehend(@NonNull String text, @NonNull Consumer<InterpretResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
try {
// First obtain the dominant language to begin analysis
final Language dominantLanguage = fetchPredominantLanguage(text);
final LanguageType language = dominantLanguage.getValue();
// Actually analyze text in the context of dominant language
final Sentiment sentiment = fetchSentiment(text, language);
final List<KeyPhrase> keyPhrases = fetchKeyPhrases(text, language);
final List<Entity> entities = fetchEntities(text, language);
final List<Syntax> syntax = fetchSyntax(text, language);
onSuccess.accept(InterpretResult.builder().language(dominantLanguage).sentiment(sentiment).keyPhrases(keyPhrases).entities(entities).syntax(syntax).build());
} catch (PredictionsException exception) {
onError.accept(exception);
}
}
use of com.amplifyframework.predictions.models.Sentiment in project amplify-android by aws-amplify.
the class AWSPredictionsInterpretTest method testPositiveSentimentDetection.
/**
* Assert that happy review is detected as positive.
* @throws Exception if prediction fails
*/
@Test
public void testPositiveSentimentDetection() throws Exception {
// Interpret positive text and assert non-null result
final String positiveReview = Assets.readAsString("positive-review.txt");
InterpretResult result = predictions.interpret(positiveReview);
assertNotNull(result);
// Assert detected sentiment is positive
Sentiment sentiment = result.getSentiment();
FeatureAssert.assertMatches(SentimentType.POSITIVE, sentiment);
}
use of com.amplifyframework.predictions.models.Sentiment in project amplify-android by aws-amplify.
the class TensorFlowTextClassificationService method classify.
/**
* Classifies text to analyze associated sentiments.
* @param text the text to classify
* @param onSuccess notified when classification succeeds
* @param onError notified when classification fails
*/
void classify(@NonNull String text, @NonNull Consumer<InterpretResult> onSuccess, @NonNull Consumer<PredictionsException> onError) {
// Escape early if the initialization failed
if (loadingError != null) {
onError.accept(loadingError);
return;
}
// Wait for initialization to complete
// TODO: encapsulate blocking logic elsewhere
boolean didLoad = false;
try {
didLoad = loaded.await(LOAD_TIMEOUT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException exception) {
onError.accept(new PredictionsException("Text classification service initialization was interrupted.", "Please wait for the required assets to be fully loaded."));
return;
}
if (!didLoad) {
onError.accept(new PredictionsException("Text classification service timed out while awaiting load.", "Your classification data may be too resource intensive?"));
}
try {
final Sentiment sentiment = fetchSentiment(text);
onSuccess.accept(InterpretResult.builder().sentiment(sentiment).build());
} catch (PredictionsException exception) {
onError.accept(exception);
}
}
use of com.amplifyframework.predictions.models.Sentiment in project amplify-android by aws-amplify.
the class TensorFlowTextClassificationService method fetchSentiment.
@VisibleForTesting
Sentiment fetchSentiment(String text) throws PredictionsException {
float[][] input;
float[][] output;
try {
// Pre-process input text
input = dictionary.tokenizeInputText(text);
output = new float[1][labels.size()];
// Run inference.
interpreter.run(input, output);
} catch (IllegalArgumentException exception) {
throw new PredictionsException("TensorFlow Lite failed to make an inference.", exception, "Verify that the label size matches the output size of the model.");
}
// Find the predominant sentiment
Sentiment sentiment = null;
for (int i = 0; i < labels.size(); i++) {
SentimentType sentimentType = SentimentTypeAdapter.fromTensorFlow(labels.get(i));
float confidenceScore = output[0][i] * PERCENT;
if (sentiment == null || sentiment.getConfidence() < confidenceScore) {
sentiment = Sentiment.builder().value(sentimentType).confidence(confidenceScore).build();
}
}
return sentiment;
}
Aggregations