Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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)
question from:https://stackoverflow.com/questions/65872477/i-made-a-voice-assistant-with-python-how-can-i-execute-only-one-when-i-use-two

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A brute solution would be to check if both of the commands are in the variable 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()
    

But i wouldn't recommend this because as you add new commands you have to keep updating the new if statement that i've added.

Instead make the user say words like "and" when giving multiple commands. Now you can split the command based on that word and run each command.

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)

I have added a comment next to all of the new code that i've added. I'm happy to help if you are still confused.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...