Page updated Jan 16, 2024

Interpret sentiment

Amplify iOS 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 Swift 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 iOS, 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).

For analyzing language on iOS we use both AWS backend services as well as Apple's on-device Natural Language Framework to provide you with the most accurate results. If your device is offline, we will return results only Natural Language. On the other hand, if you are able to connect to AWS Services, we will return a unioned result from both the service and Natural Language. Switching between backend services and Natural Language is done automatically without any additional configuration required.

Set up your backend

This will allow you to determine key phrases, sentiment, language, syntax, and entities from text. If you haven't already done so, run amplify init inside your project and then amplify add auth (we recommend selecting the default configuration).

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below
2 Identify
3 Convert
4❯ Interpret
5 Infer
6 Learn More
7
8? What would you like to interpret? (Use arrow keys)
9❯ Interpret Text
10
11? Provide a friendly name for your resource
12 <Enter a friendly name here>
13
14? What kind of interpretation would you like?
15 Language
16 Entity
17 Keyphrase
18 Sentiment
19 Syntax
20❯ All
21
22? Who should have access?
23 Auth users only
24❯ 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.

1func interpret(text: String) {
2 Amplify.Predictions.interpret(text: text) { event in
3 switch event {
4 case let .success(result):
5 print(result)
6 case let .failure(error):
7 print(error)
8 }
9 }
10}
1func interpret(text: String) -> AnyCancellable {
2 Amplify.Predictions.interpret(text: text)
3 .resultPublisher
4 .sink {
5 if case let .failure(error) = $0 {
6 print(error)
7 }
8 }
9 receiveValue: { result in
10 print(result)
11 }
12}