-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
145 lines (120 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* @typedef {Record<string, number>} Options
* Mapping strings (words, other symbols) to numbers.
* Used to insert custom values or for overwriting existing values with new
* weights.
*/
import {afinn165} from 'afinn-165'
import {emojiEmotion} from 'emoji-emotion'
import {emoticon} from 'emoticon'
import {emojiToName} from 'gemoji'
import {toString} from 'nlcst-to-string'
import {visit} from 'unist-util-visit'
const own = {}.hasOwnProperty
/** @type {Record<string, number>} */
const map = {}
/** @type {string} */
let key
for (key in afinn165) {
if (own.call(afinn165, key)) {
map[key] = afinn165[key]
}
}
let index = -1
while (++index < emojiEmotion.length) {
const {emoji, polarity} = emojiEmotion[index]
map[emoji] = polarity
// @ts-expect-error: it’s an index.
map[':' + emojiToName[emoji] + ':'] = polarity
}
index = -1
while (++index < emoticon.length) {
const {emoji, emoticons} = emoticon[index]
let offset = -1
if (emoji in map) {
while (++offset < emoticons.length) {
map[emoticons[offset]] = map[emoji]
}
}
}
/**
* Patch `polarity` and `valence` properties on nodes with a value and word
* nodes.
* Then, patch the same properties on their parents.
*
* @type {import('unified').Plugin<[Options?]>}
*/
export default function retextSentiment(options = {}) {
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*/
return (node) => {
/** @type {Parent[]} */
const queue = []
// Patch data-properties on `node`s with a value and words.
visit(node, (node) => {
if ('value' in node || node.type === 'WordNode') {
const value = toString(node)
/** @type {number|undefined} */
let polarity
if (own.call(options, value)) {
polarity = options[value]
} else if (own.call(map, value)) {
polarity = map[value]
}
if (polarity !== undefined) {
patch(node, polarity)
}
}
})
// Gather a parent if not already gathered.
visit(node, (_, _1, parent) => {
if (parent && parent.type !== 'WordNode' && !queue.includes(parent)) {
queue.push(parent)
}
})
let index = -1
queue.reverse()
while (++index < queue.length) {
// Patch all words in `parent`.
const node = queue[index]
const children = node.children
let polarity = 0
let offset = -1
let negated = false
while (++offset < children.length) {
const child = children[offset]
if (child.data && typeof child.data.polarity === 'number') {
polarity += (negated ? -1 : 1) * child.data.polarity
}
// If the value is a word, remove any present negation.
// Otherwise, add negation if the node contains it.
if (child.type === 'WordNode') {
// Detect if a value is used to negate something.
const value = toString(child).toLowerCase()
if (negated) {
negated = false
} else if (/^(not|neither|nor)$|n['’]t/.test(value)) {
negated = true
}
}
}
patch(node, polarity)
}
}
/**
* Patch a `polarity` and valence property on `node`s.
*
* @param {Node} node
* @param {number} polarity
*/
function patch(node, polarity) {
const data = node.data || (node.data = {})
data.polarity = polarity
// Classify, from a given `polarity` between `-5` and `5`, if the polarity is
// negative, neutral, or positive.
data.valence =
polarity > 0 ? 'positive' : polarity < 0 ? 'negative' : 'neutral'
}
}