DEV Community

Cover image for Build your first ChatBot in 5 minutes
Sahil Rajput
Sahil Rajput

Posted on

Build your first ChatBot in 5 minutes

I was searching the internet on "How to build a Chatbot?" and I discovered ChatterBot which is a machine learning, conversational dialog engine for creating chat bots

How ChatterBot works

processdiagram

Image source: ChatterBot

In this article we will see how to build a chatbot with ChatterBot in just 5 minutes.

Let's start

First Install ChatterBot

pip install ChatterBot

Create a file chat.py

#import ChatBot
from chatterbot import ChatBot

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

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

#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

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.

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 and train your bot in your language.

I downloaded the english language dataset and we can train our bot like this

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

# 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

python chat.py

It will train your bot first and then you can start chatting.

Source code

#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

Top comments (11)

Collapse
 
kpspersonal profile image
kpspersonal • Edited

do pip install chatterbot-corpus
Then
Make the following changes to your code
remove the following lines
bot.set_trainer(ListTrainer)
bot.set_trainer(ChatterBotCorpusTrainer)
bot.train("chatterbot.corpus.english")

Add the following
trainer = ChatterBotCorpusTrainer(bot)
trainer.train('chatterbot.corpus.english')

Remove the following
training on english dataset
for files in os.listdir('./english/'):
data=open('./english/'+files,'r').readlines()
data=open('./english/'+files,'r').readlines()
trainer.train(data)
It should work now.

Collapse
 
karthiji profile image
KarthiJi • Edited

im not able to execute this code, cud u help me out with that..? can dm me abt it..?

Collapse
 
sahilrajput profile image
Sahil Rajput

What error you are facing?

Collapse
 
karthiji profile image
KarthiJi

cant import error

Thread Thread
 
sahilrajput profile image
Sahil Rajput

Download all the libraries.

Collapse
 
oshanwisumperuma profile image
Oshan Wisumperuma

I faced an issue when training using corpus files through "ListTrainer". seems even "categories" and "conversations" tags in yml file also get inserted into the data source.

Collapse
 
sahilrajput profile image
Sahil Rajput

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer

bot = ChatBot("Candice")
bot.set_trainer(ListTrainer)
bot.set_trainer(ChatterBotCorpusTrainer)
bot.train("chatterbot.corpus.english")

Try this

Collapse
 
faisal55859081 profile image
faisal

Try chatgen.ai to create chatbots. You can create click based as well as NLP Bots.

Collapse
 
mourey2277 profile image
Jannatul Mourey

Hello
While running your code I am getting this error

AttributeError: 'ChatBot' object has no attribute 'set_trainer'

Collapse
 
sahilrajput profile image
Sahil Rajput

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer

bot = ChatBot("Candice")
bot.set_trainer(ListTrainer)
bot.set_trainer(ChatterBotCorpusTrainer)
bot.train("chatterbot.corpus.english")

Try this and if you still get the same error. You can contact me via mail: sahil.rajput.0196@gmail.com

Collapse
 
amitkum58154973 profile image
Amit Kumar Mishra

any open source code and tools to design a website-based chatbot for the business need of a company?