diff --git a/recommendation_engine.py b/recommendation_engine.py index 17972d6..dfb1f9b 100644 --- a/recommendation_engine.py +++ b/recommendation_engine.py @@ -110,14 +110,17 @@ class Engine: # If the most recent event is "login," initialize grid videos. if user.get('events', [{}])[0].get("event")=="login": + rec = self.get_recommendations(playlists, user) return { 'user': { 'keywords': user.get('keywords', {}) }, - 'videos': self.get_recommendations(playlists, user), + 'videos': rec["videos"], "_debug": { - "top_user_keywords":top_user_keywords, - "top_user_characters":top_user_characters + "top_user_keywords": top_user_keywords, + "top_user_characters": top_user_characters, + "top_user_playlists": rec["top_user_playlists"], + "top_global_playlists": rec["top_global_playlists"] } } @@ -144,14 +147,17 @@ class Engine: # if there were no grid events for all, initialize all grids. if len(prev_grid_list) < video_num: + rec = self.get_recommendations(playlists, user) return { 'user': { 'keywords': user.get('keywords', {}) }, - 'videos': self.get_recommendations(playlists, user), + 'videos': rec["videos"], "_debug": { - "top_user_keywords":top_user_keywords, - "top_user_characters":top_user_characters + "top_user_keywords": top_user_keywords, + "top_user_characters": top_user_characters, + "top_user_playlists": rec["top_user_playlists"], + "top_global_playlists": rec["top_global_playlists"] } } @@ -215,8 +221,8 @@ class Engine: vids_exclude = [e.get("playlist") for e in prev_grid_list] while None in vids_exclude: vids_exclude.remove(None) - video = self.get_recommendations(playlists, user, vids_exclude) - rec_list += [(i, video[i]) for i in next_playlist_index] + rec = self.get_recommendations(playlists, user, vids_exclude) + rec_list += [(i, rec['videos'][i]) for i in next_playlist_index] rec_list = sorted(rec_list, key=lambda k:k[0]) @@ -227,8 +233,10 @@ class Engine: }, 'videos': videos_, "_debug": { - "top_user_keywords":top_user_keywords, - "top_user_characters":top_user_characters + "top_user_keywords": top_user_keywords, + "top_user_characters": top_user_characters, + "top_user_playlists": rec["top_user_playlists"], + "top_global_playlists": rec["top_global_playlists"] } } @@ -269,9 +277,16 @@ class Engine: playlists, key=lambda playlist: -score[playlist['name']] ) + # Record the following for debug view input + top_user_playlists = [{ + 'name': playlist['name'], + 'tags': playlist['tags'] + 'score': score[playlist['name']] + } for playlist in playlists[:channels['userKeywords']]] + videos = playlists[:channels['userKeywords']] playlists = playlists[channels['userKeywords']:] - # For each playlist, compute global keyword score + # For each playlist, compute global keyword score score = {} for playlist in playlists: score[playlist['name']] = random.random() @@ -282,15 +297,26 @@ class Engine: playlists, key=lambda playlist: -score[playlist['name']] ) + # Record the following for debug view input + top_global_playlists = [{ + 'name': playlist['name'], + 'tags': playlist['tags'] + 'score': score[playlist['name']] + } for playlist in playlists[:channels['globalKeywords']]] + videos += playlists[:16 - channels['userKeywords']] # Shuffle playlists (randomize layout) and shift clips (randomize start) random.shuffle(videos) - return [{ - 'clips': video['clips'], - 'position': random.choice([i for i in range(len(video["clips"])) if video["clips"][i].get("pass",False)!=True]), - 'name': video['name'], - 'tags': video['tags'], - } for video in videos] + return { + 'videos': [{ + 'clips': video['clips'], + 'position': random.choice([i for i in range(len(video["clips"])) if not video["clips"][i].get("pass")]), + 'name': video['name'], + 'tags': video['tags'], + } for video in videos], + "top_user_playlists":top_user_playlists, + "top_global_playlists": top_global_playlists + } def update_user_playlists(self, user, watch_cutoff = 0.9): @@ -349,7 +375,7 @@ class Engine: def get_next(self, user, position): - # Update self_playlists first to reflect changes + # Update self_playlists to reflect user log history playlists = self.update_user_playlists(user) grid_events = {} @@ -362,8 +388,8 @@ class Engine: break prev_grid_list = sorted([v for v in grid_events.values()], key=lambda k:k['index']) vids_exclude = [e.get("playlist") for e in prev_grid_list] - video = self.get_recommendations(playlists, user, vids_exclude)[position] - return video + rec = self.get_recommendations(playlists, user, vids_exclude) + return rec["videos"][position] def update_state(self, data): for key in data: