diff --git a/lib/sentimental.js b/lib/sentimental.js index 5386927..8e5e774 100644 --- a/lib/sentimental.js +++ b/lib/sentimental.js @@ -1,6 +1,5 @@ var afinn = require('../wordLists/afinn.json'); - // Calculates the negative sentiment of a sentence // -------------------------------------------------- // @@ -9,8 +8,8 @@ function negativity (phrase) { hits -= score; words.push(t); }; - - var noPunctuation = phrase.replace(/[^a-zA-Z ]+/g, ' ').replace('/ {2,}/',' '), + + var noPunctuation = phrase.replace(/[^a-zA-Z0 -]+/g, ' ').replace('/ {2,}/',' '), tokens = noPunctuation.toLowerCase().split(" "), hits = 0, words = []; @@ -40,7 +39,7 @@ function positivity (phrase) { words.push(t); }; - var noPunctuation = phrase.replace(/[^a-zA-Z ]+/g, ' ').replace('/ {2,}/',' '), + var noPunctuation = phrase.replace(/[^a-zA-Z -]+/g, ' ').replace('/ {2,}/',' '), tokens = noPunctuation.toLowerCase().split(" "), hits = 0, words = []; diff --git a/test/test.js b/test/test.js index 553e831..de2ed54 100644 --- a/test/test.js +++ b/test/test.js @@ -44,6 +44,14 @@ describe('Negativity', function () { negativity("I'll be here till 5").score.should.equal(0); done(); }); + it('should properly handle hyphenated words', function (done) { + negativity("you are self-deluded").score.should.equal(2); + done(); + }); + it('should properly handle n00b (the only word in the AFINN list with a number)', function (done) { + negativity("you are a n00b").score.should.equal(2); + done(); + }); }); }); @@ -88,6 +96,11 @@ describe('Positivity', function () { positivity("I'll be here till 5").score.should.equal(0); done(); }); + + it('should properly handle hyphenated words', function (done) { + positivity("it was a once-in-a-lifetime day").score.should.equal(3); + done(); + }); }); });