diff --git a/recommendation_engine.py b/recommendation_engine.py index 59a26cd..110e524 100644 --- a/recommendation_engine.py +++ b/recommendation_engine.py @@ -119,11 +119,15 @@ class Engine: return self.get_recommendations(user) else: + if play_index is None: + video_indx = list(range(video_num)) + random.shuffle(video_indx) + else: # played index is excluded from the random shuffle and deterministically added to staySame pool. - video_indx = [*range(play_index)]+[*range(play_index+1,video_num)] - # video_indx = list(range(video_num)) - random.shuffle(video_indx) - video_indx.append(play_index) + video_indx = [*range(play_index)]+[*range(play_index+1,video_num)] + random.shuffle(video_indx) + video_indx.append(play_index) + next_clip_index = video_indx[:nc] next_playlist_index = video_indx[nc:nc+np] stay_same_index = video_indx[nc+np:] @@ -131,28 +135,37 @@ class Engine: rec_list = [] # select next clip for nextClip pool except when the playlist has only one clip. for i in next_clip_index: - for playlist in self.playlists: - if playlist.get('name')== prev_grid_list[i].get('playlist'): - if len(playlist["clips"]) == 1: - next_playlist_index.append(i) - break - # Discuss how this behavour should be: should it switch to a new playlist if it is the end of the playlist clip sequence already? - elif prev_grid_list[i].get('playlistPosition', 0) + 1 == len(playlist['clips']): - playlist_pos = 0 - else: - playlist_pos = prev_grid_list[i].get('playlistPosition', 0) + 1 - - rec_list.append((i, { - 'clips': playlist['clips'], - # 'position': random.randrange(len(playlist['clips'])), - 'position': playlist_pos, - 'name': playlist['name'], - 'tags': playlist['tags'], - })) + if prev_grid_list[i].get('playlist') is None: + # add this to deal with the absence of "playlist" data in old grid event. + # If there's no playlist data recorded, add the nextClip pool to nextPlaylist pool. + next_playlist_index.append(next_clip_index) + break + else: + # if "playlist" and "playlistPostion" (if not, default to 0) exists in grid event + for playlist in self.playlists: + if playlist.get('name')== prev_grid_list[i].get('playlist'): + if len(playlist["clips"]) == 1: + next_playlist_index.append(i) + break + # Discuss how this behavour should be: should it switch to a new playlist if it is the end of the playlist clip sequence already? + elif prev_grid_list[i].get('playlistPosition', 0) + 1 == len(playlist['clips']): + playlist_pos = 0 + else: + playlist_pos = prev_grid_list[i].get('playlistPosition', 0) + 1 + + rec_list.append((i, { + 'clips': playlist['clips'], + # 'position': random.randrange(len(playlist['clips'])), + 'position': playlist_pos, + 'name': playlist['name'], + 'tags': playlist['tags'], + })) # randomly select playlists (excluding the playlists from the current grid once "playlist" is recorded for grid events) # for nextPlaylist pool. 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) rec_list += [(i, video[i]) for i in next_playlist_index]