AttributeError: “ 'ChatBot' object has no attribute 'trains' ”

浪尽此生 提交于 2019-12-20 04:54:05

问题


I'm trying to use chatterbot to create a chatbot in telegram and use the function train() to generate the ChatBot but visual code(my editor) and atom can't recognize the library. Before I use pip install chatterbot in cmd and launched:

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

I tried to re-install the library from cmd in visual code. But don't run the code. The error that show me is:

[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'

This is the code in visual code:

from chatterbot import ChatBot

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

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

This is the code that I tried run from visual code


回答1:


The error is correct - the chatbot class does not have an attribute train. If you check out the docs, it's the ChatterBotCorpusTrainer class that you're supposed to train, and which does, indeed, have a train() function.

Check out the basic usage of chatterbot here: 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")


来源:https://stackoverflow.com/questions/55320669/attributeerror-chatbot-object-has-no-attribute-trains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!