Make your Rasa Chatbots more human by using Sentiment Analysis…

Simran Kaur Kahlon
Gray Matrix
Published in
3 min readDec 25, 2019
Image Source — Google

What is Sentiment Analysis?

Sentiment Analysis is a process of analyzing data and classifying it as a positive, negative or neutral. It helps businesses to understand their customers better, how they feel about their products and services.

Why is Sentiment Analysis needed in ChatBots?

Chatbots are the next big thing in customer support. Firstly you would want them to handle the customer queries and answer them correctly. Once you have achieved effective communication, the next step would be to improve the user experience.

After all, just providing the right answers won't create a delightful experience for your customers. This is where the sentiment analysis comes into the picture. With this, the chatbot can understand how well the conversation is going and it can respond basis on the user's emotions.

If an already agitated user is complaining about your product, as a human customer representative you would handle this situation and would calm down the person by using the right kind of words/actions, the same intellectual is something we would like to build in our bots.

Also, sentiment analysis can prove to be helpful to understand when the chatbot can no longer handle the conversation and a human agent must intervene. The chatbot can keep a tab of the whole context and if the conversation is going too negative, it can redirect the customer to the agent.

To implement this kind of analysis I would be using AWS Comprehend.

AWS Comprehend is a machine learning-powered service that makes it easy to find insights and relationships in a text. It extracts entities in your statement, its language, key phrases used, the sentiment of the text.

It provides the following sentiments -

  • Neutral
  • Positive
  • Negative
  • Mixed

We would be using boto3 library to use comprehend in Rasa.

You could use it in your Rasa Actions, or maybe in the form submit method if you want the sentiment to work only for specific use cases. But it makes more sense to capture the user emotions throughout the conversation.

So for this, it's better to have this analysis done as a part of Rasa NLU Engine.

We can create a custom RASA component for this and add its output to Rasa NLU.

So let's begin with creating this component.

  1. We will start with creating a sentiment_analysis.py file with the following code -

We are implementing the Component class of Rasa in our SentimentAnalyzer class.

The code goes as follows :

Line 28 We override its process method.

Line 33 detect_sentiment method is being used and we pass the user text to it.

We extract the required fields and convert it to a suitable rasa format in the convert_to_rasa function and finally add it to the entities key of the NLU output.

2. We add this custom pipeline to the config file as:

Here, the positioning of the component doesn’t play any role, unless you want the extracted component to be used by other pipelines. Say, for example, if the output of sentiment pipeline will be used by the whitespace featurizer, then it should be placed above it.

3. So that’s it. Let’s run rasa shell nlu to see the sentiment output:

We get the message sentiment as NEGATIVE and its confidence as 97.73% in the entities key.

This way you can configure your bot to not only answer customer queries but also add a custom touch in the responses for more human-like conversations and enhancing customer experience.

Please get in touch in case of any queries.

Thanks.

--

--

Simran Kaur Kahlon
Gray Matrix

JS/ Laravel / AWS / Chatbot Developer #AWS Solution Architect Associate #AWS Developer Associate