adding user keywords...

This commit is contained in:
rlx 2018-02-15 17:06:18 +01:00
parent 339ebe754f
commit 29199828ac

View file

@ -41,9 +41,9 @@ class Engine:
else:
self.state = {
'channels': {
'keywords': {'locked': False, 'value': 7},
'screenings': {'locked': False, 'value': 7},
'random': {'locked': True, 'value': 2}
'userKeywords': {'locked': False, 'value': 7},
'globalKeywords': {'locked': False, 'value': 7},
'screenings': {'locked': True, 'value': 2}
},
'keywords': {},
}
@ -79,8 +79,22 @@ class Engine:
def get_videos(self, user):
channels = {k: v.get('value', 0) for k, v in self.state['channels'].items()}
sliders = {k: v.get('value', 0) for k, v in self.state['keywords'].items()}
# For each playlist, compute keyword score
sliders = {k: v.get('value', 0) for k, v in self.state['globalKeywords'].items()}
# For each playlist, compute user keyword score
user_keywords = user.get('keywords', {})
score = {}
for playlist in self.playlists:
score[playlist['name']] = random.random()
for tag in [tag for tag in playlist['tags'] if tag in user_keywords]:
score[playlist['name']] += user_keywords[tag]
# Select highest scoring playlists
playlists = sorted(
self.playlists,
key=lambda playlist: -score[playlist['name']]
)
videos = playlists[:channels['userKeywords']]
playlists = playlists[channels['userKeywords']:]
# For each playlist, compute global keyword score
score = {}
for playlist in self.playlists:
score[playlist['name']] = random.random()
@ -91,8 +105,8 @@ class Engine:
self.playlists,
key=lambda playlist: -score[playlist['name']]
)
videos = playlists[:channels['keywords']]
playlists = playlists[channels['keywords']:]
videos = playlists[:channels['globalKeywords']]
playlists = playlists[channels['globalKeywords']:]
# Count tags for the user
count = defaultdict(lambda: 0)
for event in user.get('events', []):
@ -107,7 +121,7 @@ class Engine:
videos += sorted(
playlists,
key=lambda playlist: -score[playlist['name']]
)[:16 - channels['keywords']]
)[:16 - channels['userKeywords'] - channels['globalKeywords']]
# Shuffle playlists (randomize layout) and shift clips (randomize start)
random.shuffle(videos)
return [{
@ -193,13 +207,13 @@ class Engine:
def update_keywords(self):
changed = False
if 'keywords' not in self.state:
self.state['keywords'] = {}
if 'globalKeywords' not in self.state:
self.state['globalKeywords'] = {}
changed = True
for playlist in self.playlists:
for tag in playlist.get('tags', []):
if not tag.isupper() and tag not in self.state['keywords']:
self.state['keywords'][tag] = {'value': 0}
if not tag.isupper() and tag not in self.state['globalKeywords']:
self.state['globalKeywords'][tag] = {'value': 0}
changed = True
if changed:
self.save_state()