Menu
Amazon Comprehend
Developer Guide

Detecting Sentiment

To determine the overall emotional tone of text, use the DetectSentiment operation. To detect the sentiment in up to 25 documents in a batch, use the BatchDetectSentiment operation. For more information, see Using the Batch APIs.

Detecting Sentiment Using the AWS Command Line Interface

The following example demonstrates using the DetectSentiment operation with the AWS CLI. This example specifies the language of the input text.

The example is formatted for Unix, Linux, and macOS. For Windows, replace the backslash (\) Unix continuation character at the end of each line with a caret (^).

Copy
aws comprehend detect-sentiment \ --region region \ --language-code "en" \ --text "It is raining today in Seattle."

Amazon Comprehend responds with the following:

{
    "SentimentScore": {
        "Mixed": 0.014585512690246105,
        "Positive": 0.31592071056365967,
        "Neutral": 0.5985543131828308,
        "Negative": 0.07093945890665054
    },
    "Sentiment": "NEUTRAL",
    "LanguageCode": "en"
}

Detecting Sentiment Using the AWS SDK for Java

The following example Java program detects the sentiment of input text. You must specify the language of the input text.

Copy
import com.amazonaws.auth.AWSStaticCredentialsProvider; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.comprehend.AmazonComprehend; import com.amazonaws.services.comprehend.AmazonComprehendClientBuilder; import com.amazonaws.services.comprehend.model.DetectSentimentRequest; import com.amazonaws.services.comprehend.model.DetectSentimentResult; public class App { public static void main( String[] args ) { String text = "It is raining today in Seattle"; BasicAWSCredentials awsCreds = new BasicAWSCredentials("access key ID", "secret access key"); AmazonComprehend comprehendClient = AmazonComprehendClientBuilder.standard() .withCredentials(new AWSStaticCredentialsProvider(awsCreds)) .withRegion("region") .build(); // Call detectSentiment API System.out.println("Calling DetectSentiment"); DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest().withText(text) .withLanguageCode("en"); DetectSentimentResult detectSentimentResult = comprehendClient.detectSentiment(detectSentimentRequest); System.out.println(detectSentimentResult); System.out.println("End of DetectSentiment\n"); System.out.println( "Done" ); } }

Detecting Sentiment Using the AWS SDK for Python (Boto)

The following Python program detects the sentiment of input text. You must specify the language of the input text.

Copy
import boto3 import json comprehend = boto3.client(service_name='comprehend', region_name='region') text = "It is raining today in Seattle" print('Calling DetectSentiment') print(json.dumps(comprehend.detect_sentiment(Text=text, LanguageCode='en'), sort_keys=True, indent=4)) print('End of DetectSentiment\n')