update recommendation engine
This commit is contained in:
parent
bc536d93fd
commit
cecd4f770f
1 changed files with 25 additions and 11 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue