使用Python的语音助手

2024-04-25 16:40:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Python中的tkinter制作一个语音助手应用程序,我的代码有两个问题

我的代码片段:

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'

提前谢谢


Tags: 代码inopenwikipediaqueryamreplacegood
3条回答

问题2的解决方案:

query无法使用.lower()方法将简单地转换为字符串类型。这是因为识别\u google()(在SpeechRecognition模块中)是一种列表类型,.lower()只能应用于字符串。要将查询转换为小写,请遵循以下方法:

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

2.初始化myStr = query

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

4.最终,return myStr

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

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

elif 'stop' in command:
talk('Ok sir,have a great day!)
exit('name of your assistant that you have registered at the top')

我不会回答第二个问题,因为我对此不太确定。而且@AzyCrw4282已经给出了一个明确而完美的答案

enter image description hereHere are some examples

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

while = True

带着

while = (conditional_statement)

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

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

上面只是一个想法,您必须做什么,以取代您的查询小写字符

相关问题 更多 >