From cecd4f770f93687b47bc08436d3c139390cfed95 Mon Sep 17 00:00:00 2001 From: rlx Date: Mon, 22 Jan 2018 12:55:02 +0100 Subject: [PATCH] update recommendation engine --- recommendation_engine.py | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/recommendation_engine.py b/recommendation_engine.py index 7b68dcf..7482277 100644 --- a/recommendation_engine.py +++ b/recommendation_engine.py @@ -32,6 +32,21 @@ class Engine: return clips[index:] + clips[:index - 1] 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 = {} for event in user['events']: @@ -39,23 +54,22 @@ class Engine: count[event['data']['product']] = count.get( event['data']['product'], 0 ) + 1 - score = {} # For each tag in playlist, increment score by count - for playlist in self.playlists: + for playlist in playlists: 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) - # Select 16 highest scoring playlists - playlists = sorted( - self.playlists, + # Select highest scoring playlists + videos += sorted( + playlists, key=lambda playlist: -score[playlist['name']] - )[:16] + )[:16 - channels['keywords']] # Shuffle playlists (randomize layout) and shift clips (randomize start) - random.shuffle(playlists) + random.shuffle(videos) return [{ - 'clips': self._shift_clips(playlist['clips']), - 'name': playlist['name'] - } for playlist in playlists] + 'clips': self._shift_clips(video['clips']), + 'name': video['name'] + } for video in videos] def update(self): # Get all storylines with tags