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