Processing Text for Sentiment Analysis
Understanding emotion and communication is possible with Watson’s Tone Analyzing service. This post is a step-by-step guide to creating a messaging bot that returns sentiment based on inputted text.
Technologies: (Node + Express + Watson)
These are the steps you can take to create your sentiment-analyzing bot:
- Create a new messaging application.
- Create IBM Cloud credentials.
- Create an express/node server that serves a POST endpoint.
- Add
watson-developer-cloud
to your project. - Point your endpoint to the messaging application’s event subscriptions.
- Add your bot to a channel. And start communicating.
This is the configuration for the node
/ express
. This project uses a digital ocean droplet, pm2
and nginx
reverse proxy for this endpoint.
const express = require(‘express’);
const bodyParser = require(‘body-parser’);
const request = require(‘request’);
const app = express();app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));const server = app.listen(4000);
The POST event returns URL verification and handles event callbacks to the events api:
app.post('/event', (req, res) => {
switch(req.body.type) {
case 'url_verification':
res.send(req.body.challenge);
break;
case 'event_callback':
const text = req.body.event.text;
const channel = req.body.event.channel;
analyzeTone(channel, text.replace('<@BOT_ID_GOES_HERE>',''))
break;
}
});
A map was created for certain emotions into emojis:
const emotions = {
anger : '😠',
disgust : '🤢',
fear : '😱',
joy : '😄',
sadness : '😞'
}
The score for the tone in the range of 0.5 to 1. A score greater than 0.75 indicates a high likelihood that the tone is perceived in the utterance.
The unique, non-localized identifier of the tone for the results. The service can return results for the following tone IDs: sad, frustrated, satisfied, excited, polite, impolite, and sympathetic. The service returns results only for tones whose scores meet a minimum threshold of 0.5.function.
The confidence threshold >0.55
for this particular case. You can learn more about tone and utterance scores here.
You would need to communicate with the application programming interface and send out a POST
:
function postMessage(channel, message) {
let options = {
method: 'POST',
url: 'POST+MESSAGE+URL',
form: {
token: 'AUTH_TOKEN_GOES_HERE',
channel,
text: message
}
};
request(options, (error, response, body) => {
console.log(response.body)
})
}
Test and start communicating with your bot.
We can understand more about text processing with this endpoint.
Enjoy!