mirror of
https://github.com/wassname/sentimood.git
synced 2026-09-09 11:34:04 +08:00
85 lines
2.2 KiB
JavaScript
85 lines
2.2 KiB
JavaScript
// Generated by CoffeeScript 1.9.3
|
|
|
|
/*
|
|
sentimood v1.0
|
|
a CoffeeScript browser-compatible port of thinkroth's Sentimental
|
|
open-source under the MIT license, (c) Ethan Arterberry 2015
|
|
*/
|
|
var Sentimood;
|
|
var afinn;
|
|
Sentimood = (function() {
|
|
function Sentimood() {}
|
|
|
|
Sentimood.prototype.negativity = function(phrase) {
|
|
var addPush, hits, i, item, j, len, noPunctuation, tokens, words, wordScores;
|
|
addPush = function(t, score) {
|
|
hits -= score;
|
|
wordScores[t]=score
|
|
return words.push(t);
|
|
};
|
|
noPunctuation = phrase.replace(/[^a-zA-Z ]+/g, ' ').replace('/ {2,}/', ' ');
|
|
tokens = noPunctuation.toLowerCase().split(" ");
|
|
hits = 0;
|
|
words = [];
|
|
wordScores = {}
|
|
for (i = j = 0, len = tokens.length; j < len; i = ++j) {
|
|
item = tokens[i];
|
|
if (afinn.hasOwnProperty(item)) {
|
|
if (afinn[item] < 0) {
|
|
addPush(item, afinn[item]);
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
score: hits,
|
|
meanScore: hits / words.length,
|
|
words: words,
|
|
wordScores: wordScores
|
|
};
|
|
};
|
|
|
|
Sentimood.prototype.positivity = function(phrase) {
|
|
var addPush, hits, i, item, j, len, noPunctuation, tokens, words;
|
|
addPush = function(t, score) {
|
|
hits += score;
|
|
wordScores[t]=score
|
|
return words.push(t);
|
|
};
|
|
noPunctuation = phrase.replace(/[^a-zA-Z ]+/g, ' ').replace('/ {2,}/', ' ');
|
|
tokens = noPunctuation.toLowerCase().split(" ");
|
|
hits = 0;
|
|
words = [];
|
|
wordScores = {}
|
|
for (i = j = 0, len = tokens.length; j < len; i = ++j) {
|
|
item = tokens[i];
|
|
if (afinn.hasOwnProperty(item)) {
|
|
if (afinn[item] > 0) {
|
|
addPush(item, afinn[item]);
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
score: hits,
|
|
meanScore: hits / words.length,
|
|
words: words,
|
|
wordScores: wordScores
|
|
};
|
|
};
|
|
|
|
Sentimood.prototype.analyze = function(phrase) {
|
|
var neg, pos;
|
|
pos = Sentimood.prototype.positivity(phrase);
|
|
neg = Sentimood.prototype.negativity(phrase);
|
|
return {
|
|
score: pos.score - neg.score,
|
|
meanScore: pos.meanScore - neg.meanScore,
|
|
positive: pos,
|
|
negative: neg,
|
|
wordScores: pos.wordScores + neg.wordScores
|
|
};
|
|
};
|
|
|
|
return Sentimood;
|
|
|
|
})();
|