-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
136 lines (106 loc) · 3.5 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var models = require('../models/models')
, async = require('async')
, _ = require('underscore')
, languages = require('../lib/languages');
module.exports = function routes(app){
var io = require('socket.io').listen(app)
, Tweet = app.set('db').model('Tweet')
, Probability = app.set('db').model('Probability')
, twit = app.set('twit');
/* Socket IO */
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
io.set('log level', 1);
});
/* Connect to Twitter streaming API and start sending tweets to the client */
twit.stream('statuses/sample', function(stream) {
stream.on('data', classifyAndSendTweet);
});
function classifyAndSendTweet(data){
//classify a tweet based on word probabilities
var tweet = new Tweet(data)
, probability = {}
, query = []
, predicted_language
, max_prob = 0;
//build 'or' query
tweet.getWords().forEach(function(word){
query.push({ word: word });
});
Probability
.find()
.or(query)
.run(function(e, results){
var update = {};
languages.forEach(function(language){
var product = _.reduce(results, function(memo, word){ return memo * word.probability[language.code] || memo; }, 1);
var subtract = _.reduce(results, function(memo, word){ return memo * (1 - word.probability[language.code]) || memo; }, 1);
//minimum probability of 0.01
var result = product / ( product + subtract ) || 0.01;
probability[language.code] = Math.round(result*100000)/100000;
if(result > max_prob) {
max_prob = result;
predicted_language = language.code;
}
});
tweet.predicted_language = (max_prob > 0.5) ? predicted_language : 'other';
tweet.probability = probability;
io.sockets.emit('newTweet', tweet);
});
}
/* Routes */
app.get('/api/getLanguage', function(req, res){
Tweet.find()
.limit(100)
.run(function(e, results){
res.json(results);
});
});
app.get('/api/getLanguage/:language', function(req, res){
//find highest probablity tweets over 0.5
Tweet
.where('probability.' + req.params.language).$gt(0.5)
.where('predicted_language', req.params.language)
.limit(100)
.sort('probability.' + req.params.language, -1)
.run(function(e, results){
res.json(results);
});
});
app.get('/api/streamTweets', function(req, res){
twit.stream('statuses/sample', function(stream) {
console.log('Getting Tweet stream for 60 seconds');
var tweetCount = 0;
stream
.on('data', function(data){
if(data.text.charAt(0) != '@'){
var tweet = new Tweet(data);
tweet.save();
tweetCount++;
if(tweetCount % 50 === 0){
console.log(tweetCount + ' tweets collected');
}
}
})
.on('destroy', function(data){
try{
res.json({status: tweetCount + ' tweets collected'});
} catch(e) {}
});
//disconnect after 1 minute of tweets
setTimeout(stream.destroy, 60000);
});
});
app.get('/api/languages', function(req, res){
var results = {};
languages.forEach(function(language){
results[language.code] = { name: language.name };
});
res.json(results);
});
//Nothing specified
app.all('*', function notFound(req, res) {
res.send('node');
});
}