-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecommender.py
187 lines (134 loc) · 5.56 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import time
import difflib
import pandas as pd
from flask_app.model import Movie
from sklearn.feature_extraction.text import CountVectorizer, TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity, linear_kernel
from flask_app.db_dataframe import df_movie, df_music, df_user
# df1 = pd.read_csv('./tmdb.csv')
# df2 = df1['soup']
# df2 = df2.fillna('')
# count = TfidfVectorizer(analyzer='word', ngram_range=(
# 1, 2), min_df=0, stop_words='english')
# count_matrix = count.fit_transform(df2)
# # cosine_sim2 = linear_kernel(count_matrix, count_matrix)
# # cosine_sim2.shape
# df2 = df2.reset_index()
# indices = pd.Series(df1.index, index=df1['title'])
# # all_titles = [df1['title'][i] for i in range(len(df1['title']))]
# def index_from_title(title):
# title_list = df1['title'].tolist()
# common = difflib.get_close_matches(title, title_list, 1)
# titlesim = common[0]
# return titlesim
# def get_recommendations(title):
# title = index_from_title(title)
# cosine_sim = cosine_similarity(count_matrix, count_matrix)
# idx = indices[title]
# sim_scores = list(enumerate(cosine_sim[idx]))
# sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
# sim_scores = sim_scores[:11]
# movie_indices = [i[0] for i in sim_scores]
# tit = df1['title'].iloc[movie_indices]
# dat = df1['release_date'].iloc[movie_indices]
# rating = df1['vote_average'].iloc[movie_indices]
# genre = df1['genres'].iloc[movie_indices]
# return_df = pd.DataFrame(columns=['Title', 'Year', 'rating', 'genre'])
# return_df['Title'] = tit
# return_df['Year'] = dat
# return_df['rating'] = rating
# return_df['genre'] = genre
# return return_df
class Recommender:
def __init__(self, recom_type, slice1=0, slice2=1, tv_data=None):
self.type = recom_type
self.tv_data = tv_data
# cause recommender count from zero unlike db where id start from 1
self.slice1 = slice1 - 1
self.slice2 = slice2 - 1
def dataframe_tv(self):
df = pd.DataFrame({'title': [n.title for n in self.tv_data], 'soup': [
n.soup for n in self.tv_data]})
return df
def create_soup(self, x):
try:
# return x['genres'] + " " + x['artist_name']+" "+x['title']
return x['genres'] + " " + x['artist_name']
except:
print("Error occured creating soup")
def recommender_engine(self):
df1 = None
df2 = None
if self.type == 'movie':
df1 = df_movie
df2 = df1['soup']
elif self.type == 'music':
df1 = df_music
df1 = df1.loc[self.slice1:self.slice2]
df1 = df1.drop('url', axis=1).reset_index(drop=True)
# features = ['genres', 'artist_name', 'title']
features = ['genres', 'artist_name']
for feature in features:
df1[feature] = df1[feature].fillna('')
df1['soup'] = df1.apply(self.create_soup, axis=1)
df2 = df1['soup']
elif self.type == 'tv':
df1 = self.dataframe_tv()
df2 = df1['soup']
df2 = df2.fillna('')
count = TfidfVectorizer(analyzer='word', ngram_range=(
1, 2), min_df=0, stop_words='english')
count_matrix = count.fit_transform(df2)
df2 = df2.reset_index()
indices = pd.Series(df1.index, index=df1['title'])
# print(df1)
return df1, indices, count_matrix
def index_from_title(self, title, df1):
title_list = df1['title'].tolist()
common = difflib.get_close_matches(title, title_list, 1)
titlesim = common[0]
return titlesim
def get_recommendations(self, title):
df1, indices, count_matrix = self.recommender_engine()
title = self.index_from_title(title, df1)
cosine_sim = cosine_similarity(count_matrix, count_matrix)
idx = indices[title]
sim_scores = None
extra = [] # get extra index for multiple indeices index
if self.type == "movie" or self.type == "tv":
sim_scores = list(enumerate(cosine_sim[idx]))
else:
# usually we normally have multiple index so we only take that
# of the first index
try:
sim_scores = list(enumerate(cosine_sim[idx[0]]))
extra = extra + [(n + 1) for n in idx]
except:
sim_scores = list(enumerate(cosine_sim[idx]))
sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
sim_scores = sim_scores[:11]
# special case for music where we have multiple music with the same name
if extra:
sim_scores = sim_scores[1:11]
movie_indices = [i[0] for i in sim_scores]
if self.type == "music":
movie_indices = [i[0] + 1 for i in sim_scores]
if self.type == "movie" or self.type == "tv":
tit = df1['title'].iloc[movie_indices]
return_df = pd.DataFrame(columns=['Title'])
return_df['Title'] = tit
return return_df
if extra:
movie_indices = extra + movie_indices
return movie_indices
# start = time.time()
# recommend = Recommender('music', 1, (1+5000))
# # recommend = Recommender("movie")
# result_final = recommend.get_recommendations('Silent Night')
# # result_final = recommend.get_recommendations('hell boy')
# names = []
# for i in range(len(result_final)):
# names.append(result_final.iloc[i][0])
# names.append(result_final[i])
# end = time.time()
# print(names, "\n", (end - start))