Sentiment Analysis using AWS Comprehend

Written by manishmshiva | Published 2019/12/06
Tech Story Tags: aws | devops | serverless | aws-comprehend | aws-lambda | sentiment-analysis | programming | amazon

TLDR Sentiment analysis uses AI to identify the core emotion behind a piece of text. In this blog post, we will look at how to analyze the positive /negative/neutral sentiment of an amazon review using both the AWS web console and the AWS CLI. This analysis is performed through pre-trained machine learning models. You can also use AWS Comprehend for language detection, Topic modeling, Key phrases extraction, etc. If you have any questions, let me know in the comments/discussion.via the TL;DR App

Sentiment analysis uses AI to identify the core emotion behind a piece of text. In this article, we will look at how to build a sentiment analyzer using AWS Comprehend.

Sentiment Analysis

Sentiment analysis is the process of analyzing a piece of text to understand the sentiment behind it. Sentiment analysis has proven useful in analyzing emotions like anger, happiness, and sadness, thereby enabling developers to build smarter applications with better insights into textual content.
A popular use case is analyzing comments on social media to understand the type of content being posted on the platform. These type of sentiment analysis algorithms are used by companies like Facebook and Twitter to filter out provocative content.
Even though the algorithms used in real-world sentiment analysis implementations are quite complex, the science behind sentiment analysis is simple. Here is a great article on how sentiment analysis works.

AWS Comprehend

AWS Comprehend is a natural language processing (NLP) service that uses machine learning to find insights and relationships in textual data. AWS Comprehend has pre-trained models that help make use of unstructured data and make it work for your business.
AWS Comprehend provides advanced NLP features like keyword extraction, in addition to sentiment analysis. In this blog post, we will look at how to analyze the positive /negative/neutral sentiment of an amazon review using both the AWS web console and the AWS CLI.

Sentiment Analysis: AWS Console

Log in to the console and search for ‘Comprehend’. You will see AWS comprehend listed in the services dropdown. Click on the service and you will be taken to AWS Comprehend console.
Click on ‘Launch Amazon Comprehend’. You should see the ‘Real time analysis’ section where you have the built-in and custom text analysis options.
Let's find an amazon review and analyze its sentiment. For this blog post, I am using the following review from this product.
These sheets feel soft when they arrive and also after the first laundering. It was fun to put these new sheets on the bed. Then came the problems. The sheets are so thin that the bottom sheet can't possibly stay flat, so the result is a wrinkly bed all the time. Then they started to pill. I washed them in cold water and dried them on low, but still they pill. There are tiny crumbs everywhere, including on the bedroom floor. These sheets are not worth buying. AmazonBasics does not mean Amazon's Best, or even Mediocre.
As you can see, this is clearly a negative review of the product from the buyer. Let's use AWS Comprehend to analyze this review.
Copy-paste the review into the ‘Input text’ text box and click ‘Analyze’. Once the analysis is complete, the insights section provides general insights about the text in addition to sentiment analysis. Click on the ‘Sentiment’ tab to view the sentiment score for the review.
As you can see, AWS comprehend has analyzed the text and has predicted the text to be 99% negative. This analysis is performed through pre-trained machine learning models. AWS Comprehend also allows you to use custom models to perform sentiment analysis.

Sentiment Analysis: AWS CLI

If you prefer to use the AWS CLI, here is the command to use AWS Comprehend.
aws comprehend detect-sentiment \
--region us-east-1 \
--language-code "en" \
--text "These sheets feel soft when they arrive and also after the first laundering. It was fun to put these new sheets on the bed. Then came the problems. The sheets are so thin that the bottom sheet can't possibly stay flat, so the result is a wrinkly bed all the time. Then they started to pill. I washed them in cold water and dried them on low, but still they pill. There are tiny crumbs everywhere, including on the bedroom floor. These sheets are not worth buying. AmazonBasics does not mean Amazon's Best, or even Mediocre."
If you have configured your AWS CLI with the access keys and regions, the above command should return the following output with the primary sentiment and confidence scores.
The AWS SDK also uses the AWS CLI behind the screens to work with all AWS services. The output format (JSON) can also be easily consumed by third party applications if you are planning to use AWS Comprehend for sentiment analysis and other NLP applications.

Summary

AWS Comprehend offers a range of Natural Language Processing features in addition to sentiment analysis. You can also use AWS Comprehend for language detection, Topic modeling, Key phrases extraction, etc. If you have any questions, let me know in the comments/discussion.
If you have a topic you would like me to write about, let me know in the comments. Signup for my weekly newsletter and I’ll send you a summary of articles and videos every week. No spam. No ads.

Written by manishmshiva | AWS Professional - Devops | Machine Learning | Cyber Security
Published by HackerNoon on 2019/12/06