DEV Community

Piyush Goyani
Piyush Goyani

Posted on • Originally published at blog.thesourcepedia.org on

Build ChatBot with ChatterBot and Python - Part 1

Introduction

ChatterBot is a machine-learning based conversational dialogue 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. Initially, it has no knowledge, then based on training and communication it tries to respond to the user's query accurately. It matches the closest matching response by searching a statement that matches the input and how frequently the response is used. These things are highly customizable with logic adapters, training processes, preprocessors, filters, and other custom behaviour.

Installation

We'll install a chatterbot package with pip. So first, open your terminal and type the following command with the appropriate Python version. Here I'm using v3.7.10 specifically.

pip install chatterbot

Enter fullscreen mode Exit fullscreen mode

Check if the installation is successful, run the below command:

pip show chatterbot

Enter fullscreen mode Exit fullscreen mode

Once done with installation let's start with a basic example.

Create a Bot

First, create a file named greet_bot.py. import ChatBot class from chatterbot library.

from chatterbot import ChatBot

Enter fullscreen mode Exit fullscreen mode

Create a new chatbot named "GreetBot", you can give any name you want.

chatbot = ChatBot("GreetBot")

Enter fullscreen mode Exit fullscreen mode

Training your Bot

Import ListTrainer from sets of available trainers to train bot with greet data so it can respond to our inputs.

from chatterbot.trainers import ListTrainer

Enter fullscreen mode Exit fullscreen mode

Create trainer instance of ListTrainer with chatbot parameter, and train with list of sample greeting data.

trainer = ListTrainer(chatbot)
train_data = [
    "Hello",
    "Hi",
    "Hi, how are you?",
    "I'm doing great.",
    "Greetings!",
    "How do you do?",
    "Great, thanks for asking!",
    "What's up"
] 
trainer.train(train_data)

Enter fullscreen mode Exit fullscreen mode

Retrieve response from Bot

To retrieve a response from trained bot use get_response method of bot object with input parameter.

response = chatbot.get_response('hi')
print(response)

Enter fullscreen mode Exit fullscreen mode

Final Code

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer

chatbot = ChatBot('GreetBot')

trainer = ListTrainer(chatbot)

train_data = [
    "Hello",
    "Hi",
    "Hi, how are you?",
    "I'm doing great.",
    "Greetings!",
    "How do you do?",
    "Great, thanks for asking!",
    "What's up"
]

trainer.train(train_data)

response = chatbot.get_response('Hi')
print(response)

Enter fullscreen mode Exit fullscreen mode

To run a bot, run the script greet_bot.py

python greet_bot.py

Enter fullscreen mode Exit fullscreen mode

image.png

Conclusion/Notes

Thank You for reading this article. I hope this helps in some ways and get a basic idea of how to get started with bot building with Python and the Chatterbot library. This was a very basic explanation and demo. Share, Like, Subscribe and stay tuned for the same and other bot-building tutorials.

Top comments (0)