Sentiment analysis of natural language with afinn-165
and
emoji-emotion
.
- What is this?
- When should I use this?
- Install
- Use
- API
- Types
- Compatibility
- Related
- Contribute
- Security
- License
You can give this package words, and it’ll tell you the valence (“goodness” vs “badness”), and which words are positive or negative.
You can use this with your own tokenizer to do some simple sentiment analysis.
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>
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']
}
This package exports the identifier polarity
, inject
, and polarities
.
There is no default export.
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) }
words
(Array<string>
) — words to checkinject
(Record<string, number>
, optional) — custom valences for words
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
Insert custom values.
Direct access to the internal values (Record<string, number>
).
This package is fully typed with TypeScript.
It exports the additional type Polarity
(the result).
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.
afinn-165
— AFINN list from 2015, containing 3382 entriesemoji-emotion
— like AFINN, but for emoji
Yes please! See How to Contribute to Open Source.
This package is safe.