我正在创建一个声音控制的音乐系统。我现在的系统工作得很好,但做得很差。它把单词拆开,看音乐选集。但是,如果有些歌曲的标题与我说的相同(就像我说的"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()“单词”是谷歌语音识别所使用的词汇。
发布于 2018-07-04 08:33:51
有多种方法来处理这个问题,有不同程度的复杂。此方法检查与上述短语有最独特的单词的歌曲,它将帮助您开始:
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()https://stackoverflow.com/questions/51162654
复制相似问题