-
Notifications
You must be signed in to change notification settings - Fork 2
/
superuser2.py
126 lines (113 loc) · 3.61 KB
/
superuser2.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
import json, re
import csv, sys
import pickle
import cPickle as pickle
file = open("reviews.txt",'r')
group = []
###read in command line arguments
#file = open(str(sys.argv[1]), 'r')
svf = sys.argv[1]
nrm = sys.argv[2]
###keep track of number of group members for normalization
nUsers = len(sys.argv[3:])
for user in sys.argv[3:]:
group.append(str(user))
#outFile1 = open("pseudoAvg.txt", 'w')
#print 'Your Group is ' + str(group)
newUser = []
# remove all group members from group, print the rest of the lines
for review in file:
review_chars = review.split()
if review_chars[2] in group:
newUser.append({
"val": float(review_chars[0]),
"uid": str(review_chars[2]),
"bid": str(review_chars[4])})
else:
#i = 1
jfsdf = 1
print review[:-1]
bratings = {}
userAverages = {}
###populate dict (business, val/uid/bid) and calculate averages
for rating in newUser:
keyB = rating['bid'] # keyB is business id
keyU = rating['uid'] # keyU is user id
if keyB not in bratings.keys():
bratings[keyB] = []
bratings[keyB].append(rating)
if keyU not in userAverages.keys():
userAverages[keyU]=[]
userAverages[keyU].append({'count': int(0), 'average': float(0)})
count = userAverages[keyU][0]['count']
averg = userAverages[keyU][0]['average']
userAverages[keyU][0]['count'] += 1
userAverages[keyU][0]['average'] = (count * averg + rating['val'])/(count + 1)
###if normalized option selected, scale the ratings
if nrm=='norm':
groupAverage = 0.0
for key in userAverages.keys():
groupAverage += userAverages[key][0]['average']
groupAverage /= len(userAverages.keys())
for key in bratings.keys():
for i in range(0, len(bratings[key]) ):
bratings[key][i]['val'] *= groupAverage / userAverages[bratings[key][i]['uid']][0]['average']
#average the ratings for each business and print
def svf_avg(bratings):
for key in bratings.keys():
ratings = bratings[key]
sum = 0.0
count = 0.0
for rating in ratings:
sum += rating['val']
count += 1
avg = str(sum/count)
print str(avg) + ' |u SUPERUSER |i ' + str(key)
#least misery
def svf_lm(bratings):
for key in bratings.keys():
ratings = bratings[key]
m = ratings[0]['val']
for rating in ratings:
if (rating['val'] < m):
m = rating['val']
print str(m) + ' |u SUPERUSER |i ' + str(key)
#most happiness
def svf_mh(bratings):
for key in bratings.keys():
ratings = bratings[key]
max = ratings[0]['val']
for rating in ratings:
if (rating['val'] > max):
max = rating['val']
print str(max) + ' |u SUPERUSER |i ' + str(key)
def svf_expert(bratings,newUser):
dict = {}
count = 0.0
for review in newUser:
if review['uid'] not in dict:
dict[review['uid']] = 1
else:
dict[review['uid']] += 1
count += 1
for user in dict.keys():
dict[user] /= count
#print dict
for key in bratings.keys():
ratings = bratings[key]
sum = 0.0
inc = 0.0
for rating in ratings:
sum += dict[rating['uid']]*rating['val']
inc += dict[rating['uid']]
avg = sum/inc
print str(avg) + ' |u SUPERUSER ' + str(key)
#choose specified social value function to apply
if svf=='avg':
svf_avg(bratings)
elif svf=='lm':
svf_lm(bratings)
elif svf=='mh':
svf_mh(bratings)
elif svf=='expert':
svf_expert(bratings, newUser)