natural 6.7.2 → 6.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -24,18 +24,20 @@ const dutchLexicon = require('../data/Dutch/brill_Lexicon.json')
|
|
|
24
24
|
const DEBUG = false
|
|
25
25
|
|
|
26
26
|
// Constructor creates a Lexicon for language
|
|
27
|
-
function Lexicon (language, defaultCategory, defaultCategoryCapitalised) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
27
|
+
function Lexicon (language, defaultCategory, defaultCategoryCapitalised, extendedLexicon) {
|
|
28
|
+
const lexicon = (() => {
|
|
29
|
+
switch (language) {
|
|
30
|
+
case 'EN':
|
|
31
|
+
return englishLexicon
|
|
32
|
+
case 'DU':
|
|
33
|
+
return dutchLexicon
|
|
34
|
+
default:
|
|
35
|
+
return dutchLexicon
|
|
36
|
+
}
|
|
37
|
+
})()
|
|
38
|
+
|
|
39
|
+
this.lexicon = Object.assign(lexicon, extendedLexicon || {})
|
|
40
|
+
|
|
39
41
|
if (defaultCategory) {
|
|
40
42
|
this.defaultCategory = defaultCategory
|
|
41
43
|
if (defaultCategoryCapitalised) {
|
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
|
|
23
23
|
'use strict'
|
|
24
24
|
|
|
25
|
+
const DEBUG = true
|
|
26
|
+
|
|
25
27
|
// Afinn
|
|
26
28
|
const englishAfinnVoca = require('afinn-165')
|
|
27
29
|
const spanishAfinnVoca = require('./Spanish/afinnShortSortedSpanish.json')
|
|
@@ -87,6 +89,17 @@ class SentimentAnalyzer {
|
|
|
87
89
|
// this.vocabulary must be a copy of the languageFiles object
|
|
88
90
|
// or in subsequent execution the polarity will be undefined
|
|
89
91
|
// shallow copy - requires ES6
|
|
92
|
+
if (languageFiles[type]) {
|
|
93
|
+
if (languageFiles[type][language]) {
|
|
94
|
+
if (languageFiles[type][language][0]) {
|
|
95
|
+
this.vocabulary = Object.create(languageFiles[type][language][0])
|
|
96
|
+
}
|
|
97
|
+
} else {
|
|
98
|
+
throw new Error('Type ' + type + ' for Language ' + language + ' not supported')
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
throw new Error('Type Language ' + type + ' not supported')
|
|
102
|
+
}
|
|
90
103
|
this.vocabulary = Object.assign({}, languageFiles[type][language][0])
|
|
91
104
|
Object.setPrototypeOf(this.vocabulary, null)
|
|
92
105
|
if (type === 'senticon') {
|
|
@@ -98,7 +111,6 @@ class SentimentAnalyzer {
|
|
|
98
111
|
Object.keys(this.vocabulary).forEach(word => {
|
|
99
112
|
this.vocabulary[word] = this.vocabulary[word].polarity
|
|
100
113
|
})
|
|
101
|
-
// console.log(JSON.stringify(this.vocabulary, null, 2));
|
|
102
114
|
}
|
|
103
115
|
}
|
|
104
116
|
|
|
@@ -120,24 +132,24 @@ class SentimentAnalyzer {
|
|
|
120
132
|
getSentiment (words) {
|
|
121
133
|
let score = 0
|
|
122
134
|
let negator = 1
|
|
123
|
-
|
|
135
|
+
let nrHits = 0
|
|
124
136
|
|
|
125
137
|
words.forEach((token) => {
|
|
126
138
|
const lowerCased = token.toLowerCase()
|
|
127
139
|
if (this.negations.indexOf(lowerCased) > -1) {
|
|
128
140
|
negator = -1
|
|
129
|
-
|
|
141
|
+
DEBUG && nrHits++
|
|
130
142
|
} else {
|
|
131
143
|
// First try without stemming
|
|
132
144
|
if (this.vocabulary[lowerCased] !== undefined) {
|
|
133
145
|
score += negator * this.vocabulary[lowerCased]
|
|
134
|
-
|
|
146
|
+
DEBUG && nrHits++
|
|
135
147
|
} else {
|
|
136
148
|
if (this.stemmer) {
|
|
137
149
|
const stemmedWord = this.stemmer.stem(lowerCased)
|
|
138
150
|
if (this.vocabulary[stemmedWord] !== undefined) {
|
|
139
151
|
score += negator * this.vocabulary[stemmedWord]
|
|
140
|
-
|
|
152
|
+
DEBUG && nrHits++
|
|
141
153
|
}
|
|
142
154
|
}
|
|
143
155
|
}
|
|
@@ -145,7 +157,7 @@ class SentimentAnalyzer {
|
|
|
145
157
|
})
|
|
146
158
|
|
|
147
159
|
score = score / words.length
|
|
148
|
-
|
|
160
|
+
DEBUG && console.log('Number of hits: ' + nrHits)
|
|
149
161
|
|
|
150
162
|
return score
|
|
151
163
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "natural",
|
|
3
3
|
"description": "General natural language (tokenizing, stemming (English, Russian, Spanish), part-of-speech tagging, sentiment analysis, classification, inflection, phonetics, tfidf, WordNet, jaro-winkler, Levenshtein distance, Dice's Coefficient) facilities for node.",
|
|
4
|
-
"version": "6.
|
|
4
|
+
"version": "6.8.1",
|
|
5
5
|
"homepage": "https://github.com/NaturalNode/natural",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|