Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AFINN Injections #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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:
Expand All @@ -42,8 +43,7 @@ then run the tests:
$ make test



## License
## License

(The MIT License)

Expand Down
31 changes: 25 additions & 6 deletions lib/sentimental.js
Original file line number Diff line number Diff line change
@@ -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,}/',' ');
Expand All @@ -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 = [];
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : "[email protected]",
"url" : "https://samcus.co"
}],
"dependencies": {
"lodash": "^4.17.2"
},
"scripts": {
"test": "make test"
Expand Down