diff --git a/recommendation_engine.py b/recommendation_engine.py index 3082c4a..ece3b06 100644 --- a/recommendation_engine.py +++ b/recommendation_engine.py @@ -58,10 +58,6 @@ class Engine: self.state['userKeywordsWeights'] = { 'themeTags': {'locked': False, 'value': 0.3}, 'characterTags': {'locked': False, 'value': 0.7} -<<<<<<< HEAD -======= - ->>>>>>> iss2 } self.update_keywords() @@ -96,14 +92,14 @@ class Engine: def get_videos(self, user): # Update self_playlists first to reflect changes - update_user_playlists(self.playlists, user) + playlists = self.update_user_playlists(user) if user.get('events', [{}])[0].get("event")=="login": return { 'user': { 'keywords': user.get('keywords', {}) }, - 'videos': self.get_recommendations(user) + 'videos': self.get_recommendations(playlists, user) } channels = {k: v.get('value', 0) for k, v in self.state['channels'].items()} @@ -128,16 +124,12 @@ class Engine: prev_grid_list = sorted([v for v in grid_events.values()], key=lambda k:k['index']) # if there were no grid events for all, initialize all grids. -<<<<<<< HEAD - if len(prev_grid_list) < video_num: -======= if len(prev_grid_list) < video_num: ->>>>>>> iss2 return { 'user': { 'keywords': user.get('keywords', {}) }, - 'videos': self.get_recommendations(user) + 'videos': self.get_recommendations(playlists, user) } else: @@ -163,7 +155,7 @@ class Engine: next_playlist_index.append(next_clip_index) break else: - for playlist in self.playlists: + for playlist in playlists: if playlist.get('name')== prev_grid_list[i].get('playlist'): if len(playlist["clips"]) == 1: next_playlist_index.append(i) @@ -187,7 +179,7 @@ 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(user, vids_exclude) + video = self.get_recommendations(playlists, user, vids_exclude) rec_list += [(i, video[i]) for i in next_playlist_index] #staySame pool @@ -201,20 +193,15 @@ class Engine: }, 'videos': videos_ } -<<<<<<< HEAD - -======= ->>>>>>> iss2 - def get_recommendations(self, user, vids_exclude = []): + def get_recommendations(self, playlists, user, vids_exclude = []): channels = {k: v.get('value', 0) for k, v in self.state['channels'].items()} sliders = {k: v.get('value', 0) for k, v in self.state['globalKeywords'].items()} gridChange = {k: v.get('value', 0) for k, v in self.state['gridChange'].items()} userKeywordsWeights = {k: v.get('value', 1) for k, v in self.state['userKeywordsWeights'].items()} # Exclude playlists from the most recent grid - playlists = copy.deepcopy(self.playlists) if len(vids_exclude) > 0: for playlist in playlists: if playlist["name"] in vids_exclude: @@ -231,11 +218,6 @@ class Engine: character_tags["FEDOR MIKHAILOVICH SOFRONOV"] = character_tags.get("FYODOR MIKHAILOVICH SOFRONOV",0) character_tags["SHKABARNYA OLGA SERGEEVNA"] = character_tags.get("OLGA SERGEEVNA SHKABARNYA",0) character_tags["VICTORIA OLEGOVNA SKITSKAYA"] = character_tags.get("VIKTORIA OLEGOVNA SKITSKAYA",0) -<<<<<<< HEAD - -======= - ->>>>>>> iss2 score = {} for playlist in playlists: score[playlist['name']] = random.random() * 0.001 @@ -273,7 +255,7 @@ class Engine: } for video in videos] - def update_user_playlists(playlists, user, watch_cutoff = 0.9): + def update_user_playlists(user, watch_cutoff = 0.9): # Output: playlists with updated in/out time of clips that have been watched. # Watched is defined as a video being played in full screen. # "watch_cutoff" parameter: the portion of the clip duration to be determined as watched the whole clip. should be [0,1] @@ -281,6 +263,7 @@ class Engine: # + If (play, pause) pairs exceed XX(80-90?) percent of the clip length, eliminate the clip from the playlist. # + Otherwise, find the last pause position of a clip and record it as "in" position of the clip. # + If the clips are all eliminated from a playlist, eliminate the playlist. + playlists = copy.deepcopy(self.playlists) play = {} watched = [] clip_max_dur = 10800 # = 3 hours; arbitrary max duration allowed for (pause time - play time) to detect outlier/bugs @@ -333,7 +316,7 @@ class Engine: def get_next(self, user, position): # Update self_playlists first to reflect changes - update_user_playlists(self.playlists, user) + playlists = self.update_user_playlists(user) grid_events = {} video_num = 16 @@ -345,7 +328,7 @@ 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(user, vids_exclude)[position] + video = self.get_recommendations(playlists, user, vids_exclude)[position] return video def update_state(self, data):