added likely_metadata

--HG--
extra : convert_revision : svn%3A41726ec3-264d-0410-9c23-a9f1637257cc/trunk%40196
This commit is contained in:
adrian.sampson 2009-04-07 23:57:19 +00:00
parent 7f6ace92be
commit 155c9f7303
2 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1,45 @@
from collections import defaultdict
from beets.autotag.mb import match_album
def likely_metadata(items):
"""Returns the most likely artist and album for a set of Items.
Each is determined by tag reflected by the plurality of the Items.
"""
# The tags we'll try to determine.
keys = 'artist', 'album'
# Make dictionaries in which to count the freqencies of different
# artist and album tags. We'll use this to find the most likely
# artist and album. Defaultdicts let the frequency default to zero.
freqs = {}
for key in keys:
freqs[key] = defaultdict(int)
# Count the frequencies.
for item in items:
for key in keys:
value = getattr(item, key)
if value: # Don't count empty tags.
freqs[key][value] += 1
# Find max-frequency tags.
likelies = {}
for key in keys:
max_freq = 0
likelies[key] = None
for tag, freq in freqs[key].items():
if freq > max_freq:
max_freq = freq
likelies[key] = tag
return (likelies['artist'], likelies['album'])
if __name__ == '__main__': # Smoke test.
from beets.library import Item
items = [Item({'artist': 'The Beatles', 'album': 'The White Album'}),
Item({'artist': 'The Beetles', 'album': 'The White Album'}),
Item({'artist': 'The Beatles', 'album': 'Teh White Album'})]
print likely_metadata(items)

View file

@ -6,7 +6,18 @@ import sys
import time import time
import musicbrainz2.model import musicbrainz2.model
sys.path.append('..') sys.path.append('..')
from beets import autotag
from beets.autotag import mb from beets.autotag import mb
from beets.library import Item
class AutotagTest(unittest.TestCase):
def test_likely_metadata_finds_pluralities(self):
items = [Item({'artist': 'The Beetles', 'album': 'The White Album'}),
Item({'artist': 'The Beatles', 'album': 'The White Album'}),
Item({'artist': 'The Beatles', 'album': 'Teh White Album'})]
l_artist, l_album = autotag.likely_metadata(items)
self.assertEqual(l_artist, 'The Beatles')
self.assertEqual(l_album, 'The White Album')
class MBQueryWaitTest(unittest.TestCase): class MBQueryWaitTest(unittest.TestCase):
def setup(self): def setup(self):