-
Notifications
You must be signed in to change notification settings - Fork 310
/
Copy pathindex.js
105 lines (93 loc) · 2.83 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
var tokenize = require('./tokenize');
var languageProcessor = require('./language-processor');
/**
* Constructor
* @param {Object} options - Instance options
*/
var Sentiment = function (options) {
this.options = options;
};
/**
* Registers the specified language
*
* @param {String} languageCode
* - Two-digit code for the language to register
* @param {Object} language
* - The language module to register
*/
Sentiment.prototype.registerLanguage = function (languageCode, language) {
languageProcessor.addLanguage(languageCode, language);
};
/**
* Performs sentiment analysis on the provided input 'phrase'.
*
* @param {String} phrase
* - Input phrase
* @param {Object} opts
* - Options
* @param {Object} opts.language
* - Input language code (2 digit code), defaults to 'en'
* @param {Object} opts.extras
* - Optional sentiment additions to AFINN (hash k/v pairs)
* @param {function} callback
* - Optional callback
* @return {Object}
*/
Sentiment.prototype.analyze = function (phrase, opts, callback) {
// Parse arguments
if (typeof phrase === 'undefined') phrase = '';
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = opts || {};
var languageCode = opts.language || 'en';
var labels = languageProcessor.getLabels(languageCode);
// Merge extra labels
if (typeof opts.extras === 'object') {
labels = Object.assign(labels, opts.extras);
}
// Storage objects
var tokens = tokenize(phrase),
score = 0,
words = [],
positive = [],
negative = [],
calculation = [];
// Iterate over tokens
var i = tokens.length;
while (i--) {
var obj = tokens[i];
if (!Object.prototype.hasOwnProperty.call(labels, obj)) continue;
words.push(obj);
// Apply scoring strategy
var tokenScore = labels[obj];
// eslint-disable-next-line max-len
tokenScore = languageProcessor.applyScoringStrategy(languageCode, tokens, i, tokenScore);
if (tokenScore > 0) positive.push(obj);
if (tokenScore < 0) negative.push(obj);
score += tokenScore;
var zipObj = {};
// Calculations breakdown
zipObj[obj] = tokenScore;
calculation.push(zipObj);
}
var result = {
score: score,
comparative: tokens.length > 0 ? score / tokens.length : 0,
calculation: calculation,
tokens: tokens,
words: words,
positive: positive,
negative: negative
};
// Handle optional async interface
if (typeof callback === 'function') {
process.nextTick(function () {
callback(null, result);
});
} else {
return result;
}
};
module.exports = Sentiment;