Sentiment analysis with Sentiment npm module

Performing sentiment analysis on message text helps monitor potential issues within a given community and can help spot issues much quicker. That sounds great, but how can you go about implementing something like this?

Luckily there is an NPM module called sentiment that does just that. Sentiment uses the AFINN-111 word list composed of 2,477 words that are assigned a value from -5 to 5. The lower the number, the more negative the word is generally perceived. After running `npm install sentiment` you can get started with the following code:

var sentiment = require(‘sentiment’);
console.log(sentiment(“This module is pretty awesome!”));

Your output should look like this:

{ score: 5,
comparative: 1,
tokens: [ ‘this’, ‘module’, ‘is’, ‘pretty’, ‘awesome’ ],
words: [ ‘awesome’, ‘pretty’ ],
positive: [ ‘awesome’, ‘pretty’ ],
negative: [] }

From the above example, the score is the total of the value assigned to each word and comparative is the average. With this data you can quickly determine how a message might be perceived and set alerts if there are negative messages or store this information and turn it into a graph of mood over time. That should hopefully be enough to get you started with sentiment analysis in your personal projects!