How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Reading Time: 10 minutes
How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

In our previous blog, How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis(Part 1)? we have discussed NLP, Amazon Comprehend as one of the best NLP services, its features, benefits, use cases, etc. In this blog, we will demonstrate how to configure Amazon Comprehend with AWS Lambda and Amazon Lex for sentiment analysis to create interactive, efficient, and helpful Amazon Lex bots.

Hands-on

Even with the best voice bot systems, customers often get frustrated. If the voice bot is not able to help with some queries then people often want to be directed to an actual agent directly. But nowadays that’s not the case. Bots can be frustrating for some users. What if your bot was smart enough to admit, “I’m sorry, I cannot help you with this. Let me find someone for you.”? What if your bot could automatically detect the sentiments of a user, analyze them, and reply back accordingly? Wouldn’t that make it easy for companies to configure a single bot that becomes very useful and helpful to its customers?

In this blog, we will see how we can use and configure Amazon Comprehend with AWS Lambda and Amazon Lex for sentiment analysis to make your Amazon Lex bots interactive, efficient and helpful. The Lambda function is straightforward. It analyses the input transcript field of the Amazon Lex event using the Amazon Comprehend Service. 

To implement this, we will do the following:

  • Log in to the AWS console and navigate to the Lambda function dashboard.
  • Create a new Lambda function with the Runtime as Python 3.8.
  • Navigate to the Permissions tab on the Lambda function dashboard and alter the policies to attach a new policy.
  • Add the provided code in the Editor pane of the Lambda Function.
  • Create a new test event with the provided JSON code.
  • Test the Lambda function for Neutral, Negative, and Positive utilization.
  • Search for the Amazon Lex service and navigate to the Bots dashboard.
  • Create a new bot with the provided configuration settings.
  • Create a new intent for the newly created bot.
  • Edit the Sample Utterances (if needed) and provide permission to invoke the created Lambda function for the already existing and the newly created intents attached to the bot.
  • Save all the intents with the newly configured settings.
  • Build the bot.
  • Test the functioning of the bot.
  • Publish the newly created bot to be integrated with various mobile or web applications.

Login to your AWS console and search for Lambda in the search bar.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On the Lambda function dashboard, click on Create function to create a new function.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On the create function dashboard, choose Author from scratch for the function. 

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Enter a name for the function.

For the runtime, open the dropdown and select the runtime as Python 3.8.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

For the execution role, select Create a new role with Basic Lambda permissions and once done, click on Create function.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On success, you will see the success message as shown in the image below and will be navigated to the function dashboard.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

To alter the IAM role and attach some new permissions to it, scroll down on the dashboard and select the Configuration tab.

From the list of configurations available, select the Permissions tab.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Click on the Role name and you will be navigated to the IAM console where you have to alter the role attaching a new policy.

On the role dashboard, click on Attach policies to attach a new policy.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Select Create policy and you will be navigated to the policies dashboard in a new tab.

On the Create policy dashboard, you can either select an already existing policy or write it down in the JSON format.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Select the JSON tab and add the below policy in the editor.

Once done, click on Next:Tags.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Enter a tag (key/value) pair if you wish to for the policy. Once done, click on Next: Review.

Enter a name for the newly created policy and review the policies before creating it. 

Once done, scroll down and click on Create policy.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On success, you will see the message as shown below. You can search for the newly created policy by entering its name in the search bar.

Navigate back to the IAM role dashboard and click on Refresh. Search for the policy you just created by entering its name in the search bar.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Select the checkbox of the newly created policy and click on Attach policy.

On success, you will see the success message as shown below. If you scroll down, you will see the policy attached to the role.

Now, navigate back to the Lambda functions editor pane.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

In the editor pane, clone the below repository and add the code in the comprehend.py file.

https://github.com/workfall/amazon_comprehend.git

Once done, click on the Deploy button to deploy the latest changes.

On success, you will see that the changes have been deployed successfully and your lambda function is ready to be tested and executed.

Click on the Configuration tab to add an environment variable for the Amazon Lex bot to detect the intent.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Select the Environment variables tab in the left navigation pane and click on Edit.

