top of page

AWS Comprehend: Sentiment Detection

Updated: Nov 29, 2021

I'm going to take a look at Native Language Processing or NLP from AWS comprehend. This Post will look at one part of the NPL suite from AWS, Sentiment Detection.

Why? I had idea that maybe some NPL could integrate with my animatronic Parrot Bosco. His creation is documented in another post on this blog. He has basic speech recognition abilities. I'm always looking for new and better ways to interactive with him.... Before I get into some long winded crazy idea, let's take a look at what NPL Sentiment Detection does. No better way than hands on which is why I made this little web app. Enter a phase below like "I love this" or "I hate all your products!". It totally works :)

I hope by getting see what it does first hand has give you a few ideas about how to use it: Maybe you want to route your more aggravated complaints to more advanced CS agents? Maybe you would like the positive comments on your Insta account to go to marketing to be reviewed for addition to the home page? Or maybe you run a blog, and you only want to read your positive comments. (life is too short for the negative ones) For whatever reason you want to use it, I'll show you how I set this app up, and maybe it will help you get started on your idea.

My Implementation in this page

To get started I reading a guide. It used PostMan to talk to AWS using GET. I followed along and I got that working in about 15 minutes. However, I wanted to use POST and from my own web page. That took it a bit more time about 8 hours to get it working using POST from this web site. CORS was by far the hardest thing for me to get worked out, it hates me. It turned out, as it always does to just getting the right setting set on AWS. I hope I can save you from some of my headaches.

Lambda Lambda Duck

Amazon offers a large number of web-services that can be tapped into and implemented into about anything you like. My goal is to make a simple web app (the one included in this page) to allow users to type in a message and to get a sentiment score.

I got started by logging into my Amazon AWS account, and searching for Lambda component so I can create one.

Then I selected the create function option.

I selected the "authored from scratch" check box. Then filed my new lambda's name. Select Python 3.8 Be sure to select a security role. I didn't the first time, and I wasted time with authorization errors.

Then I wrote my python code. This is pretty straight forward. Line 12 & 13 are the lines with all the magic. It will call comprehend to get the sentiment scores for the text that has been passed in.

You can run "test", as I recall it failed when I first tried, but after I got the security and API configured the test worked.

Setting up the API Rest service for the Lambda

Do a search for API, and select API gateway.

Select Create API

Name your API, but the defaults can be kept.

After the API is created, you'll see a directory tree view. Select add method. This will add a line in the list, click on it, and you'll see the drop down list on the left. Select GET.

Add the previously made Lambda to the on the function line, and save.

You should see a screen that looks something like this. The API should now be connected using GET to the lambda function. Give it a test in postman

Wait a minute Mr. Postman!

This is what my postman screen looked like. The only trick here is to add the json to the body of the request. Select Body -> Raw -> Json.

Once I got that working, I went and set up the POST method and turned on CORS.

The POST method is set up exactly like the GET. Enable CORS.

BE SURE TO DEPLOY the api. I beat my head on why things weren't working for awhile, once I discovered that action things started working.

The little UI

I'm going to paste in the my html below. I made this in using brackets. The reason all the CSS and Javascript is in one page was so that it can be cut and pasted into a blog, and work as a widget. I hope this is helpful, if you try and have any problems. I'm happy to help. We are all in this together.




<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />




awsheader {

line-height: 1.5;

font-weight: 400;

font-family: "Poppins",Arial,sans-serif;

color: #000;

font-size: 1.75rem;

width: 435px;

display: block;

border-bottom: 1px solid #cdcdcd;


sentiment {

display: flex;

flex-direction: row;

margin: 10px;

color: #999999;


sentimentscores {

display: flex;

flex-direction: column;



margin: 0px 10px 0px 0px;


.awsscoreitem {

padding: 0px;

font-family: "Poppins",Arial,sans-serif;

font-size: 12px;



resize: none;

font-family: 'Poppins',Arial,sans-serif;

font-size: 16px;

border: none;

border-bottom: 1px solid rgba(0,0,0,.1);

margin: 0px;

padding: 10px;

background: #fafafa;



background: #46b5d1;

border-color: #46b5d1;

margin: 10px 0px 0px 0px;

color: #fff;

padding: 12px 16px;

cursor: pointer;

border-width: 1px;

border-radius: 5px;

font-size: 14px;

font-weight: 400;

-webkit-box-shadow: 0 10px 20px -6px rgb(0 0 0 / 12%);

-moz-box-shadow: 0 10px 20px -6px rgba(0,0,0,.12);

box-shadow: 0 10px 20px -6px rgb(0 0 0 / 12%);

position: relative;

margin-bottom: 20px;

-webkit-transition: .3s;

-o-transition: .3s;

transition: .3s;

cursor: pointer;



background: #2279AD;

border-color: #666666;


sicon {

font-size: 30pt;



<awsheader>AWS Sentiment Detection</awsheader>

<span id="responce">




<mixed class="awsscoreitem"></mixed>

<negative class="awsscoreitem"></negative>

<neutral class="awsscoreitem"></neutral>

<positive class="awsscoreitem"></positive>




<form action="" id="usrform" enctype="multipart/form-data">

<textarea rows="4" cols="50" id="Text" name="Text" form="usrform" placeholder="Enter a nice or mean message here"




<input type="submit" class="awssubmit">


<script src=""></script>


function setSentimentTable(icon, mixed, negative, neutral, positive)


mixed = parseFloat(mixed * 100).toFixed(2);

negative = parseFloat(negative * 100).toFixed(2);

neutral = parseFloat(neutral * 100).toFixed(2);

positive = parseFloat(positive * 100).toFixed(2);


$("mixed").html(`Mixed: ${mixed}%`);

$("negative").html(`Negative: ${negative}%`);

$("neutral").html(`Neutral: ${neutral}%`);

$("positive").html(`Positive: ${positive}%`);


$("#usrform").submit(function(e) {

var sentimentIcons = {"POSITIVE": "&#128513", "NEUTRAL": "&#128528", "NEGATIVE": "&#128544", "MIXED": "&#128533"};

e.preventDefault(); // avoid to execute the actual submit of the form.

var form = $(this);

var url = ""; //Enter your AWS link here

var data = '{"Text":"';

data = data + $('textarea#Text').val() + '"}';


type: "Post",

url: url,

data: data,

success: function(data)


var scores = data.body.SentimentScore;












40 views0 comments

Recent Posts

See All
bottom of page