Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/sentimental.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var afinn = require('../wordLists/afinn.json');


// Calculates the negative sentiment of a sentence
// -------------------------------------------------- //

Expand All @@ -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 = [];
Expand Down Expand Up @@ -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 = [];
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

Expand Down Expand Up @@ -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();
});
});
});

Expand Down