Click on Add environment variable to add a new variable.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Enter the key/value pair as shown in the image below. Once done, click on Save.

On success, you will see the message as shown below and a new environment variable entry created for your Lambda function.

Now select the Test tab to create an event and test out your Lambda function.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Enter a name for your test event and in the JSON editor pane paste the following.

Once done, click on Save changes to create a test event.

On success, you will see the message as shown below. Now let’s invoke the Lambda function and test its execution. Click on the Invoke button in the Test event section.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On success, you will see the Execution result: succeeded on the dashboard in the test section. Expand Details to view the output.

You will see that the Amazon Comprehend has performed the sentiment analysis for the input transcript in the test event. It detects the sentiment as Neutral.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Now let’s test the same lambda function for a negative sentiment. Scroll down to the JSON editor pane and change the inputTranscript as below. Once done, save the changes and click on Invoke.

Now if you see the output, Amazon Comprehend detects the sentiment as Negative for the inputTranscript.

Now let’s test the same function for a positive sentiment. Change the inputTranscript as below and invoke the function.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On successful execution, you will see that the sentiment detected is Positive.

Now, search for Amazon Lex in the search bar of the AWS console. Click on the service to navigate to its dashboard.

On the Amazon Lex dashboard, under the Bots tab, click on Create Bot to create a new bot.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

You can create a custom bot or select some other bot based on your requirements. In this blog, we will be selecting the BookTrip bot.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Scroll down and enter a name for the bot.

Select a Language based on your preferences for the bot.

For the sentiment analysis section, make sure to select yes to allow Amazon Comprehend to perform sentiment analysis.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

An IAM role will already be attached for your bot so you need not make any changes over there. For the COPPA, select No, add tags (if any)and once done, click on Create.

You will be navigated to the created Bot dashboard. Wait for a few seconds till the bot is built.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On success, you will see the message as shown below. Now to create a new intent, click on the + sign under the Intents section in the left navigation pane.

A modal will appear with 3 options for intent. Click on Create intent.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Enter a name for the new intent to be created and click on Add.

You will see the newly created intent in the list of Intents. Now, click on the BookCar intent.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

On the dashboard, select the checkbox under the Lambda initialization and validation section to allow the intent to invoke the Lambda function.

Click on OK to confirm the action and add the permission for the same.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Select the Lambda function you created above under the Lambda function section from the dropdown.

Scroll to the bottom and click on Save intent to save all the changes made.

Now open the BookHotel section and repeat the same configuration changes as done above.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Scroll down to the bottom and save the intent to save all the changes for the BookHotel intent.

Switch to the Escalate intent and repeat the same steps and save the intent once done.

Now on the top of the dashboard, click on Build to build the bot. A modal will appear as shown in the image below. Click on Build to confirm the action.

It might take a few seconds for the bot to be built and get into action.

On success, you can test out your bot using the pane provided on the right-hand side of the dashboard. Enter the following as shown in the image below and test out the bot.

Now let’s publish the bot. Click on the Publish button on the top of the dashboard. For the Create an alias section, enter a name for your alias. Click on Publish.

Wait for a few minutes for the bot to be published.

On success, you will see the message as shown below wherein you can download the connection info for your bot and integrate it easily with your web or mobile application to have a bot providing answers based on performing the sentiment analysis for the input Transcript entered by the user.

How can we use Amazon Comprehend with AWS Lambda and Amazon Lex for Sentiment Analysis?

Conclusion

In this blog, we have integrated Amazon Comprehend with AWS Lambda function and Amazon Lex bot to create an efficient and helpful bot. We implemented the sentiment analysis feature for the bot via Lambda Function integrating it with Amazon Comprehend. We even saw how this approach gives us the flexibility to analyze user input and then configure our Lex bot to act accordingly. We will discuss more use cases of Amazon Comprehend and its integration with other services in our upcoming blogs. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.

Meanwhile …

Keep Exploring -> Keep Learning -> Keep Mastering

This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.

Back To Top