Amazon Comprehend is a natural language processing (NLP) service by AWS that uses machine learning algorithms to extract insights from text. It provides various features like sentiment analysis, keyphrase extraction, entity recognition, and language detection APIs so you can easily integrate natural language processing into your applications.
There are various sets of text that need analysis for your business: product reviews, support emails, even advertising copy. Analyzing customer sentiment can be very useful for your business growth.
The question is how to get at it? As it turns out, Machine Learning accurately identifies the specific items from a text and then uses the context to analyze the sentiment behind the language, such as identifying positive or negative reviews at any particular scale of data.
You can explore the service here.
Sentiment analysis is the process of analyzing the sentiments in a piece of text with the help of various algorithms. Using sentiment analysis, we can analyze various emotions, particularly if it is positive, neutral, or negative. This has enabled the development of intelligent applications with a better understanding of the text received.
Companies like Twitter, Facebook, and Google use such algorithms to monitor the content posted on their apps to make a secure experience for their users.
In this article, we will learn how to analyze the sentiments from a piece of text using AWS services like Amazon Comprehend, AWS IAM, AWS Lambda, and Amazon S3. So before you get started, make sure you have access to all these services. You can access all of these services through your management console.
Let’s get started!
Using your management console, select the IAM service. Once you land on the IAM dashboard, on the left side, you will see Access Management. In that, select Roles to add an IAM role to give various permissions. Click on Create role button.
As we will be using AWS Lambda, we need to select Lambda and click on Permissions (right-hand side below) to add various permissions to our role.
You can filter out the permissions, and then once selected, click next!
Give an appropriate name and description to your role, and once you select Create role, a new role gets created, which can be accessed anytime from your existing list of roles from the IAM Management Console.
Once the role is created, we will move to the S3 Management Console. Create a text document on your local device and then use that to analyze the docs' sentiments. For that, first, create a bucket. Make sure you are keeping the bucket as public to test with the Lambda function for text analysis.
Once it is created, then upload a text document like that, and then the S3 bucket will look like this after uploading various documents. Max size of the file needs to be lesser than 5000 bytes.
Once we upload the files, this is how our bucket will look like; you can manage the permissions and perform various actions over it as per your needs.
The last and final step is to create a Lambda function to analyze the text document we uploaded in the S3 bucket.
Here, I have created the function
. We will use Python 3.8 for our runtime. The existing role
we created earlier will be used with the Lambda function.
Through the Lambda Management Console, we can access the created function, and then we will click on that to write a function code for performing our task of sentiment analysis using Python 3.8. Here, we will enter the name of the S3 bucket we created, and the key will be the name of the file to be analyzed (uploaded in the same bucket)
import boto3 from pprint import pprint def lambda_handler(event, context): s3 = boto3.client("s3") bucket = "comprehend-lambda-analysis" key = "analysisdata.txt" file = s3.get_object(Bucket = bucket, Key = key) analysisdata = str(file['Body'].read()) comprehend = boto3.client("comprehend") sentiment = comprehend.detect_sentiment(Text = analysisdata, LanguageCode = "en") print(sentiment) return 'Sentiment detected'
Save your function code and then test your code to see the results with the analysis of the text you directed through your bucket and key.
Once you test your function code, it will give the results for your text. The section of results shows the logging calls in the code. They correspond to a single row within the CloudWatch log group with this Lambda function.
Access the analysis text I uploaded (it is a keynote extract from the WWDC 2020 keynote by Apple): https://comprehend-lambda-analysis.s3.us-east-2.amazonaws.com/analysisdata.txt
It identifies the Sentiment of the Keynote extract to be Neutral with the Sentiment scores for each sentiment.
You can use the same for analyzing various text files like the ones I have tried.
Here is a Twitter comment I have chosen to analyze: https://comprehend-lambda-analysis.s3.us-east-2.amazonaws.com/twitterhatecomment.txt
This is a hate comment from Twitter, so it shows the Sentiment to be Negative.
Here is another file I have chosen to analyze. It is a short story; you will enjoy it when you read: https://comprehend-lambda-analysis.s3.us-east-2.amazonaws.com/shortstory.txt
It is a very short and motivating story, so it gives the result to be Positive.
You can use various other files to analyze; this process will enable you to understand the sentiments of your files.
Amazon Comprehend offers a range of Natural Language Processing features in addition to sentiment analysis. The key features are identifying the language of the given text, extracting specific elements to understand how positive or negative the text is. This is then used to organize a set of text by topic automatically. For extracting complex medical information, you can use Amazon Comprehend Medical.
I hope this blog helped you understand how to perform sentiment analysis on text using the AWS Lambda interface. You can use various other approaches like serverless framework, or using React or Angular to test your output on the frontend side.
For any queries/suggestions, feel free to contact me. 😃