natural 6.8.0 → 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.
@@ -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
- // let nrHits = 0
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
- // nrHits++
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
- // nrHits++
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
- // nrHits++
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
- // console.log("Number of hits: " + nrHits);
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.8.0",
4
+ "version": "6.8.1",
5
5
  "homepage": "https://github.com/NaturalNode/natural",
6
6
  "repository": {
7
7
  "type": "git",