...
 
Commits (2)
......@@ -54,7 +54,7 @@ for genre in genres:
# rating – set to '0' (no thumb), '1' (down thumb), or '5' (up thumb).
track_pool = [t for t in library if t['genre'] == genre and str(t['rating']) != str(1)]
p_tracks = selector.select_pool(track_pool, selector.RandomizedPlayCountSelector(), playlist_size)
p_tracks = selector.select_pool(track_pool, selector.RandomizedPlayCountSelector(2), playlist_size)
print('Populating playlist', p_name)
api.add_songs_to_playlist(p_id, [t['id'] for t in p_tracks])
......
......@@ -40,22 +40,23 @@ class PlayCountSelector(Selector):
class RandomizedPlayCountSelector(Selector):
def __init__(self):
def __init__(self, factor=1.0):
self._rand = Random()
self._factor = factor
def select(self, tracks: List[Dict]) -> int:
if len(tracks) == 0:
return None
else:
maxPlayCount = max([t['playCount'] for t in tracks])
totalPlayCount = sum([1 + maxPlayCount - t['playCount'] for t in tracks])
totalPlayCount = sum([pow(1 + maxPlayCount - t['playCount'], self._factor) for t in tracks])
targetPlayCount = self._rand.randrange(0, totalPlayCount)
ix = 0
for t in tracks:
targetPlayCount -= 1 + maxPlayCount - t['playCount']
targetPlayCount -= pow(1 + maxPlayCount - t['playCount'], self._factor)
if targetPlayCount <= 0:
if targetPlayCount < 0:
return ix
ix += 1
......
......@@ -64,3 +64,19 @@ class SelectorTest(unittest.TestCase):
tracks = select_pool(library, selector, 100)
self.assertEqual(len(tracks), 100)
def testAgainRandomizedPlayCountSelector(self):
maxcount = 6
library = [{'playCount': i} for i in range(0, maxcount)]
selections = {i: 0 for i in range(0, maxcount)}
selector = RandomizedPlayCountSelector(1)
for i in range(10000):
ix = selector.select(library)
selections[ix] += 1
print(selections)
self.assertNotIn(0, selections.values())
self.assertGreater(selections[0], selections[1])