// Listen: tweet
$(window).bind('Roar.tweet', function(e, tweet) {
// console.log('trends: indexing tweet');
if (!tweet.text) return;
var matches = tweet.text.match(RE_break_into_words);
var word, hashes = {};
// check tweet for hashtags
for (tag in hashtags) {
if (!hashtags.hasOwnProperty(tag)) continue;
if (matches.indexOf(tag)>=0) {
if (!hashes[tag]) hashes[tag]=0;
hashes[tag]++;
}
if (hashtags[tag].alternate) {
for (var subtag in hashtags[tag].alternate) {
if (matches.indexOf(hashtags[tag].alternate[subtag])) {
if (!hashes[tag]) hashes[tag]=0;
hashes[tag]++;
}
}
}
}
// add the words to the trends
for (var i=0, l=matches.length; i<l ;i++) {
word = matches[i].toLowerCase();
// increment each word
if (trend_words[word]) {
trend_words[word].count++;
} else {
trend_words[word] = {
count:1,
hashtags: hashtags,
hash:{}
};
}
// increment the hashes
for (var hash in hashes) {
if (!hashes.hasOwnProperty(hash)) continue;
if (trend_words[word].hash[hash]) {
trend_words[word].hash[hash] += hashes[hash];
} else {
trend_words[word].hash[hash] = hashes[hash];
}
}
}
});
var tidyup_words = setInterval(function() {
var word;
for (word in trend_words) {
if (trend_words.hasOwnProperty(word)) {
trend_words[word].count--;
// TODO: perf test this
if (trend_words[word].count<=0) delete trend_words[word];
}
}
}, 3000);
// update the blobs
var ballTimer = setInterval(function() {
var word;
for (word in silent_words) {
if (!silent_words.hasOwnProperty(word)) continue;
delete trend_words[silent_words[word]];
}
$(window).trigger('Roar.newWordset', [trend_words]);
}, 1000);