我今天开始用Python编写代码,我试着遵循本教程(https://www.analyticsvidhya.com/blog/2021/10/complete-guide-to-build-your-ai-chatbot-with-nlp-in-python/) --您可以找到许多其他的博客,它们的代码完全相同。
import numpy as np
import speech_recognition as sr
# Beginning of the AI
class ChatBot():
def __init__(self, name):
print("----- starting up", name, "-----")
self.name = name
def speech_to_text(self):
recognizer = sr.Recognizer()
with sr.Microphone() as mic:
print("listening...")
audio = recognizer.listen(mic)
try:
self.text = recognizer.recognize_google(audio)
print("me --> ", self.text)
except:
print("me --> ERROR")然而,当尝试
if __name__ == "__main__":
ai = ChatBot(name="Dev")
while True:
ai.speech_to_text()将显示以下错误消息:
AttributeError: 'ChatBot' object has no attribute 'speech_to_text'如果我用对象资源管理器检查ai,就没有'speec_to_text',所以错误是有意义的。但是,我不知道如何解决这个问题。
如果我设置
ai.speech_to_text = speech_to_text(ai)很管用,但在我看来是不对的。所有的网站都是这样做的,我不明白。
发布于 2022-08-20 14:39:43
这是一个任天堂错误,实际上,您必须将speech_to_text函数放入该类中。像这样
class ChatBot():
def __init__(self, name):
print("----- starting up", name, "-----")
self.name = name
def speech_to_text(self):
recognizer = sr.Recognizer()
with sr.Microphone() as mic:
print("listening...")
audio = recognizer.listen(mic)
try:
self.text = recognizer.recognize_google(audio)
print("me --> ", self.text)
except:
print("me --> ERROR")https://stackoverflow.com/questions/73427422
复制相似问题