This repository was archived by the owner on Aug 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
138 lines (115 loc) · 4.25 KB
/
Copy patheval.py
File metadata and controls
138 lines (115 loc) · 4.25 KB
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
# evaluation metrics
# current main script takes a subreddit name as input, outputs
# sorted lines of users and their karma within that subreddit
import sqlite3
import sys
from scipy.stats import spearmanr
# given username and subreddit strings, find user's karma within the subreddit
def findUserKarma(username, subreddit, cursor):
totalKarma = 0
# select submissions where author=username and subreddit=subreddit
cursor.execute('select score from Submission where subreddit=? and author=?', (subreddit, username))
submissions = cursor.fetchall()
for sub in submissions:
totalKarma += sub[0]
# select comments where author=username and subreddit=subreddit
cursor.execute('select score from Comment where subreddit=? and author=?', (subreddit, username))
comments = cursor.fetchall()
for comment in comments:
totalKarma += comment[0]
return totalKarma
# return a dict of usernames:karma for a particular subreddit
def findSubKarma(subreddit, cursor):
# retrieve usernames
cursor.execute('select username from User')
users = cursor.fetchall()
# initialize mapping of users->karma
userKarma = {}
for user in users:
userKarma[user[0]] = 0
# retrieve comments
cursor.execute('select author, score from Comment where subreddit=?', [subreddit,])
comments = cursor.fetchall()
for comment in comments:
userKarma[comment[0]] += comment[1]
# retrieve submissions
cursor.execute('select author, score from Submission where subreddit=?', [subreddit,])
submissions = cursor.fetchall()
for sub in submissions:
userKarma[sub[0]] += sub[1]
return userKarma
# return a dict of usernames:karma for multiple subreddits
def findSubKarma(subreddits, cursor):
# retrieve usernames
cursor.execute('select username from User')
users = cursor.fetchall()
# initialize mapping of users->karma
userKarma = {}
for user in users:
userKarma[user[0]] = 0
# retrieve comments
cursor.execute('select author, subreddit, score from Comment')
comments = cursor.fetchall()
for comment in comments:
if comment[1] in subreddits:
userKarma[comment[0]] += comment[2]
# retrieve submissions
cursor.execute('select author, subreddit, score from Submission')
submissions = cursor.fetchall()
for sub in submissions:
if sub[1] in subreddits:
userKarma[sub[0]] += sub[2]
return userKarma
# uses the Kendell-Tau algorithm to compare rankings
# as input, takes sorted dicts of username:rank
# as output, prints a single float
def kendellTau(dict1, dict2):
if len(dict1) != len(dict2):
sys.exit("Error: cannot compare rankings of different size.")
# naive direct computation (replace with bubble-sort alg?)
n = 0
for i in range(len(dict1)):
for j in range(i):
n += n + sign(dict1[i]-dict1[j]) * sign(dict2[i]-dict2[j])
return n
# runs the Spearman algorithm on two dicts
# returns list with (correlation coeff, 2-tailed p-value)
def spearman(dict1, dict2):
if len(dict1) != len(dict2):
sys.exit("Error: cannot compare rankings of different size.")
list1 = []
list2 = []
for key in dict1.keys():
list1.append(dict1[key])
list2.append(dict2[key])
results = spearmanr(list1, list2)
return results
# retrieves a dict of username:combo_score
def get_combo_score(cursor):
cursor.execute('select username, combo_score from Rank')
rows = cursor.fetchall()
combo_ranks = {}
for row in rows:
combo_ranks[row[0]] = row[1]
return combo_ranks
# main script
def main():
if len(sys.argv) < 2:
sys.exit("Not enough args: please provide subreddit name(s)")
conn = sqlite3.connect("merge.sqlite")
conn.text_factory = str
c = conn.cursor()
uk = findSubKarma(sys.argv[1:], c)
myRanks = get_combo_score(c)
c.close()
conn.close()
"""
sortedList = list(sorted(uk, key=uk.__getitem__, reverse=True))
for key in sortedList:
print key, uk[key]
"""
spear_results = spearman(myRanks, uk)
print "Spearman rank:", spear_results[0]
print "p-value: ", spear_results[1]
if __name__ == "__main__":
main()