【问题标题】:Why do I keep getting a AttributeError: 'NoneType' object has no attribute 'lower'?为什么我不断收到 AttributeError:'NoneType' 对象没有属性 'lower'?
【发布时间】:2020-12-30 08:37:05
【问题描述】:

我正在研究类似于 Siri 和 Cortana 的弱 Ai,但是我注意到我一直收到“AttributeError:'NoneType' 对象没有属性'lower'”,除了我的代码而不是我的查询,它总是打印出“对不起,我没听懂”。有人知道如何解决这个问题吗?谢谢

错误:

if 'wikipedia' in query.lower():
AttributeError: 'NoneType' object has no attribute 'lower'

代码:

import pyttsx3
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import pythoncom

print("Initializing Karren")

MASTER = "Bob"

engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)

def speak(text):
    engine.say(text)
    engine.runAndWait()


def wishMe():
    hour = int(datetime.datetime.now().hour)

    if hour>=0 and hour <12:
        speak("Good Morning" + MASTER)

    elif hour>=12 and hour<18:
        speak("Good Afternoon" + MASTER)
    else:
        speak("Good Evening" + MASTER)


   # speak("I am Karren. How may I assist you?") # deliberately on not included for now

def takeCommand():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening...")
        audio = r.listen(source)

    try :
        print("Recognizing...")
        query = r.recognize_google(audio, Language = 'en-uk')
        print(f"User said: {query}\n")

    except Exception as e:
        print("Sorry i didn't catch that...")
        query = None
    return query 

speak("Initializing Karren...")
wishMe()
query = takeCommand()


if 'wikipedia' in query.lower():
    speak("Searching wikipedia")
    query = query.replace("wikipedia", "")
    results = wikipedia.summary(query, sentences=2)
    speak(results)

【问题讨论】:

  • 您可能需要缩进您的 try 语句,以便它属于 with sr.Microphone() as source... 代码块。
  • 在您的takeCommand 函数中,如果识别失败,它可能会返回query = None。如果您不希望 .lower() 出现此错误,可以将其设置为返回空字符串。
  • 您在takeCommand 中设置了query = None。该路径应该已经向您打印了额外的错误信息。请注意,通过破坏程序来“处理”异常是没有意义的 - 如果遇到异常时无法有意义地设置 query,请不要抑制异常。

标签: python python-3.x speech-recognition speech-to-text


【解决方案1】:

这是因为takeCommand 正在返回NoneNone 没有 lower 方法,因此您会收到此错误。

您可以做的一件事是记录更多关于在takeCommand 中引发的异常导致其进入except 块的信息,或者只是删除try 块以便异常停止程序,然后阅读追溯。

【讨论】:

  • 对不起,我对 python 还很陌生,请您详细解释一下。
  • 当你执行query = takeCommand(),其中takeCommand 返回None,然后是query.lower(),这与执行None.lower() 相同。由于None 没有lower 方法,因此失败。我的答案的第二部分解决了如何解决您的实际问题,即r.recognize_google 失败。最简单的方法是删除所有错误处理代码,以便您可以看到它引发的异常。希望该异常包含一些允许您调试的信息。
【解决方案2】:
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
    print("Listening...")
    audio = r.listen(source)

try :
    print("Recognizing...")
    query = r.recognize_google(audio, Language = 'en-uk')
    print(f"User said: {query}\n")

except Exception as e:
    print("Sorry i didn't catch that...")
    query = "None" # or whatever str
return query 

我自己试过了。在 Takecommand() 函数中,其中 query = none,将 none 替换为字符串。原因是当您遇到错误时,Nonetype 没有 lower 属性,query = none 并且由于它不是 str 或其他什么,lower() 无法对其进行转换。如上所述,这是解决此错误而不再遇到任何错误的一种方法。测试响应。

Otherwsie 删除查询 = 无

【讨论】:

    猜你喜欢
    • 2020-12-08
    • 2019-12-04
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 2021-03-29
    相关资源
    最近更新 更多