好的,这是我的代码:
import subprocess
import playsound
import speech_recognition as sr
from gtts import gTTS
knownCommands = {"open lunar client": subprocess.call('C:/Users/Joshua/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Lunar Client')}
def speak(text):
tts = gTTS(text=text)
filename = "voice.mp3"
tts.save(filename)
playsound.playsound(filename)
def get_audio():
r = sr.Recognizer()
with sr.Microphone as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
except Exception as e:
print("Exception: " + str(e))
return said
speak("Hello. I am SuperAssitant 1. How can I help you today?")
text = get_audio()
if text == "open lunar client":
subprocess.call('C:/Users/Joshua/AppData/Roaming/Microsoft/Windows/Start Menu/Programs/Lunar Client')
我想知道我是否可以运行字典中的值,在本例中是knownCommands
。如果你不明白我的意思,举个例子:
是的,可以将函数作为值存储在字典中。在iPython演示:
选项1:如果您知道要为每个
knownCommand
调用subprocess.call
,只需将命令存储为字符串:选项2:如果每个命令可以有不同的行为,请使用lambda:
相关问题 更多 >
编程相关推荐