I am making a voice assistant with Python. I'm giving voice commands to the assistant. There are several predefined sentences and answers. However, if there are two commands defined in the code before, they start working at the same time. How can I prevent this. For example, if the commands "what time is it (saat kaç)" and "assistant shutdown (asistan kapan)" occur in a sentence, they do both. Instead I perceived two commands as assistant, "what time is it" and "assistant shut down". How can I get me to ask questions like which one would you like me to do?
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)