-
Notifications
You must be signed in to change notification settings - Fork 1
/
process.py
executable file
·537 lines (450 loc) · 14.5 KB
/
process.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
import re
import watson
#import azure_query
import json
import os
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
import simplejson
from urlparse import urlparse
from urlparse import *
import ast
from pprint import pprint
# fixed nearly 30 items to remove quotes
# 3711.json tags super broken
# 256841.json had a \ to take out
# g1: super users?
# g2: languages?
# g3: static or dynamic graphs? per type?
# g4: what is being graphed? (based on tags, disc, axis names)
# g5: are certain graphs most commented upon? most shared?
# g6: do people make the same types of graphs?
# g7: how well do people describe graphs?
# g8: makeup of a channel (i.e. how many graphs / what grouping?)
# channel structure '{"name": "' + self.name + '", "gauges": "' + self.gauges + '", "gauges2": "' + self.gauges2 + '", "statuses": "' + self.statuses + '", "url": "' + self.url + '", "author": "' + self.author + '", "disc": "' + self.disc + '", "tags": ' + self.tags + ', "shares": "' + self.shares + '", "charts": ' + self.charts + ', "comments": ' + self.comments + ', "maps": "' + self.maps + '", "matlabs": "' + self.matlabs + '", "language": "' + self.language + '"}'
# forum structure forum[threads[posts[author,content]
# feeds {"channel":{"id":292812,"name":"BME280"
d = {} #channels
r = {} #forums
l = {} #feeds
a = {} #author index
language_count = {}
chart_list = []
tag_list = []
discs = []
author_count = {}
tags_per_author = {}
num_authors = 0
num_tags = 0
num_languages = 0
num_disc = 0
num_graphs_w_xaxis = 0
num_graphs_w_yaxis = 0
avg_number_graphs_w_any_axis_label_per_author = 0
avg_tags_per_author = 0
avg_graphs_per_author = 0
avg_disc_length = 0
num_comments = 0
num_comments_with_link = 0
num_line_charts = 0
num_gauges = 0
num_images = 0
def word_cloud():
global tag_list
tag_list2 = filter( lambda x: ( 'temp' not in x.lower() ) and ( 'hum' not in x.lower() ) and ( 'dew' not in x.lower() ) and ( 'weather' not in x.lower() ), tag_list )
word_str = '+'.join(tag_list2)
wordcloud = WordCloud(width=1000, height=500, regexp=r"\w[\w' ]+").generate(word_str)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
wordcloud = WordCloud(max_font_size=40).generate(word_str)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
def word_cloud_array(array):
word_str = ''.join(array)
wordcloud = WordCloud().generate(word_str)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
wordcloud = WordCloud(max_font_size=40).generate(word_str)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()
def load_to_mem(forums_on, channels_on, feeds_on):
global d
global r
global a
global r
cnt = 0
if forums_on:
for filename in os.listdir("./forums"):
#print filename
if ".json" in filename and not filename == ".json":
fi = open("./forums/"+filename, "r")
j_str = fi.read()
j = ast.literal_eval(j_str)
url = j['url']
url_s = url.split("/")
forum_name = url_s[-3] # + "-" + url_s[-2]
#reverse forum into author index
f_dd = {}
for thread in j['threads']:
thread_url = thread['url']
posts = thread['posts']
for post in posts:
rev_post = {}
author = post['author']
date = post['date']
content = post['content']
rev_post['date'] = date
rev_post['content'] = content
rev_post['thread_url'] = thread_url
rev_post['forum_name'] = forum_name
rev_post['forum_url'] = url
print author + "," + clean_text(content)
#wt = watson.run_watson(clean_text(content))
#print author + "@@@" + clean_text(content) + "@@@" + wt
#exit()
'''
try:
f_dd[content] = f_dd[content] + 1
except:
f_dd[content] = 1
'''
try:
a[author].append(rev_post)
except:
a[author] = []
a[author].append(rev_post)
try:
g = r[forum_name]
r[forum_name] = g.append(j)
except:
r[forum_name] = []
r[forum_name].append(j)
'''
for author, value in a.iteritems():
for post in value:
ct = clean_text(post['content'])
wt = watson.run_watson(ct)
print author + "@@@" + ct + "@@@" + str(wt)
'''
'''
threads = j['threads']
for thread in threads:
posts = thread['posts']
for post in posts:
print post
exit()
'''
'''
for r in a:
print r
exit()
'''
if channels_on:
for filename in os.listdir("./json/"):
if ".json" in filename and not filename == ".json":
#print filename
fi = open("./json/"+filename,"r")
j_str = fi.read()
if '"tags": ]' in j_str: #fix broken tags section :/
j_str = j_str.replace('"tags": ]', '"tags": []')
if '"charts": ]' in j_str: #fix broken tags section :/
j_str = j_str.replace('"charts": ]', '"charts": []')
j_str = j_str.replace("\n",'').replace('\r','').replace('\t','')
if "}{" in j_str: #fix append error
n = j_str.find("}{")
j_str = j_str[:n+1]
js = simplejson.loads(j_str)
d[filename[:-5]] = j_str
if feeds_on:
for filename in os.listdir("./feeds/"):
if ".json" in filename and not filename == ".json":
#print filename
fi = open("./feeds/"+filename,"r")
j_str = fi.read()
#print j_str
js = simplejson.loads(j_str)
l[filename[:-5]] = j_str
def author_histogram():
global author_count
num_channels_np = map(int, author_count.values())
num_channels = np.array(num_channels_np)
max_cnt = 0
for num in num_channels_np:
if max_cnt < num:
max_cnt = num
bins = range(0, max_cnt+10)
plt.hist(num_channels, bins=bins)
#plt.yscale('log', nonposy='clip')
plt.yscale('log')
plt.title("histogram")
plt.show()
def tag_per_author():
global tags_per_author
#print tags_per_author
def extract(j_str):
global tag_list
global author_count
global tags_per_author
global language_count
global discs
#print j_str
#exit()
'''
uf_discs = {}
f = open('discs_labeled.csv','r')
dis = f.read()
dis_s = dis.split("\n")
for line in dis_s:
if len(line.split(",")) == 2:
l_spl = line.split(",")
disc = l_spl[0]
label = l_spl[1]
uf_discs[disc] = label
'''
j = json.loads(j_str)
tags = j['tags']
author = j['author']
charts = j['charts']
language = j['language']
disc = j['disc']
c_id = j['url'].split("/")[-1]
REPLACE_NO_SPACE = re.compile("(\.)|(\;)|(\:)|(\!)|(\')|(\?)|(\,)|(\")|(\()|(\))|(\[)|(\])")
REPLACE_WITH_SPACE = re.compile("(<br\s*/><br\s*/>)|(\-)|(\/)")
if not len(disc) == 0 and language == 'en':
#disc = [REPLACE_NO_SPACE.sub("", line.lower()) for line in disc]
#disc = [REPLACE_WITH_SPACE.sub(" ", line) for line in disc]
discs.append(clean_text(disc))
# print author + "@@@" + clean_text(disc) + "@@@" + c_id + "@@@" + azure.run_azure_keywords(clean_text(disc))
# exit()
try:
cur = language_count[language]
cur = cur + 1
language_count[language] = cur
except:
language_count[language] = 0
# construct word_str for word cloud
for tag in tags:
tag_list.append(tag)
# sort into author list
try:
author_count[author] = author_count[author] + 1
except:
author_count[author] = 1
#print "authors: " + str(len(author_count.keys()))
# tags per author
try:
cur_tag_list = tags_per_author[author];
for tag in tags:
cur_tag_list.append(tag)
tags_per_author[author] = cur_tag_list
except:
cur_tag_list = []
for tag in tags:
cur_tag_list.append(tag)
tags_per_author[author] = cur_tag_list
# all charts
for chart in charts:
chart_list.append([chart,c_id])
def parse_update(url):
qs = urlparse(url)[4]
o = parse_qs(qs)
try:
return o['update']
except:
return None
def parse_title(url):
qs = urlparse(url)[4]
o = parse_qs(qs)
try:
return o['title']
except:
return None
def test_retrieve(index):
global d
try:
j_str = d[str(index)]
extract(j_str)
except:
print index
print j_str
cnt = cnt + 1
def query_forum():
global a
for key, value in a.iteritems():
#print key
#exit()
try:
print key, len(f[key])
except:
pass
#exit()
def query_all():
global d
for key,value in d.iteritems():
extract(d[key])
def feed_parsing():
global l
for key, value in l.iteritems():
js = simplejson.loads(value)
print js['channel']['id']
def chart_parsing():
line = 0
line_titles = []
line_update = []
video = 0
status = 0
maps = 0
total = 0
column = 0
column_titles = []
column_update = []
spline = 0
spline_titles = []
spline_update = []
bar = 0
bar_titles = []
bar_update = []
step = 0
step_titles = []
step_update = []
unknown = 0
unknown_titles = []
unknown_update = []
for chart_l in chart_list:
chart = chart_l[0]
uid = chart_l[1]
total += 1
if "type=line" in chart:
line += 1
print "line,"+uid
title = parse_title(chart)
if title:
line_titles.append(title[0])
update = parse_update(chart)
if update:
line_update.append(update[0])
elif "www.youtube.com" in chart:
video += 1
print "video,"+uid
elif "status/recent" in chart:
status += 1
print "s,"+uid
elif "type=column" in chart:
column += 1
print "column,"+uid
title = parse_title(chart)
if title:
column_titles.append(title[0])
update = parse_update(chart)
if update:
column_update.append(update[0])
elif "type=spline" in chart:
spline += 1
print "spline,"+uid
title = parse_title(chart)
if title:
spline_titles.append(title[0])
update = parse_update(chart)
if update:
spline_update.append(update[0])
elif "type=bar" in chart:
bar += 1
print "bar,"+uid
title = parse_title(chart)
if title:
bar_titles.append(title[0])
update = parse_update(chart)
if update:
bar_update.append(update[0])
elif "type=step" in chart:
step += 1
print "step,"+uid
title = parse_title(chart)
if title:
step_titles.append(title[0])
update = parse_update(chart)
if update:
step_update.append(update[0])
elif "maps/channel_show" in chart:
print "map,"+uid
maps += 1
elif "channel" in chart and "chart" in chart:
print "unknown,"+uid
unknown += 1
title = parse_title(chart)
if title:
unknown_titles.append(title[0])
update = parse_update(chart)
if update:
unknown_update.append(update[0])
else:
pass
# print chart
#word_cloud_array(line_titles)
# print line, video, status, maps, total, column, spline, bar, step, unknown
def rake():
from rake_nltk import Rake, Metric
#r = Rake()
r = Rake(ranking_metric=Metric.WORD_FREQUENCY)
words = ""
for disc in discs:
words = words + ". " + disc
r.extract_keywords_from_text(words)
print r.get_ranked_phrases_with_scores()
'''
tags = ""
for tag in tag_list:
tags = tags + " " + tag
r.extract_keywords_from_text(tags)
print r.get_ranked_phrases_with_scores()
'''
def create_author_index():
pass
# words = get_all_descriptions()
def clean_text(text):
text = text.encode('utf-8')
text = text.lower()
text = re.sub(r"what's", "what is ", text)
text = re.sub(r"\'s", " ", text)
text = re.sub(r"\'ve", " have ", text)
text = re.sub(r"can't", "can not ", text)
text = re.sub(r"n't", " not ", text)
text = re.sub(r"i'm", "i am ", text)
text = re.sub(r"\'re", " are ", text)
text = re.sub(r"\'d", " would ", text)
text = re.sub(r"\'ll", " will ", text)
text = re.sub(r"\'scuse", " excuse ", text)
text = re.sub('\W', ' ', text)
text = re.sub('\s+', ' ', text)
text = text.strip(' ')
return text
'''
for line in dis.split("\n"):
l_spl = line.split(",")
print line
'''
load_to_mem(False, True, False)
query_all()
#for disc in discs:
# print clean_text(disc)
#query_forum()
#rake()
#print len(language_count.keys())
# chart_parsing()
#feed_parsing()
#print len(tag_list)
#print len(discs)
#print line, video, status, maps, total
#print tag_list
word_cloud()
#author_histogram()
#tag_per_author()
#test_retrieve(40150)
#for key, value in d.iteritems() :
# print key