Create a Tone Analyzer using node.js and IBM's Watson.

Michael Carneal
4 min readAug 26, 2019

IBM’s Watson is one of the most powerful AI’s around today. On January 14th 2011 Watson was the victor in a world famous round of Jeopardy, proving that AI can go toe to toe with the brightest minds of today.

Fortunately it does not take a Jeopardy champion or a mathematician to start working with and using Watson. IBM has provided an extensive API that allows users from all over to plug their apps into Watson and start using its wonderful data analysis tools.

For this article we will be using Node to connect to IBM. JavaScript happens to be my favorite language due to its versatility but other languages can and will connect to IBM’s Watson tool kit just fine.

To start register and login to IBM’s website. For this tutorial we will be creating a tone analyzer. We will need to generate an API key for security. Follow the prompts here https://www.ibm.com/watson/services/tone-analyzer/ to get started with the key and region.

This tool will require three credentials to gain access. A version, URL, and API key. The version and URL may vary depending on when and where you are using this tool. For this project we will be using Version ‘2017–09–21’ and the URL ‘https://gateway.watsonplatform.net/tone-analyzer/api’. For the API key I would recommend putting it in a .ENV file.

If you are new to JavaScript a .env file is a hidden environment that you can import from. I wont be going over the setup and implementation of .env but some quick Google action should get you in a safe place. If this is a home project and not being deployed to github feel free to hard code the API. But for best practice and security I recommend creating a .env file.

Alright lets begin!

Within the directory you choose to create this app lets install the watson packages

npm install ibm-watson

Now lets create a blank .js file and start coding.

The first thing we want to do is import our tone analyzer tool.

const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');

Now lets pass our credentials into this function to gain access. Again for the sake of this demo the api key is in another file but you can hard code it if youd like.

const toneAnalyzer = new ToneAnalyzerV3({
version: '2017-09-21',
iam_apikey: API_KEY,
url: 'https://gateway.watsonplatform.net/tone-analyzer/api'
});

Now lets build some text that we will pass to our toneAnalyzer.

const text = "Good morning everyone. I see that our quarter four metrics are lower than expected. I know we have had a tough year and moral is down. Can we think of ways to increase our performance next year";const toneParams = {
tone_input: { 'text': text },
content_type: 'application/json',
};

We have our text and we are including it in a new variable called toneParams. toneParams will be an object we send to toneAnalyzer that will than send a JSON back to us with a break down of the text.

toneAnalyzer.tone(toneParams)
.then(toneAnalysis => {
console.log(JSON.stringify(toneAnalysis, null, 2));
})
.catch(err => {
console.log('error:', err);
});

We should now see the following JSON as a result.

{
"document_tone": {
"tones": [
{
"score": 0.50949,
"tone_id": "joy",
"tone_name": "Joy"
},
{
"score": 0.927488,
"tone_id": "analytical",
"tone_name": "Analytical"
}
]
},
"sentences_tone": [
{
"sentence_id": 0,
"text": "Good morning everyone.",
"tones": [
{
"score": 0.845073,
"tone_id": "joy",
"tone_name": "Joy"
}
]
},
{
"sentence_id": 1,
"text": "I see that our quarter four metrics are lower than expected.",
"tones": [
{
"score": 0.653099,
"tone_id": "analytical",
"tone_name": "Analytical"
}
]
},
{
"sentence_id": 2,
"text": "I know we have had a tough year and moral is down.",
"tones": [
{
"score": 0.696397,
"tone_id": "sadness",
"tone_name": "Sadness"
},
{
"score": 0.620279,
"tone_id": "analytical",
"tone_name": "Analytical"
}
]
},
{
"sentence_id": 3,
"text": "Can we think of ways to increase our performance next year",
"tones": [
{
"score": 0.501672,
"tone_id": "joy",
"tone_name": "Joy"
},
{
"score": 0.944551,
"tone_id": "analytical",
"tone_name": "Analytical"
}
]
}
]
}

We have successfully sent a string of text over to watson, had it break down our sentences and return a object containing the tone type and a score of each sentence.

This method is obviously a more simplistic way of capturing this data. For a programmer looking for a new project, Imagine how you would take a users input, send it over to watson and return this JSON in a more easy to read format using React and Node.js.

Hopefully this article helps you dive a little bit deeper into the Watson API. We have only scratched the surface of what is capable with this toolkit.

Thank you for reading.

--

--