我正在用 Python 做一个语音助手。我正在向助手发出语音命令。有几个预定义的句子和答案。但是,如果之前在代码中定义了两个命令,它们会同时开始工作。我该如何防止这种情况。例如,如果命令“现在是什么时候(saat kaç)”和“助手关机(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)