-
Notifications
You must be signed in to change notification settings - Fork 0
/
RECS.py
57 lines (53 loc) · 2.16 KB
/
RECS.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
import pickle
import operator
def recbook(course):
print course+' '+coursenames[course]
if len(sims[course])<1:
return "Nothing yet"
simlist = sims[course]
totals={}
counts={}
recs = {}
for key in simlist.keys():
for isbn in ratings[key].keys():
if isbn in ratings[course]:
#print 'already read this book '+isbn+' '+booktitles[isbn]
continue
rating = str(ratings[key][isbn])
factor = sims[course][key]
department=0
weighted = (int(rating)*factor)+department
#already saw this book in another record
if isbn in counts:
#print key+' isbn '+isbn
count = counts[isbn]
#print count
count=count+1
counts[isbn]=count
existing = totals[isbn]
new = existing+weighted
#print 'existing '+str(existing)
#print 'new '+str(new)
totals[isbn]=new
#havent seen this yet
else:
totals[isbn]=weighted
counts[isbn]=1
for key in totals.keys():
recs[key]=totals[key]/counts[key]
max = 0
maxisbn = 0
if len(recs) >0:
sorted_x = sorted(recs.iteritems(), key=operator.itemgetter(1))
sorted_x.reverse()
print str(sorted_x[0][0])+" "+str(sorted_x[1][0])+" "+str(sorted_x[2][0])+" "+str(sorted_x[3][0])+" "+str(sorted_x[4][0])
print '\t'+booktitles[sorted_x[0][0]]+' '+sorted_x[0][0]+' '+str(sorted_x[0][1])
print '\t'+booktitles[sorted_x[1][0]]+' '+sorted_x[1][0]+' '+str(sorted_x[1][1])
print '\t'+booktitles[sorted_x[2][0]]+' '+sorted_x[2][0]+' '+str(sorted_x[2][1])
print '\t'+booktitles[sorted_x[3][0]]+' '+sorted_x[3][0]+' '+str(sorted_x[3][1])
print '\t'+booktitles[sorted_x[4][0]]+' '+sorted_x[4][0]+' '+str(sorted_x[4][1])
ratings = pickle.load( open( "newRatings.p", "rb" ) )
sims = pickle.load( open( "newSims.p", "rb" ) )
coursenames = pickle.load( open( "coursenames.p", "rb" ) )
booktitles = pickle.load( open( "newBookTitles.p", "rb" ) )
recbook("NE200")