Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Building Simple Chatbot using Python

Building Simple Chatbot using Python

February 8th, 2020

A chatbot is an AI-based software that is deployed in an application, device or websites to communicate with the users or to perform a task e.g., Google Assistant, Alexa, Siri, etc.
Most of the companies started using chatbots as customer support and now it is emerging as a task performer.

Two types of chatbots

  1. Rule-Based
  2. Self-learning

1. Rule-Based

A bot is developed in such a way that it analyzes the questions based on specific rules.And based on these rules data will be trained. These types of bots are developed to communicate with simple questions.

2. Self-learning

Self-learning bots are developed using machine learning libraries and these are considered as more efficient bots. Self-learning can be classified as two types-Retrieval Based and Generative.

Retrieval Based

This type of bots chooses responses from a predefined message library. It analyses the conversation and selects the best response from the library.


These bots create responses on their own apart from selecting messages from the predefined library.

Building Chatbot

Let’s build chatbot using chatterBot library
First, let’s download the library using the following command

pip install chatterbot

Then Import the same to build a chatbot and also import chatterbot.trainers in order to train the data.

from chatterbot import *
#importing ListTrainer
from chatterbot.trainers import *

Creating and training a bot

First, create an instance of ChatBot class
robo = ChatBot(name=’Bot’, readonly=true,

  • name – It represents bot name
  • readonly – To prevent the bot from learning after actual conversation
  • logic adapters – List of modules required to train. In our case, we have chosen MathematicalEvalution to solve maths problems and BestMatch to choose the best response.

Let us train a few messages and mathematical operations.

greetings = [
'Hi, hello!',
'What’s Up?',
'How are you?',
'Fine, How about You?',
'Im doing Good,
'Im fine, thank you',
'Im Glad, you are talking to me',
'My pleasure',
'Whats your name?',
'My name is Bot. Ask me an interesting math question.'
maths1 = ['add', 'a+b']
maths2 = ['square', '**']

Now let us train the above-mentioned responses by using ListTrainer.

training_of_data = ListTrainer(robo)
for i in (greetings, maths1, maths2):

Now Bot ready to start a conversation with trained messages

Below is the sample of chatbot conversation
>>> print(robo.get_response("hello"))
Hi, hello!
>>> print(robo.get_response("What’s Up"))
Im Glad, you are talking to me.
>>> print(robo.get_response("what is your name?"))
My name is Bot. ask me a interesting maths question.
>>> print(robo.get_response("Tell me how to add two numbers"))
>>> print(robo.get_response
("do you know the operator that is used to find
square or cube root of the number"))

Related Blogs