Pages

Wednesday, May 31, 2017

Measuring Twitter Sentiment with Watson Natural Language Understanding

View Comments
It's been a couple of months now since my last project on Node.js . The other day I wanted to set up an example of a dashboard with sentiment coming from Twitter around a keyword.

To keep things simple, I used IFTTT to export tweets to a CSV file. With that I could then use  Watson's libraries to measure sentiment. To start, I created a new trial account on BlueMix. Once there, go to ServicesWatsonCreate Watson service. Once there pick Natural Language Understanding and it will create an instance of the service. This Watson service helps to make sense out of pretty much any text.

The screen should look like this:



With the tweets now in a CSV and Watson at the ready, it was time to put the pieces together with node.js. I took advantage of the following node.js libraries: csv-parser, csv-write-stream, watson-developer-cloud.

Let me walk through the code (which I'll put on Github soon). First, import and setup the libraries (you get the Watson credentials directly from Bluemix and my preference is to store them as environment variables):


Next, read the CSV, parse it, measure it and write a new CSV out all with this one line of code:


The function sentimentFromWatson is key above. That function takes advantage of the Watson library already available. Here's what that function looks like:


And that's it! In total, you have a quick sample in 20-30 lines of code to measure sentiment from Twitter. Below is the graphic that I created. In a span of a weekend, the keyword that I was monitoring had some positive tweets and others that were negative. Overall, that weekend was pretty neutral.



(Note: the free plan for Watson Natural Language Understanding gives you 1,000 calls / day. If you expect more than 1,000 tweets / day you'll need a paid plan).

Overall, this took me about 30 minutes to put together and a bit of troubleshooting. If you haven't played with Node.js and Watson yet, go for it!
blog comments powered by Disqus