New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.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 - npm Package Compare versions

Comparing version 4.0.0 to 4.0.1

37

index.d.ts
/**
* Get a polarity result from given values, optionally with one time injections.
*
* @param {Array.<string>} values
* @param {Inject} inject
* @param {Array<string>} values
* @param {Inject} [inject]
* @returns {Polarity}
*/
export function polarity(values: Array<string>, inject: Inject): Polarity
export function polarity(
values: Array<string>,
inject?: Inject | undefined
): Polarity
/**

@@ -15,12 +18,32 @@ * Inject values on the `polarities` object.

export function inject(values: Inject): void
export var polarities: {}
/** @type {Inject} */
export const polarities: Inject
/**
* Info.
*/
export type Polarity = {
/**
* Calculated polarity of input.
*/
polarity: number
/**
* Total positivity.
*/
positivity: number
/**
* Total negativity.
*/
negativity: number
/**
* All positive words.
*/
positive: Array<string>
/**
* All negative words.
*/
negative: Array<string>
}
export type Inject = {
[x: string]: number
}
/**
* Values to inject.
*/
export type Inject = Record<string, number>

65

index.js
/**
* @typedef {Object} Polarity
* @typedef Polarity
* Info.
* @property {number} polarity
* Calculated polarity of input.
* @property {number} positivity
* Total positivity.
* @property {number} negativity
* @property {Array.<string>} positive
* @property {Array.<string>} negative
* Total negativity.
* @property {Array<string>} positive
* All positive words.
* @property {Array<string>} negative
* All negative words.
*
* @typedef {Object.<string, number>} Inject
* @typedef {Record<string, number>} Inject
* Values to inject.
*/
import {afinn165} from 'afinn-165'
import {emoji} from './emoji.js'
import {emojiEmotion} from 'emoji-emotion'
export var polarities = {}
/** @type {Inject} */
export const polarities = {}
var own = {}.hasOwnProperty
const own = {}.hasOwnProperty
/** @type {Inject} */
const emoji = {}
let index = 0
while (++index < emojiEmotion.length) {
const info = emojiEmotion[index]
polarities[info.emoji] = info.polarity
polarities[':' + info.name + ':'] = info.polarity
}
inject(afinn165)

@@ -25,23 +43,19 @@ inject(emoji)

