Giter Club home page Giter Club logo

sentiment's Introduction

sentiment

AFINN-based sentiment analysis for Node.js

CircleCI codecov Greenkeeper badge

Sentiment is a Node.js module that uses the AFINN-165 wordlist and Emoji Sentiment Ranking to perform sentiment analysis on arbitrary blocks of input text. Sentiment provides several things:

  • Performance (see benchmarks below)
  • The ability to append and overwrite word / value pairs from the AFINN wordlist
  • The ability to easily add support for new languages
  • The ability to easily define custom strategies for negation, emphasis, etc. on a per-language basis

Table of contents

Installation

npm install sentiment

Usage example

var Sentiment = require('sentiment');
var sentiment = new Sentiment();
var result = sentiment.analyze('Cats are stupid.');
console.dir(result);    // Score: -2, Comparative: -0.666

Adding new languages

You can add support for a new language by registering it using the registerLanguage method:

var frLanguage = {
  labels: { 'stupide': -2 }
};
sentiment.registerLanguage('fr', frLanguage);

var result = sentiment.analyze('Le chat est stupide.', { language: 'fr' });
console.dir(result);    // Score: -2, Comparative: -0.5

You can also define custom scoring strategies to handle things like negation and emphasis on a per-language basis:

var frLanguage = {
  labels: { 'stupide': -2 },
  scoringStrategy: {
    apply: function(tokens, cursor, tokenScore) {
      if (cursor > 0) {
        var prevtoken = tokens[cursor - 1];
        if (prevtoken === 'pas') {
          tokenScore = -tokenScore;
        }
      }
      return tokenScore;
    }
  }
};
sentiment.registerLanguage('fr', frLanguage);

var result = sentiment.analyze('Le chat n\'est pas stupide', { language: 'fr' });
console.dir(result);    // Score: 2, Comparative: 0.4

Adding and overwriting words

You can append and/or overwrite values from AFINN by simply injecting key/value pairs into a sentiment method call:

var options = {
  extras: {
    'cats': 5,
    'amazing': 2
  }
};
var result = sentiment.analyze('Cats are totally amazing!', options);
console.dir(result);    // Score: 7, Comparative: 1.75

API Reference

var sentiment = new Sentiment([options])

Argument Type Required Description
options object false Configuration options (no options supported currently)

sentiment.analyze(phrase, [options], [callback])

Argument Type Required Description
phrase string true Input phrase to analyze
options object false Options (see below)
callback function false If specified, the result is returned using this callback function

options object properties:

Property Type Default Description
language string 'en' Language to use for sentiment analysis
extras object {} Set of labels and their associated values to add or overwrite

sentiment.registerLanguage(languageCode, language)

Argument Type Required Description
languageCode string true International two-digit code for the language to add
language object true Language module (see Adding new languages)

How it works

AFINN

AFINN is a list of words rated for valence with an integer between minus five (negative) and plus five (positive). Sentiment analysis is performed by cross-checking the string tokens (words, emojis) with the AFINN list and getting their respective scores. The comparative score is simply: sum of each token / number of tokens. So for example let's take the following:

I love cats, but I am allergic to them.

That string results in the following:

{
    score: 1,
    comparative: 0.1111111111111111,
    calculation: [ { allergic: -2 }, { love: 3 } ],
    tokens: [
        'i',
        'love',
        'cats',
        'but',
        'i',
        'am',
        'allergic',
        'to',
        'them'
    ],
    words: [
        'allergic',
        'love'
    ],
    positive: [
        'love'
    ],
    negative: [
        'allergic'
    ]
}
  • Returned Objects
    • Score: Score calculated by adding the sentiment values of recognized words.
    • Comparative: Comparative score of the input string.
    • Calculation: An array of words that have a negative or positive valence with their respective AFINN score.
    • Token: All the tokens like words or emojis found in the input string.
    • Words: List of words from input string that were found in AFINN list.
    • Positive: List of positive words in input string that were found in AFINN list.
    • Negative: List of negative words in input string that were found in AFINN list.

In this case, love has a value of 3, allergic has a value of -2, and the remaining tokens are neutral with a value of 0. Because the string has 9 tokens the resulting comparative score looks like: (3 + -2) / 9 = 0.111111111

This approach leaves you with a mid-point of 0 and the upper and lower bounds are constrained to positive and negative 5 respectively (the same as each token! 😸). For example, let's imagine an incredibly "positive" string with 200 tokens and where each token has an AFINN score of 5. Our resulting comparative score would look like this:

