Get the emotion behind a conversation with IBM Watson

Get the emotion behind a conversation with IBM Watson

Using the IBM Watson Tone Analyser API to analyse the Tone of chats and sentences.

·

7 min read

“It's not a faith in technology. It's faith in people.” Steve Jobs, Co-founder of Apple

Objectives

At the end of this article you should be able to:

  • Access the IBM Watson Tone Analysis API.
  • Find the emotional tone behind a sentence.
  • Find the emotional Tone with messages in a chat conversation.

IBM Watson is a PAAS that is used to develop, deploy and scale AI-powered applications. Today we are going to use Watson to analyse the tone of sentences.

Before we begin I will give a simple background on tone analysis

tone_intro.png Above is a picture showing a computer classifying human emotions

In simple words, Tone Analysis involves the use of AI to detect seven distinct conversational tones. The seven conversational tones include; frustration, impoliteness, sadness, sympathy, politeness, satisfaction and excitement. To get a more in-depth understanding of tone analysis, click here

Tone Analysis is not Sentiment Analysis

People usually mistake tone analysis for sentiment analysis but in reality, they are subtly different concepts.

Sentiment analysis tools are typically used to determine the tone of short social media communication like tweets, stays updates etc. Based on the usage of words the social media content is tagged as positive, negative and neutral.

Tone analysis involves a more detailed document-level analysis of larger documents or communication. It primarily reveals the author's writing style and the social and emotional sentiment of the article or communication. This is typically recommended on marketing material, employee commination, chat transcripts etc. to ensure that the document is in line with strategy or standards expected.

Now that we've given a base for the topic let's look at...

The Prerequisites for this Article

  • Knowledge of Python and use of pip.
  • Internet Connection.
  • An email address (to get an IBMId).
  • A text editor or Jupyter Notebook.

Before we continue you need to have an IBMID, if you don't have one, click here to get one.

Getting an IBMID is essential because we will be using the IBM text-to-speech API to build our software if you have never used IBM PAAS don't fret I'll walk you through it.

Now you've gotten your IBMID it's time to.....

Get your API Key

Here I am going to explain how to get your API Key so you can make use of the API

First: You go to the IBM Cloud Catalog page. IBM CATALOG Landing.png Above is a picture of the IBM Cloud Catalog page.

Second:In the category drop down you will see services offered by IBM Watson like Compute, Containers, Networking, Storage etc.

We are going to make use of the AI & Machine learning service because that is where our tone analyser is located, so click on the AI and Machine Learning link. AI and ML Page .png Above is a picture which shows the AI and Machine Learning page

Third:Once you click on the AI and Machine Learning link you will then be given a Catalog of Watson's AI and Machine Learning services.

Amongst the services offered in the AI and Machine Learning category you will see services such as Watson Assistant, Watson Studio, Knowledge Studio, etc click on the Tone Analyser service.

Tone Analyser SERVICE LINK IN AI and ML PAGE.png Above is a picture showing the Tone Analyser service in the AI and Machine Learning page

Fourth:Once you have clicked on the Tone Analyser service you will be taken to the page displayed below. Tone_Analyser create page.png

Now, this is where we make use of that IBMID I asked you to create earlier, to make use of the IBM Watson Tone Analyser API we need an IBMID. If you haven't created one, click here to get one.

If you have gotten your IBMID look towards to top right corner of the page and click on the Log in link.

If you have successfully logged in or signed up(depending on your situation) then click on the blue create button you will be taken to the Tone Analyser API page.

Once you are on the page look towards the left part of your screen you will notice 5 options -Manage, Getting Started, Service Credentials, Plan and Connections- click on the Manage option and you will be taken to a page where you will given your API Key and your URL, now don't delete the page we will make use of the API Key and the URL later in the tutorial.

Below is a picture showing the API key and URL located in the manage option. Tone_Analyser End Point URL.png

I'm happy to say you have gotten access to the IBM Watson Tone Analyser API

Let's Get Building

Before we get building, if you at any point during the tutorial get confused you can:

  • View the article's GitHub repository by clicking here.
  • Edit and Play around with the code in google's colab by clicking here.

    Project Flow

    Below I will explain the steps which this project is going to take
  • Install Dependencies:: Here we are going to install the necessary python packages needed to use the API.
  • Authenticate:Here we are going to provide our API credentials and link to the API.
  • Analyze Tone:Here we are going to analyze the tone of a particular sentence.
  • Analyze Chat Tone:Here we are going to analyze the tone of chats. Let us Begin

    Install Dependencies

    I am assuming you have python, pip and a text editor or jupyter notebooks installed.

If you have the above-listed let's begin, to make use of the IBM Text-to-Speech API we are going to install the IBM-Watson library, by typing in the command listed below in your terminal or command line.

pip install ibm-watson

If you are using the jupyter notebook environment you can put down the command listed below in a cell.

!pip install ibm-watson

Authenticate

In this section, we are going to link to the IBM Watson Tone Analysis API.

To begin we are going to put in the URL and the API key given to us on the Tone Analysis page. So go back to the page with your API Key and URL and copy your API Key and your URL.

Below is a picture showing the page containing the API key and the URL

Tone_Analyser End Point URL.png

url = 'https://api.eu-gb.text-to-speech.watson.cloud.ibm.com/instances/d04b5ea5-e6b9-4'

apikey = 'McPLvaUHWywOhceFBH4inrgnvirrwoomckw[omlap'

#I have used a fake URL and API key.

Now we are going to import the necessary tools

from ibm_watson import ToneAnalyzerV3

from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

Next we are going to set up our Tone Analysis Service so we can make use of it

#setup service
authenticator = IAMAuthenticator(apikey)

#Tone Analyser service
ta = ToneAnalyzerV3(version='2017-09-21', authenticator=authenticator )

#service URL
ta.set_service_url(url)

We have successfully set up our Tone Analyser service let's...

Analyze Tone

Here we are going to analyse the tone of a sentence to see if the sentence was made out of sadness, anger, happiness etc.

res = ta.tone('This sucks, I hate my job so much').get_result()

In the above code we called the tone object from the Ton Analyser API and then we put in our sentence and we called get_result to give us the tone of the sentence.

Now we are going to get the tone of the sentence according to the Tone Analyser service, we will do that by calling on our res variable.

res

Now you'll get the tone of whatever sentence you wrote

We have successfully analysed the tone of a sentence now let's see if we can...

Analyze Chat Tone

Here we going to analyse the tone of a chat conversation.

First, we are going to put the chat conversation in the form of a python dictionary

chat = [
    {
        "text":"I feel great, it's sunny outside and I have gotten all my work done.",
        "user":"He who shall not be named"
    },
      {
        "text":"This sucks, I have like 500 hundred hours of coding left this sucks.",
        "user":"jack"
    }
]

We are going to be analysing a chat conversation between two people named He who shall not be named and jack respectively.

Now, we are going to call on the tone object the same way we did when analysing a sentence but now we will call the tone_chat object since we are analysing a chat conversation.

res = ta.tone_chat(chat).get_result()

Next up we are going to call on the result of the analysis by calling the res variable the same way we did when analysing a sentence.

res

When you call up the res variable then you will see a detailed analysis of every message in the conversation.

That wraps it up for Tone Analysis using IBM Watson If you want to find other cool features of IBM Watson's Tone Analyser check out their comprehensive docs by clicking here

I want to thank Tarek Jellali for reading drafts for this article and making necessary corrections❤.

Happy Building🚀