-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
79 lines (65 loc) · 2.61 KB
/
app.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
app = Flask(__name__)
movies = pd.read_csv('tmdb_5000_movies.csv')
credits = pd.read_csv('tmdb_5000_credits.csv')
movies = movies.merge(credits, on='title')
movies = movies[['movie_id', 'title', 'overview', 'genres', 'keywords', 'cast', 'crew']]
movies.dropna(inplace=True)
def dlist(x):
list_ = []
try:
for i in ast.literal_eval(x):
list_.append(i['name'])
except (ValueError, SyntaxError):
return []
return list_
movies['genres'] = movies['genres'].apply(dlist)
movies['keywords'] = movies['keywords'].apply(dlist)
movies['cast'] = movies['cast'].apply(dlist)
movies['crew'] = movies['crew'].apply(dlist)
def get_director(x):
try:
for i in ast.literal_eval(x):
if i['job'] == 'Director':
return i['name']
except (ValueError, SyntaxError):
return ''
return ''
movies['director'] = movies['crew'].apply(get_director)
movies['overview'] = movies['overview'].apply(lambda x: x.split())
movies['genres'] = movies['genres'].apply(lambda x: [i.replace(" ", "") for i in x])
movies['keywords'] = movies['keywords'].apply(lambda x: [i.replace(" ", "") for i in x])
movies['cast'] = movies['cast'].apply(lambda x: [i.replace(" ", "") for i in x])
movies['director'] = movies['director'].apply(lambda x: x.replace(" ", "") if isinstance(x, str) else '')
movies['tags'] = movies['overview'] + movies['genres'] + movies['keywords'] + movies['cast'] + movies['director'].apply(lambda x: [x])
movies['tags'] = movies['tags'].apply(lambda x: " ".join(x))
ps = PorterStemmer()
def stem(text):
y = []
for i in text.split():
y.append(ps.stem(i))
return " ".join(y)
movies['tags'] = movies['tags'].apply(stem)
df = movies[['movie_id', 'title', 'tags']]
tfidf = TfidfVectorizer(max_features=5000, stop_words='english')
vectors = tfidf.fit_transform(df['tags']).toarray()
similarity = cosine_similarity(vectors)
def recommend(movie):
if movie not in df['title'].values:
return []
movie_index = df[df['title'] == movie].index[0]
distances = similarity[movie_index]
movie_list = sorted(list(enumerate(distances)), reverse=True, key=lambda x: x[1])[1:6]
recommendations = []
for i in movie_list:
recommendations.append(df.iloc[i[0]].title)
return recommendations
@app.route('/')
def home():
return render_template('index.html')
@app.route('/recommend', methods=['POST'])
def get_recommendations():
movie = request.form['movie']
recommendations = recommend(movie)
return render_template('index.html', movie=movie, recommendations=recommendations)
if __name__ == '__main__':
app.run(debug=True)