(max positive score * number of tokens) / number of tokens
(5 * 200) / 200 = 5

Tokenization

Tokenization works by splitting the lines of input string, then removing the special characters, and finally splitting it using spaces. This is used to get list of words in the string.


Benchmarks

A primary motivation for designing sentiment was performance. As such, it includes a benchmark script within the test directory that compares it against the Sentimental module which provides a nearly equivalent interface and approach. Based on these benchmarks, running on a MacBook Pro with Node v6.9.1, sentiment is nearly twice as fast as alternative implementations:

sentiment (Latest) x 861,312 ops/sec ±0.87% (89 runs sampled)
Sentimental (1.0.1) x 451,066 ops/sec ±0.99% (92 runs sampled)

To run the benchmarks yourself:

npm run test:benchmark

Validation

While the accuracy provided by AFINN is quite good considering it's computational performance (see above) there is always room for improvement. Therefore the sentiment module is open to accepting PRs which modify or amend the AFINN / Emoji datasets or implementation given that they improve accuracy and maintain similar performance characteristics. In order to establish this, we test the sentiment module against three labelled datasets provided by UCI.

To run the validation tests yourself:

npm run test:validate

Rand Accuracy

Amazon:  0.726
IMDB:    0.765
Yelp:    0.696

Testing

npm test

sentiment's People

Contributors

blazergame avatar craigthomasfrost avatar elyas-bhy avatar evanhahn avatar greenkeeper[bot] avatar greenkeeperio-bot avatar jalners avatar joshbeckman avatar kant avatar kevinchappell avatar micahfulton avatar monteslu avatar oleoneto avatar pisaacs avatar rishpandey avatar thisandagain avatar thorbert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sentiment's Issues

Replace makefile with npm scripts

When I first built this thing I was using Makefiles for everything. While they work well for me, they cause a lot of problems for folks on Windows and other platforms where GNU Make is not available.

Project maintained?

Is this project still being maintained? The negation and emoji support would be helpful if it could be implemented.

AFINN phrases do not register

$ cat AFINN-111.txt | grep " "
can't stand -3
cashing in  -2
cool stuff  3
does not work   -3
dont like   -2
fed up  -3
green wash  -3
green washing   -3
messing up  -2
no fun  -3
not good    -2
not working -3
right direction 3
screwed up  -3
some kind   0
$ node -pe "require('sentiment')('it does not work').score"
0
``

Some negative words can be used to emphasize the meaning of another word.

e.g:

sentiment('this is good'); // score: 3
sentiment('this is crazy good'); // score: 1
sentiment('this is fucking good'); // score: -1

I don't think this is a duplicate of "Support for double negatives" (#109) as it's not really a "double negative", it just contains a word that could in some contexts be negative but in other contexts be used to emphasize the meaning of another word.

Ideally sentiment would understand that in the string this is fucking good the word fucking is emphasizing the word good.

So fucking good would have a score of good multiplied by the intensity of fucking.

So if good has a score of 3, crazy has an intensity of 1.2 and fucking has an intensity of 2. You'd get:

sentiment('this is good'); // score: 3
sentiment('this is crazy good'); // score: 3.6
sentiment('this is fucking good'); // score: 6

I don't really have a clue what I'm talking about so maybe this is way outside of the scope of this module. It would be great if this worked though.

Multiword Sentiments

Currently sentiment analyzer is not able to findout sentiments properly for multi-word like

"does not work": -3,

Below code is checking only one word at a time not multiple

var len = tokens.length;
    while (len--) { 
        var obj = tokens[len];
        var item = afinn[obj];
        if (!afinn.hasOwnProperty(obj)) continue;

        words.push(obj);
        if (item > 0) positive.push(obj);
        if (item < 0) negative.push(obj);

        score += item;
    }

Fixed the same in my code.

Support additional languages

From PR #93:

We'll need to establish a set of labeled sentences (see #103 as well as the original AFINN paper) in order be able to determine the accuracy of each translation. While Google Translate is great, sentiment can be pretty subtle and so I wonder if something like Pootle or Transifex might be able to help us source translations of the AFINN word list with greater confidence.

I've been thinking about ways to fund a CrowdFlower campaign to handle establishing labeled corpora for different languages. I don't think it would take much (maybe $60 - $120 USD) per language to get things started.

