-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Home
ChatterBot is a machine-learning based conversational dialog engine build in Python which makes it possible to generate responses based on collections of known conversations. The language independent design of ChatterBot allows it to be trained to speak any language.
An example of typical input would be something like this:
user: Good morning! How are you doing?
bot: I am doing very well, thank you for asking.
user: You're welcome.
bot: Do you like hats?
An untrained instance of ChatterBot starts off with no knowledge of how to communicate. Each time a user enters a statement, the library saves the text that they entered and the text that the statement was in response to. As ChatterBot receives more input the number of responses that it can reply and the accuracy of each response in relation to the input statement increase. The program selects the closest matching response by searching for the closest matching known statement that matches the input, it then returns the most likely response to that statement based on how frequently each response is issued by the people the bot communicates with.
from chatterbot import ChatBot
chatbot = ChatBot("Ron Obvious")
# Train based on the english corpus
chatbot.train("chatterbot.corpus.english")
# Get a response to an input statement
chatbot.get_response("Hello, how are you today?")
Chatterbot comes with a data utility module that can be used to train chat bots.
At the moment there is only English training data in this module. Contributions
of additional training data or training data in other languages would be greatly
appreciated. Take a look at the data files in the chatterbot.corpus
directory
if you are interested in contributing.
from chatterbot import ChatBot
chatbot = ChatBot("Ron Obvious")
Note: The only required parameter for the ChatBot
is a name. This can be any anything you want.
After creating a new ChatterBot instance it is also possible to train the bot. Training is a good way to ensure that the bot starts off with knowledge about specific responses. The current training method takes a list of statements that represent a conversation. Additional notes on training can be found here.
Note: Training is not required but it is recommended.
conversation = [
"Hello",
"Hi there!",
"How are you doing?",
"I'm doing great.",
"That is good to hear",
"Thank you.",
"You're welcome."
]
chatbot.train(conversation)
response = chatbot.get_response("Good morning!")
print(response)
Your ChatterBot will learn based on each new input statement it receives.
If you want to disable this learning feature after your bot has been trained, you can set read_only=True
as a parameter when initializing the bot.
chatbot = ChatBot("Johnny Five", read_only=True)
ChatterBot uses adapter modules to control the behavior of specific types of tasks. There are three distinct types of adapters that ChatterBot uses, these are storage adapters, IO adapters, and logic adapters.
Storage adapters provide an interface for ChatterBot to connect to various storage systems such as MongoDB or local file storage.
IO adapters prove methods that allow ChatterBot to get input from a defined data source and return a response as output.
Logic adapters define the logic that ChatterBot uses to respond to input it receives.
Read more about the various adapters that are available.
By default, ChatterBot uses the JsonDatabaseAdapter
adapter for storage,
the ClosestMatchAdapter
for logic, and the TerminalAdapter
for IO.
Each adapter can be set by passing in the dot-notated import path to the constructor as shown.
bot = ChatBot(
"My ChatterBot",
storage_adapter="chatterbot.adapters.storage.JsonDatabaseAdapter",
logic_adapter="chatterbot.adapters.logic.ClosestMatchAdapter",
io_adapter="chatterbot.adapters.io.TerminalAdapter",
database="../database.db"
)