-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsocialV4.py
87 lines (68 loc) · 1.73 KB
/
socialV4.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
import glob
import time
import ujson
import nltk
import os
import datetime
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-??'
print( os.path.isfile(file_to_open))
i = 0
clean_tokens = list()
political_words = ['donald','trump','hillary','clinton']
dates_x = list()
popularity_y = [0] *5
day = 0
previous_day = 0
day_count = 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']
createdDate = jo['created_utc']
d = datetime.datetime.utcfromtimestamp(createdDate)
# print(d.day)
day = d.day
if(day != previous_day):
dates_x.append(day)
previous_day = day
day_count += 1
print(previous_day)
print(day_count)
# print(d)
tokens += [t for t in body.split()]
i += 1
if(i> ITERATIONS):
break
for token in tokens:
# if token not in political_words:
# clean_tokens.remove(token)
if token.lower() in political_words:
popularity_y[day_count] += 1
clean_tokens.append(token.lower())
# day_count += 1
# elif token in stopwords.words('english') or len(token) <= 4 :
# clean_tokens.remove(token)
# print(jo['body'])
# subcnt[jo['subreddit']] += 1
# print(dates_x)
print(popularity_y)
freq = nltk.FreqDist(clean_tokens)
# print(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)