-
Notifications
You must be signed in to change notification settings - Fork 254
/
wordcloud.py
executable file
·298 lines (279 loc) · 6.51 KB
/
wordcloud.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#!/usr/bin/env python
from __future__ import print_function
import re
import sys
import json
import fileinput
def main():
try:
from urllib import urlopen # Python 2
except ImportError:
from urllib.request import urlopen # Python 3
MAX_WORDS = 100
word_counts = {}
stop_words = set(
[
"a",
"able",
"about",
"across",
"actually",
"after",
"against",
"agreed",
"all",
"almost",
"already",
"also",
"am",
"among",
"an",
"and",
"any",
"anyone",
"anyway",
"are",
"as",
"at",
"be",
"because",
"been",
"being",
"between",
"but",
"by",
"can",
"cannot",
"come",
"could",
"dear",
"did",
"do",
"does",
"either",
"else",
"ever",
"every",
"for",
"from",
"get",
"getting",
"got",
"had",
"has",
"have",
"he",
"her",
"here",
"hers",
"hey",
"hi",
"him",
"his",
"how",
"however",
"i",
"i'd",
"i'll",
"i'm",
"if",
"in",
"into",
"is",
"isnt",
"isn't",
"it",
"its",
"just",
"kind",
"last",
"latest",
"least",
"let",
"like",
"likely",
"look",
"make",
"may",
"me",
"might",
"more",
"most",
"must",
"my",
"neither",
"new",
"no",
"nor",
"not",
"now",
"of",
"off",
"often",
"on",
"only",
"or",
"other",
"our",
"out",
"over",
"own",
"part",
"piece",
"play",
"put",
"putting",
"rather",
"real",
"really",
"said",
"say",
"says",
"she",
"should",
"simply",
"since",
"so",
"some",
"than",
"thanks",
"that",
"that's",
"thats",
"the",
"their",
"them",
"then",
"there",
"these",
"they",
"they're",
"this",
"those",
"tis",
"to",
"too",
"try",
"twas",
"us",
"use",
"used",
"uses",
"via",
"wants",
"was",
"way",
"we",
"well",
"were",
"what",
"when",
"where",
"which",
"while",
"who",
"whom",
"why",
"will",
"with",
"would",
"yet",
"you",
"your",
"you're",
"youre",
]
)
for line in fileinput.input():
try:
tweet = json.loads(line)
except:
pass
for word in text(tweet).split(" "):
word = word.lower()
word = word.replace(".", "")
word = word.replace(",", "")
word = word.replace("...", "")
word = word.replace("'", "")
word = word.replace(":", "")
word = word.replace("(", "")
word = word.replace(")", "")
if len(word) < 3:
continue
if len(word) > 15:
continue
if word in stop_words:
continue
if word[0] in ["@", "#"]:
continue
if re.match("https?", word):
continue
if word.startswith("rt"):
continue
if not re.match("^[a-z]", word, re.IGNORECASE):
continue
word_counts[word] = word_counts.get(word, 0) + 1
sorted_words = list(word_counts.keys())
sorted_words.sort(key=lambda x: word_counts[x], reverse=True)
top_words = sorted_words[0:MAX_WORDS]
words = []
count_range = word_counts[top_words[0]] - word_counts[top_words[-1]] + 1
size_ratio = 100.0 / count_range
for word in top_words:
size = int(word_counts[word] * size_ratio) + 15
words.append({"text": word, "size": size})
wordcloud_js = urlopen(
"https://raw.githubusercontent.com/jasondavies/d3-cloud/master/build/d3.layout.cloud.js"
).read()
output = """<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>twarc wordcloud</title>
<script src="https://d3js.org/d3.v3.min.js"></script>
</head>
<body>
<script>
// embed Jason Davies' d3-cloud since it's not available in a CDN
%s
var fill = d3.scale.category20();
var words = %s
d3.layout.cloud().size([800, 800])
.words(words)
.rotate(function() { return ~~(Math.random() * 2) * 90; })
.font("Impact")
.fontSize(function(d) { return d.size; })
.on("end", draw)
.start();
function draw(words) {
d3.select("body").append("svg")
.attr("width", 1000)
.attr("height", 1000)
.append("g")
.attr("transform", "translate(400,400)")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return d.size + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.attr("text-anchor", "middle")
.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
})
.text(function(d) { return d.text; });
}
</script>
</body>
</html>
""" % (
wordcloud_js.decode("utf8"),
json.dumps(words, indent=2),
)
sys.stdout.write(output)
def text(t):
if "full_text" in t:
return t["full_text"]
return t["text"]
if __name__ == "__main__":
main()