A Simple Guide to Build Your First Chatbot using Python

Oindrila Sen
Jun 25, 2020 · 4 min read

Have you ever meet a Chatbot or chatterbot? Of course, you have! It is everywhere nowadays. I have met an annoying one, a smart one, and even a kind one. So, let’s start at the very beginning and go step by step from there to build a simple stupid one.

What is a chatbot?

How I define this is:

A chatbot is a human-less solution for answering a question or resolving a complaint or communicating with brands or just a personal assistant without a body.

Now, how wiki defines it is:

A chatbot is a software application used to conduct an online chat conversation via text or text-to-speech, in lieu of providing direct contact with a live human agent.

Thus, combining both my personal thoughts and wiki’s insights, we can say a chatbot is a software program driven by artificial intelligence that serves the purpose of making a conversation with the user by texts or by speech.

How does it work?

A chatbot is trained to answer questions formulated to it in natural language and respond like a real person. Now, most organizations have a chatbot that maintains logs of previous discussions. Then the developers use these logs to analyze what clients are trying to ask. Using machine learning tools and models, then the developers organize client inquiries and reply with the best suitable answer.

Thus, when a chatbot is asked a question, it will respond based on the knowledge database available to it at that point in time. Now, what if the conversation introduces a new concept or a new question? That is where I call a chatbot a smart one or a stupid one and there comes the beauty of programming.

Sometimes, they are programmed to deflect the conversation or potentially pass the communication to a human operator or answer something witty. Either way, it will learn from that interaction for similar future interactions. Thus, the chatbot will gradually grow in scope and learn from previous experience and thus gain relevance.

Where it is used?

My 5-year-old daughter talks a lot with Alexa. A sample conversation goes like this:

Oli: Alexa, how are you feeling today?

Alexa: I am feeling like a wiz. If you feel like a challenge, ask me a quiz.

Now, that is a clever reply, Right? I just love to listen to their conversation and my daughter thinks that a sweet and kind lady is really listening to her and answering all her questions.

Apart from Alexa and Siri, Chatbots are extensively used in customer assistance. When u open a website and a chat window pops up down below, that says “Hi there! How can I help you today?” Even though it seems like you are talking with a real person, in most scenarios, it is a chatbot.

Now, that is enough of definitions and stories about chatbots. Let’s write some code.

Build your first own dimwitted ChatBot

Let’s follow this step by step method and build our very own simple chatbot. We will try to improvise it with time.

Install Libraries

pip install chatterbot

So, I started installing the “Chatterbot” library and I was stuck at the very first step. I got an error like below:

ERROR: Cannot uninstall ‘PyYAML’. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

After a few digging around, the below workaround worked fine for me.

pip install chatterbot==1.0.4
python -m chatterbot --version

Import Libraries

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
from chatterbot.trainers import ChatterBotCorpusTrainer
Hide warnings
import warnings
warnings.filterwarnings('ignore')

Train a Chatot with Corpus Data

ChatterBot comes with a corpus data and utility module that makes it easy to train a bot to communicate. I want to train my chatbot in English.

A Chat with my very first Chatbot

Okay, so my first chatbot is all ready to be trained. So, let’s call the above function to train my chatbot named “MiniMe” and let’s have a chat!

Hmm, well I am really ashamed to share the first conversation I had with my MiniMe. I need to work more on that. But, it made me smile today. That’s why I am sharing it for a hearty laugh.

Well, does that make any sense? No! But it was talking to me and that was my plan for today. Also, I had a series of more stupid conversation and that was really funny. Oh! I can’t stop chatting with MiniMe!

BTW, if any of the above code is not working for you, you can check my Jupyter Notebook in my Github.

Now, I am off to my chatbot. Share your first conversation and let’s share a laugh.

Keep Coding, Keep having Fun, and Keep reading.

Thank You!

A decade of experience in collecting, cleaning, and visualizing data. But now I am learning and practicing new tools and different techniques. Let’s dig and find out what is the story behind that Data. Let’s do it together! Let’s solve, fail, and then try again because discovering something new is always fun.

Originally published at https://oindrilasen.com on June 25, 2020.

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…