*
* @param {Array.<string>} values
* @param {Inject} inject
* @param {Array<string>} values
* @param {Inject} [inject]
* @returns {Polarity}
*/
export function polarity(values, inject) {
var words = values || []
var index = words.length === 0 ? 1 : words.length
var positivity = 0
var negativity = 0
/** @type {Array.<string>} */
var positive = []
/** @type {Array.<string>} */
var negative = []
/** @type {string} */
var value
/** @type {number} */
var weight
const words = values || []
let index = words.length === 0 ? 1 : words.length
let positivity = 0
let negativity = 0
/** @type {Array<string>} */
const positive = []
/** @type {Array<string>} */
const negative = []
while (index--) {
value = words[index]
weight = getPolarity(value, inject)
const value = words[index]
const weight = getPolarity(value, inject)

@@ -77,3 +91,3 @@ if (!weight) {

/** @type {string} */
var value
let value

@@ -91,3 +105,4 @@ for (value in values) {

* @param {string} value
* @param {Inject} inject
* @param {Inject} [inject]
* @returns {number}
*/

@@ -94,0 +109,0 @@ function getPolarity(value, inject) {

{
"name": "polarity",
"version": "4.0.0",
"version": "4.0.1",
"description": "Detect the polarity (sentiment) of text",

@@ -35,32 +35,27 @@ "license": "MIT",

"index.d.ts",
"index.js",
"emoji.js"
"index.js"
],
"dependencies": {
"afinn-165": "^2.0.0"
"afinn-165": "^2.0.0",
"emoji-emotion": "^3.0.0"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"@types/node": "^18.0.0",
"c8": "^7.0.0",
"emoji-emotion": "^3.0.0",
"gemoji": "^7.0.0",
"nyc": "^15.0.0",
"prettier": "^2.0.0",
"remark-cli": "^9.0.0",
"remark-preset-wooorm": "^8.0.0",
"rimraf": "^3.0.0",
"tape": "^5.0.0",
"tinyify": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
"xo": "^0.38.0"
"xo": "^0.52.0"
},
"scripts": {
"prepack": "npm run build && npm run format",
"build": "rimraf \"*.d.ts\" && tsc && type-coverage",
"generate": "node build",
"build": "tsc --build --clean && tsc --build && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test/index.js",
"test": "npm run generate && npm run build && npm run format && npm run test-coverage"
"test-api": "node --conditions development test/index.js",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},

@@ -76,8 +71,3 @@ "prettier": {

"xo": {
"prettier": true,
"rules": {
"import/no-mutable-exports": "off",
"no-var": "off",
"prefer-arrow-callback": "off"
}
"prettier": true
},

@@ -84,0 +74,0 @@ "remarkConfig": {

@@ -8,12 +8,37 @@ # polarity

Detect the polarity of text, based on [`afinn-165`][afinn] and
Sentiment analysis of natural language with [`afinn-165`][afinn] and
[`emoji-emotion`][emoji].
## Contents
* [What is this?](#what-is-this)
* [When should I use this?](#when-should-i-use-this)
* [Install](#install)
* [Use](#use)
* [API](#api)
* [`polarity(words[, inject])`](#polaritywords-inject)
* [`inject(words)`](#injectwords)
* [`polarities`](#polarities)
* [Types](#types)
* [Compatibility](#compatibility)
* [Related](#related)
* [Contribute](#contribute)
* [Security](#security)
* [License](#license)
## What is this?
You can give this package words, and it’ll tell you the
[valence][valence-wiki] (β€œ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: Node 12+ is needed to use it and it must be `import`ed
instead of `require`d.
This package is [ESM only][esm].
In Node.js (version 14.14+, 16.0+), install with [npm][]:
[npm][]:
```sh

@@ -23,2 +48,16 @@ npm install polarity

In Deno with [`esm.sh`][esmsh]:
```js
import {polarity} from 'https://esm.sh/polarity@4'
```
In browsers with [`esm.sh`][esmsh]:
```html
<script type="module">
import {polarity} from 'https://esm.sh/polarity@4?bundle'
</script>
```
## Use

@@ -62,4 +101,3 @@

This package exports the following identifiers: `polarity`, `inject`,
`polarities`.
This package exports the identifier `polarity`, `inject`, and `polarities`.
There is no default export.

@@ -71,26 +109,26 @@

**polarity** does not tokenize values.
There are better tokenizers around ([**parse-latin**][latin]).
However, the following will work pretty good:
> πŸ‘‰ **Note**: `polarity` does not tokenize values.
> There are good tokenizers around (such as [`parse-latin`][latin]).
> However, the following will work pretty good:
>
> ```js
> function tokenize(value) {
> return value.toLowerCase().match(/\S+/g)
> }
> ```
```js
function tokenize(value) {
return value.toLowerCase().match(/\S+/g)
}
```
###### Parameters
* `words` (`Array.<string>`) β€” Words to parse
* `inject` (`Object.<number>`, optional) β€” Custom valences for words
* `words` (`Array<string>`) β€” words to check
* `inject` (`Record<string, number>`, optional) β€” custom valences for words
###### Returns
`Object`:
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
* `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

@@ -103,15 +141,31 @@ ### `inject(words)`

Direct access to the internal values.
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-96`](https://github.com/words/afinn-96)
β€” AFINN list from 2009, containing 1468 entries
* [`afinn-111`](https://github.com/words/afinn-111)
β€” AFINN list from 2011, containing 2477 entries
* [`afinn-165`](https://github.com/words/afinn-165)
β€” AFINN list from 2015, containing 3382 entries
* [`emoji-emotion`](https://github.com/words/emoji-emotion)
β€” Like AFINN, but for emoji
β€” like AFINN, but for emoji
## Contribute
Yes please!
See [How to Contribute to Open Source][contribute].
## Security
This package is safe.
## License

@@ -141,2 +195,10 @@

[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
[esmsh]: https://esm.sh
[typescript]: https://www.typescriptlang.org
[contribute]: https://opensource.guide/how-to-contribute/
[license]: license

@@ -151,1 +213,3 @@

[latin]: https://github.com/wooorm/parse-latin
[valence-wiki]: https://en.wikipedia.org/wiki/Valence_\(psychology\)
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