Exclude zero value penalties from Distance.sorted.

This commit is contained in:
Tai Lee 2013-06-03 00:20:19 +10:00
parent ac4e86981f
commit 809ea8c7f9
2 changed files with 18 additions and 15 deletions

View file

@ -336,9 +336,14 @@ class Distance(object):
@property
def sorted(self):
"""Returns a list of (dist, key) pairs, with `dist` being the weighted
distance, sorted from highest to lowest.
distance, sorted from highest to lowest. Does not include penalties
with a zero value.
"""
list_ = [(self[key], key) for key in self._penalties]
list_ = []
for key in self._penalties:
dist = self[key]
if dist:
list_.append((dist, key))
return sorted(list_, key=lambda (dist, key): (0-dist, key))
def update(self, dist):
@ -542,14 +547,13 @@ def _recommendation(results):
# Downgrade to the max rec if it is lower than the current rec for an
# applied penalty.
for dist, key in results[0].distance.sorted:
if dist:
max_rec = config['match']['max_rec'][key].as_choice({
'strong': recommendation.strong,
'medium': recommendation.medium,
'low': recommendation.low,
'none': recommendation.none,
})
rec = min(rec, max_rec)
max_rec = config['match']['max_rec'][key].as_choice({
'strong': recommendation.strong,
'medium': recommendation.medium,
'low': recommendation.low,
'none': recommendation.none,
})
rec = min(rec, max_rec)
return rec

View file

@ -169,11 +169,10 @@ def penalty_string(distance, limit=None):
"""
penalties = []
for dist, key in distance.sorted:
if dist:
key = key.replace('album_', '')
key = key.replace('track_', '')
key = key.replace('_', ' ')
penalties.append(key)
key = key.replace('album_', '')
key = key.replace('track_', '')
key = key.replace('_', ' ')
penalties.append(key)
if penalties:
if limit and len(penalties) > limit:
penalties = penalties[:limit] + ['...']