Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

polarity

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

polarity

Detect the polarity (sentiment) of text

  • 4.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by10%
Maintainers
2
Weekly downloads
 
Created
Source

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.

Contribute

Yes please! See How to Contribute to Open Source.

Security

This package is safe.

License

MIT © Titus Wormer

Keywords

FAQs

Package last updated on 07 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc