def __init__(self, quality='all', **kwargs): """Creates a GenreTreeProvider with a certain quality. A GenreTreeProvider will try to normalize a genre by using a Tree of 705 single genres that will be matched with the input genre in a fast way. The result will be a list of Paths. A Path is a tuple of indices, representing a possible way through the Tree. For debugging purpose you can use GenreTreeProvider.resolve_path() on the path to get the full genre back. The Quality levels are: - ``all``: Try to find all possible paths through the Tree, sorted by the first index (which is useful for comparing.) - ``single``: Simply take the first possible path found. Fastest. - ``best_two``: Like list, but also uses the reverse word list in a second try. Might give better results than 'single' at the cost of some speed. Default is ``all``. This provider is reversible. :param quality: One of ``all``, ``best_two`` ``single`` [*default:* ``all``] :type quality: String """ Provider.__init__(self, **kwargs) self._root = load_genre_tree(get_cache_path('genre_tree.dump')) self._build_func = { 'all': build_genre_path_all, 'best_two': build_genre_path_best_of_two, 'single': build_genre_path_single }.get(quality, build_genre_path_all)
def __init__(self, **kwargs): if not HAS_PLYR: raise LookupError('Plyr could be imported, which is needed for lyrics') self._cache_failures = kwargs.pop('cache_failures', True) self.database = plyr.Database(get_cache_path(None)) Provider.__init__(self, **kwargs)
def __init__(self, use_cache=True, cache_fails=True, **kwargs): """ :param use_cache: Cache found results? :param cache_fails: Also cache missed results? """ Provider.__init__(self, **kwargs) self._use_cache, self._cache_fails = use_cache, cache_fails self._shelve = shelve.open(get_cache_path('discogs_genre.dump'), writeback=True)
def __init__(self, use_cache=True, cache_fails=True, **kwargs): """ :param use_cache: Cache found results? :param cache_fails: Also cache missed results? """ Provider.__init__(self, **kwargs) self._use_cache, self._cache_fails = use_cache, cache_fails self._shelve = shelve.open( get_cache_path('discogs_genre.dump'), writeback=True )
def load_genrelist(): """Load a genre list from both wikipedia and echonest (merge them). If there's a 'genre.list' file in the same directory as this script it gets loaded, otherwise a live search is performed and the result gets written to 'genre.list'. """ relative_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'genre.list' ) try: with open(relative_path, 'r') as handle: genres = [] for genre in handle: genres.append(genre.strip()) return genres except OSError: pass wiki_genres = load_genrelist_from_wikipedia( get_cache_path('genre_list_wikipedia.dump') ) echo_genres = load_genrelist_from_echonest( get_cache_path('genre_list_echonest.dump') ) # Merge and sort them. genres = sorted(wiki_genres | echo_genres) # Pickle the list if desired: try: with open(relative_path, 'w') as handle: for genre in genres: handle.write(genre.strip() + '\n') except OSError: pass return genres
def load_genre_tree(pickle_path): """Load the genre by either (in this order): 1) Load it from a locally pickled file as given by pickle_path 2) Load a list of genres from 'genre_list.dump' and build up the tree. 3) Load a list of genres from echonest.com and build up the tree. :returns: The head of the tree. """ # Load the tree from the disk or rebuild it: try: with open(pickle_path, 'rb') as fh: return pickle.load(fh) except (OSError, IOError, AttributeError): # AttributeError might happen when the pickle file is invalid. check_or_mkdir(get_cache_path(None)) root = build_genre_tree() # Write it to disk for the next time: if root is not None: with open(pickle_path, 'wb') as f: pickle.dump(root, f) return root
resolved = prov.reverse(tuple([prov.process(value)])) for expectation in expected: self.assertTrue(expectation in resolved[0]) unittest.main() ########################################################################### # Other commandline utility for visualization # ########################################################################### else: if len(sys.argv) < 3: print('Usage: {program} --cli "metalcore/melodic death metal"'. format(program=sys.argv[0])) sys.exit(1) root = load_genre_tree(get_cache_path('genre_tree.dump')) # Uncomment to get the whole list: root.print_tree() # Split the genre description and normalize each before finding the path: for sub_genre in prepare_genre_list(sys.argv[2]): words = prepare_single_genre(sub_genre) print() print('///////////////') print('All Possible :') for path in build_genre_path_all(root, words): print(' ', path, root.resolve_path(path)) paths = build_genre_path_best_of_two(root, words)
for expectation in expected: self.assertTrue(expectation in resolved[0]) unittest.main() ########################################################################### # Other commandline utility for visualization # ########################################################################### else: if len(sys.argv) < 3: print('Usage: {program} --cli "metalcore/melodic death metal"'.format( program=sys.argv[0] )) sys.exit(1) root = load_genre_tree(get_cache_path('genre_tree.dump')) # Uncomment to get the whole list: root.print_tree() # Split the genre description and normalize each before finding the path: for sub_genre in prepare_genre_list(sys.argv[2]): words = prepare_single_genre(sub_genre) print() print('///////////////') print('All Possible :') for path in build_genre_path_all(root, words): print(' ', path, root.resolve_path(path)) paths = build_genre_path_best_of_two(root, words)