AttributeError:“'ChatBot'对象没有属性'trains'”

时间:2019-03-24 04:15:15

标签: python chatbot chatterbot

我正在尝试使用chatterbot在电报中创建一个聊天机器人,并使用函数train()生成ChatBot,但是可视代码(我的编辑器)和atom无法识别该库。在我使用pip之前,请在cmd中安装chatterbot并启动:

Successfully installed PyYAML-3.13 chatterbot-1.0.4 chatterbot-corpus-1.2.0 mathparse-0.1.2 nltk-3.4 pint-0.9 pymongo-3.7.2 singledispatch-3.4.0.3 sqlalchemy-1.2.18

我试图用可视代码从cmd重新安装该库。但是不要运行代码。显示给我的错误是:

[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     C:\Users\KatiusKa\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping taggers\averaged_perceptron_tagger.zip.
[nltk_data] Downloading package punkt to
[nltk_data]     C:\Users\KatiusKa\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping tokenizers\punkt.zip.
[nltk_data] Downloading package stopwords to
[nltk_data]     C:\Users\KatiusKa\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping corpora\stopwords.zip.
[nltk_data] Downloading package wordnet to
[nltk_data]     C:\Users\KatiusKa\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping corpora\wordnet.zip.
Traceback (most recent call last):
  File "C:\Users\KatiusKa\Documents\Python\chbot.py", line 8, in <module>
    chatbot.train(
AttributeError: 'ChatBot' object has no attribute 'train'

这是可视代码中的代码:

from chatterbot import ChatBot

chatbot = ChatBot(
    "Ejemplo Bot",
    trainer = "chatterbot.trainers.ChatterBotCorpusTrainer"
)

chatbot.train(
    "chatterbot.corpus.spanish"
)

这是我尝试从视觉代码运行的代码

1 个答案:

答案 0 :(得分:3)

错误是正确的-chatbot类没有属性train。如果您签出文档,那是您应该训练的ChatterBotCorpusTrainer类,并且确实具有train()函数。

在此处查看chatterbot的基本用法:https://github.com/gunthercox/ChatterBot#basic-usage

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

chatbot = ChatBot('Ron Obvious')

# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")