我目前正在放学期间构建个人助理机器人作为一项挑战。它以前没有问题,但我开始收到 TypeErrors,尽管我能够检查“语音”是否作为字符串返回。我知道这是因为 TypeError 会在 while True 循环运行几次后随机弹出(所以“语音”不能作为字符串返回......)
我发现其他关于 TypeErrors 的页面以及返回 None 和 NonTypes 的东西,比如 one和 two ,但似乎都无济于事。我还浏览了无数其他页面,试图找到有帮助的东西,但无济于事。
错误总是出现在第 19 行:
import speech_recognition as sr
from chatterbot import ChatBot
import Messanger_Alert as MA
import Weather
import Email_Alert
import Chat
import Run
def start():
#bot = Chat.start()
MA_client = MA.login()
print "Hello Luke!"
print "I am ready for your command! :D"
while True:
speech = return_audio()
try:
if any(word in speech for word in ("Jason", "jason")): #This is line 19
if any(word in speech for word in ("Message", "message", "Messenger", "messenger", "Facebook", "facebook")):
if any(word in speech for word in ("Send", "send")):
MA.send_message(MA_client)
print "Ready for next command!"
elif any(word in speech for word in ("Search Friend", "search friend", "Seach friend", "search Friend", "friend", "Friend", "friends", "Friends")):
MA.friend_search(MA_client)
print "Ready for next command!"
elif any(word in speech for word in ("Check", "check")):
MA.check_message(MA_client)
print "Ready for next command!"
elif any(word in speech for word in ("Email", "email")):
if any(word in speech for word in ("Personal", "personal", "Home", "home")):
Email_Alert.login1()
print "Ready for next command!"
elif any(word in speech for word in ("School", "school", "Dorm", "dorm", "UCSD", "ucsd")):
Email_Alert.login2()
print "Ready for next command!"
elif any(word in speech for word in ("Weather", "weather", "Forecast", "forecast")):
Weather.decide()
"""else:
Chat.talk(bot)
else:
Chat.talk(bot)
"""
except sr.UnknownValueError:
print "Error! Could not process that audio."
except sr.RequestError as e:
print "Error! No internet connection to Google Sound Recognizer."
def return_audio():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
speech = r.recognize_google(audio)
try:
speech = str(speech)
print speech
return speech
except TypeError:
print "Error! Could not convert speech to string!"
except sr.UnknownValueError:
print "Error! Could not process that audio."
except sr.RequestError as e:
print "Error! No internet connection to Google Sound Recognizer."
这正是我得到的错误:
Traceback (most recent call last):
File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 25, in <module>
startup()
File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 14, in startup
voice()
File "C:/Users/lukec/PycharmProjects/Sysgen_AI/Run_File.py", line 20, in voice
Listen.start()
File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Listen.py", line 19, in start
if any(word in speech for word in ("Jason", "jason")):
File "C:\Users\lukec\PycharmProjects\Sysgen_AI\Listen.py", line 19, in <genexpr>
if any(word in speech for word in ("Jason", "jason")):
TypeError: argument of type 'NoneType' is not iterable
我注意到当听到无法区分为单词的奇怪噪音时,TypeError 会更频繁地出现,但我不明白为什么。当我没有发出命令时让 Chatbot 库对话机器人接管时,我第一次注意到这个错误,并认为当我将它从我的代码中删除时,错误会停止,但这并没有解决它。任何帮助修复它并向我解释原因的帮助将不胜感激。
如果您需要我的额外文件(Messanger_Alert、Weather 等),如果可能有帮助,我愿意提供该代码。
感谢您的帮助!
请您参考如下方法:
使用 if any(word in speech for word in ("Jason", "jason")):,Python 试图检查 word 是否在 语音。您从 return_audio() 获得 speech。当 return_audio() 遇到任何指定的异常时,它不会执行任何显式的 return 语句,因此隐式返回 None,这不能被迭代以寻找任何东西。
要解决此问题,只需在尝试查看该结果之前检查该函数是否产生了真实结果。
while True:
speech = return_audio()
if not speech:
continue
