Micro Search Engine

This module provides a class to search for songs, albums and artists. A fuzzy search algorithm gets used. This not only allows typos, it also adapts the search to the natural situation that song names are hard to remember.

Before an object can be used, the UpdateCache() method must be called to generate the cache.

The cache consists of three lists of tuples. One list for Artists, Albums and Songs. The Tuples contain the ID and the normalized Name (see NormalizeString()).

If the Levenshtein module is installed, the search through all artists, albums and song is usually done in less than 100ms. Without this module, you better not use this module only if time does matter.

Example

database = MusicDBConfig()
mise     = MusicDBMicroSeachEngine(database)

mise.UpdateCache()

(artists, _ , _ ) = mise.Find("Rammsein")
for artistid, conf in artists:
    artist = database.GetArtistById(artistid)
    print("Artist: %s, Confidence: %d"%(artist["name"], conf))

MusicDBMicroSearchEngine Class

class musicdb.mdbapi.mise.MusicDBMicroSearchEngine(config)[source]

Before an object can be used, the UpdateCache() method must be called to generate the cache.

Because a cache update can be triggered from any thread, a new database instance will be created for reading information from the database.

Parameters

configmusicdb.lib.cfg.musicdb.MusicDBConfig object for the path to the music database.

Raises

TypeError – When the database argument is invalid.

Find(userinput)[source]

This method searches through the caches of song, album and artist names. A fuzzy search gets applied and so the results matches only with a certain probability.

Parameters

userinput (str) – Search-Sting to search for. This string gets normalized before it is used to search.

Returns

A tuple of lists of artists, albums and songs that were found. Each list entry is a tuple of an ID and the Confidence

Example:

(artists, albums, songs) = mise.Find("Rammsein")
for artistid, conf in artists:
    print("ID: %d, Confidence: %d"%(artistid, conf))
NormalizeString(string)[source]

This method normalizes a string so that it is easier to find. The normalization is done in the following steps:

  1. Unicode normalization (method: NFKC)

  2. all characters to Lower Case

  3. removes leading and training spaces

  4. replaces multiple spaces to one space

Parameters

string (str) – A string that shall be normalized

Returns

A normalized string

UpdateCache()[source]

Updates the internal cache used for searching.

This is the only place in this class where the music database gets accessed.

Warning

This method must be called in the same thread the database object was instantiated!

Returns

None