ChatterBot is designed to be easy to use and customize. You can use it to build a chatbot for a variety of purposes, such as customer service, information retrieval, or entertainment. You can also train it on your own dataset of conversations to make it more personalized and relevant to your use case.
To use ChatterBot, you will need to install it using pip install chatterbot
. Then, you can import it into your Python script and use it to create a chatbot instance. You can specify the type of storage adapter and logic adapter you want to use, and train the chatbot on a corpus of conversations. Finally, you can use the chatbot’s get_response()
method to generate responses to user input.
This script creates a chatbot with a SQL storage adapter, a set of logic adapters, and a corpus trainer, and trains the chatbot on a set of greetings and conversations. It then enters an infinite loop, prompting the user for input and printing the chatbot’s response. The conversation is saved to a file by writing the user’s input and the chatbot’s response on separate lines. To exit the loop, the user can type exit
.
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
# Create the chatbot
bot = ChatBot(
'ChatBot',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
'chatterbot.logic.BestMatch',
'chatterbot.logic.MathematicalEvaluation',
'chatterbot.logic.TimeLogicAdapter'
],
database_uri='sqlite:///database.db'
)
# Create a trainer and train the chatbot
trainer = ChatterBotCorpusTrainer(bot)
trainer.train('chatterbot.corpus.english.greetings',
'chatterbot.corpus.english.conversations')
# Save the conversation to a file
with open('conversation.txt', 'w') as f:
while True:
request = input('You: ')
if request == 'exit':
break
response = bot.get_response(request)
print('Bot:', response)
f.write('You: ' + request + '\n')
f.write('Bot: ' + str(response) + '\n')