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

Release 5.0.0 #139

Merged
merged 17 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.nyc_output
/coverage
/node_modules
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
sudo: false
language: node_js
node_js:
- "4.0"
- "stable"
- "8"
- "node"
cache:
directories:
- node_modules
Expand Down
37 changes: 0 additions & 37 deletions Makefile

This file was deleted.

154 changes: 117 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,128 @@
## sentiment
#### AFINN-based sentiment analysis for Node.js
# sentiment
### AFINN-based sentiment analysis for Node.js

[![Build Status](https://travis-ci.org/thisandagain/sentiment.svg?branch=develop)](https://travis-ci.org/thisandagain/sentiment)
[![Coverage Status](https://coveralls.io/repos/thisandagain/sentiment/badge.svg?branch=develop&service=github)](https://coveralls.io/github/thisandagain/sentiment?branch=develop)
[![Greenkeeper badge](https://badges.greenkeeper.io/thisandagain/sentiment.svg)](https://greenkeeper.io/)

Sentiment is a Node.js module that uses the [AFINN-165](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010) wordlist and [Emoji Sentiment Ranking](http://journals.plos.org/plosone/article?id=10.1371/journal.pone.0144296) to perform [sentiment analysis](http://en.wikipedia.org/wiki/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
- A build process that makes updating sentiment to future wordlists trivial

### Installation
- 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](#installation)
- [Usage example](#usage-example)
- [Adding new languages](#adding-new-languages)
- [Adding and overwriting words](#adding-and-overwriting-words)
- [API Reference](#api-reference)
- [How it works](#how-it-works)
- [Benchmarks](#benchmarks)
- [Validation](#validation)
- [Testing](#testing)

## Installation
```bash
npm install sentiment
```

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

var r1 = sentiment('Cats are stupid.');
console.dir(r1); // Score: -2, Comparative: -0.666
## Adding new languages
You can add support for a new language by registering it using the `registerLanguage` method:

```js
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
```

var r2 = sentiment('Cats are totally amazing!');
console.dir(r2); // Score: 4, Comparative: 1
You can also define custom scoring strategies to handle things like negation and emphasis on a per-language basis:
```js
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 / overwriting words
## Adding and overwriting words
You can append and/or overwrite values from AFINN by simply injecting key/value pairs into a sentiment method call:
```javascript
var sentiment = require('sentiment');

var result = sentiment('Cats are totally amazing!', {
var options = {
extras: {
'cats': 5,
'amazing': 2
});
'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 |

---

### How it works
#### AFINN
#### `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](#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.`
Expand Down Expand Up @@ -80,10 +160,10 @@ That string results in the following:
* __Score__: Score calculated by adding the sentiment values of recongnized words.
* __Comparative__: Comparative score of the input string.
* __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.
* __Words__: List of words from input string that were found in AFINN list.
* __Positive__: List of postive 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`

Expand All @@ -94,51 +174,51 @@ This approach leaves you with a mid-point of 0 and the upper and lower bounds ar
(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.
### 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](https://github.com/thinkroth/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 **twice as fast** as alternative implementations:
## 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](https://github.com/thinkroth/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:

```bash
sentiment (Latest) x 448,788 ops/sec ±1.02% (88 runs sampled)
Sentimental (1.0.1) x 240,103 ops/sec ±5.13% (81 runs sampled)
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:
```bash
make benchmark
npm run test:benchmark
```

---

### Validation
## 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](https://archive.ics.uci.edu/ml/datasets/Sentiment+Labelled+Sentences).

To run the validation tests yourself:
```bash
make validate
npm run test:validate
```

#### Rand Accuracy (AFINN Only)
### Rand Accuracy (AFINN Only)
```
Amazon: 0.70
IMDB: 0.76
Yelp: 0.67
```

#### Rand Accuracy (AFINN + Additions)
### Rand Accuracy (AFINN + Additions)
```
Amazon: 0.72 (+2%)
IMDB: 0.77 (+1%)
Yelp: 0.70 (+3%)
IMDB: 0.76 (+0%)
Yelp: 0.69 (+2%)
```

---

### Testing
## Testing
```bash
npm test
```