简体   繁体   中英

AttributeError: 'ChatBot' object has no attribute 'set_trainer'

PS C:\Users\asus\Downloads\chatbot> python app.py
[nltk_data] Downloading package stopwords to
[nltk_data]     C:\Users\asus\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping corpora\stopwords.zip.
[nltk_data] Downloading package wordnet to
[nltk_data]     C:\Users\asus\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping corpora\wordnet.zip.
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     C:\Users\asus\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping taggers\averaged_perceptron_tagger.zip.
Traceback (most recent call last):
  File "app.py", line 8, in <module>
    bot.set_trainer(ListTrainer)

i've installed chatterbot and had the following error while trying to run it in flask. Im using python 3.7 64 bit. could not find answers. I'm quite new to python please help me out This is the code

from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer

app = Flask(__name__)
bot = ChatBot("Candice")
bot.set_trainer(ListTrainer)
bot.set_trainer(ChatterBotCorpusTrainer)
bot.train("chatterbot.corpus.english")

@app.route("/")
def home():    
    return render_template("home.html") 
@app.route("/get")
def get_bot_response():    
    userText = request.args.get('msg')    
    return str(bot.get_response(userText)) 
if __name__ == "__main__":    
    app.run()

I had this problem until I rearranged placing of the set_trainer statement, as in my code here:

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

def bot(title, read_only = True):    # , don't learn from exchanges (responses exist)
    Bot = ChatBot(title,
                   #filters=["chatterbot.filters.RepetitiveResponseFilter"],
                   logic_adapters=[{                       
                       'import_path': 'chatterbot.logic.BestMatch',
                       "statement_comparision_function": "chatterbot.comparisions.levenshtein_distance",
                       "response_selection_method": "chatterbot.response_selection.get_first_response"
                       }])
    print('Training corpus',title)
    trainer=ChatterBotCorpusTrainer(Bot)
    trainer.train("chatterbot.corpus.english." + title)     
    return Bot                                         # trained instance

bots = {}
# check what corpora available and traina a bot for each one
for item in os.listdir('C:/Python/Python38/lib/site-packages/chatterbot_corpus/data/english/'):
    corpus = item.split('.')        # of the form 'item.yml'
    name = corpus[0]
    bots[name]= bot(name)  # add bot instance to dictionary of available bots
                                                        
def exchange(input):  # let's get a response to our input
    # try suggested corpora to find best fit. If first corpus < theshold, try another.
    # neeed to avoid random responses confidence 0
    bot = bots[context]
    response = bot.get_response(input)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM