-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathsentiment.js
112 lines (81 loc) · 2.54 KB
/
sentiment.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
106
107
108
109
110
111
112
/** @module */
var db = require('../db')
var log = require('../log')
var request = require('request')
var sentiment = require('sentiment')
var METAMIND_URL = 'https://www.metamind.io/language/classify'
var NEG_THRESHOLD = -0.8
var ADMIN_GROUP = '1461229807499165'
var checked = {}
var reportedRef = db.child('reported')
var reported = {}
reportedRef.on('value', function (snapshot) {
reported = snapshot.val() || {}
})
// An unfortunate hack due to the limitations of the MetaMind free tier, which
// appears to be the only tier available at the moment.
var lastPromise = Promise.resolve()
function getSentiment(post) {
if (process.env.METAMIND_KEY) {
var apiKey = process.env.METAMIND_KEY
var options = {
method: 'POST',
headers: {
Authorization: 'Basic ' + apiKey
},
json: {
classifier_id: 155,
value: post.message
}
}
lastPromise = lastPromise.then(function () {
return new Promise(function (resolve, reject) {
request(METAMIND_URL, options, function (err, res, body) {
if (err) return reject(err)
if (res.statusCode == 200) {
for (var c of body.predictions) {
if (c.class_id === 1) {
return resolve(-c.prob)
}
}
}
reject(res)
})
})
})
return lastPromise
} else {
var s = sentiment(post.message)
return Promise.resolve(s.comparative)
}
}
module.exports = function(group, thread) {
var promises = []
for (var post of thread) {
var checksum = post.getChecksum()
if (checksum in checked ||
post.id in reported ||
post.message == null) continue
checked[checksum] = true
!(function (post, checksum) {
var p = getSentiment(post).then(function (s) {
if (s < NEG_THRESHOLD) {
reportedRef.child(post.id).set(new Date().getTime())
var params = {
name: '⚠ ' + post.fromName + ' // ' + post.id,
description: '(Score: ' + s + ') ' + post.message,
link: 'http://facebook.com/' + post.id
}
return group.client.post(ADMIN_GROUP + '/feed', params).then(function () {
log.info({ post: post }, 'reported post with ID: %s (Score: %s)', post.id, s)
})
}
}, function (err) {
log.error(err, 'error classifying post with ID: %s. Retrying...', post.id)
delete checked[checksum]
})
promises.push(p)
})(post, checksum)
}
return Promise.all(promises)
}