Getting Started with IBM Watson Tone Analyzer

Bayo Opesanya
4 min readMar 23, 2018

--

In this tutorial, we’ll be looking at the Watson Tone Analyzer service. I assume you already have an IBM Cloud account. If you don’t, go through my Getting Started with IBM Watson tutorial to create one. It’s pretty straightforward.

The Tone Analyzer service uses linguistic analysis to detect joy, fear, sadness, anger, analytical, confident and tentative tones in user inputs (text). Knowing that communication is incomplete without taking tone into consideration, we can glean the importance of such a service.

I can think of the following use cases for this service:

  • A logistics company wants to know the emotions their customers express on Twitter regarding their product. They can generate tweets that either contain their username or hashtags and run the tweets through the service to get this information.
  • You want to send a message via email but don’t want to give off a wrong impression by giving off an angry tone? You can copy/paste the email content before sending it to check the tone of the message.
  • A company wants to find out how its customer care unit has been doing with telephone calls. The conversations can be transcribed and sent through the service to get tone insights and see who in the team needs to be retrained.

Any more ideas? You can use the comment section.

To demonstrate the power of this service, I created an application that does social listening on Twitter using a hashtag of choice and gives tone analysis based on curated tweets. You can check it out on Github. There’s a demo of the service by IBM here

First step is to create a Tone Analyzer service. Visit the IBM Cloud Catalog, select Tone Analyzer and create the service. Then, create service credentials, and we are ready to go! If you have any creating the service and generating credentials, visit my Getting Started with IBM Watson tutorial.

Next step will be to create a Twitter application and generate credentials for your app. Visit the Twitter Developers platform to create an application and generate credentials for your application. We’ll be writing code in Node.js, so make sure you have Node.js and NPM installed on your computer.

To start using the Node.js SDK for IBM Watson, open your terminal (or command line), navigate to your project folder, and run npm i watson-developer-cloud — save. You should have watson-developer-cloudadded to your package.json file.

Adding IBM Watson Node.js SDK to your project

Once this is done, you can now write the following code to your project (save file as toneAnalyzer.js) to use the Tone Analyzer service. Please note that I am loading my credentials from a .env file, and i’m using the dotenv module. You can run npm i dotenv --save to add this to your package.json file and be able to use it in your project. Your code should look like this now:

Now it’s time to call the service and send some text to it. To get more details of how to call the service and what options you have available, you can visit the service API documentation. It’s concise.

We’ll use the readline module so we can receive input to our application through the command line. Let’s update our code accordingly:

now run node toneAnalyzer in your terminal to run your application. You should see something like this:

Then type in the text you want Watson to analyze, and hit Enter. You should get a JSON response like this:

response from Tone Analyzer

We can see that Watson gave results based on the general tone of the text I typed in, and results based on each sentence. Based on what I typed in, there were three emotions for the general tone of the text (Sadness, Confident and Analytical) and they had scores (0.745764, 0.640967, 0.912662) respectively. We can also see that I entered three sentences, and the tones which were detected in each sentence were also given by Watson.

Of course, we can do more complex stuff, but this is essentially a basic introduction to using the service. Like I said earlier, you can access the source code for an application I built with the service on Github.

I hope this tutorial was helpful. If you have any questions, you can drop a comment below.

I’ll be working on some basic applications in subsequent posts, so get ready for an interesting ride!

Also follow me on Twitter if you would like to send a DM.

--

--

Bayo Opesanya

Backend Software Engineer, Country Director at Node.js Nigeria, AI/ML Enthusiast, Writer