diff --git a/recommendation_engine.py b/recommendation_engine.py index 30cbbb7..d227bfd 100644 --- a/recommendation_engine.py +++ b/recommendation_engine.py @@ -3,6 +3,7 @@ Recommendation Engine Example 1 Nov 2017, 0x2620 ''' +from collections import defaultdict import json import os import random @@ -48,17 +49,18 @@ class Engine: videos = playlists[:channels['keywords']] playlists = playlists[channels['keywords']:] # Count tags for the user - count = {} - for event in user.get('events', []): - if 'product' in event['data']: - count[event['data']['product']] = count.get( - event['data']['product'], 0 - ) + 1 + count = defaultdict(lambda: 0) + for activity in user.get('activities', []): + if activity.get('type') == 'SCREENING_CHECK_IN': + product = activity.get('extras', {}).get('event', {}).get('alias') + if product: + product = product.replace('_', ' ') + count[product] += 1 # For each tag in playlist, increment score by count for playlist in playlists: score[playlist['name']] = random.random() 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[tag] # Select highest scoring playlists videos += sorted( playlists,