-
Notifications
You must be signed in to change notification settings - Fork 0
/
labelTweets.js
58 lines (51 loc) · 1.5 KB
/
labelTweets.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
// **************************
// ****** DEPENDENCIES ******
// **************************
var _ = require('lodash');
var database = require('./api/database');
var schema = require('./api/schema');
var request = require('request');
// **************************
// ********* LABEL **********
// **************************
var classifierUrl = 'http://174.129.228.98/cgi-bin/R/fp_classifier?text=';
// connect to database
database.connect(function() {
console.log('Opened db connection.');
database.getTweetsWithLabel(false, function(err, tweets) {
if (err) {
bail('Failed to get tweets.', err);
}
console.log('Labeling ' + tweets.length + ' tweets.');
label(tweets);
});
}, function(err) {
bail('Failed to connect to db.', err);
});
label = function(tweets) {
if (!tweets.length) {
console.log('Done!');
process.exit(0);
}
var tweet = tweets[0];
var url = classifierUrl + encodeURIComponent(tweet.text);
request(url, function(err, resp, body) {
if (err || resp.statusCode != 200) {
bail('Failed to classify tweet.', err);
}
var databaseId = tweet['_id'];
var class_label = body.replace(/\s+/g, '');
database.updateTweet(databaseId, {class_label:class_label}, function(err, updated) {
if (err) {
bail('Failed to save tweet.', err);
}
console.log('Updated label to ' + updated.class_label);
label(_.rest(tweets));
});
});
};
bail = function(msg, err) {
console.log(msg);
console.log(err);
process.exit(1);
};