YouTube Icon

Learn to Build Your first Chatbot using Python

Learn to Build Your first Chatbot using Python

Conversational NLP, or characteristic language preparing, is having a major impact in content examination through chatbots. A chatbot is a man-made brainpower based instrument worked to talk with people in their local language. These chatbots have gotten mainstream across ventures, and are viewed as one of the most valuable uses of normal language preparing.

Also Read:- What is Python?

Right now, will figure out how to fabricate your first chatbot utilizing Python

Benchmark Libraries 

You'll be utilizing the nltk library right now. NLTK represents Natural Language Toolkit and is a main python library to work with content information. The primary line of code underneath imports the library, while the subsequent line utilizes the module to import the necessary utilities.

import nltk
from import Chat, reflections

The code below shows that utility Chat is a class that provides logic for building the chatbot.



<class ''>

The other import you did above was Reflections, which is a word reference that contains a lot of info content and its comparing yield esteems. You can analyze the word reference with the code beneath. This is a discretionary word reference and you can make your own word reference in a similar configuration as beneath. 



{'i am': 'you are',
     'i was': 'you were',
     'i': 'you',
     "i'm": 'you are',
     "i'd": 'you would',
     "i've": 'you have',
     "i'll": 'you will',
     'my': 'your',
     'you are': 'I am',
     'you were': 'I was',
     "you've": 'I have',
     "you'll": 'I will',
     'your': 'my',
     'yours': 'mine',
     'you': 'me',
     'me': 'you'}

Also Read:- Why learning Python is now essential for all data scientists

Building the Chatbot 

The initial step is to make decides that will be utilized to prepare the chatbot. The lines of code underneath make a straightforward arrangement of rules. The primary component of the rundown is the client input, while the subsequent component is the reaction from the bot. A few such records are made in the set_pairs object.

set_pairs = [
        r"my name is (.*)",
        ["Hello %1, How are you doing today ?",]
        ["Hello", "Hey there",]
        r"what is your name?",
        ["You can call me a chatbot ?",]
        r"how are you ?",
        ["I am fine, thank you! How can i help you?",]
        r"I am fine, thank you",
        ["great to hear that, how can i help you?",]
        r"how can i help you? ",
        ["i am looking for online guides and courses to learn data science, can you suggest?", "i am looking for data science training platforms",]
        r"i'm (.*) doing good",
        ["That's great to hear","How can i help you?:)",]
        r"i am looking for online guides and courses to learn data science, can you suggest?",
        ["Pluralsight is a great option to learn data science. You can check their website",]
        r"thanks for the suggestion. do they have great authors and instructors?",
        ["Yes, they have the world class best authors, that is their strength;)",]
        r"(.*) thank you so much, that was helpful",
        ["Iam happy to help", "No problem, you're welcome",]
    ["Bye, take care. See you soon :) ","It was nice talking to you. See you soon :)"]

Also Read:- How Python H2O Wave Open-Source Expansion Make Information Research Easier?

After creating the pairs of rules above, we define the chatbot using the code below. The code is simple and prints a message whenever the function is invoked.


Hi, I'm the chatbot you built

The next step is to instantiate the Chat() function containing the pairs and reflections.

chat = Chat(set_pairs, reflections)

Also Read:- How to Build a Real-time Chat App With NodeJS, Socket.IO, and MongoDB


< object at 0x7f49c76e3be0>

You have created a simple rule-based chatbot, and the last step is to initiate the conversation. This is done using the code below where the converse() function triggers the conversation.

if __name__ == "__main__":

The code above will generate the following chatbox in your notebook, as shown in the image below.


You're ready to interact with the chatbot. Start by typing a simple greeting, "hi", in the box, and you'll get the response "Hello" from the bot, as shown in the image below.

Also Read:- The Ultimate Guide to Hiring Python Developers for Startups


You can continue conversing with the chatbot and quit the conversation once you are done, as shown in the image below.

Also Read:- Why Python Django Development Are Viewed As The Strongest Choice for Web Development?



Right now, found out about making a basic chatbot in Python. You utilized straightforward guidelines and the amazing nltk library to manufacture the chatbot. Progressively intricate principles can be added to additionally reinforce the chatbot.

Also Read:- How to Create a Chatbot Using Dialogflow

Author Biography.

Lokesh Gupta
Lokesh Gupta

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working in the information technology and services industry. I enjoy solving complex problems within budget and deadlines putting my skills on PHP, MySQL, Python, Codeigniter, Yii2, Laravel, AngularJS, ReactJS, NodeJS to best use. Through Knowledge of UML & visual modeling, application architecture design & business process modeling. Successfully delivered various projects, based on different technologies across the globe.

Join Our Newsletter.

Subscribe to CrowdforThink newsletter to get daily update directly deliver into your inbox.

CrowdforGeeks is where lifelong learners come to learn the skills they need, to land the jobs they want, to build the lives they deserve.


CrowdforThink is a leading Indian media and information platform, known for its end-to-end coverage of the Indian startup ecosystem.


Our mission is "Har Koi Dekhe Video, Har Ghar Dekhe Video, Ghar Ghar Dekhe Video" so we Provide videos related to Tutorials, Travel, Technology, Wedding, Cooking, Dance, Festivals, Celebration.

Apna Video Wala

News & Blogs


Why Python Django Development Are Viewed As The...

Two of the most popular programming languages seem to be the Django web framework and Python prog...


How Python H2O Wave Open-Source Expansion Make ...

One of the significant things engaged with the cycle of web application improvement is the determ...


What is Python?

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics...

Top Authors

Lamia Rochdi is the Marketing Manager at Bell Flavors & Fragrances EMEA. A successful family-...

Lamia Rochdi

I’m Mertin Wilson a technician in a camera company and certified expert of different P...

Mertin Wilson

Zakariya has recently joined the PakWheels team as a Content Marketing Executive, shortly after g...

Zakariya Usman

Pankaj Singh is a Senior Digital Marketing Consultant with more than 2 years of experience in SEO...

Pankaj Singh

Our Client Says

WhatsApp Chat with Our Support Team