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'}
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 ?",]
],
[
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 :)"]
],
]
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.
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.
Also Read:- The Ultimate Guide to Hiring Python Developers for Startups
Output:
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?
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.