-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranked.py
executable file
·114 lines (88 loc) · 3.02 KB
/
ranked.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
import operator
#
# @author: Ritesh Agrawal
# @Date: 13 Feb 2013
# @Description: This is an implementation of rank biased overlap score
# (Refererence: http://www.umiacs.umd.edu/~wew/papers/wmz10_tois.pdf).
# This is a modified implementation of https://github.com/maslinych/linis-scripts/blob/master/rbo_calc.py
# It is a linear implementation of the RBO and assumes there are no
# duplicates and doesn't handle for ties.
#
def RBO(l1, l2, p=0.9):
"""
Calculates Ranked Biased Overlap (RBO) score.
l1 -- Ranked List 1
l2 -- Ranked List 2
"""
if l1 == None: l1 = []
if l2 == None: l2 = []
sl,ll = sorted([(len(l1), l1),(len(l2),l2)])
s, S = sl
l, L = ll
if s == 0: return 0
# Calculate the overlaps at ranks 1 through l
# (the longer of the two lists)
ss = set([]) # contains elements from the smaller list till depth i
ls = set([]) # contains elements from the longer list till depth i
x_d = {0: 0}
sum1 = 0.0
for i in range(l):
x = L[i]
y = S[i] if i < s else None
d = i + 1
# if two elements are same then
# we don't need to add to either of the set
if x == y:
x_d[d] = x_d[d-1] + 1.0
# else add items to respective list
# and calculate overlap
else:
ls.add(x)
if y != None: ss.add(y)
x_d[d] = x_d[d-1] + (1.0 if x in ss else 0.0) + (1.0 if y in ls else 0.0)
#calculate average overlap
sum1 += x_d[d]/d * pow(p, d)
sum2 = 0.0
for i in range(l-s):
d = s+i+1
sum2 += x_d[d]*(d-s)/(d*s)*pow(p,d)
sum3 = ((x_d[l]-x_d[s])/l+x_d[s]/s)*pow(p,l)
# Equation 32
rbo_ext = (1-p)/p*(sum1+sum2)+sum3
return rbo_ext
def convertToDict(currentUser):
tagN = {}
tagN[1] = currentUser.multipanel
tagN[2] = currentUser.celebrity
tagN[3] = currentUser.singleimage
tagN[4] = currentUser.anime
tagN[5] = currentUser.gaming
tagN[6] = currentUser.politics
tagN[7] = currentUser.wholesome
tagN[8] = currentUser.race
tagN[9] = currentUser.total
return tagN
def getRankedList(tagN):
sortedTag = sorted(tagN.items(), key=operator.itemgetter(1))
rankedTag =[]
for i in sortedTag:
rankedTag.append(i[0])
return rankedTag
def simplified(tagN):
rankedTag = []
sortedTag = sorted(tagN.items(), key=operator.itemgetter(1))
for i in sortedTag:
rankedTag.append(i[0])
return rankedTag
if __name__ == "__main__":
list1 = [0,1,2,3,4,5,6,7,8]
list2 = [1,0,2,3,4,5,6,7,8]
list3 = {1:5, 2:10, 3:15, 4:20, 5:2, 6:3}
print RBO(list1,list2, p = 0.9)
print simplified(list3)
list1 = ['0','1','2','3','4','5','6','7','8']
list2 = ['0','1','2','3','4','5','7','6','8']
print RBO(list1, list2, p = 0.9)
list1 = ['0','1','2','3','4','5']
list2 = ['5','4','3','2','1','0']
print RBO(list1, list2, p = 0.9)