### 无法在我的语音识别代码中生成来自谷歌/YouTube的搜索结果

我正在尝试构建一个可以与人互动并帮助他们快速获取更新的聊天机器人。以下是我用来从YouTube/谷歌获取搜索结果的代码。请告诉我问题出在哪里?

maya_google_search.py 代码:

import speech_recognitionimport pyttsx3import pywhatkitfrom wikipedia import wikipediaimport wikipedia as googleScrapimport webbrowserengine = pyttsx3.init("sapi5")voices = engine.getProperty("voices")engine.setProperty("voice", voices[1].id)engine.setProperty("rate", 150)def speak(audio):    engine.say(audio)    engine.runAndWait()def takeCommand():    r = speech_recognition.Recognizer()    with speech_recognition.Microphone() as source:        print("listening.............")        r.pause_threshold = 1        r.energy_threshold = 300        audio = r.listen(source,0,4)    try:        print("Understanding............")        query = r.recognize_google(audio, language='en-in')        print(f"You said: {query}\n")    except Exception as e:        print("Say that again")        speak("Say that again")        return "None"        return queryquery = takeCommand().lower()def Googlesearch(query):        if "google" in query:        query = query.replace("Maya", "")        query = query.replace("google search", "")        query = query.replace("google", "")        speak("This is what I found on Google.....")                try:            pywhatkit.search(query)            result = googleScrap.summary(query,sentences=2)            speak("According to Google..........")            speak(result)                except:            speak("No speakable output available")def Youtubesearch(query):    if "youtube" in query:        query = query.replace("Maya", "")        query = query.replace("youtube search", "")        query = query.replace("youtube", "")        speak("This is what I found for your search!")         web = "https://www.youtube.com/results?search_query=" + query        webbrowser.open(web)        pywhatkit.playonyt(query)                speak("Done, sir")

maya_ai.py 代码:

import pyttsx3import speech_recognitionengine = pyttsx3.init("sapi5")voices = engine.getProperty("voices")engine.setProperty("voice", voices[1].id)engine.setProperty("rate", 150)def speak(audio):    engine.say(audio)    engine.runAndWait()    def takeCommand():    r = speech_recognition.Recognizer()    with speech_recognition.Microphone() as source:        print("listening.............")        r.pause_threshold = 1        r.energy_threshold = 300        audio = r.listen(source,0,4)    try:        print("Understanding............")        query = r.recognize_google(audio, language='en-in')        print(f"You said: {query}\n")        # speak(query)    except Exception as e:        print("Say that again")        return "None"        return queryif __name__ == "__main__":        while True:        query = takeCommand().lower()        if "wake up" in query:            from maya_greeting import greetMe            greetMe()            while True:                query = takeCommand().lower()                if "go to sleep" in query:                    speak("Ok sir, You can call me anytime...")                    break                                elif "hello" in query:                    speak("Hello Sir, how are you?")                elif "i am fine" in query:                    speak("That's really great to know sir....")                elif "how are you":                    speak("i am perfectly alright sir.")                elif "thank you" in query:                    speak("you're welcome sir")                elif "google" in query:                    from maya_google_search import Googlesearch                    Googlesearch(query)                elif "youtube" in query:                    from maya_google_search import Youtubesearch                    Youtubesearch(query)                                elif "wikipedia" in query:                    from maya_google_search import Wikisearch                    Wikisearch(query)

如果我说“谷歌 @人名”,它只会打印我说的话,然后说“我很好,先生”或者什么也不说。

请帮帮我。


回答:

更改

elif "how are you":

elif "how are you" in query:

然后你需要添加一个最终的 else 语句,以防之前的条件都没有触发

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注