Table of Content
chatterbot作为聊天机器人训练一多就性能不行.
挂载google硬盘
In [0]:## 挂载google硬盘from google.colab import drivedrive.mount('/content/gdrive', force_remount=True)root_dir = "/content/gdrive/My Drive/"base_dir = root_dir + 'fastai-v3/'app_dir = base_dir+"ChatterBot"!mkdir /content/gdrive/My\ Drive/fastai-v3/ChatterBotIn [0]:ChatterBot
ChatterBot is a machine learning, conversational dialog engine for creating chat bots https://chatterbot.readthedocs.io
# 安装
In [0]:!pip install chatterbot测试使用
In [0]:from chatterbot import ChatBotfrom chatterbot.trainers import ChatterBotCorpusTrainerchatbot = ChatBot('Ron Obvious')# Create a new trainer for the chatbottrainer = ChatterBotCorpusTrainer(chatbot)# Train the chatbot based on the english corpus or chinesetrainer.train("chatterbot.corpus.english")# Get a response to an input statementchatbot.get_response("Hello, how are you today?")Out[0]:进阶使用
Training data
In [0]:from chatterbot import ChatBotfrom chatterbot.trainers import ChatterBotCorpusTrainerfrom chatterbot.conversation import Statementimport logging# bot = ChatBot('叨叨')# 初始化数据库bot = ChatBot( '叨叨',# storage_adapter='chatterbot.storage.MongoDatabaseAdapter', logic_adapters=[ 'chatterbot.logic.BestMatch', 'chatterbot.logic.MathematicalEvaluation', # 'chatterbot.logic.TimeLogicAdapter' ], input_adapter='chatterbot.input.TerminalAdapter', # 命令行端 output_adapter='chatterbot.output.TerminalAdapter', storage_adapter="chatterbot.storage.SQLStorageAdapter" # database_uri='mongodb://localhost:27017/chatterbot-database')# # # Create a new trainer for the chatbot# # 载入基本训练# # Create a new trainer for the chatbot# trainer = ChatterBotCorpusTrainer(bot)# # # Train the chatbot based on the english corpus# # # trainer.train("chatterbot.corpus.chinese")# trainer.train(# # "chatterbot.corpus.chinese",# # "./data/greetings_corpus/custom.corpus.json",# "./Dialog_Corpus/"# )# 对话测试# The following loop will execute each time the user enters input# while True:# try:# user_input = input()# bot_response = bot.get_response(user_input)# print(bot_response)# # Press ctrl-c or ctrl-d on the keyboard to exit# except (KeyboardInterrupt, EOFError, SystemExit):# break# while True:# message = input('You: ')# if message.strip() != 'Bye':# reply = bot.get_response(message)# print('Bot: ', reply)# if message.strip() == 'bye':# print('Bot : Bye')# break# 对话训练def get_feedback(): text = input() if 'y' in text.lower(): return True elif 'n' in text.lower(): return False else: print('是否训练 "Yes(y)" or "No(n)"') return get_feedback()print('输入要开始的东西......')# The following loop will execute each time the user enters inputwhile True: try: input_statement = Statement(text=input('>')) response = bot.generate_response( input_statement ) print('>>>\n 机器人返回: "{}" 是否与:"{}" 相关吗? \n 是否需要训练 "Yes(y)" or "No(n)"'.format( response.text, input_statement.text )) if get_feedback(): print('请输入正确的') correct_response = Statement(text=input('>')) bot.learn_response(correct_response, input_statement) print('已经修正机器人') print('————————————————————————') print('———开始新的训练————————————') print('————————————————————————') # Press ctrl-c or ctrl-d on the keyboard to exit except (KeyboardInterrupt, EOFError, SystemExit): break导出训练数据
In [0]:# Now we can export the data to a filetrainer.export_for_training( app_dir+'/my_export.json')