Targeted Sentiment Analysis in real-time using Amazon Comprehend
Published: 2022-09-23 22:59:13

On September 21 2022, AWS announced that Amazon Comprehend supports synchronous processing for targeted sentiments. In other words, Amazon Comprehend is now capable of extracting sentiments associated with entities in a document in real-time(synchronously).Earlier we were able to do this asynchronously only using a Comprehend analysis job.

First, let's try to grasp relevant use cases.
#Use cases
- Enable accurate and scalable brand and competitor insights.
- Live market research
- Producing brand experience
- Improving customer satisfaction.

Let's take one of the real world customer review to understand it further.
##Why do you need targeted sentiments Analysis?
#### A real world customer review for a hotel:
"The hotel itself was beautiful and clean. I only give it 3 stars because paying $43 USD for parking is DISGUSTING!!!!!!! What a ripoff!!!!! As well, hotel cleaning staff don't clean the rooms anymore but yet the prices are still VERY high."

#### Sentiment analysis
Sentiment analysis determines the overall sentiment of an input document, but doesn't provide further information about sentiment of each entity(word) in the document.
eg: - Using Sentiment analysis you can only find out whether Customer feedback was positive, negative, neutral, or mixed.


![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/civpdbacvhwtlw05l3vf.png)

For this scenario, after performing sentiment analysis, it suggest that the overall statement(document) sentiment is MIXED at a confidence level of 94%. However, if you want improve your hotel by understanding the negative areas, and take actions immediately for those then this information is not sufficient. 

#### Targeted Sentiment Analysis
In Targeted sentiment analysis, you can identify the emotions connected to particular entities in your input documents.
> Note:- An entity is a textual reference to the unique name of a real-world object such as people, places, and commercial items, and to precise references to measures such as dates and quantities.
When you call Targeted sentiment, it provides the following information:

- It identifies the entities in the documents.
- Classification of the entity type for each entity mentioned.
> You can find the list of entity types here. [Entity types](https://docs.aws.amazon.com/comprehend/latest/dg/how-targeted-sentiment.html#how-targeted-sentiment-entities
> )

- 
For each entity mentioned, the sentiment and a sentiment score will be evaluated
- 
Groups of mentions that correspond to a single entity.

See the outputs of the targeted sentiment analysis for the same example.

![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6tpxt424qxotskr925xh.png)

As you can see, you can definitely get insights on areas of improvement in their hotel service. The staff and prices have negative sentiments where they can take immediate actions 
to those areas.

Why do you need it synchronous?
- 
 You want to take action immediately to upon customer review.
When you call this new API(synchronous), you can immediately get the results and it can  be send through any messaging channel(eg:-sms,whatsapp) or update in a real-time dashboard. Therefore you can update the required staff immediately upon review received from customer to take action to rectify the issues, so this new API is super helpful.

### Programmatically accessing this new API

I am going to use Python to demonstrate the usage of this new API `detect_targeted_sentiment`. Please ensure that you have the most recent boto3 version installed, otherwise it will not work because this is a really new API.

#### Code
```
import boto3
import subprocess

session = boto3.Session()
comprehend_client = session.client(service_name='comprehend', region_name='us-east-2')
text="The hotel itself was beautiful and clean. I only give it 3 starts because paying $43 USD for parking is DISGUSTING!!!!!!! What a ripoff!!!!! As well, hotel cleaning staff don't clean the rooms anymore but yet the prices are still VERY high."
response = comprehend_client.detect_targeted_sentiment(
LanguageCode='en',
Text = text
)
print(response)
```
#### Output
![Image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fwrpjd463jnter8vcbxh.png)