/
emotion_analyzer.js
49 lines (44 loc) · 1.48 KB
/
emotion_analyzer.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
/**
* Created by chamod on 4/29/18.
*/
var express = require('express');
var util = require("./util");
module.exports = {
findPopularEmotions: function (reviews) {
return new Promise(function (resolve, reject) {
var obj = [];
var emotion_obj = {};
var promises = [];
for (j in reviews) {
promises.push(util.extractEmotions(reviews[j].text).then(function (emotions) {
for (i in emotions) {
var e = emotions[i];
if (e in emotion_obj == false) {
emotion_obj[e] = 1;
} else {
emotion_obj[e] += 1;
}
}
}, function (err) {
console.log(err)
}));
}
Promise.all(promises).then(function () {
for (i in emotion_obj) {
obj.push({
emotion : i,
count : emotion_obj[i],
percentage: Math.round(100 * emotion_obj[i]/ reviews.length),
});
}
obj.sort(function (a, b) {
return b.count - a.count;
});
console.log(obj);
resolve(obj);
}).catch(function (err) {
reject(err);
});
});
},
};