unified

Project: retextjs/retext-sentiment

Package: retext-sentiment@6.0.0

  1. Dependents: 3
  2. retext plugin to detect the sentiment of text
  1. unified 173
  2. plugin 135
  3. retext 38
  4. retext-plugin 25
  5. language 12
  6. natural 10

retext-sentiment

Build Coverage Downloads Size Sponsors Backers Chat

retext plugin to detect sentiment.

Useful for other plugins as it adds information to nlcst nodes.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install retext-sentiment

Use

Say we have the following file, example.txt:

I hate forgetting to bring a book somewhere I
definitely should have brought a book to.

This product is not bad at all.

Hai sexy! 😏

You’ll note that bad is a negative word, but that it’s actually positive as it’s preceded by not.

…and our script, example.js, looks like this:

import {readSync} from 'to-vfile'
import {inspect} from 'unist-util-inspect'
import {unified} from 'unified'
import retextEnglish from 'retext-english'
import retextSentiment from 'retext-sentiment'

const file = readSync('example.txt')
const processor = unified().use(retextEnglish).use(retextSentiment)

const tree = processor.runSync(processor.parse(file), file)

console.log(inspect(tree))

Note that we’re not using .process(), as that would not give access to our tree.

Now, running node example yields (abbreviated):

RootNode[6] (1:1-7:1, 0-135) [data={"polarity":5,"valence":"positive"}]
β”œβ”€ ParagraphNode[1] (1:1-2:42, 0-87) [data={"polarity":-3,"valence":"negative"}]
β”‚  └─ SentenceNode[32] (1:1-2:42, 0-87) [data={"polarity":-3,"valence":"negative"}]
β”‚     β”œβ”€ WordNode[1] (1:1-1:2, 0-1)
β”‚     β”‚  └─ TextNode: "I" (1:1-1:2, 0-1)
β”‚     β”œβ”€ WhiteSpaceNode: " " (1:2-1:3, 1-2)
β”‚     β”œβ”€ WordNode[1] (1:3-1:7, 2-6) [data={"polarity":-3,"valence":"negative"}]
β”‚     β”‚  └─ TextNode: "hate" (1:3-1:7, 2-6) [data={"polarity":-3,"valence":"negative"}]
β”‚     β”œβ”€ WhiteSpaceNode: " " (1:7-1:8, 6-7)
β”‚     ...
β”‚     └─ PunctuationNode: "." (2:41-2:42, 86-87)
β”œβ”€ WhiteSpaceNode: "\n\n" (2:42-4:1, 87-89)
β”œβ”€ ParagraphNode[1] (4:1-4:32, 89-120) [data={"polarity":3,"valence":"positive"}]
β”‚  └─ SentenceNode[14] (4:1-4:32, 89-120) [data={"polarity":3,"valence":"positive"}]
β”‚     β”œβ”€ WordNode[1] (4:1-4:5, 89-93)
β”‚     β”‚  └─ TextNode: "This" (4:1-4:5, 89-93)
β”‚     ...
β”‚     β”œβ”€ WordNode[1] (4:17-4:20, 105-108)
β”‚     β”‚  └─ TextNode: "not" (4:17-4:20, 105-108)
β”‚     β”œβ”€ WhiteSpaceNode: " " (4:20-4:21, 108-109)
β”‚     β”œβ”€ WordNode[1] (4:21-4:24, 109-112) [data={"polarity":-3,"valence":"negative"}]
β”‚     β”‚  └─ TextNode: "bad" (4:21-4:24, 109-112) [data={"polarity":-3,"valence":"negative"}]
β”‚     β”œβ”€ WhiteSpaceNode: " " (4:24-4:25, 112-113)
β”‚     ...
β”‚     └─ PunctuationNode: "." (4:31-4:32, 119-120)
β”œβ”€ WhiteSpaceNode: "\n\n" (4:32-6:1, 120-122)
β”œβ”€ ParagraphNode[1] (6:1-6:13, 122-134) [data={"polarity":5,"valence":"positive"}]
β”‚  └─ SentenceNode[6] (6:1-6:13, 122-134) [data={"polarity":5,"valence":"positive"}]
β”‚     β”œβ”€ WordNode[1] (6:1-6:4, 122-125)
β”‚     β”‚  └─ TextNode: "Hai" (6:1-6:4, 122-125)
β”‚     β”œβ”€ WhiteSpaceNode: " " (6:4-6:5, 125-126)
β”‚     β”œβ”€ WordNode[1] (6:5-6:9, 126-130) [data={"polarity":3,"valence":"positive"}]
β”‚     β”‚  └─ TextNode: "sexy" (6:5-6:9, 126-130) [data={"polarity":3,"valence":"positive"}]
β”‚     β”œβ”€ PunctuationNode: "!" (6:9-6:10, 130-131)
β”‚     β”œβ”€ WhiteSpaceNode: " " (6:10-6:11, 131-132)
β”‚     └─ SymbolNode: "😏" (6:11-6:13, 132-134) [data={"polarity":2,"valence":"positive"}]
└─ WhiteSpaceNode: "\n" (6:13-7:1, 134-135)

API

This package exports no identifiers. The default export is retextSentiment.

unified().use(retextSentiment[, options])

Automatically detects the sentiment of each text and word (using afinn-165 and emoji-emotion), and stores the valence in node.data.valence and polarity in node.data.polarity.

Valence? Either "neutral", "positive", or "negative". Polarity? A number between -5 and 5 (both including).

In addition, the plugin reflects a calculated sentiment on parents (sentences, paragraphs, and roots), through the same valence and polarity properties. This calculated sentiment includes negation, so a word such as bad, with a polarity of -3, is calculated as 3 when preceded by a word such as not, neither, nor, or a word ending in n’t.

options

Mapping strings (words, other symbols) to numbers (Object.<number>, optional). Used to insert custom values or for overwriting existing values with new weights.

Support

retext-sentiment supports all afinn-165 words and emoji-emotion emoji / gemoji.

Contribute

See contributing.md in retextjs/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT Β© Titus Wormer