-
Notifications
You must be signed in to change notification settings - Fork 1
/
socialV3.py
61 lines (46 loc) · 1.24 KB
/
socialV3.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
import glob
import time
import ujson
import nltk
import os
from nltk.corpus import stopwords
from collections import defaultdict
ITERATIONS = 100000
print("Starting Social Program")
sr = stopwords.words('english')
start_time = time.time()
subcnt = defaultdict(int)
tokens = []
print(os.getcwd())
file_to_open = 'data_new/RC_2016-11-08'
print( os.path.isfile(file_to_open))
i = 0
for fn in glob.iglob(file_to_open):
with open(fn) as f:
for line in f:
# print(line)
jo = ujson.loads(line)
body = jo['body']
tokens += [t for t in body.split()]
i += 1
if(i> ITERATIONS):
break
# print(jo['body'])
# subcnt[jo['subreddit']] += 1
clean_tokens = list()
political_words = ['donald','trump','hillary','clinton']
for token in tokens:
# if token not in political_words:
# clean_tokens.remove(token)
if token.lower() in political_words:
clean_tokens.append(token.lower())
# elif token in stopwords.words('english') or len(token) <= 4 :
# clean_tokens.remove(token)
freq = nltk.FreqDist(clean_tokens)
print("--- %s seconds ---" % (time.time() - start_time))
# for key,val in freq.items():
# print(str(key) + ':' + str(val))
freq.plot(30,cumulative = False)
#for k, v in subcnt.items():
# print(v, k)
#print(tokens)