[Python] problem with using speech_recognition with if() statement and while loop [closed]

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a voice assistant and there's a problem with the result. The code has been made as one controlling file and one module file. Here's the controlling file "project1.py".

    import project1_module1
    import project1_module3

    def voice(text):
    speech = project1_module1.s_inform(text)
    return speech

    def home():
    while True:
    if "Info" in speak:
    voice("Please Choose the info which you wanted to know. There are four option weather, economy, news and books. ")
    if "weather" in speak:
    project1_module3.weather_cast()
    continue

    elif "economy" in speak:
    project1_module3.economy_info()
    continue

    elif "news" in speak:
    project1_module3.news_info()
    continue

    elif "book" in speak:
    project1_module3.book_info()
    continue

    elif "turn off"in speak:
    break

    elif "turn off" in speak:
    break

    home()


    Here's the module file

    #project1_module1
    import os
    import time
    import playsound
    import speech_recognition as sr
    from gtts import gTTS
    import random

    def number_generator():
    ran = random.randint(1,5000)
    ran = str(ran)
    return ran

    def s_inform(text):
    tts = gTTS(text = text, lang = 'en')
    number = number_generator()
    fileaname = "Annie"+number+".mp3"
    tts.save(fileaname)
    playsound.playsound(fileaname)
    try:
    os.remove(fileaname)
    except:
    print("I can't")

    def get_audio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
    while True:
    audio = r.listen(source)
    said = ""
    try:
    said = r.recognize_google(audio)
    print(said)
    except Exception as e:
    print("Exception:" + str(e))
    return said


    It's the Python voice assistant that recognized my voice and provided the information that I wanted by answering, it does recognize my voice by printing out exceptions or my speaking, and it also breaks the program when I say "turn off", but when I said "info" it recognizes the word "info" itself, but afterward it just keeps showing the blank terminal. I expected it to answer the sentence inside of the voice function, recognize my voice again, and provide the information that I demanded. The "project1_module3" doesn't have any problem with it and it's a module that has nothing to do with speech_recognition or voicing. So I need help.

    Continue reading...

Compartilhe esta Página