Skip to content
Permalink
Browse files

Add basic sentiment negation

This is a pretty simple change that seems to work quite well for most inputs to handle negation. 

This accounts for the different between things like "I love cats" and "I don't love cats", "I can't love cats", "I won't love cats". 

This approach could be extended to also include double negatives, but let's leave it simple for now.
  • Loading branch information...
akumpf committed Dec 16, 2014
1 parent cb63052 commit 23407526a146efbfdd9929e9bd74462e65d52ada
Showing with 14 additions and 1 deletion.
  1. +14 −1 lib/index.js
@@ -11,6 +11,13 @@
var extend = require('extend-object');
var afinn = require('../build/AFINN.json');

/**
* These words fundamentally change the nature of the following word (and flip it's sentiment.)
*/
var negators = {
"dont":1, "doesnt":1, "not":1, "non":1, "cant":1, "wont":1
}

/**
* Tokenizes an input string.
*
@@ -59,7 +66,13 @@ module.exports = function (phrase, inject, callback) {
var obj = tokens[len];
var item = afinn[obj];
if (!afinn.hasOwnProperty(obj)) continue;


// check for negation.
if(len > 0){
var prevtoken = tokens[len-1];
if(negators[prevtoken]) item = -item;
}

words.push(obj);
if (item > 0) positive.push(obj);
if (item < 0) negative.push(obj);

0 comments on commit 2340752

Please sign in to comment.
You can’t perform that action at this time.