chatterbot chatbot实例介绍
chatbot实例
-
Create a chatbot: This is done using the create_bot function. The function takes the name of the bot as an input argument. This function returns an object, called bot, which is further used in the program. You may set the name of your choice. In our case, we have set it to Jordan.
-
Train the chatbot: This is done using the train_all_data function. The data we are training the chatbot for is shown here. The input argument of this function is the bot.
-
Train with custom data: We train the chatbot with custom data using the custom_train function.
- The first input argument of this function is the bot itself.
- The second argument is the custom data we want to train. This custom data takes the form of a Python list. The first element of the list is the question and the second element is its answer (lines 10-13). You may train the chatbot with as much specific custom data as you want.
-
Start the chatbot: Start the chatbot using the start_chatbot function. The input argument of this function is the bot we want to start.
Important note: Please note that this is just the simplistic version of what is happening in the functions.py file, where the detailed code implementation may be found. For some of you, the detailed explanation might be above the scope of this activity. We have shown the codes for you to explore it further on your own.
以上使用chatterbot库的函数详细代码均在functions.py中,附在本文后。
以下是包含身份认证的chatbot,可以通过custom_train()进行个性化训练(如favorite book, movie等)。
from functions import *
# create chatbot
home_bot = create_bot('Jordan')
# train all data
train_all_data(home_bot)
# check identity
identity = input("State your identity please: ")
# rules for responding to different identities
if identity == "Mark":
print("Welcome, Mark. Happy to have you at home.")
elif identity == "Jane":
print("Mark is out right now, but you are welcome to the house.")
else:
print("Your access is denied here.")
exit()
# custom data
house_owner = [
"Who is the owner of this house?",
"Mark Nicholas is the owner of this house."
]
custom_train(home_bot, house_owner)
print("------ Training custom data ------")
# write and train your custom data here IF the identity is Mark
if identity == 'Mark':
city_born = [
"Where was I born?",
"Mark, you were born in Seattle."
]
fav_book = [
"What is my favourite book?",
"That is easy. Your favourite book is The Great Gatsby."
]
fav_movie = [
"What is my favourite movie?",
"You have watched Interstellar more times than I can count."
]
fav_sports = [
"What is my favourite sport?",
"You have always loved baseball."
]
# train chatbot with your custom data
custom_train(home_bot, city_born)
custom_train(home_bot, fav_book)
custom_train(home_bot, fav_movie)
custom_train(home_bot, fav_sports)
# start chatbot
start_chatbot(home_bot)
开始运行:
对话过程:
附functions.py
# function to create the chatbot
# we have the read_only to false so the chatbot learns from the user input as
def create_bot(name):
from chatterbot import ChatBot
Bot = ChatBot(name = name,
read_only = False,
logic_adapters = ["chatterbot.logic.BestMatch"],
storage_adapter = "chatterbot.storage.SQLStorageAdapter")
return Bot
# function to train the bot with a variety of topics
# the language we have chosen is english
# we can train the bot for other languages as well
def train_all_data(Bot):
from chatterbot.trainers import ChatterBotCorpusTrainer
corpus_trainer = ChatterBotCorpusTrainer(Bot)
corpus_trainer.train("chatterbot.corpus.english")
# function to train the bot with custom data
# it uses ListTrainer to train data from lists
def custom_train(Bot, conversation):
from chatterbot.trainers import ListTrainer
trainer = ListTrainer(Bot)
trainer.train(conversation)
# function to start and take responses from the chatbot
# the chatbot stays running unless a word is typed from the bye_list
def start_chatbot(Bot):
print('\033c')
print("Hello, I am Jordan. How can I help you")
bye_list = ["bye jordan", "bye", "good bye"]
while (True):
user_input = input("me: ")
if user_input.lower() in bye_list:
print("Jordan: Good bye and have a blessed day!")
break
response = Bot.get_response(user_input)
print("Jordan:", response)