About ChatterBot
ChatterBot is a Python library that makes it easy to generate automated responses to a user’s input. ChatterBot uses a selection of machine learning algorithms to produce different types of responses. This makes it easy for developers to create chat bots and automate conversations with users. For more details about the ideas and concepts behind ChatterBot see the process flow diagram.
Installing
f you are just getting started with ChatterBot, it is recommended that you start by installing the latest version from the Python Package Index (PyPi). To install ChatterBot from PyPi using pip run the following command in your terminal.
pip install chatterbot
Creating your first chat bot
Create a new file named chatbot_basic.py. Then open chatbot_basic.py in your editor of choice.
Before we do anything else, ChatterBot needs to be imported. The import for ChatterBot should look like the following line.
from chatterbot import ChatBot
Create a new instance of the ChatBot class.
bot = ChatBot('Dev.to')
This line of code has created a new chat bot named Dev.to
Setting the storage adapter
ChatterBot comes with built in adapter classes that allow it to connect to different types of databases. In this tutorial, we will be using the SQLStorageAdapter which allows the chat bot to connect to SQL databases. By default, this adapter will create a SQLite database.
The database parameter is used to specify the path to the database that the chat bot will use. For this example we will call the database sqlite:///database.sqlite3. this file will be created automatically if it doesn’t already exist.
bot = ChatBot(
'Dev.to',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///database.sqlite3'
)
Specifying logic adapters
The logic_adapters parameter is a list of logic adapters. In ChatterBot, a logic adapter is a class that takes an input statement and returns a response to that statement.
You can choose to use as many logic adapters as you would like. In this example we will use two logic adapters. The TimeLogicAdapter returns the current time when the input statement asks for it. The MathematicalEvaluation adapter solves math problems that use basic operations.
bot = ChatBot(
'Norman',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
'chatterbot.logic.MathematicalEvaluation',
'chatterbot.logic.TimeLogicAdapter'
],
database_uri='sqlite:///database.sqlite3'
)
Training with corpus data
chatterbot.trainers.ChatterBotCorpusTrainer(chatbot, **kwargs)[source]
Allows the chat bot to be trained using data from the ChatterBot dialog corpus.
ChatterBot comes with a corpus data and utility module that makes it easy to quickly train your bot to communicate. To do so, simply specify the corpus data modules you want to use.
from chatbot import chatbot
from chatterbot.trainers import ChatterBotCorpusTrainer
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train("chatterbot.corpus.english")
Simple Example
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
# Create a new ChatBot with name Dev.to
chatbot = ChatBot(
'Dev.to',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
'chatterbot.logic.MathematicalEvaluation',
'chatterbot.logic.BestMatch'
],
database_uri='sqlite:///database.sqlite3'
)
# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(chatbot)
# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")
print("\nHello i'm Dev.to Bot\n")
while True:
try:
bot_input = input()
if bot_input.strip()=='Stop':
print('Dev.to: Bye')
break
bot_response = chatbot.get_response(bot_input)
print(bot_response)
except(KeyboardInterrupt, EOFError, SystemExit):
break
Hello i'm Dev.to Bot
Hi
How are you doing?
pretty good thanks
That's good to hear.
What is 4 + 13?
4 + 13 = 17
Stop
Dev.to: Bye
Creating a new training class
You can create a new trainer to train your chat bot from your own data files. You may choose to do this if you want to train your chat bot from a data source in a format that is not directly supported by ChatterBot.
Your custom trainer should inherit chatterbot.trainers.Trainer class. Your trainer will need to have a method named train, that can take any parameters you choose.
Take a look at the existing trainer classes on GitHub for examples.
Top comments (0)