diff --git a/Readme.md b/Readme.md index b4450f7..53cb80c 100644 --- a/Readme.md +++ b/Readme.md @@ -1,10 +1,10 @@ [![Build Status](https://secure.travis-ci.org/thinkroth/Sentimental.png)](http://travis-ci.org/thinkroth/Sentimental) # SentiMental - Putting the Mental in Sentimental - + Sentiment analysis tool for node.js based on the [AFINN-111 wordlist](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010). Version 1.0 introduces performance improvements making it both the first, and now fastest, AFINN backed Sentiment Analysis tool for node. - + ## Install $ npm install Sentimental @@ -13,6 +13,7 @@ * Positivity ranking * Negativity ranking * Analyze - combines Positivity and Negativity ranking into an aggregate sentiment score + * Inject changes to AFINN library programmatically ## Example ```js @@ -27,10 +28,10 @@ analyze("I am happy"); //Score: 3, Comparative: 1 analyze("I am so happy"); //Score: 6, Comparative: 1.5 analyze("I am extremely happy"); //Score: 12, Comparative: 3 analyze("I am really sad"); //Score: -4, Comparative: -1 +analyze("I made 'Good' a 0 since it is in my brand name.",{"good":0}); // This will change the score and persist through the rest of the application +analyze("I added noob to the AFINN!",{"noobie":-5}); ``` - - ## Running Tests To run the test suite first invoke the following command within the repo, installing the development dependencies: @@ -42,8 +43,7 @@ then run the tests: $ make test - -## License +## License (The MIT License) diff --git a/lib/sentimental.js b/lib/sentimental.js index e2211e8..bf6ff9f 100644 --- a/lib/sentimental.js +++ b/lib/sentimental.js @@ -1,4 +1,5 @@ var afinn = require('../wordLists/afinn.json'); +var assign = require('lodash/assign'); var tokenizeWithNoPunctuation = function (phrase) { var noPunctuation = phrase.replace(/[^a-zA-Z ]+/g, ' ').replace('/ {2,}/',' '); @@ -8,12 +9,13 @@ var tokenizeWithNoPunctuation = function (phrase) { // Calculates the negative sentiment of a sentence // -------------------------------------------------- // -function negativity (phrase) { +function negativity (phrase, inject) { + validateInjection(inject); var addPush = function(t, score){ hits -= score; words.push(t); }; - + var tokens = tokenizeWithNoPunctuation(phrase), hits = 0, words = []; @@ -37,7 +39,8 @@ function negativity (phrase) { // Calculates the positive sentiment of a sentence // -------------------------------------------------- // -function positivity (phrase) { +function positivity (phrase, inject) { + validateInjection(inject); var addPush = function(t, score){ hits += score; words.push(t); @@ -66,10 +69,11 @@ function positivity (phrase) { // Calculates overall sentiment // -------------------------------------------------- // -function analyze (phrase) { +function analyze (phrase, inject) { + validateInjection(inject); - var pos = positivity(phrase), - neg = negativity(phrase); + var pos = positivity(phrase, inject), + neg = negativity(phrase, inject); return { score : pos.score - neg.score, @@ -80,6 +84,21 @@ function analyze (phrase) { } +// Validates injection parameter, updates AFINN library scores programmatically (persists) +// -------------------------------------------------- // +function validateInjection(inject){ + if (typeof inject === 'undefined') { + inject = null; + } + if (inject != null){ + if (typeof(inject) != "object"){ + throw "Injection must be an object"; + } + assign(afinn,inject); + } +} + + module.exports = { analyze : analyze, negativity : negativity, diff --git a/package.json b/package.json index 5dbf2a8..94f3700 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,15 @@ "name": "Sentimental", "description": "Sentiment Analysis module", "repository": "git://github.com/thinkroth/Sentimental", - "version": "1.0.1", + "version": "1.0.2", "author": "Kevin M Roth", + "contributors": [{ + "name" : "Samuel Anthony Custer", + "email" : "samuel.a.custer@gmail.com", + "url" : "https://samcus.co" + }], "dependencies": { + "lodash": "^4.17.2" }, "scripts": { "test": "make test"