Search in sources :

Example 6 with Sentiment

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

the class TextClassificationTest method testFetchSentiment.

/**
 * Test that fetchSentiment() can do the following.
 *
 *  - Choose the output with highest score,
 *  - obtain the column index,
 *  - obtain the label located at that index,
 *  - convert the label into {@link SentimentType}, and
 *  - return appropriate {@link Sentiment}
 *
 * @throws Exception if sentiment fetch fails
 */
@Test
// Double comparison delta epsilon
@SuppressWarnings("MagicNumber")
public void testFetchSentiment() throws Exception {
    // List of labels to expect
    final List<String> labels = Arrays.asList("negative", "positive", "unknown1", "unknown2");
    // Mock random confidence score
    final float confidenceScore = new Random().nextFloat();
    // Make mock interpreter set confidence score for
    // "positive" label into pre-determined random value
    doAnswer(invocation -> {
        float[][] output = invocation.getArgument(1, float[][].class);
        output[0][labels.indexOf("positive")] = confidenceScore;
        return null;
    }).when(mockInterpreter).run(any(), any(float[][].class));
    // Make mock dictionary return any valid input format
    when(mockDictionary.tokenizeInputText(anyString())).thenReturn(new float[0][]);
    // Make mock labels emulate an actual list of labels
    when(mockLabels.size()).thenReturn(labels.size());
    when(mockLabels.get(anyInt())).thenAnswer(invocation -> labels.get(invocation.getArgument(0)));
    // Assert that detected sentiment is positive
    Sentiment sentiment = service.fetchSentiment(RandomString.string());
    assertEquals(SentimentType.POSITIVE, sentiment.getValue());
    assertEquals(confidenceScore * 100, sentiment.getConfidence(), 1E-5);
}
Also used : Random(java.util.Random) RandomString(com.amplifyframework.testutils.random.RandomString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Sentiment(com.amplifyframework.predictions.models.Sentiment) Test(org.junit.Test)

Aggregations

Sentiment (com.amplifyframework.predictions.models.Sentiment)6 PredictionsException (com.amplifyframework.predictions.PredictionsException)3 Test (org.junit.Test)3 InterpretResult (com.amplifyframework.predictions.result.InterpretResult)2 VisibleForTesting (androidx.annotation.VisibleForTesting)1 DominantLanguage (com.amazonaws.services.comprehend.model.DominantLanguage)1 Entity (com.amplifyframework.predictions.models.Entity)1 KeyPhrase (com.amplifyframework.predictions.models.KeyPhrase)1 Language (com.amplifyframework.predictions.models.Language)1 LanguageType (com.amplifyframework.predictions.models.LanguageType)1 SentimentType (com.amplifyframework.predictions.models.SentimentType)1 Syntax (com.amplifyframework.predictions.models.Syntax)1 RandomString (com.amplifyframework.testutils.random.RandomString)1 Random (java.util.Random)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1