modified for new user data schema and one clip playlist cases
This commit is contained in:
parent
71a9effbba
commit
b7eeb0bd95
1 changed files with 43 additions and 48 deletions
|
@ -96,22 +96,22 @@ class Engine:
|
||||||
# Make sure sanity check exists in front-end (error if it does not add up to 16).
|
# Make sure sanity check exists in front-end (error if it does not add up to 16).
|
||||||
video_num = nc + np + ns
|
video_num = nc + np + ns
|
||||||
|
|
||||||
for event in user.get('events', []):
|
|
||||||
if event.get('event') == "grid" and event.get('data').get('index') not in grid_events:
|
|
||||||
grid_events[event.get('data').get('index')] = event.get('data')
|
|
||||||
if len(grid_events) == video_num:
|
|
||||||
break
|
|
||||||
|
|
||||||
# # The version where the loop also extract play_index (requires "index" in play event data):
|
|
||||||
# play_index = None
|
|
||||||
# for event in user.get('events', []):
|
# for event in user.get('events', []):
|
||||||
# if event.get('event') == "grid" and event.get('data').get('index') not in grid_events:
|
# if event.get('event') == "grid" and event.get('data').get('index') not in grid_events:
|
||||||
# grid_events[event.get('data').get('index')] = event.get('data')
|
# grid_events[event.get('data').get('index')] = event.get('data')
|
||||||
# if event.get('event') == "play" and not play_index:
|
# if len(grid_events) == video_num:
|
||||||
# play_index = event.get('data').get('index')
|
|
||||||
# if len(grid_events) == video_num and play_index:
|
|
||||||
# break
|
# break
|
||||||
|
|
||||||
|
# # The version where the loop also extract play_index (requires "index" in play event data):
|
||||||
|
play_index = None
|
||||||
|
for event in user.get('events', []):
|
||||||
|
if event.get('event') == "grid" and event.get('data').get('index') not in grid_events:
|
||||||
|
grid_events[event.get('data').get('index')] = event.get('data')
|
||||||
|
if event.get('event') == "play" and not play_index:
|
||||||
|
play_index = event.get('data').get('index')
|
||||||
|
if len(grid_events) == video_num and play_index:
|
||||||
|
break
|
||||||
|
|
||||||
prev_grid_list = sorted([v for v in grid_events.values()], key=lambda k:k['index'])
|
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.
|
# if there were no grid events for all, initialize all grids.
|
||||||
|
@ -119,50 +119,45 @@ class Engine:
|
||||||
return self.get_recommendations(user)
|
return self.get_recommendations(user)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Once the "index" is added to play event data, played index will be excluded from the random shuffle
|
# played index is excluded from the random shuffle and deterministically added to staySame pool.
|
||||||
# and deterministically added to staySame pool.
|
video_indx = [*range(play_index)]+[*range(play_index+1,video_num)]
|
||||||
|
# video_indx = list(range(video_num))
|
||||||
# video_indx = [*range(playIndex)]+[*range(playIndex+1,video_num)]
|
|
||||||
video_indx = list(range(video_num))
|
|
||||||
random.shuffle(video_indx)
|
random.shuffle(video_indx)
|
||||||
rec_list = []
|
video_indx.append(play_index)
|
||||||
# For now, randomly choose a clip in the same playlist instead of switching to the next clip.
|
next_clip_index = video_indx[:nc]
|
||||||
# This will be changed once the clip position is tracked in the user data as "playlistPostion"
|
next_playlist_index = video_indx[nc:nc+np]
|
||||||
for i in video_indx[:nc]:
|
stay_same_index = video_indx[nc+np:]
|
||||||
if prev_grid_list[i].get('playlist') is None:
|
|
||||||
# add this to deal with the absence of "playlist" data in a grid event temporarily.
|
|
||||||
# If there's no playlist data recorded, add the nextClip pool to nextPlaylist pool for now.
|
|
||||||
np += nc
|
|
||||||
nc = 0
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
# if "playlist" and "playlistPostion" data exists in grid event (it should be in the future)
|
|
||||||
for playlist in playlists:
|
|
||||||
if playlist.get('name')== prev_grid_list[i].get('playlist'):
|
|
||||||
# Discuss how this behavour should be: should it switch to a new playlist if it is the end of the playlist clip sequence already?
|
|
||||||
# if prev_grid_list[i].get('index') == len(playlist['clips']):
|
|
||||||
# playlist_pos = 0
|
|
||||||
# else:
|
|
||||||
# playlist_pos = prev_grid_list[i].get('index') + 1
|
|
||||||
|
|
||||||
rec_list.append((i, {
|
rec_list = []
|
||||||
'clips': playlist['clips'],
|
# select next clip for nextClip pool except when the playlist has only one clip.
|
||||||
'position': random.randrange(len(playlist['clips'])),
|
for i in next_clip_index:
|
||||||
# 'position': playlist_pos,
|
for playlist in self.playlists:
|
||||||
'name': playlist['name'],
|
if playlist.get('name')== prev_grid_list[i].get('playlist'):
|
||||||
'tags': playlist['tags'],
|
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') + 1 == len(playlist['clips']):
|
||||||
|
playlist_pos = 0
|
||||||
|
else:
|
||||||
|
playlist_pos = prev_grid_list[i].get('playlistPosition') + 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)
|
# randomly select playlists (excluding the playlists from the current grid once "playlist" is recorded for grid events)
|
||||||
# for nextPlaylist pool.
|
# for nextPlaylist pool.
|
||||||
vids_exclude = []
|
vids_exclude = [e.get("playlist") for e in prev_grid_list]
|
||||||
# vids_exclude = [e.get("playlist") for e in prev_grid_list]
|
|
||||||
video = self.get_recommendations(user, vids_exclude)
|
video = self.get_recommendations(user, vids_exclude)
|
||||||
rec_list += [(i, video[i]) for i in video_indx[nc:nc+np]]
|
rec_list += [(i, video[i]) for i in next_playlist_index]
|
||||||
|
|
||||||
#staySame pool
|
#staySame pool
|
||||||
# video_indx.append(playIndex)
|
rec_list += [(i,{}) for i in stay_same_index]
|
||||||
rec_list += [(i,{}) for i in video_indx[nc+np:]]
|
|
||||||
|
|
||||||
rec_list = sorted(rec_list, key=lambda k:k[0])
|
rec_list = sorted(rec_list, key=lambda k:k[0])
|
||||||
return [e[1] for e in rec_list]
|
return [e[1] for e in rec_list]
|
||||||
|
|
Loading…
Reference in a new issue