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 nltk.chat module to import the necessary utilities.

import nltk
from nltk.chat.util import Chat, reflections

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

Also Read:- PYTHON AND MACHINE LEARNING 2019 THE EASIEST WAY TO START FOR BEGINNERS

Output:

<class 'nltk.chat.util.Chat'>

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. 

reflections

Output:

{'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'}

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 ?",]
    ],
    [
        r"hi|hey|hello",
        ["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",]
    ],
    [
        r"quit",
    ["Bye, take care. See you soon :) ","It was nice talking to you. See you soon :)"]
],
]

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.

Output:

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)
print(chat)

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

Output:

<nltk.chat.util.Chat 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.

chat.converse()
if __name__ == "__main__":
    chatbot()

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

Output:

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.

Output:

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

Output:

Conclusion

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.

CrowdforJobs is an advanced hiring platform based on artificial intelligence, enabling recruiters to hire top talent effortlessly.

CrowdforJobs

CrowdforApps brings to you the well researched list of the most successful and finest App development companies, Web software developers.

CrowdforApps

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

CrowdforGeeks

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

CrowdforThink
CFT

News & Blogs

de4044db43e075239a86bfe4dcabd2a8.png

What is Python?

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

b41ea37095fa431a92ab1284ac143480.jpg

PYTHON AND MACHINE LEARNING 2019 THE EASIEST WA...

Python and Machine Learning is a really fun and rewarding language to learn, and anyone can get t...

Top Authors

Hey, I am Suraj - a full-time blogger and a social media expert currently working on the Growth H...

Suraj Kumar

With good communication and writing skiils, Astha Sharma is a full-time content writer working wi...

Astha Sharma

Overall 3+ years of experience as a Full Stack Developer with a demonstrated history of working i...

Lokesh Gupta

Aditya Sehgal is a recognised financial adviser, tech and gadget writer and blogger. Still he has...

Aditya Sehgal
CFT

Our Client Says

WhatsApp Chat with Our Support Team