-
Notifications
You must be signed in to change notification settings - Fork 147
/
twitter-trends.js
103 lines (92 loc) · 2.59 KB
/
twitter-trends.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
/*
Trends
Kenneth Kufluk 2011
I take tweets and break them down into words.
I index the tweets by word and maintain a count of popular words.
The aim is to show micro-trends.
Listens:
- Roar.tweet
- common-words
Fires:
- Roar.newWordset
*/
$(function() {
var $tweetDiv =$('#activity');
var trend_words = {};
var hashtags = {};
var RE_break_into_words = /\b(\w{4,})\b/g;
var silent_words = ['http', 'bit', 'com'];
// Get: common-words
$.get('roar/common-words.txt', function(data) {
// silent_words = silent_words.concat(data.replace(/[\n ]/g, '').split(/,/));
});
// log the hashtags
$(window).bind('Roar.addHashtags', function(e, data) {
$.extend(hashtags, data);
});
// 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);
});