-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.py
53 lines (39 loc) · 1.26 KB
/
script.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 json
import sys
import os
import pickle
import numpy as np
def recommend(movie):
try:
index = movies[movies['title'] == movie].index[0]
# print(movies)
distances = sorted(list(enumerate(similarity[index])), reverse=True, key=lambda x: x[1])
recommended_movie_id = []
for i in distances[0:20]:
# fetch the movie poster
recommended_movie_id.append(movies.iloc[i[0]].movie_id)
# recommended_movie_names.append(movies.iloc[i[0]].title)
return recommended_movie_id
except :
print("Movie not found")
# print("I'm running here 1 :/")
# sys.stdout.flush()
movies = pickle.load(open('movie_list.pkl','rb'))
similarity = pickle.load(open('similarity.pkl','rb'))
data = recommend(sys.argv[1])
obj= {
1:data
}
# print(data)
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return super(NpEncoder, self).default(obj)
path = os.path.abspath("static")
with open(path+"/xyz.json", "w") as fp:
json_string = json.dump(obj,fp,indent=4,cls=NpEncoder)