Page updated Jan 16, 2024

Interpret sentiment

Amplify Android v1 is now in Maintenance Mode until May 31st, 2024. This means that we will continue to include updates to ensure compatibility with backend services and security. No new features will be introduced in v1.

Please use the latest version (v2) of Amplify Library for Android to get started.

If you are currently using v1, follow these instructions to upgrade to v2.

Amplify libraries should be used for all new cloud connected applications. If you are currently using the AWS Mobile SDK for Android, you can access the documentation here.

The following API allows you to analyze text for language, entities (places, people), key phrases, sentiment (positive, neutral, negative), and syntax (pronouns, verbs, adjectives).

Set up your backend

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below (Use arrow keys)
2 `Interpret`
3? What would you like to interpret? (Use arrow keys)
4 `Interpret Text`
5? Provide a friendly name for your resource
6 `interpretText`
7? What kind of interpretation would you like? (Use arrow keys)
8 `All`
9? Who should have access? (Use arrow keys)
10 `Auth and Guest users`

Run amplify push to create the resources in the cloud.

Working with the API

Here is an example of sending text for interpretation such as sentiment analysis or natural language characteristics.

1Amplify.Predictions.interpret(
2 "I like to eat spaghetti",
3 result -> Log.i("MyAmplifyApp", result.getSentiment().getValue().toString()),
4 error -> Log.e("MyAmplifyApp", "Interpret failed", error)
5);
1Amplify.Predictions.interpret("I like to eat spaghetti",
2 { Log.i("MyAmplifyApp", "${it.sentiment?.value}") },
3 { Log.e("MyAmplifyApp", "Interpret failed", it) }
4)
1val text = "I like to eat spaghetti"
2try {
3 val result = Amplify.Predictions.interpret(text)
4 Log.i("MyAmplifyApp", "${result.sentiment?.value}")
5} catch (error: PredictionsException) {
6 Log.e("MyAmplifyApp", "Interpret failed", error)
7}
1RxAmplify.Predictions.interpret("I like to eat spaghetti")
2 .subscribe(
3 result -> Log.i("MyAmplifyApp", result.getSentiment().getValue().toString()),
4 error -> Log.e("MyAmplifyApp", "Interpret failed", error)
5 );

As a result of running this code, you will see the sentiment of the text printed to the console.

1I/MyAmplifyApp: POSITIVE