adding user keywords...
This commit is contained in:
parent
339ebe754f
commit
29199828ac
1 changed files with 26 additions and 12 deletions
|
@ -41,9 +41,9 @@ class Engine:
|
||||||
else:
|
else:
|
||||||
self.state = {
|
self.state = {
|
||||||
'channels': {
|
'channels': {
|
||||||
'keywords': {'locked': False, 'value': 7},
|
'userKeywords': {'locked': False, 'value': 7},
|
||||||
'screenings': {'locked': False, 'value': 7},
|
'globalKeywords': {'locked': False, 'value': 7},
|
||||||
'random': {'locked': True, 'value': 2}
|
'screenings': {'locked': True, 'value': 2}
|
||||||
},
|
},
|
||||||
'keywords': {},
|
'keywords': {},
|
||||||
}
|
}
|
||||||
|
@ -79,8 +79,22 @@ class Engine:
|
||||||
|
|
||||||
def get_videos(self, user):
|
def get_videos(self, user):
|
||||||
channels = {k: v.get('value', 0) for k, v in self.state['channels'].items()}
|
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()}
|
sliders = {k: v.get('value', 0) for k, v in self.state['globalKeywords'].items()}
|
||||||
# For each playlist, compute keyword score
|
# 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 = {}
|
score = {}
|
||||||
for playlist in self.playlists:
|
for playlist in self.playlists:
|
||||||
score[playlist['name']] = random.random()
|
score[playlist['name']] = random.random()
|
||||||
|
@ -91,8 +105,8 @@ class Engine:
|
||||||
self.playlists,
|
self.playlists,
|
||||||
key=lambda playlist: -score[playlist['name']]
|
key=lambda playlist: -score[playlist['name']]
|
||||||
)
|
)
|
||||||
videos = playlists[:channels['keywords']]
|
videos = playlists[:channels['globalKeywords']]
|
||||||
playlists = playlists[channels['keywords']:]
|
playlists = playlists[channels['globalKeywords']:]
|
||||||
# Count tags for the user
|
# Count tags for the user
|
||||||
count = defaultdict(lambda: 0)
|
count = defaultdict(lambda: 0)
|
||||||
for event in user.get('events', []):
|
for event in user.get('events', []):
|
||||||
|
@ -107,7 +121,7 @@ class Engine:
|
||||||
videos += sorted(
|
videos += sorted(
|
||||||
playlists,
|
playlists,
|
||||||
key=lambda playlist: -score[playlist['name']]
|
key=lambda playlist: -score[playlist['name']]
|
||||||
)[:16 - channels['keywords']]
|
)[:16 - channels['userKeywords'] - channels['globalKeywords']]
|
||||||
# Shuffle playlists (randomize layout) and shift clips (randomize start)
|
# Shuffle playlists (randomize layout) and shift clips (randomize start)
|
||||||
random.shuffle(videos)
|
random.shuffle(videos)
|
||||||
return [{
|
return [{
|
||||||
|
@ -193,13 +207,13 @@ class Engine:
|
||||||
|
|
||||||
def update_keywords(self):
|
def update_keywords(self):
|
||||||
changed = False
|
changed = False
|
||||||
if 'keywords' not in self.state:
|
if 'globalKeywords' not in self.state:
|
||||||
self.state['keywords'] = {}
|
self.state['globalKeywords'] = {}
|
||||||
changed = True
|
changed = True
|
||||||
for playlist in self.playlists:
|
for playlist in self.playlists:
|
||||||
for tag in playlist.get('tags', []):
|
for tag in playlist.get('tags', []):
|
||||||
if not tag.isupper() and tag not in self.state['keywords']:
|
if not tag.isupper() and tag not in self.state['globalKeywords']:
|
||||||
self.state['keywords'][tag] = {'value': 0}
|
self.state['globalKeywords'][tag] = {'value': 0}
|
||||||
changed = True
|
changed = True
|
||||||
if changed:
|
if changed:
|
||||||
self.save_state()
|
self.save_state()
|
||||||
|
|
Loading…
Reference in a new issue