语音控制音乐系统,如何选择音乐?



我正在创建一个语音控制的音乐系统。 我现在拥有的系统可以工作,但工作效果很差。 它将单词拆分并查看音乐选择。 但是,如果某些歌曲在我说的标题中有相同的单词(例如我说"的"并且它找到了 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()

"单词"是谷歌语音识别所选择的单词。

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

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()

最新更新