Skip to content

words/polarity

Repository files navigation

polarity

Build Coverage Downloads Size

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 as parse-latin). However, the following will work pretty good:

function tokenize(value) {
  return value.toLowerCase().match(/\S+/g)
}
Parameters
  • words (Array<string>) — words to check
  • inject (Record<string, number>, optional) — custom valences for words
Returns

Object with the following fields:

  • polarity (number) — calculated polarity of input
  • positivity (number) — total positivity
  • negativity (number) — total negativity
  • positive (Array<string>) — all positive words
  • negative (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

Contribute

Yes please! See How to Contribute to Open Source.

Security

This package is safe.

License

MIT © Titus Wormer