-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecom.py
271 lines (217 loc) · 11.2 KB
/
recom.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import requests
import spacy
from rake_nltk import Rake
import random
import nltk
from dotenv import load_dotenv
import os
class MovieRecommendation:
def __init__(self):
load_dotenv()
self.TMDB_API_KEY = os.getenv("TMDB_API_KEY")
self.TMDB_ACCESS_TOKEN = os.getenv("TMDB_ACCESS_TOKEN")
self.positive_keywords = [
"3-D", "absorbing", "acclaimed", "adult", "adventurous", "ambitious", "artistic",
"astonishing", "avant-garde", "award-winning", "awe-inspiring", "based on", "beautiful",
"beautifully filmed", "beautifully shot", "big-budget", "bold", "breathtaking", "brilliant",
"captured", "cerebral", "character-driven", "charismatic", "cinematic", "coherent", "colorful",
"comic", "compelling", "complex", "conceptual", "contemplative", "contemporary", "controversial",
"conversational", "convincing", "creative", "critically acclaimed", "cult", "current", "daring",
"deep", "important", "in-depth", "independent", "infused", "insightful", "inspirational", "inspired",
"intellectual", "intellectually invigorating", "intelligent", "intense", "intensive", "interesting",
"introspective", "intuitive", "inventive", "inventively edited", "ironic", "layered", "legendary",
"light-hearted", "magical", "magnetic", "mature", "meaningful", "memorable", "mind-blowing", "modern",
"moving", "must-see", "mysterious", "mystical", "narrative", "non-stop", "offbeat", "original",
"passionate", "phenomenal", "playful", "plot-driven", "ponderous", "delightful", "dizzying", "dramatic",
"edgy", "effective", "elevating", "eloquent", "emotional", "emotionally charged", "emotionally resonant",
"enchanted", "engaging", "engrossing", "enigmatic", "entertaining", "epic", "evocative", "exceptional",
"exciting", "exquisite", "extraordinary", "family-friendly", "fascinating", "fast-paced", "feel-good",
"filmed", "filmed live", "fluid", "fresh", "fun", "funny", "futuristic", "graceful", "graphic", "gripping",
"highly original", "historical", "honest", "humorous", "imaginative", "immensely talented",
"potent", "powerful", "profound", "provoking", "pure", "quirky", "rated", "realistic", "recommended",
"refined", "refreshing", "relevant", "remarkable", "resourceful", "revealing", "rich", "riveting",
"romantic", "rousing", "sad", "sappy", "satirical", "sentimental", "sexy", "small-budget", "star-studded",
"strong", "stunning", "superb", "suspenseful", "sweet", "theatrical", "thrilling", "touching",
"underground", "unforgettable", "visionary", "visual", "well-paced", "worthwhile"
]
nltk_data_path = os.path.join(os.path.dirname(__file__), 'nltk_data')
nltk.data.path.append(nltk_data_path)
self.ensure_nltk_data()
self.load_spacy_model()
def ensure_nltk_data(self):
try:
nltk.data.find('tokenizers/punkt')
nltk.data.find('corpora/stopwords')
except LookupError:
raise Exception("Required NLTK datasets are missing!")
def load_spacy_model(self):
try:
self.nlp = spacy.load("en_core_web_md", disable=["parser", "ner"])
except OSError:
print("Downloading NLTK data and SpaCy models...")
from spacy.cli import download
download("en_core_web_md")
self.nlp = spacy.load("en_core_web_md", disable=["parser", "ner"])
# Fetch movie reviews from TMDB API
def get_movie_reviews(self,movie_id):
url = f'https://api.themoviedb.org/3/movie/{movie_id}/reviews?&page=1'
headers = {
'Authorization': 'Bearer ' + self.TMDB_ACCESS_TOKEN,
'accept': 'application/json',
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
reviews_data = response.json()
reviews_list = []
for review in reviews_data.get("results", []):
review_info = {
"movie_id": movie_id,
"rating": review["author_details"].get("rating"),
"content": review["content"]
}
reviews_list.append(review_info)
return reviews_list
else:
print("Failed to fetch reviews from TMDB API")
return []
# Fetch reviews for a list of watched movies
def fetch_reviews_for_watched_movies(self,watched_movies):
movie_reviews_dict = {}
for movie_id in watched_movies:
reviews = self.get_movie_reviews(movie_id)
movie_reviews_dict[movie_id] = reviews
return movie_reviews_dict
def extract_keywords_from_all_reviews(self,movie_reviews_dict):
self.ensure_nltk_data()
movie_keywords_dict = {}
for movie_id, reviews in movie_reviews_dict.items():
rake = Rake()
all_phrases_with_scores = []
for review in reviews:
rake.extract_keywords_from_text(review['content'])
phrases_with_scores = rake.get_ranked_phrases_with_scores()
all_phrases_with_scores.extend(phrases_with_scores)
# Sort phrases by score in descending order and keep unique
sorted_phrases_with_scores = sorted(all_phrases_with_scores, key=lambda x: x[0], reverse=True)
unique_keywords_with_scores = []
seen_keywords = set()
for score, phrase in sorted_phrases_with_scores:
if phrase not in seen_keywords:
unique_keywords_with_scores.append((score, phrase))
seen_keywords.add(phrase)
if len(unique_keywords_with_scores) == 100:
break
movie_keywords_dict[movie_id] = unique_keywords_with_scores
return movie_keywords_dict
def filter_keywords(self,text, positive_keywords, nlp):
doc = nlp(text)
matched_keywords = []
keywords_tokens = [nlp(keyword)[0] for keyword in positive_keywords]
for token in doc:
if token.pos_ == 'ADJ':
for keyword_token in keywords_tokens:
if token.has_vector and keyword_token.has_vector:
if token.text.lower() == keyword_token.text.lower() or token.similarity(keyword_token) > 0.8:
matched_keywords.append(token.text.lower())
return list(set(matched_keywords))
def list_to_pipe_string(self,lst):
return "|".join(map(str, lst))
def process_reviews(self,movie_reviews_dict, positive_keywords, nlp):
keywords = self.extract_keywords_from_all_reviews(movie_reviews_dict)
all_phrases = []
for keywords_with_scores in keywords.values():
for _, phrase in keywords_with_scores:
all_phrases.append(phrase) # Collecting phrases only
text = '. '.join(all_phrases)
final_output = self.filter_keywords(text, positive_keywords, nlp)
return final_output
def fetch_reviews_for_genre_movies(self,genre_ids):
movies = self.discover_movies_by_genre(genre_ids)
movie_reviews_dict = {}
# Iterate over each movie to fetch its reviews
for movie in movies:
movie_id = movie['id']
reviews = self.fetch_movie_reviews(movie_id)
movie_reviews_dict[movie_id] = reviews
return movie_reviews_dict
def discover_movies_by_genre(self,genre_ids):
all_filtered_results = []
total_pages = 30
range_size = 10
start_page = random.randint(1, total_pages - range_size + 1)
end_page = start_page + range_size
url = f'https://api.themoviedb.org/3/discover/movie?with_genres={"|".join(genre_ids)}&page={1}'
headers = {
'Authorization': 'Bearer ' + self.TMDB_ACCESS_TOKEN,
'accept': 'application/json',
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
results = response.json()['results']
filtered_results = [
{
'id': movie['id'],
'original_title': movie['original_title'],
}
for movie in results
]
all_filtered_results.extend(filtered_results)
else:
print(f"Failed to fetch movies from TMDB API for page {page}")
for page in range(start_page, end_page):
url = f'https://api.themoviedb.org/3/discover/movie?with_genres={"|".join(genre_ids)}&page={page}'
headers = {
'Authorization': 'Bearer ' + self.TMDB_ACCESS_TOKEN,
'accept': 'application/json',
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
results = response.json()['results']
filtered_results = [
{
'id': movie['id'],
'original_title': movie['original_title'],
}
for movie in results
]
all_filtered_results.extend(filtered_results)
else:
print(f"Failed to fetch movies from TMDB API for page {page}")
break
print(f"Total movies fetched: {len(all_filtered_results)}")
return all_filtered_results
# i have another funtion
def fetch_movie_reviews(self,movie_id):
reviews_url = f'https://api.themoviedb.org/3/movie/{movie_id}/reviews?language=en-US&page=1'
headers = {
'Authorization': 'Bearer '+self.TMDB_ACCESS_TOKEN,
'accept': 'application/json',
}
response = requests.get(reviews_url,headers=headers)
if response.status_code == 200:
reviews_data = response.json()
reviews_list = []
for review in reviews_data.get("results", []):
review_info = {
"movie_id": movie_id,
"rating": review["author_details"].get("rating"),
"content": review["content"]
}
reviews_list.append(review_info)
return reviews_list
return []
def get_recommended_movies(self, movie_keywords_dict, user_keywords, nlp):
recommended_movies = []
count = 0
for movie_id, keywords_with_scores in movie_keywords_dict.items():
keywords_text = '. '.join([phrase for score, phrase in keywords_with_scores])
matched_keywords = self.filter_keywords(keywords_text, user_keywords, nlp)
count += 1
if len(matched_keywords) >= 5:
recommended_movies.append(movie_id)
if len(recommended_movies) == 20:
print(f"Total filtered movies: {count}")
print("Total recommended movies: 20")
return recommended_movies
print(f"Total recommended movies: {len(recommended_movies)}")
return recommended_movies