Permalink
Browse files

Updates to streaming

  • Loading branch information...
1 parent 782454b commit 4c4f2a6bbb675161a5ae5d1928d3982db66b1c17 @brendannee committed Mar 31, 2012
Showing with 49 additions and 37 deletions.
  1. +13 −3 public/js/script.js
  2. +36 −34 routes/index.js
View
16 public/js/script.js
@@ -77,13 +77,13 @@ $(document).ready(function(){
var tweetDivs = $('.tweetContainer .tweet');
tweetQueue.push(tweet);
- if(tweetQueue.length >= 10 || tweetDivs.length <= 50){
+ if(tweetQueue.length >= 10 || tweetDivs.length <= 20){
renderTweets(tweetQueue);
tweetQueue = [];
//remove elements from the dom every 10 tweets
if(tweetDivs.length > 60){
- var tweetsToRemove = $('.tweetContainer .tweet:lt(' + (tweetDivs.length - 50) +')');
+ var tweetsToRemove = $('.tweetContainer .tweet:lt(' + (tweetDivs.length - 20) +')');
tweetsToRemove.slideUp(function(){
tweetsToRemove.remove();
@@ -114,15 +114,25 @@ $(document).ready(function(){
name: languages[tweet.predicted_language]['name']
, code: tweet.predicted_language
}
+
+ //only show the top 4 probabilities over 0.3
tweet.probabilities = [];
for(var language in tweet.probability){
- if(tweet.probability[language] > 0.3){
+ if(tweet.probability[language] > 0.3 && tweet.probabilities.length <= 4){
tweet.probabilities.push( { language:language, probability: Math.min(0.99, Math.round(tweet.probability[language]*100)/100 ), name: languages[language]['name'] } );
}
}
+
+ //if nothing, then mark it unknown
+ if(!tweet.probabilities.length){
+ tweet.probabilities.push( {language:other, probability:0.99 } );
+ }
+
tweet.probabilities.sort(function(a, b){ return b.probability - a.probability; });
content.append(ich.showTweet(tweet));
+
} catch(e){}
+
});
$('#content .tweets').append(content.html());
$('.timeago').timeago();
View
70 routes/index.js
@@ -21,48 +21,50 @@ module.exports = function routes(app){
/* Connect to Twitter streaming API and start sending tweets to the client */
twit.stream('statuses/sample', function(stream) {
- 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 });
- });
+ stream.on('data', classifyAndSendTweet);
+ });
- 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);
+ 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 });
+ });
- //minimum probability of 0.01
- var result = product / ( product + subtract ) || 0.01;
+ 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);
- probability[language.code] = Math.round(result*100000)/100000;
+ //minimum probability of 0.01
+ var result = product / ( product + subtract ) || 0.01;
- if(result > max_prob) {
- max_prob = result;
- predicted_language = language.code;
- }
- });
+ probability[language.code] = Math.round(result*100000)/100000;
- tweet.predicted_language = (max_prob > 0.5) ? predicted_language : 'other';
+ if(result > max_prob) {
+ max_prob = result;
+ predicted_language = language.code;
+ }
+ });
- tweet.probability = probability;
+ tweet.predicted_language = (max_prob > 0.5) ? predicted_language : 'other';
- io.sockets.emit('newTweet', tweet);
+ tweet.probability = probability;
- });
- }
- });
+ io.sockets.emit('newTweet', tweet);
+
+ });
+ }
/* Routes */

0 comments on commit 4c4f2a6

Please sign in to comment.