def _split(text): # TODO: make this logic common """Split an album title into parts. @param text: string to split into parts @return: name, kind, featuring """ kind = featuring = None # Strip featured artists match = re.search(RE_FEATURING, text, re.IGNORECASE | re.VERBOSE) if match: featuring = match.group(1) logging.debug("match found: {0}".format(featuring)) text = text.replace( match.group(0), '').strip() # remove the match from the remaining text # Strip song kinds for kind in settings.KINDS: re_kind = RE_KIND.replace('<kind>', kind) match = re.search(re_kind, text, re.IGNORECASE | re.VERBOSE) if match: logging.debug("match found: {0}".format(kind)) text = text.replace( match.group(0), '').strip() # remove the match from the remaining text break else: kind = None # Return parts return TextTitle.fromstring(text), TextEnum.fromstring( kind), TextList.fromstring(featuring)
def test_parse_list_with_and(self): """Verify a 3-item text list is parsed.""" lst = TextList("This, That, and the Other ") self.assertEqual( (TextName("This"), TextName("That"), TextName("the Other")), lst.items) self.assertEqual("This, That, the Other", str(lst))
def _split(text): # TODO: make this logic common """Split an album title into parts. @param text: string to split into parts @return: name, kind, featuring """ kind = featuring = None # Strip featured artists match = re.search(RE_FEATURING, text, re.IGNORECASE | re.VERBOSE) if match: featuring = match.group(1) logging.debug("match found: {0}".format(featuring)) text = text.replace(match.group(0), '').strip() # remove the match from the remaining text # Strip song kinds for kind in settings.KINDS: re_kind = RE_KIND.replace('<kind>', kind) match = re.search(re_kind, text, re.IGNORECASE | re.VERBOSE) if match: logging.debug("match found: {0}".format(kind)) text = text.replace(match.group(0), '').strip() # remove the match from the remaining text break else: kind = None # Return parts return TextTitle.fromstring(text), TextEnum.fromstring(kind), TextList.fromstring(featuring)
def test_ep(self): """Verify an EP can be parsed.""" album = Album("Tracks (feat. The Artist) [EP]") self.assertEqual(TextTitle("Tracks"), album.name) self.assertEqual(TextEnum("EP"), album.kind) self.assertEqual(TextList("The Artist"), album.featuring) self.assertEqual("Tracks [EP]", str(album))
def test_similar_equal(self): """Verify two equal text lists are similar.""" similarity = TextList("This + That") % TextList("This + That") self.assertTrue(similarity) self.assertEqual(1.0, similarity)
def test_similar_unequal(self): """Verify two different text lists are not similar.""" similarity = TextList("This, That, and the Other ") % TextList( "This Other Thing & One") self.assertFalse(similarity) self.assertGreater(0.31, similarity)
def test_parse_pair(self): """Verify a 2-item text list is parsed.""" lst = TextList(" Something & This Thing") self.assertEqual((TextName("Something"), TextName("This Thing")), lst.items) self.assertEqual("Something, This Thing", str(lst))
def test_similar_close(self): """Verify two close text lists are similar.""" similarity = TextList("This, That, and the Other ") % TextList( "That, This & the Other ") self.assertTrue(similarity) self.assertLess(0.99, similarity)