update recommendation engine

This commit is contained in:
rlx 2018-01-22 12:55:02 +01:00
parent bc536d93fd
commit cecd4f770f

View file

@ -32,6 +32,21 @@ class Engine:
return clips[index:] + clips[:index - 1] return clips[index:] + clips[:index - 1]
def get_videos(self, user): def get_videos(self, user):
channels = {'keywords': 7, 'screenings': 7, 'random': 2}
sliders = {'dau': -1, 'physics': 0, 'sex': 1}
# For each playlist, compute keyword score
score = {}
for playlist in self.playlists:
score[playlist['name']] = random.random()
for tag in [tag for tag in playlist['tags'] if tag in sliders]:
score[playlist['name']] += sliders[tag]
# Select highest scoring playlists
playlists = sorted(
self.playlists,
key=lambda playlist: -score[playlist['name']]
)
videos = playlists[:channels['keywords']]
playlists = playlists[channels['keywords']:]
# Count tags for the user # Count tags for the user
count = {} count = {}
for event in user['events']: for event in user['events']:
@ -39,23 +54,22 @@ class Engine:
count[event['data']['product']] = count.get( count[event['data']['product']] = count.get(
event['data']['product'], 0 event['data']['product'], 0
) + 1 ) + 1
score = {}
# For each tag in playlist, increment score by count # For each tag in playlist, increment score by count
for playlist in self.playlists: for playlist in playlists:
score[playlist['name']] = random.random() score[playlist['name']] = random.random()
for tag in playlist['tags']: for tag in [tag for tag in playlist['tags'] if tag not in sliders]:
score[playlist['name']] += count.get(tag, 0) score[playlist['name']] += count.get(tag, 0)
# Select 16 highest scoring playlists # Select highest scoring playlists
playlists = sorted( videos += sorted(
self.playlists, playlists,
key=lambda playlist: -score[playlist['name']] key=lambda playlist: -score[playlist['name']]
)[:16] )[:16 - channels['keywords']]
# Shuffle playlists (randomize layout) and shift clips (randomize start) # Shuffle playlists (randomize layout) and shift clips (randomize start)
random.shuffle(playlists) random.shuffle(videos)
return [{ return [{
'clips': self._shift_clips(playlist['clips']), 'clips': self._shift_clips(video['clips']),
'name': playlist['name'] 'name': video['name']
} for playlist in playlists] } for video in videos]
def update(self): def update(self):
# Get all storylines with tags # Get all storylines with tags