Comparing version 0.0.1 to 0.1.0
88
index.js
'use strict'; | ||
var afinn, isOwnProperty; | ||
/** | ||
* Dependencies. | ||
*/ | ||
var afinn; | ||
afinn = require('afinn-111'); | ||
isOwnProperty = Object.prototype.hasOwnProperty; | ||
/** | ||
* Cached methods. | ||
*/ | ||
var has; | ||
has = Object.prototype.hasOwnProperty; | ||
/** | ||
* Get the polarity of a word. | ||
* | ||
* @param {string} value | ||
* @param {Object.<string, number>} inject | ||
* @return {number} | ||
*/ | ||
function getPolarity(value, inject) { | ||
if (isOwnProperty.call(afinn, value)) { | ||
if (has.call(afinn, value)) { | ||
return afinn[value]; | ||
} else if (inject && isOwnProperty.call(inject, value)) { | ||
} else if (inject && has.call(inject, value)) { | ||
return inject[value]; | ||
@@ -19,4 +37,17 @@ } | ||
/** | ||
* Define `polarity` | ||
* | ||
* @param {Array.<string>} values | ||
* @param {Object.<string, number>} inject | ||
* @return {Object} | ||
*/ | ||
function polarity(values, inject) { | ||
var iterator, length, value, weigth, positive, negative, positivity, | ||
var index, | ||
value, | ||
weigth, | ||
positive, | ||
negative, | ||
positivity, | ||
negativity; | ||
@@ -28,10 +59,13 @@ | ||
length = values.length || 0; | ||
positivity = negativity = 0; | ||
iterator = -1; | ||
index = values.length || 1; | ||
positivity = 0; | ||
negativity = 0; | ||
positive = []; | ||
negative = []; | ||
while (++iterator < length) { | ||
value = values[iterator]; | ||
while (index--) { | ||
value = values[index]; | ||
weigth = getPolarity(value, inject); | ||
@@ -45,5 +79,7 @@ | ||
positive.push(value); | ||
positivity += weigth; | ||
} else { | ||
negative.push(value); | ||
negativity += weigth; | ||
@@ -54,10 +90,16 @@ } | ||
return { | ||
'polarity' : positivity + negativity, | ||
'positivity' : positivity, | ||
'negativity' : negativity, | ||
'positive' : positive, | ||
'negative' : negative | ||
'polarity': positivity + negativity, | ||
'positivity': positivity, | ||
'negativity': negativity, | ||
'positive': positive, | ||
'negative': negative | ||
}; | ||
} | ||
/** | ||
* Inject values on the `afinn` object. | ||
* | ||
* @param {Object.<string, number>} inject | ||
*/ | ||
function inject(values) { | ||
@@ -68,3 +110,3 @@ var value; | ||
/* istanbul ignore else */ | ||
if (isOwnProperty.call(values, value)) { | ||
if (has.call(values, value)) { | ||
afinn[value] = values[value]; | ||
@@ -75,6 +117,18 @@ } | ||
/** | ||
* Expose `inject` on `polarity`. | ||
*/ | ||
polarity.inject = inject; | ||
/** | ||
* Expose `afinn` on `polarity`. | ||
*/ | ||
polarity.afinn = afinn; | ||
exports = module.exports = polarity; | ||
/** | ||
* Expose `polarity`. | ||
*/ | ||
module.exports = polarity; |
{ | ||
"name": "polarity", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Detect the polarity (sentiment) of text", | ||
@@ -18,50 +18,33 @@ "license": "MIT", | ||
], | ||
"dependencies": { | ||
"afinn-111": "^0.1.3" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/polarity.git" | ||
}, | ||
"author": "Titus Wormer <tituswormer@gmail.com>", | ||
"devDependencies": { | ||
"eslint": "^0.7.4", | ||
"eslint": "^0.9.0", | ||
"istanbul": "^0.3.0", | ||
"jscs": "^1.5.3", | ||
"jscs": "^1.7.0", | ||
"matcha": "^0.5.0", | ||
"mocha": "~1.20.1" | ||
"mocha": "^2.0.0" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wooorm/polarity.git" | ||
}, | ||
"scripts": { | ||
"test": "node_modules/.bin/_mocha --reporter spec --check-leaks -u exports spec/polarity.spec.js", | ||
"test-travis": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec --check-leaks -u exports spec/polarity.spec.js", | ||
"lint": "npm run lint-api && npm run lint-test && npm run lint-benchmark && npm run lint-style", | ||
"lint-api": "node_modules/.bin/eslint index.js build-fixtures-file.js --env node --env browser --rule 'quotes: [2, single]'", | ||
"lint-test": "node_modules/.bin/eslint spec/polarity.spec.js --env node --env mocha --rule 'quotes: [2, single]'", | ||
"lint-benchmark": "node_modules/.bin/eslint benchmark/index.js --env node --global 'suite, bench, set' --rule 'quotes: [2, single]'", | ||
"lint-style": "node_modules/.bin/jscs index.js build-fixtures-file.js benchmark/index.js spec/polarity.spec.js --reporter=inline", | ||
"install-browser-test": "npm install browserify", | ||
"test": "node_modules/.bin/_mocha --reporter spec --check-leaks -u exports test/index.js", | ||
"test-travis": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec --check-leaks -u exports test/index.js", | ||
"lint": "npm run lint-api && npm run lint-test && npm run lint-benchmark && npm run lint-script && npm run lint-style", | ||
"lint-api": "node_modules/.bin/eslint index.js", | ||
"lint-test": "node_modules/.bin/eslint test/index.js --env mocha", | ||
"lint-benchmark": "node_modules/.bin/eslint benchmark.js --global 'suite, bench, set'", | ||
"lint-script": "node_modules/.bin/eslint build-fixtures-file.js", | ||
"lint-style": "node_modules/.bin/jscs index.js build-fixtures-file.js benchmark.js test/index.js --reporter=inline", | ||
"build-fixtures": "node build-fixtures-file.js", | ||
"build-browser-test": "node_modules/.bin/browserify spec/polarity.spec.js -o spec/browser.spec.js", | ||
"install-benchmark": "npm install sentiment Sentimental sediment", | ||
"benchmark": "node_modules/.bin/matcha", | ||
"coverage": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -- spec/polarity.spec.js", | ||
"benchmark": "node_modules/.bin/matcha benchmark.js", | ||
"coverage": "node_modules/.bin/istanbul cover node_modules/.bin/_mocha -- -- test/index.js", | ||
"prepublish": "npm run build-fixtures", | ||
"make": "npm run lint && npm run coverage" | ||
}, | ||
"testling": { | ||
"files": "spec/polarity.spec.js", | ||
"harness": "mocha", | ||
"browsers": [ | ||
"iexplore/latest", | ||
"chrome/latest", | ||
"chrome/canary", | ||
"firefox/latest", | ||
"firefox/nightly", | ||
"opera/latest", | ||
"opera/next", | ||
"safari/latest", | ||
"iphone/latest", | ||
"android-browser/latest" | ||
] | ||
}, | ||
"dependencies": { | ||
"afinn-111": "0.0.1" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# polarity [![Build Status](https://travis-ci.org/wooorm/polarity.svg?branch=master)](https://travis-ci.org/wooorm/polarity) [![Coverage Status](https://img.shields.io/coveralls/wooorm/polarity.svg)](https://coveralls.io/r/wooorm/polarity?branch=master) | ||
# polarity [![Build Status](https://img.shields.io/travis/wooorm/polarity.svg?style=flat)](https://travis-ci.org/wooorm/polarity) [![Coverage Status](https://img.shields.io/coveralls/wooorm/polarity.svg?style=flat)](https://coveralls.io/r/wooorm/polarity?branch=master) | ||
@@ -9,3 +9,3 @@ Detect the polarity of text, based on [AFINN-111](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6010). | ||
NPM: | ||
npm: | ||
```sh | ||
@@ -15,3 +15,3 @@ $ npm install polarity | ||
Component.js: | ||
Component: | ||
```sh | ||
@@ -21,2 +21,7 @@ $ component install wooorm/polarity | ||
Bower: | ||
```sh | ||
$ bower install polarity | ||
``` | ||
## Usage | ||
@@ -88,27 +93,13 @@ | ||
## Other polarity detection libraries | ||
- [thisandagain/sentiment](https://github.com/thisandagain/sentiment) — Slower (but pretty good nonetheless), has tokeniser, async support; | ||
- [mileszim/sediment](https://github.com/mileszim/sediment) — Slower, has tokeniser. | ||
- [thinkroth/Sentimental](https://github.com/thinkroth/Sentimental) — Slower, has tokeniser. | ||
## Benchmark | ||
Run the benchmark yourself: | ||
On a MacBook Air, it detects the polarity of 141,980 tweets per second. | ||
This is with a manual tokenisation step (like the competition) taken into account. Without the tokenisation step, **polarity** is much faster. | ||
```sh | ||
$ npm run install-benchmark # Just once of course. | ||
$ npm run benchmark | ||
``` | ||
On a MacBook Air, it detects the polarity of 168,820 tweets per second. | ||
This is with a manual tokenisation step (like the others do) taken into account. | ||
Without the tokenisation step, **polarity** is of course much faster. | ||
``` | ||
benchmarks * 20 tweets (10 pos, 10 neg) | ||
8,441 op/s » polarity -- this module | ||
7,708 op/s » sentiment | ||
3,950 op/s » sediment | ||
2,604 op/s » Sentimental | ||
7,099 op/s » polarity -- this module | ||
4,484 op/s » sentiment | ||
3,207 op/s » sediment | ||
2,298 op/s » Sentimental | ||
``` | ||
@@ -118,2 +109,2 @@ | ||
MIT | ||
MIT © Titus Wormer |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
0
7497
4
97
106
1
+ Addedafinn-111@0.1.5(transitive)
- Removedafinn-111@0.0.1(transitive)
Updatedafinn-111@^0.1.3