Practice in your account · 20 min
Sentiment and entities in reviews with Amazon Comprehend
In this lab you will analyze three Spanish-language reviews with Amazon Comprehend, in the console and from CloudShell, and you will see what sentiment, entities and personal data a model that AWS already trained for you detects.
Amazon Comprehend is one of the AWS managed AI services, which come with a model that is already trained and are used through an API, without training anything. A trained model is a program that has already learned a task from many examples, and AWS bringing it ready means you only send it your text and get the result. Comprehend analyzes text: from a review it tells you whether the customer is happy or upset, which people, places, dates and companies it mentions, which phrases summarize the text and whether it contains personal data. For example, an online store that gets hundreds of reviews can find out which ones are complaints without reading them one by one.
Comprehend is used from the console, with a real-time analysis, or through the API, which is how an application uses it. Real time means you paste the text and the answer arrives right then. In both cases it charges in units of 100 characters, so analyzing a short review costs a fraction of a cent. For example, a two-sentence review fits in a few units, and since each request counts at least 3, it is charged as 3.

In a call center, Comprehend is the step that decides whether the supervisor needs to be alerted: it receives the text of the call already transcribed, that is, turned from voice into text, and translated, and returns the sentiment and the entities. None of the services in that flow requires training a model, because each one already comes with its own. For example, if a customer says on the phone that they are fed up with waiting, Comprehend marks the call as negative and the supervisor gets the alert.
Before you start
This lab is only for a full account. In the new experience, Comprehend doesn't appear on the free plan list or on the paid plan list, and AWS says that what isn't on those lists isn't available until you turn on the advanced features. For example, if you open the Comprehend console from a new experience account, you won't be able to run the analysis.
If your account is a new experience account, do Compare Amazon Bedrock models with the same prompt instead, with this prompt: Clasifica el sentimiento de esta reseña como positivo, negativo, neutral o mixto: .... A prompt is the instruction you write to the model, and in this case it asks the model to do the same job as Comprehend. With Bedrock you will get a sentiment label, but without the confidence score that Comprehend gives, which is the number that says how sure the model is of its answer.
In a full account you can use any Region where Comprehend is available, and us-east-2 works. In Spanish, entities, key phrases, sentiment, syntax and personally identifiable information (PII) detection work. An entity is a person, place, date or company that the text mentions by name, and personal information is any piece of data that identifies someone, such as their phone number or email. Targeted sentiment, on the other hand, only exists in English. For example, in a Spanish-language review Comprehend tells you whether the whole review is negative, but not whether the complaint is about the shipping or about the product.
Match each result with the question it answers
0 of 4 pairsTap a Comprehend result and then the question it answers.
0 of 4 pairs.
Analyze the reviews in the console
The console has a real-time analysis that uses Comprehend's built-in models and shows each result in its own tab. That way, with a single analysis you see the sentiment, entities, key phrases, language and personal data of a review. In this part you'll analyze three reviews from an online store, a happy one, an upset one and one with both, to see how each result changes.
- Open the Amazon Comprehend console (https://console.aws.amazon.com/comprehend/).
- In the left menu, choose Real-time analysis.
- Under Analysis type, choose the built-in analysis (Built-in).
- Delete the sample text and paste the first review:
Compré unos audífonos en la tienda de Monterrey el 3 de octubre. Llegaron rápido y suenan muy bien. Los recomiendo.
- Choose Analyze. Check the Sentiment tab, where it should come out positive with its confidence, the Entities tab, where Monterrey and the date appear, and the Key phrases and Language tabs.
- Repeat with the second review:
Pedí una mochila y llegó rota. Llamé al 55 1234 5678 y nadie contestó. No vuelvo a comprar aquí.
Open the PII tab, where the phone number is marked as personal information. 7. Repeat with the third one:
El envío fue rapidísimo, pero la talla no corresponde a la tabla de la página.
This review has a good part and a bad part. Check the four sentiment scores and see whether the label comes out mixed or which of the others wins.
The Sentiment tab always gives one of four labels (positive, negative, neutral or mixed) and a confidence for each one. The higher a label's confidence, the surer the model is that it's the right one. That confidence is what lets you, for example, review by hand only the reviews where the model isn't sure.
Analyze the reviews from CloudShell
An application uses Comprehend with the same operations as the console, but through the API. CloudShell is a terminal inside the console that already comes with the AWS CLI, the program that sends those operations to AWS written as commands. Open CloudShell and send the second review:
aws comprehend detect-sentiment --language-code es \
--text "Pedí una mochila y llegó rota. Llamé al 55 1234 5678 y nadie contestó. No vuelvo a comprar aquí."
The response includes Sentiment, with the label, and SentimentScore, with four scores: Positive, Negative, Neutral and Mixed. For example, for this review the Negative score should come out much higher than the other three. To look for the personal data in the same review, run this other command:
aws comprehend detect-pii-entities --language-code es \
--text "Pedí una mochila y llegó rota. Llamé al 55 1234 5678 y nadie contestó."
Each entity in the response includes its type, its position in the text (BeginOffset and EndOffset) and its confidence. The positions say at which character the piece of data starts and at which one it ends within the text. With those positions, an application can mask the phone number before saving the review. For example, the store can save the complaint as Llamé al XXXX y nadie contestó, without the customer's number.
Which service do you use?
A store wants to mark each new review as positive or negative, with a confidence score, without training anything and paying per 100 characters. Which service do you choose?
Extra: the batch job from the AWS guide
The "Getting started with Amazon Comprehend" guide uses an asynchronous job, which runs in the background: Comprehend reads some AWS example documents and writes the result to a bucket of yours. Asynchronous means you don't wait for the answer on screen; instead the job finishes later and leaves its result stored. For example, the store could analyze all of the month's reviews this way in one go and check the result the next day. If you want to do it, follow these steps:
- Create a private bucket. You can use the course template
[lab5c-bucket-documentos.yaml](/roadmaps/plantillas/lab5c-bucket-documentos.yaml), whoseNombreDelBucketoutput is the bucket name. - In Analysis jobs, choose Create job, type Entities, source Example documents, output to your bucket and Create an IAM role.
- When the status changes to
COMPLETED, downloadoutput.tar.gzfrom the output link.
The template in step 1 is used with CloudFormation, the service that creates what the template describes. The IAM role in step 2 lets Comprehend write the result to your bucket, because without that permission it couldn't get in.
This job is charged per unit of text processed, and the guide doesn't say how much text the example documents contain. So check the pricing page before you start it.
How to know you're done
You have finished the lab when you have these two results:
- The three reviews analyzed in the console, where the first came out positive, the second negative with a phone number detected as PII and the third with the sentiment split.
- The
detect-sentimentresponse, which returnedNEGATIVEfor the second review.
Delete what you created
The real-time analysis and the commands don't create anything, so you only have to delete something if you did the extra. In that case, empty the bucket, delete the CloudFormation stack and delete in IAM the role that the Comprehend console created. For example, the bucket is emptied first because CloudFormation can't delete a bucket that still has files in it.
Next
- Getting started with Amazon Comprehend: https://docs.aws.amazon.com/comprehend/latest/dg/getting-started.html
- cloud path practice exams: /examenes?lang=en