-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommender.py
60 lines (50 loc) · 1.75 KB
/
recommender.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
import json
from data.dataset.testData import test_data
from back.engine.CollaborativeFiltering import *
from back.engine import k, CLUSTER_NAME
from data.scrub.dataAPI import getItemName
from config import test
from runCluster import run_cluster
new_user = {}
data = dataAPI.prepareData(trainingData)
def readCluster():
with open(CLUSTER_NAME, 'rb') as input:
cluster = pickle.load(input)
return cluster
def top5Movies(new_user, k, cluster):
# get the key and ratings of new user
user_key = list(new_user)[0]
user_ratings = new_user[user_key]
n = 5
# find the closest cluster to the user
cluster_no = calculateSimilarities(user_ratings, cluster, k)
user_list = cluster[cluster_no].user_list
dist = {}
for i in range(len(user_list)):
dist[i] = euclideanDistance(user_ratings, data[user_list[i]])
closer_user = sorted(dist, key=dist.get)
recommended_movies = []
for j in range(n):
for m in range(no_of_items):
if m > 20:
rating_of_closer_user = data[closer_user[j]][m]
if int(rating_of_closer_user) == 5:
recommended_movies.append(m + 1)
break
recommended_movies_name = []
for each_movie_id in recommended_movies:
recommended_movies_name.append(getItemName(each_movie_id))
return json.dumps(recommended_movies_name)
def recommend(rate):
try:
cluster = readCluster()
except FileNotFoundError:
run_cluster(k)
cluster = readCluster()
userId = random.randint(101, 999)
new_user[userId] = rate
if test == 1:
recommendation = top5Movies(test_data, k, cluster)
else:
recommendation = top5Movies(new_user, k, cluster)
return recommendation