Dustin
Dustin

Reputation: 6307

Python - Best Matching Search

I have a SiriServerCore setup that I'm programming to essentially automate functions for my media center. This media center has movies, tv shows, and music on it. Essentially the voice to text will receive something like "Play The Final Episode by Asking Alexandria". In a python list containing all my music's file locations, there is a file called "Asking Alexandria - 2 - The Final Episode (Let's Change Channel).mp3". How would I go about having the text speech "best match" to the items in the list? Any help is appreciated!

Upvotes: 3

Views: 2011

Answers (2)

Pawel Miech
Pawel Miech

Reputation: 7822

Assuming that your voice input will be transformed into a string you can simply iterate over the song list, look at first two or three words of the song title, compare each title with the input string and if some or most words of the song title are present in string representing your voice you can make a decision about the right song.This would be something like:

>>> a = "Play The Final Episode by Asking Alexandria" # voice input
>>> songList = ["Asking Alexandria - 2 - The Final Episode (Let's Change Channel).mp3",      "Angie", "Yesterday.mp3"]
>>> for songTitle in SongsList:
        songTitleWords = songTitle.split(" ")
        if " ".join(songTitleWords[:2]) in voiceInput: 
            # play the song

But this is more of a hunch rather then a perfect solution. I think that first words of a song title are usually most important. At the same time it is better to start from song title and match song title to voice input rather then the other way around because voice input can contain unnecessary elements, first three words of a voice input may be something like "please play me a song of the title". First words of song title are usually informative.

Upvotes: 2

Rushy Panchal
Rushy Panchal

Reputation: 17532

Based on this question, you might want to use this library for fuzzy string comparisons. It checks the similarity between two strings; you can use that to find the best match.

Upvotes: 1

Related Questions