polarity
v4.0.1
Published
Detect the polarity (sentiment) of text
Downloads
53
Readme
polarity
Sentiment analysis of natural language with afinn-165
and
emoji-emotion
.
Contents
What is this?
You can give this package words, and it’ll tell you the valence (“goodness” vs “badness”), and which words are positive or negative.
When should I use this?
You can use this with your own tokenizer to do some simple sentiment analysis.
Install
This package is ESM only. In Node.js (version 14.14+, 16.0+), install with npm:
npm install polarity
In Deno with esm.sh
:
import {polarity} from 'https://esm.sh/polarity@4'
In browsers with esm.sh
:
<script type="module">
import {polarity} from 'https://esm.sh/polarity@4?bundle'
</script>
Use
import {polarity} from 'polarity'
polarity(['some', 'positive', 'happy', 'cats'])
Yields:
{
polarity: 5,
positivity: 5,
negativity: 0,
positive: ['happy', 'positive'],
negative: []
}
polarity(['darn', 'self-deluded', 'abandoned', 'dogs'])
Yields:
{
polarity: -4,
positivity: 0,
negativity: -4,
positive: [],
negative: ['abandoned', 'self-deluded']
}
API
This package exports the identifier polarity
, inject
, and polarities
.
There is no default export.
polarity(words[, inject])
Get a polarity result from given values, optionally with one time injections.
👉 Note:
polarity
does not tokenize values. There are good tokenizers around (such asparse-latin
). However, the following will work pretty good:function tokenize(value) { return value.toLowerCase().match(/\S+/g) }
Parameters
words
(Array<string>
) — words to checkinject
(Record<string, number>
, optional) — custom valences for words
Returns
Object with the following fields:
polarity
(number
) — calculated polarity of inputpositivity
(number
) — total positivitynegativity
(number
) — total negativitypositive
(Array<string>
) — all positive wordsnegative
(Array<string>
) — all negative words
inject(words)
Insert custom values.
polarities
Direct access to the internal values (Record<string, number>
).
Types
This package is fully typed with TypeScript.
It exports the additional type Polarity
(the result).
Compatibility
This package is at least compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. It also works in Deno and modern browsers.
Related
afinn-165
— AFINN list from 2015, containing 3382 entriesemoji-emotion
— like AFINN, but for emoji
Contribute
Yes please! See How to Contribute to Open Source.
Security
This package is safe.