我用 Python 做了一个语音助手。当我在同一个句子中使用两个不同的语音命令时,如何只执行一个?

I made a voice assistant with Python. How can I execute only one when I use two different voice commands in the same sentence?

我正在用 Python 制作语音助手。我正在向助手发出语音指令。有几个预定义的句子和答案。但是,如果之前的代码中定义了两个命令,它们会同时开始工作。我该如何防止这种情况。例如,如果命令“现在几点了 (saat kaç)”和“assistant shutdown (asistan kapan)”出现在一个句子中,它们会同时执行这两个命令。相反,我将两个命令视为助手,“现在几点了”和“助手关闭”。我怎样才能让我问一些问题,比如你希望我做哪一个?

import ... 
r = sr.Recognizer()
def record(ask=False):
    with sr.Microphone() as source:
        if ask:
            speak(ask)
        audio = r.listen(source)
        voice = ''
        try:
            voice = r.recognize_google(audio, language='tr-TR')
        except sr.UnknownValueError:
            print("anlayamadım") #anlayamadım = i couldn't understand
        except sr.RequestError:
            speak("sistem çalışmıyor") #sistem çalışmıyor= system is not work
        return voice

def response(voice):
    if 'saat kaç' in voice: #saat kaç = what time is it
        speak(datetime.now().strftime('%H:%M:%S'))
    if 'Asistan kapan' in voice: #asistan kapan = assistant shutdown
        speak("görüşürüz") #görüşürüz = bye
        exit()
#The command that consists of voice responses in the range of 1, 10000 files and deletes after the answer is answered
def speak(string):
    tts = gTTS(string, lang='tr')
    rand = random.randint(1, 10000)
    file = 'audio' + str(rand) + '.mp3'
    tts.save(file)
    playsound(file)
    os.remove(file)

speak("nasıl yardımcı olabilirim") #nasıl yardımcı olabilirim = how can i help you
time.sleep(1)
while True:
    voice = record()
    print(voice)
    response(voice)

一个粗暴的解决方案是检查这两个命令是否都在变量 voice

def response(voice):
    if 'saat kaç' in voice and 'Asistan kapan' in voice:
    speak("which one would you like me to do first?")
    new_voice = record()
    response(new_voice)
    else:
        if 'saat kaç' in voice: #saat kaç = what time is it
            speak(datetime.now().strftime('%H:%M:%S'))
        if 'Asistan kapan' in voice: #asistan kapan = assistant shutdown
            speak("görüşürüz") #görüşürüz = bye
            exit()
    

但我不推荐这样做,因为当您添加新命令时,您必须不断更新我添加的新 if 语句。

相反,让用户在发出多个命令时说“和”之类的词。现在您可以根据该词和 运行 每个命令拆分命令。

def split_command(voice):
    commands = voice.split(" and ") # splits the command into multiple parts for each " and "  that means command like "what time is it and assistant shutdown" would become ["what time is it", "assistant shutdown"]
    for command in commands:
        response(command) # now for each command in the list it is calling the response function

def response(voice):
    if 'saat kaç' in voice: #saat kaç = what time is it
        speak(datetime.now().strftime('%H:%M:%S'))
    elif 'Asistan kapan' in voice: #asistan kapan = assistant shutdown
        speak("görüşürüz") #görüşürüz = bye
        exit()  
    # now instead of multiple ifs you can use elif 

# now inside the infinite while loop call split_command function instead of the response function
while True:
    voice = record()
    print(voice)
    split_command(voice)

我在我添加的所有新代码旁边添加了注释。如果您仍然感到困惑,我很乐意为您提供帮助。