def distance(items, album_info, mapping): """Determines how "significant" an album metadata change would be. Returns a float in [0.0,1.0]. `album_info` is an AlbumInfo object reflecting the album to be compared. `items` is a sequence of all Item objects that will be matched (order is not important). `mapping` is a dictionary mapping Items to TrackInfo objects; the keys are a subset of `items` and the values are a subset of `album_info.tracks`. """ cur_artist, cur_album, _ = current_metadata(items) cur_artist = cur_artist or '' cur_album = cur_album or '' # These accumulate the possible distance components. The final # distance will be dist/dist_max. dist = 0.0 dist_max = 0.0 # Artist/album metadata. if not album_info.va: dist += string_dist(cur_artist, album_info.artist) * ARTIST_WEIGHT dist_max += ARTIST_WEIGHT dist += string_dist(cur_album, album_info.album) * ALBUM_WEIGHT dist_max += ALBUM_WEIGHT # Matched track distances. for item, track in mapping.iteritems(): dist += track_distance(item, track, album_info.va) * TRACK_WEIGHT dist_max += TRACK_WEIGHT # Extra and unmatched tracks. for track in set(album_info.tracks) - set(mapping.values()): dist += MISSING_WEIGHT dist_max += MISSING_WEIGHT for item in set(items) - set(mapping.keys()): dist += UNMATCHED_WEIGHT dist_max += UNMATCHED_WEIGHT # Plugin distances. plugin_d, plugin_dm = plugins.album_distance(items, album_info) dist += plugin_d dist_max += plugin_dm # Normalize distance, avoiding divide-by-zero. if dist_max == 0.0: return 0.0 else: return dist / dist_max
def distance(items, album_info): """Determines how "significant" an album metadata change would be. Returns a float in [0.0,1.0]. The list of items must be ordered. """ cur_artist, cur_album, _ = current_metadata(items) cur_artist = cur_artist or '' cur_album = cur_album or '' # These accumulate the possible distance components. The final # distance will be dist/dist_max. dist = 0.0 dist_max = 0.0 # Artist/album metadata. if not album_info.va: dist += string_dist(cur_artist, album_info.artist) * ARTIST_WEIGHT dist_max += ARTIST_WEIGHT dist += string_dist(cur_album, album_info.album) * ALBUM_WEIGHT dist_max += ALBUM_WEIGHT # Track distances. for i, (item, track_info) in enumerate(zip(items, album_info.tracks)): if item: dist += track_distance(item, track_info, i+1, album_info.va) * \ TRACK_WEIGHT dist_max += TRACK_WEIGHT else: dist += MISSING_WEIGHT dist_max += MISSING_WEIGHT # Plugin distances. plugin_d, plugin_dm = plugins.album_distance(items, album_info) dist += plugin_d dist_max += plugin_dm # Normalize distance, avoiding divide-by-zero. if dist_max == 0.0: return 0.0 else: return dist/dist_max