diff --git a/server/djrandom/model/markov.py b/server/djrandom/model/markov.py index 7fa666263290480dd96c5c617d20c6ed00ca8a4d..baaeb33dd491e492ed7d25bd47601741971aefcf 100644 --- a/server/djrandom/model/markov.py +++ b/server/djrandom/model/markov.py @@ -53,17 +53,21 @@ class MarkovModel(object): def normalize(self): norm_map = {} for key, target_map in self._map.iteritems(): + # We will explicitly drop this target to avoid repeating + # the same song more than once. + last_song = key[-1] + norm_vec = [] - tot = sum(target_map.itervalues()) - cur = 0 + tot = cur = 0 + for target, count in target_map.iteritems(): + if target != last_song: + tot += count for target, count in target_map.iteritems(): - # We explicitly drop this target to avoid repeating - # the same song more than once. - if target == key[-1]: - continue - cur += float(count) / tot - norm_vec.append((cur, target)) + if target != last_song: + cur += float(count) / tot + norm_vec.append((cur, target)) norm_map[key] = norm_vec + self._map = norm_map def suggest(self, prev):