-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject.py
260 lines (207 loc) · 9.08 KB
/
project.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
import sys
import praw
import operator
import time
class Frequencies:
def __init__(self):
self.frequencies = {}
def add_frequency(self, key, value):
if key in self.frequencies:
self.frequencies[key] += value
else:
self.frequencies[key] = value
def add_by_frequencies(self,frequencies):
for key in frequencies.frequencies:
self.add_frequency(key, frequencies.frequencies[key])
def multiply_frequency(self, key, value):
if key in self.frequencies:
self.frequencies[key] *= value
else:
self.frequencies[key] = 0.0
def multiply_by_frequencies(self, frequencies):
for key in frequencies.frequencies:
self.multiply_frequency(key, frequencies.frequencies[key])
def multiply_by_scalar(self, scalar):
for key in self.frequencies:
self.multiply_frequency(key,scalar)
def divide_frequency(self, key, value):
if key in self.frequencies:
if value != 0:
if self.frequencies[key] == 0:
self.frequencies[key] = 1.0
else:
self.frequencies[key] /= (0.0 + value)
else:
if self.frequencies[key] == 0:
self.frequencies[key] = 1.0
else:
self.frequencies[key] = float('inf')
else:
if value > 0:
self.frequencies[key] = 0.0
else:
self.frequencies[key] = 1.0
def divide_by_frequencies(self, frequencies):
for key in frequencies.frequencies:
self.divide_frequency(key, frequencies.frequencies[key])
class User:
def __init__(self,projection, user, userName=None):
if user:
self.userName = str(user.name)
elif userName:
self.userName = userName
self.subredditFrequencies = Frequencies()
self.extendedFrequencies = Frequencies()
self.userObject = user
self.projection = projection
self.totalComments = 0.0
def get_frequencies(self):
print "Processing User " + self.userName
while True:
try:
#for comment in self.userObject.get_comments(limit=100):
for comment in self.userObject.get_comments(limit=self.projection.thing_limit):
self.subredditFrequencies.add_frequency(str(comment.subreddit.display_name.strip(' ').lower()), 1.0)
if str(comment.subreddit.display_name.strip(' ').lower()) is str(self.projection.subreddit.display_name.strip(' ').lower()):
self.projection.totalComments += 1
self.totalComments += 1
break
except:
print "error..."
def get_extended_profile(self):
for subreddit in self.subredditFrequencies.frequencies:
if self.subredditFrequencies.frequencies[subreddit] > 0.1:
subProj = Projection(subreddit, 100)
subProj.get_comments()
subProj.get_commentor_frequencies()
subProj.register_subreddit_frequencies()
self.extendedFrequencies.add_by_frequencies(subProj.frequencies)
class Projection:
def __init__(self, subredditName, thing_limit):
user_agent = ("Testing Reddit Functionality by /u/Reddit_Projector https://github.com/joshlemer/RedditProject")
self.reddit = praw.Reddit(user_agent)
self.thing_limit = thing_limit
self.subreddit = self.reddit.get_subreddit(subredditName)
self.comments = {}
self.subredditFrequencies = Frequencies()
self.commentors = []
self.commentorNames = []
def get_comments(self):
while True:
try:
self.comments = list(self.subreddit.get_comments(limit=self.thing_limit))
break
except:
print "error..."
def register_subreddit_frequency (self, subredditName, frequency):
if subredditName in self.subredditFrequencies.frequencies:
self.subredditFrequencies[subredditName] += frequency
else:
self.subredditFrequencies[subredditName] = frequency
def register_subreddit_frequencies(self):
for commentor in self.commentors:
for freq in commentor.subredditFrequencies.frequencies:
self.subredditFrequencies.add_frequency(freq, self.calculate_frequency(commentor, freq))
def calculate_frequency(self, commentor, subreddit):
#(commentorComments in origSub / totalInOriginalSub) * (frequencyOfCommentorInSubreddit / totalCommenterComments)
origSubComments = 0
for comment in self.comments:
if comment is not None and hasattr(comment,'author') and comment.author is not None and hasattr(comment.author, 'name') and comment.author.name is not None and str(commentor.userName) == str(comment.author.name):
origSubComments += 1
if origSubComments is None:
origSubComments = 0.0
absFreq = commentor.subredditFrequencies.frequencies.get(subreddit)
if absFreq is None:
absFreq = 0.0
result = ((origSubComments + 0.0)/len(self.comments)) * ((0.0 + absFreq) / commentor.totalComments)
#print "%s %s %s %s %s" % (origSubComments, len(self.comments), absFreq, commentor.totalComments, result)
return result
def get_commentor_frequencies(self):
i = 0
for comment in self.comments:
i += 1
print "On comment %d / %d" % (i, self.thing_limit)
if comment is not None and hasattr(comment, 'author') and hasattr(comment.author, 'name') and str(comment.author) not in self.commentorNames:
self.commentorNames.append(str(comment.author))
newUser = User(self,comment.author)
newUser.get_frequencies()
self.commentors.append(newUser)
def run_analysis(subreddit, depth):
myProj = Projection(subreddit, depth)
myProj.get_comments()
myProj.get_commentor_frequencies()
myProj.register_subreddit_frequencies()
myList = []
mySum = 0.0
for key, value in myProj.subredditFrequencies.frequencies.iteritems():
temp = [key, value]
mySum += value
myList.append(temp)
myList = sorted(myList, key=operator.itemgetter(1),reverse=True)
filename = subreddit + "_" + time.strftime("%Y-%m-%d") + "_" + time.strftime("%X") + ".txt"
file = open(filename, "w")
for item in myList:
file.write("%s %s\n" % (item[0], item[1]))
file.close()
print "Information printed to " + filename
def run_diff_analysis(subredditA, subredditB, depth):
projA = Projection(subredditA, depth)
projA.get_comments()
projA.get_commentor_frequencies()
projA.register_subreddit_frequencies()
projB = Projection(subredditB, depth)
projB.get_comments()
projB.get_commentor_frequencies()
projB.register_subreddit_frequencies()
diff_freqs = Frequencies()
negative_B_freqs = Frequencies()
negative_B_freqs.add_by_frequencies(projB.subredditFrequencies)
negative_B_freqs.multiply_by_scalar(-1)
diff_freqs.add_by_frequencies(projA.subredditFrequencies)
diff_freqs.add_by_frequencies(negative_B_freqs)
myList = []
mySum = 0.0
for key, value in diff_freqs.frequencies.iteritems():
temp = [key, value]
mySum += value
myList.append(temp)
myList = sorted(myList, key=operator.itemgetter(1),reverse=True)
filename = subredditA + "-" + subredditB + "_" + time.strftime("%Y-%m-%d") + "_" + time.strftime("%X") + ".txt",
file = open(filename , "w")
for item in myList:
file.write("%s %s\n" % (item[0], item[1]))
file.close()
print "Information recorded to file " + filename
def run_percent_analysis(subredditA, subredditB, depth):
projA = Projection(subredditA, depth)
projA.get_comments()
projA.get_commentor_frequencies()
projA.register_subreddit_frequencies()
projB = Projection(subredditB, depth)
projB.get_comments()
projB.get_commentor_frequencies()
projB.register_subreddit_frequencies()
percent_freqs = Frequencies()
percent_freqs.add_by_frequencies(projA.subredditFrequencies)
percent_freqs.divide_by_frequencies(projB.subredditFrequencies)
myList = []
mySum = 0.0
for key, value in percent_freqs.frequencies.iteritems():
temp = [key, value]
mySum += value
myList.append(temp)
myList = sorted(myList, key=operator.itemgetter(1),reverse=True)
filename = subredditA + "-percent-of" + subredditB + "_" + time.strftime("%Y-%m-%d") + "_" + time.strftime("%X") + ".txt"
file = open(filename, "w")
for item in myList:
file.write("%s %s\n" % (item[0], item[1]))
file.close()
print "Information printed to " + filename
if len(sys.argv) >= 2:
if len(sys.argv) >= 3:
if len(sys.argv) >= 4:
run_percent_analysis(sys.argv[1], sys.argv[2],int(sys.argv[3]))
else:
run_analysis(sys.argv[1], int(sys.argv[2]))
else:
run_analysis(sys.argv[1], 10)