【问题标题】:Voice assistant using Python使用 Python 的语音助手
【发布时间】:2021-07-26 23:52:39
【问题描述】:

我正在尝试在 Python 中使用 tkinter 制作语音助手应用程序,但我的代码有两个问题。

我的代码中的一个 sn-p:

def listen_to_me():
    msg2 = Message(root, text="Listening...", bg='yellow', font=('times', 14, 'italic'))
    msg2.pack
    msg2.place(x=200, y=220)

    with sr.Microphone() as source:

        audio = r.listen(source)

        global query

        query = r.recognize_google(audio, language='en-IN', show_all=True)
        if query:
            try:
                (f"[Me]: {query}")                                       
            except:
                engine.say("Sorry didn't quite catch that. Please repeat.")   


    return query



def reply():
    while True:
        global query

        # Logic for executing tasks based on query
        if 'wikipedia' in query:
            speak('Searching Wikipedia...')
            query = query.replace("wikipedia", "")
            query = query.replace("search", "")
            query = query.replace("for", "")
            results = wikipedia.summary(query, sentences=1)
            speak(f'According to Wikipedia, {results}')

        elif 'open youtube' in query:
            speak('Opening Youtube')
            webbrowser.open("https://youtube.com")

        elif 'open stack overflow' in query:
            speak('Opening StackOverflow')
            webbrowser.open("https://stackoverflow.com")

        elif 'what' in query and 'time' in query:
            strTime = datetime.datetime.now().strftime("%H:%M:%S")    
            speak(f"Sir, The time is {strTime}")

        elif 'how are you' or 'what\'s up' in query:
            speak('I am doing jolly good, sir.')

问题 1: 我得到了输出,但它似乎陷入了循环:

[MyAssistant]: Good evening! I am Jarvis. How may I help you today?
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.
[MyAssistant]: I am doing jolly good, sir.

问题 2: 我想将我的查询转换为小写。我尝试了以下方法:

query = query.lower()

错误:

AttributeError: 'list' object has no attribute 'lower'

提前致谢!

【问题讨论】:

标签: python tkinter speech-recognition


【解决方案1】:

对于您的第一个问题,错误是由while True 行引起的。这只是设置在一个无限循环中。您可能希望以更好的方式来构造它,以避免在循环中出现这种情况,例如替换

while = True

有一个

while = (conditional_statement)

您的第二个问题不清楚,因为您显示的输出确实达到了您想要的效果。但是您可以做的一种方法是使用循环替换字典中的键和值,例如

for key,val in query['alternative'].items():
    key = key.lower()
    key = val[0].lower()

以上只是一个想法,你必须做什么才能用小写字符替换你的查询。

【讨论】:

  • 第一个问题解决了。但是,使用您为第二个问题建议的代码会显示此错误 - AttributeError: 'list' object has no attribute 'items'。我打算做的是将识别的文本分配给查询(在listen_to_me中)并在reply()中使用该查询变量,以便使语音助手能够根据我所说的(查询)执行特定任务。
  • 看来你的第二个问题也解决了。太好了!
【解决方案2】:

问题 2 的解决方案:

query 不能简单地使用.lower()方法转换为字符串类型。这是因为 recognize_google()(在 SpeechRecognition 模块中)是一个列表类型,而 .lower() 只能应用于字符串。为了将查询转换为小写,请遵循以下方法:

1. 创建另一个字符串变量 myStr(例如)

2. 初始化 myStr = query

3. 转为小写 myStr= myStr.lower()

4. 最后, return myStr

如果要在另一个函数中使用该字符串,只需初始化 any_other_variable_name = myStr

【讨论】:

    【解决方案3】:

    对于第一个,您只需将while True 语句添加到代码底部,然后在其下方键入run_name of the voice assistant。而且要停止循环,只需像其他人所说的那样添加一个 if 语句

    elif 在命令中“停止”:
    谈话('好的先生,祝你有美好的一天!)
    exit('顶部注册的助手名字')

    我没有给出第二个答案,因为我对此不太确定。并且@AzyCrw4282 已经对此给出了明确而完美的答案。

    【讨论】:

      猜你喜欢
      • 2020-10-21
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2020-10-18
      • 1970-01-01
      相关资源
      最近更新 更多