From 4618e1c44b8151cad9f08d46b20bae3017d2c063 Mon Sep 17 00:00:00 2001 From: ale <ale@incal.net> Date: Mon, 1 Aug 2011 14:21:46 +0100 Subject: [PATCH] remove last_song from the total for normalization too --- server/djrandom/model/markov.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/server/djrandom/model/markov.py b/server/djrandom/model/markov.py index 7fa6662..baaeb33 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): -- GitLab