Build your first ChatBot in 5 minutes 
Tags: chatbot, python, easy
Published: 2018-11-23 06:56:14

---
title: Build your first ChatBot in 5 minutes 
published: true
description: A tutorial for creating a chatbot
tags: chatbot, python, easy
cover_image: https://thepracticaldev.s3.amazonaws.com/i/yy0g0ejzcl9zf008c5bl.jpeg
---
I was searching the internet on <i>"How to build a Chatbot?"</i> and I discovered [ChatterBot](https://chatterbot.readthedocs.io/en/stable/) which is a machine learning, conversational dialog engine for creating chat bots

### How ChatterBot works
<img width="512" alt="processdiagram" src="https://user-images.githubusercontent.com/20112458/48929710-3ce62b80-ef11-11e8-822b-bfff0167c2c6.png">
<center><i>Image source: [ChatterBot](https://chatterbot.readthedocs.io/en/stable/#process-flow-diagram)</i></center>

In this article we will see how to build a chatbot with ChatterBot in just 5 minutes.
## Let's start 

First [Install ChatterBot](https://pypi.org/project/ChatterBot/)

```sh
pip install ChatterBot
```
Create a file `chat.py`

```sh
#import ChatBot
from chatterbot import ChatBot
```

Create a new chat bot with the name of your choice(I am using "Candice").

```sh
bot = ChatBot('Candice')
```
Your bot is created but at this point your bot has no knowledge, for that you have to train it on some data.
`Also, by default the ChatterBot library will create a sqlite database to build up statements of the chats.`

### Train your bot

```sh
#import ListTrainer
from chatterbot.trainers import ListTrainer

bot.set_trainer(ListTrainer)
# Training 
bot.train(['What is your name?', 'My name is Candice'])
bot.train(['Who are you?', 'I am a bot, created by you' ])
```
Your bot is now trained on 2 statements. When you ask your bot "what is your name", it will reply back with "My name is Candice".

You can also trained it on multiple statements like
```sh
bot.train(['Do you know me?', 'Yes, you created me', 'No', 'Sahil?', 'No idea'])
```
As you can see it is difficult to train the bot on every single statements. So, we will use `ChatterBotCorpusTrainer` to train our bot on the large dataset.

```sh
from chatterbot.trainers import ChatterBotCorpusTrainer
# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(bot)

# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")

# Get a response to an input statement
chatbot.get_response("Hello, how are you today?")
```

or you can download the [dataset of your language](https://github.com/gunthercox/chatterbot-corpus/tree/master/chatterbot_corpus/data) and train your bot in your language.

I downloaded the [english language dataset](https://github.com/sahil-rajput/FileTransfer/files/2609894/english.zip) and we can train our bot like this
```sh
for files in os.listdir('./english/'):
	data=open('./english/'+files,'r').readlines()
	bot.train(data)
```
`NOTE: Make sure the dataset and the program file is on same folder, otherwise edit the path.`

### Chat feature
```sh
# To exit say "Bye"
while True:
        # Input from user
	message=input('\t\t\tYou:')
        #if message is not "Bye"
	if message.strip()!='Bye':
		reply=bot.get_response(message)
		print('Candice:',reply)
        # if message is "Bye"
	if message.strip()=='Bye':
		print('Candice: Bye')
		break
``` 
To run it, Go to terminal
```sh
python chat.py
```
It will train your bot first and then you can start chatting.


#### Source code
```sh
#import libraries
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os

#Create a chatbot
bot=ChatBot('Candice')
bot.set_trainer(ListTrainer)

#training on english dataset
for files in os.listdir('./english/'):
	data=open('./english/'+files,'r').readlines()
	bot.train(data)

#chat feature
while True:
	message=input('\t\t\tYou:')
	if message.strip()!='Bye':
		reply=bot.get_response(message)
		print('Candice:',reply)
	if message.strip()=='Bye':
		print('Candice: Bye')
		break
```