/cc @dkocich

Support for 2-grams

Hello,
I'm trying to override the AFINN scores for 2-grams, but it doesn't seem to work:

sentiment.analyze( 'This stuff is made up', { extras: { 'made up': -1 } } )

{ score: 0,
  comparative: 0,
  tokens: [ 'this', 'is', 'made', 'up' ],
  words: [],
  positive: [],
  negative: [] }

The effect is even more accentuated when a 2-gram would flip the overall score of a phrase; here "fucking good" reinforces a positive word, but the overall score is -1:

sentiment.analyze( 'This stuff is fucking good', { extras: { 'fucking good': 3 } } )
{ score: -1,
  comparative: -0.2,
  tokens: [ 'this', 'stuff', 'is', 'fucking', 'good' ],
  words: [ 'good', 'fucking' ],
  positive: [ 'good' ],
  negative: [ 'fucking' ] }
>

Would it be possible and a good idea to add support for overridden 2-grams?

Certain words are being modified by the regex pattern match, leading to inaccurate scores.

I was testing the the the library with the string "I'll be the one who killing the buy guys",
So, the replacement text .replace(/[^a-z0-9á-úñäâàéèëêïîöôùüûœç\-\' ]+/g, '') in tokenize.js is chancing "I'll" to "ill" and killing and ill addup and give a more negative score.

The change I made works for me, I did this .replace(/[^a-z0-9á-úñäâàéèëêïîöôùüûœç\-\' ]+/g, '')
but unfortunately it's failing 3 tests. I am not every good at regular expressions.

Let me know what you think.

Add Twitter validation dataset

We currently validate against a dataset from UCI that includes Amazon, Yelp, and IMDB. This is great but it would be nice to have less formal texts (particularly those that include emoji) included in validation. Various NLP areas are well explored using Twitter as a corpus so I don't think this should be too difficult to track down, but will require some research.

Evaluate Alternative Sentiment Lexicons

The AFINN-111 wordlist was published in 2011. While it works well for quite a large range of tasks, a few lexicons have been published since that may provide improved accuracy. Using this issue as a place to track the evaluation of each. A little background on alternate approaches:

http://sentiment.christopherpotts.net/lexicons.html
http://www.umiacs.umd.edu/~saif/WebPages/Abstracts/NRC-SentimentAnalysis.htm
http://dmir.inesc-id.pt/project/SentiLex-PT_02_in_English

remove async library

The code that does the scoring of the tokens isn't actually doing anything asynchronously.

Adding an async call for each iteration adds a bit more overhead.

I suggest leaving the callback at the top level, but just doing a simple forEach on the tokens.

Support smileys

Currently we have no way of processing smileys since they are stripped away before analysis (in tokenize.js).
Such a feature serves the same purpose as supporting emojis.

For example, here is a non-exhaustive list of smileys:

  • :)
  • :(
  • :D
  • :-D
  • ;)
  • :p

Spanish

Hello, actually for support your sentiment algorithm my api translate spanish to english and response a phrase in english, but I need to support spanish lenguage, The process is add the spanish words in AFINN.txt or I need to add in other places?.

Thanks

Negative expressions

At the moment the sentence "John is not a good person" is evaluated as with a positive score. It would be great if you could implement a "negative expression" detection and proper score.

Thanks

Provide calculation used to calculate score

New feature request: display the score calculation. Something like the following:

const sentiment = new Sentiment();
sentiment.analyze("Great job! 😃 👏", {debug: true} );  
// returns { score: 7, calculation: [{'👏': 2 }, {'😃': 2 }, {'great': 3 }] , ... }

Improve documentation

Related to #111 #107

  • Briefly describe the approach taken by AFINN
  • Briefly describe the how the tokenizer works
  • Briefly describe each property of the returned object

Support overwriting emoji

Love this package, but is there a way to be able to overwrite the value of emoji in a similar way to overwriting word values? Thanks!

Comparative and Score are coming out the same

I have version 2.1.0 on both my computer and on a server. On my computer it works, on my server, I get the following

{ score: 3,
comparative: 3,
tokens: [ 'happy' ],
words: [ 'happy' ],
positive: [ 'happy' ],
negative: [] }

{ score: -2,
comparative: -2,
tokens: [ 'sad' ],
words: [ 'sad' ],
positive: [],
negative: [ 'sad' ] }

{ score: 0,
comparative: 0,
tokens: [ 'ok' ],
words: [],
positive: [],
negative: [] }

Any thoughts?

Emoji Support

The AFINN wordlist is missing a fairly critical piece of contemporary discourse: the emoji. I'd like to add emoji support to sentiment. Major questions:

  • Do any approaches to evaluating the sentiment of emoji exist?
  • How do we tokenize emoji?
  • Should this be an "opt-in" feature?
  • How do we determine the accuracy of such an implementation?

Bundle for browser usage?

Nice work on this library! I found it helpful for one of my pet-projects, but when I tried to use it in the browser I saw you don't have a bundled version.

Have you considered using something like rollup to bundle you packages into a single deliverable?

Smiple Question

This is a very sweet project!

I would like to know how can I convert the comparative value to a whole number (percentage 100), where 100 is the most positive, 50 is neutral and 0 is most negative?

This can also be out of 10.

Update code formatting

Is maintaining compatibility with Node 4.x a priority, or could we bump it up to at least Node 6.9 (LTS)?
This would allow us to use the newer ES6 syntax, such as the class construct, const, let, arrow functions, etc. This would also be timely with the upcoming major release.

Furthermore, could we update the linter to allow 2-spaces indents and 100 max line length?

Negation Support

sentiment is a straightforward implementation of the AFINN wordlist which has some drawbacks including lack of support for negation (e.g. "that is not awesome"). This has become a fairly common question / feature request and something I'd like to support. The major questions in my mind are:

  • What are some successful approaches to negation in other sentiment analysis algorithms?
  • Where do we derive the negation wordlist from?
  • How do we implement negation in such a way that it does not adversely impact performance or at least makes the performance impact "opt-in" by allowing developers to enable the feature?

Allow tokenizer to split emoji without space delimiter

PR #74 initially incorporated a change which used spliddit's hasPair method to split emoji without a "space" or other delimiter. While useful, this change had fairly serious performance consequences. I think it's worthwhile to include this functionality, but it should be optional.

Giving wrong result when using negative word in positive way

Thanks for creating such a nice lib. but I regret to inform you that it is giving wrong results when a negative word is used in a positive manner.


var sentiment = require('sentiment');
 
var result = sentiment('I am dying to eat a kitkat!');
console.dir(result);   

///or

result = sentiment('your internet is not bad', knowladgeBase);
console.dir(result);

Please help

Missing something for "not"

"Not" can totally flip a string's meaning, but it and other similar adverbs are getting overlooked.

i.e.,

"I am not happy"

scores a 3, when, because of the negative adverb in there preceding the adjective, it should get a score of -3.

Allow token processing "middleware"

Hi,

It is possible to allow for a option which first finds string distances to words in the positive/negative list, and then, if it is above some threshold, categorize it as that word so spelling mistakes and/casual writing style are not lost.

e.g.

> sentiment('Cats are dumb');
{ score: -3,
  comparative: -1,
  tokens: [ 'cats', 'are', 'dumb' ],
  words: [ 'dumb' ],
  positive: [],
  negative: [ 'dumb' ] }
> sentiment('Cats are dumbbb');
{ score: 0,
  comparative: 0,
  tokens: [ 'cats', 'are', 'dumbbb' ],
  words: [],
  positive: [],
  negative: [] }

This example dumbbb is so close to dumb that it should be classified as such. Using a library like natural makes this easy.

require('natural').JaroWinklerDistance('dumb', 'dumbbb')
0.9333333333333333

If adding natural is out of scope, maybe a way that someone could inject it in some processing step could work too.

What do you think? Would this work?

Remove lodash

All we really need at this point is extend (which can be brought in directly) and the forEach iterator is best rewritten as a for loop (performance) anyway.

AFINN error

I keep getting the following error when I try to use your package:

ERROR in ./~/sentiment/build/AFINN.json
Module parse failed: /Users/joneslloyd/my_project/node_modules/sentiment/build/AFINN.json Unexpected token (1:10)

The JSON file in question looks to be correct though..

inflected words and adjectives in other languages.

This code to see if a token has a corresponding AFINN label:
if (!labels.hasOwnProperty(obj)) continue;

in many non English language does not work for inflected words.

In English you say: "the cat is stupid" and "the cats are stupid" (btw cats are smart! :D )

In French you say: "le chat est stupide" and "les chats sont stupides".
The stemmed word "stupid" is flected into stupide and stupides.

if we use "stupid" in the labels, the scoring is never done. Using all the possible inflected words is tedious and long.

may be you could change the line of code to take into account stemmed (or truncated) words?

Using in the browser

I don't have much experience with js and I'm probably doing something wrong, but is there a proper way to use this in the browser? I'm trying to pass a variable from php to js in the browser so I can display the sentiment scores on a webpage. I have tried browserify but I when I attempt to output the variable it says undefined. Sorry if this makes no sense, I appreciate any help!

Install sentiment

when I use NPM install sentiment - the installation fails - here is the output of the install
This install under windows

:\Program Files\NodeJS>npm install sentiment
npm WARN package.json [email protected] No README.md file found!
npm WARN package.json [email protected] No README.md file found!
npm WARN package.json [email protected] No README.md file found!
npm WARN package.json [email protected] No README.md file found!
npm http GET https://registry.npmjs.org/sentiment
npm http 200 https://registry.npmjs.org/sentiment
npm http GET https://registry.npmjs.org/sentiment/-/sentiment-0.2.0.tgz
npm http 200 https://registry.npmjs.org/sentiment/-/sentiment-0.2.0.tgz
npm http GET https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/lodash
npm http 200 https://registry.npmjs.org/async
npm http GET https://registry.npmjs.org/async/-/async-0.1.22.tgz
npm http 200 https://registry.npmjs.org/lodash
npm http GET https://registry.npmjs.org/lodash/-/lodash-1.0.0-rc.3.tgz
npm http 200 https://registry.npmjs.org/async/-/async-0.1.22.tgz
npm http 200 https://registry.npmjs.org/lodash/-/lodash-1.0.0-rc.3.tgz

[email protected] install c:\Program Files\NodeJS\node_modules\sentiment\node_
modules\lodash
node build/post-install

Oops! There was a problem detecting the install mode. If you're installing the
Lo-Dash command-line executable (via npm install -g lodash), you'll need to
manually install UglifyJS and the Closure Compiler by running:

curl -H 'Accept: application/vnd.github.v3.raw' api.github.comhttps://api.github
.com/repos/bestiejs/lodash/git/blobs/a2787b470c577cee2404d186c562dd9835f779f5 |
tar xvz -C 'c:\Program Files\NodeJS\node_modules\sentiment\node_modules\lodash\v
endor'
curl -H 'Accept: application/vnd.github.v3.raw' api.github.comhttps://api.github
.com/repos/bestiejs/lodash/git/blobs/7ecae09d413eb48dd5785fe877f24e60ac3bbcef |
tar xvz -C 'c:\Program Files\NodeJS\node_modules\sentiment\node_modules\lodash\v
endor'

Please submit an issue on the GitHub issue tracker: https://github.com/bestiejs/
lodash/issues
{ [Error: Command failed: CreateProcessW: The system cannot find the file specif
ied.
] killed: false, code: 127, signal: null }
[email protected] node_modules\sentiment
├── [email protected]
└── [email protected]

Load sentiment with own wordlist

I would like sentiment to be able to load external word lists of some kind. One of the following ways would be appreciated:

  • call sentiment with the path of the wordlist file (preferred)
  • call sentiment with an object / array which contains the wordlist
  • call sentiment with an string object which contains the wordlist

I think this would make sentiment much more versatile for other languages (german in my case).

Make the async interface optional

As sentiment is actually a sync method under the hood – the async interface is confusing to some users. This is particularly an issue as it doesn't use process.nextTick to ensure that this behavior is normative. The default interface should become sync, while allowing an optional callback which is wrapped using process.nextTick to ensure that the async interface works as expected.

Sentiment never reaches certain AFINN words

The first expression in tokenize takes out hyphens, wheres some words with hyphens exist in the AFINN database.

For example, "self-deluded" is mapped to -2, whereas "self" and "deluded" don’t exist in the AFINN database (thus result in a score of 0).

This can be fixed by allowing the hyphen in tokens.

Note: I haven’t tested if other punctuation exists in AFINN words.

Sentiment fails when `"constructor"` is passed

In Node:

> s = require('sentiment')
> s('constructor')
{ score: '0function Object() { [native code] }',
  comparative: NaN,
  tokens: [ 'constructor' ],
  words: [ 'constructor' ],
  positive: [],
  negative: [] }

This can be fixed by using a hasOwnProperty check, instead of just getting it from the AFINN object.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.