Permalink
Browse files

Improved handling of punctuation

  • Loading branch information...
1 parent 96dba82 commit 99656edbdbf00f0c25968dc186f0fee39f94f91b @thinkroth committed Mar 10, 2012
Showing with 9 additions and 7 deletions.
  1. +4 −7 lib/sentimental.js
  2. +5 −0 test/test.js
View
@@ -9,22 +9,19 @@ var neg5 = require('../wordLists/neglist.js').neg5,
pos2 = require('../wordLists/poslist.js').pos2,
pos1 = require('../wordLists/poslist.js').pos1;
-// -------------------------------------------------- //
-// -------------------------------------------------- //
// Calculates the negative sentiment of a sentence
// -------------------------------------------------- //
-
function negativity (phrase) {
var addPush = function(t, score){
hits += score;
words.push(t);
};
- // Cache it
- var tokens = phrase.toLowerCase().split(" "),
+ var noPunctuation = phrase.replace(/[^\w\s]|_/g,"").replace(/\s+/g," "),
+ tokens = noPunctuation.toLowerCase().split(" "),
hits = 0,
words = [];
@@ -59,8 +56,8 @@ function positivity (phrase) {
words.push(t);
};
- // Cache it
- var tokens = phrase.toLowerCase().split(" "),
+ var noPunctuation = phrase.replace(/[^\w\s]|_/g,"").replace(/\s+/g," "),
+ tokens = noPunctuation.toLowerCase().split(" "),
hits = 0,
words = [];
View
@@ -93,6 +93,11 @@ describe('Analyze', function () {
analyze("Hey scumbag").score.should.equal(-4);
done();
});
+ it('should ignore punctuation', function (done) {
+ analyze("Fearless!").score.should.equal(2);
+ analyze("Crash!").score.should.equal(-2);
+ done();
+ });
});
describe('#comparative', function () {
it('should equal (positive - negative) / word count ', function (done) {

0 comments on commit 99656ed

Please sign in to comment.