Amplify has re-imagined the way frontend developers build fullstack applications. Develop and deploy without the hassle.

Page updated Apr 29, 2024

Interpret sentiment

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:

? Please select from one of the categories below
Identify
Convert
❯ Interpret
Infer
Learn More
? What would you like to interpret? (Use arrow keys)
❯ Interpret Text
? Provide a friendly name for your resource
<Enter a friendly name here>
? What kind of interpretation would you like?
Language
Entity
Keyphrase
Sentiment
Syntax
❯ All
? Who should have access?
Auth users only
❯ 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.

func interpret(text: String) async throws -> Predictions.Interpret.Result {
do {
let result = try await Amplify.Predictions.interpret(text: text)
print("Interpreted text: \(result)")
return result
} catch let error as PredictionsError {
print("Error interpreting text: \(error)")
throw error
} catch {
print("Unexpected error: \(error)")
throw error
}
}
func interpret(text: String) -> AnyCancellable {
Amplify.Publisher.create {
try await Amplify.Predictions.interpret(text: text)
}
.sink(receiveCompletion: { completion in
if case let .failure(error) = completion {
print("Error interpreting text: \(error)")
}
}, receiveValue: { value in
print("Interpreted text: \(value)")
})
}