-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaserecommender.py
53 lines (49 loc) · 2.04 KB
/
baserecommender.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
import os
import itembasedsimilarity
class BaseRecommender:
def __init__(self, output_file, similarity_measure, path, training_set, predicting_set):
self.output_file = os.getcwd()+'//results/' + output_file
self.path = path
self.training_set = training_set
self.predicting_set = predicting_set
self.likes = {}
self.predictLikes = []
self.movieTag = {}
self.similarity_measure = similarity_measure
def loadTraining_set(self):
likes = {}
try:
with open(self.path + self.training_set) as train:
for line in train:
(userId, movieId, rating, time) = line.split('\t')
likes.setdefault(userId, {})
likes[userId][movieId] = float(rating)
except IOError as err:
print('File error: ' + str(err))
self.likes = likes
def loadPredicting_set(self):
likes = []
try:
with open(self.path + self.predicting_set) as predict:
for line in predict:
(userId, movieId, rating, time) = line.split('\t')
movieId = movieId.replace('\r\r\n', '')
likes.append((userId, movieId))
except IOError as err:
print('File error: ' + str(err))
self.predictLikes = likes
def transformLikes(self, likes):
result = {}
for person in likes:
for item in likes[person]:
result.setdefault(item, {})
result[item][person] = likes[person][item]
return result
def topMatches(self, likes, item, similarity_measure, n):
if similarity_measure == itembasedsimilarity.predict_cosine_improved_tag:
scores = [(similarity_measure(likes, item, other, self.movieTag), other) for other in likes if other != item]
else:
scores = [(similarity_measure(likes, item, other), other) for other in likes if other != item]
scores.sort()
scores.reverse()
return scores[0:n]