声控音乐系统,如何select音乐?

Voice controlled music system, How to select music?

我正在创建一个声控音乐系统。 我现在拥有的系统可以正常工作,但效果不佳。 它将单词分开并查看音乐选择。 但是如果某些歌曲在我说的标题中有相同​​的词(比如我说 "of" 并且它找到 5 首歌曲)它会选择音乐列表中的最后一首歌曲。 有人知道选择音乐的更好方法吗?

def detectWords(response):
    response = response.lower()
    words = response.split()

    if 'play' in words:
            os.chdir("/home/pi/Desktop/Stemherkenning/Music")
            for file in glob.glob("*mp3"):
                fileSplit = (file.lower()).split()
                if any(word in fileSplit for word in words):
                    mixer.music.load(file)
                    mixer.music.play()
                    print("Playing " + file) 

    if 'stop' in words:
        print("Stopped")
        mixer.music.stop()

'words' 是 Google 语音识别识别出的词。

有多种方法可以解决这个问题,但复杂程度各不相同。此方法检查与所述短语具有最多共同词的歌曲,它应该有助于您入门:

import os
import glob
import mixer
import operator

def title_match(title, songlist):
    """Returns the song from `songlist` whose title has 
    the most unique words in common with `title`.
    If two such songs have the same amount of words intersecting, 
    only returns one song."""

    title = set(tuple(title))
    matches = {song: len(title & set(song.lower().split())) for song in songlist}
    result = max(matches.items(), key=operator.itemgetter(1))[0]

    return result


def detectWords(response):
    response = response.lower()
    words = response.split()

    if 'play' in words:
        os.chdir("/home/pi/Desktop/Stemherkenning/Music")
        songlist = [file for file in glob.glob("*mp3")]
        title = words[words.index("play")+1:]
        file = title_match(title, songlist) #grabs closest matching file
        print("Playing: " + file)
        mixer.music.load(file)
        mixer.music.play()

    if 'stop' in words:
        print("Stopped")
        mixer.music.stop()