-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun.py
266 lines (225 loc) · 10 KB
/
run.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
import praw
import pandas as pd
from datetime import datetime
# TODO: Make this program faster! (a lot faster, this is way too slow)
def display_praw(name):
reddit = praw.Reddit(client_id='Pj5o8QpNXXJY9A',
client_secret='pQKMRBmhp0In48NoNvvktfRo2eA',
password='prawisgreat',
user_agent='Reddit Unlocked CS196 Project @ UIUC',
username='RedditUnlocked196')
subreddit = reddit.subreddit(name)
threads_df = pd.DataFrame({
'Title': (),
'URL': (),
'Upvote Ratio (%)': (),
'Net Score': (),
'# of Upvotes': (),
'# of Downvotes': (),
'Post Date': (),
'Self Post?': (),
'Video Post?': (),
'Domain': ()
})
threads_df = threads_df[['Title', 'URL', 'Upvote Ratio (%)', 'Net Score', '# of Upvotes', '# of Downvotes',
'Post Date', 'Self Post?', 'Video Post?', 'Domain']]
for thread in subreddit.top('week', limit=40): # TODO: change limit number when actually deploying program. 15 is the testing number.
# if thread.is_video:
# continue
if 'fb' in thread.url:
continue
actualUps = float(thread.upvote_ratio * thread.score) / float(thread.upvote_ratio * 2 - 1)
actualDowns = actualUps - thread.score
gather = pd.Series([thread.title, thread.url, thread.upvote_ratio * 100, thread.score,
actualUps, actualDowns, thread.created_utc,
thread.is_self, thread.is_video, thread.domain],
index=['Title', 'URL', 'Upvote Ratio (%)', 'Net Score', '# of Upvotes', '# of Downvotes',
'Post Date', 'Self Post?', 'Video Post?', 'Domain'])
threads_df = threads_df.append(gather, ignore_index=True)
threads_dict = threads_df.to_dict(orient='records')
for entry in threads_dict:
if isinstance(str(entry['Post Date']), str):
time = datetime.fromtimestamp(entry['Post Date'])
formatTime = time.strftime('%b %d, %Y')
else:
formatTime = None
entry['Post Date'] = formatTime
return threads_dict
def stats_praw(name):
reddit = praw.Reddit(client_id='Pj5o8QpNXXJY9A',
client_secret='pQKMRBmhp0In48NoNvvktfRo2eA',
password='prawisgreat',
user_agent='Reddit Unlocked CS196 Project @ UIUC',
username='RedditUnlocked196')
info = reddit.request('GET', '/r/' + name + '/about.json')
infoDict = {}
infoDict['Current Users'] = info['data']['active_user_count']
infoDict['Creation Date'] = (datetime.fromtimestamp(info['data']['created_utc'])).strftime('%b %d, %Y')
infoDict['Subscriber Count'] = info['data']['subscribers']
infoDict['Title'] = info['data']['title']
infoDict['Icon'] = info['data']['icon_img']
return infoDict
import plotly
plotly.tools.set_credentials_file(username='reddit_unlocked', api_key='gfnXKc7JvUKST4HRJyFX')
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.graph_objs import *
# takes a dictionary of dictionaries of keywords from body text as input and returns the url for the plotly html embedding of
# scatterplot made from the keywords and their attributes
# 'Keyword','Occurences', 'Upvotes', 'Downvotes', "Score", "Subjectivity", "Polarity", "Domain"
def body_to_graph(words = {}, subreddit = str):
"""
:type subreddit: String
"""
frames = []
#Turns dictionary of dictionaries into list of dataframes
for key, value in words.items():
frames.append(pd.DataFrame(data = value, columns = [key], index = ['Keyword','Occurences', 'Upvotes', 'Downvotes', 'Score', 'Subjectivity', 'Polarity', 'Domain']).transpose())
#Concatenates the list of dataframes
data_df = pd.concat(frames)
trace1 = go.Scatter(
y = data_df.Subjectivity, #Subjectivity of the text the keyword was found in on y axis
x = data_df.Occurences * data_df.Score,#Occurrences * Score on x-axis for more spread out data
mode = 'markers',
marker = dict(
size = (data_df.Occurences) * 20, #Occurrences of Keyword for size
color = data_df.Polarity, #Polarity for color of the post (blue is sad, red is happy)
colorscale = 'Portland',
showscale = True
),
text = "Keyword: " + data_df.Keyword
)
layout = go.Layout(
annotations=Annotations([
Annotation(
x=0.5,
y=-0.123,
showarrow=False,
text='(Occurrences * Score)',
xref='paper',
yref='paper'
),
Annotation(
x=1.055,
y=0.5,
showarrow=False,
text='Text Polarity',
textangle=-90,
xref='paper',
yref='paper'
),
Annotation(
x=.01,
y=1,
showarrow=False,
text='Size = Occurrences',
textangle=0,
xref='paper',
yref='paper',
bordercolor = '#1f77b4',
font=dict(
family='Courier New, monospace',
size=16,
color='#ff7f0e'
)
)
]),
title = 'Stats of top reddit /r/' + subreddit + ' keywords',
yaxis = dict(
title = 'Subjectivity',
ticks = 5,
),
xaxis = dict(
title = 'popularity',
ticklen = 10,
)
)
data = [trace1]
fig = go.Figure(data = data, layout = layout)
url = py.plot(fig, filename='reddit plot', auto_open=False)
return "" + url
import operator
import rake as rake
rake_object = rake.Rake("SmartStoplist.txt", 1, 2, 1)
from textblob import TextBlob, Word, Blobber
import newspaper
from newspaper import Article
import numpy as np
def get_keyword_dict(input_dict):
# Transforms dict returned by display_praw into DataFrame for working with
top10news_df = pd.DataFrame.from_dict(input_dict)
words = {}
## NEWSPAPER STUFF HERE ##
# Get keywords out of all articles
for i in range(len(top10news_df)):
if "self" in top10news_df.iloc[i]["Domain"]:
continue
elif "youtube" in top10news_df.iloc[i]["Domain"]:
continue
elif "imgur" in top10news_df.iloc[i]["Domain"]:
continue
myArticle = Article(top10news_df.iloc[i]['URL'])
try:
myArticle.download()
myArticle.parse()
except:
continue
myArticle.nlp()
# Run sentiment analysis on each article, fetch subjectivity and polarity
text = myArticle.text
blob = TextBlob(text)
polarity = blob.sentiment.polarity
subjectivity = blob.sentiment.subjectivity
# Get associated Reddit post info for each keyword, store in dictionary
for keyword in myArticle.keywords:
# Don't waste time with numeric keywords, skip them if they contain numbers
if any(char.isdigit() for char in keyword):
continue
if keyword not in words:
words[keyword] = [keyword, 1,
top10news_df.iloc[i]['# of Upvotes'],
top10news_df.iloc[i]["# of Downvotes"],
top10news_df.iloc[i]["Net Score"],
subjectivity, polarity,
{(top10news_df.iloc[i]["Domain"]):1}]
else:
words[keyword][1] += 1
words[keyword][2] += top10news_df.iloc[i]['# of Upvotes']
words[keyword][3] += int(top10news_df.iloc[i]['# of Downvotes'])
words[keyword][4] += int(top10news_df.iloc[i]['Net Score'])
words[keyword][5] = np.mean([subjectivity, words[keyword][5]])
words[keyword][6] = np.mean([polarity, words[keyword][6]])
if top10news_df.iloc[i]["Domain"] in words[keyword][7]:
words[keyword][7][(top10news_df.iloc[i]["Domain"])] += 1
else:
words[keyword][7][top10news_df.iloc[i]["Domain"]] = 1
## RAKE STUFF HERE ##
# Pull keywords from title strings
for wordPair in rake_object.run(top10news_df.iloc[i]['Title']):
currentWord = wordPair[0]
# Don't waste time with numeric keywords, skip them if they contain numbers
if any(char.isdigit() for char in currentWord):
continue
# Grab associated Reddit post data for each keyword, store in dictionary
if currentWord not in words:
words[currentWord] = [currentWord, 1,
top10news_df.iloc[i]['# of Upvotes'],
top10news_df.iloc[i]["# of Downvotes"],
top10news_df.iloc[i]["Net Score"],
subjectivity, polarity,
{(top10news_df.iloc[i]["Domain"]):1}]
else:
words[currentWord][1] += 1
words[currentWord][2] += int(top10news_df.iloc[i]['# of Upvotes'])
words[currentWord][3] += int(top10news_df.iloc[i]['# of Downvotes'])
words[currentWord][4] += int(top10news_df.iloc[i]['Net Score'])
if top10news_df.iloc[i]["Domain"] in words[currentWord][7]:
words[currentWord][7][(top10news_df.iloc[i]["Domain"])] += 1
else:
words[currentWord][7][top10news_df.iloc[i]["Domain"]] = 1
### FOR GARY'S USE ###
# Output dictionary is named 'words' #
# Format is as such: #
# key = keyword #
# value = [Occurences, Upvotes, Downvotes, Score, Subjectivity, Polarity, Domain Dictionary] #
return words