In this article, you will see that how you can create your own chatbot in Python language. Before getting started to coding part let me give you a quick introduction to what is a chatbot and why it is so relevant in business.
What is a Chatbot?
A chatbot os computer program that mimics human conversation in the international format using AI technique such as natural language processing.
If you see in today's business world chatbot is the major talking point why it is so relevant in business. We are living in an era where customers are more proficient in using modern technologies and they are always connected to social platforms through their Smart mobile devices. So the arrival of chatbots has opened up a new scope in customer engagement. This new generation of customers always expects the real-time response to their queries and also they demand more personalized suggestion from the brand.
So in order to satisfy these kinds of customer demands and to address the increasing competition in the business space businesses are now focusing on building a chatbot that can manage customer queries round the clock without any delay.
In this tutorial, we will use a Python Package called chatterbot. So you have to install this package chatterbot first. To install chatterbot type the command in terminal or command prompt given below-
and press enter. We are also going to use chatterbot corpus for this chatbot. You can download this from GitHub.
Download chatterbot corpus here.
Download Zip file from above link and extract it in the same folder where you are going to create your chatbot file. You will see inside chatterbot-corpus folder there will be a directory named data and inside this, you will see multiple directories naming multiple languages. Inside these directories, we have data of conversation which we will use to train our chatbot.
Let's start the coding part. Follow step by step to create your own chatbot in Python-
IMPORTANT NOTE: Please write your own code instead of copying the code from here because you may face indentation problem in copying.
1- first of all we need to import chatter bot packages
2 - Next we need to define our bot. You can name it any name you want to. I am going to name it simply as a bot and next I need to set my trainer to train my bot.
3 - Next we need to load chatterbot corpus files that we have already downloaded in our local system. Copy the path of English data files and next, we need to open data so we will use the same part to open data with concatenated files iterator so that we will have the entire path to our files.
and after that, we need to train out bot using bot.train() with the data we have loaded into the script-
4 - When the training is completed we need to get the inputs from the user by using input function and labeling it as you.
Next the reply will be generated from thebot using bot.getresponse() function of the message which the user enters.
Next, we need to print the reply from chatbot.
To end this conversation we will create a condition when the message entered by the user is equal to bye then beak the conversation by printing bye.
we will also create an if condition before chatbot reply. That it only start the conversation if the user's message is not bye.
save the complete code in a file and execute it. you will see that list trainer that we have set to train our bot is now training the bot using the data which have used in the script.
You can also create your own corpus like a collection of files which has a conversation, which has a dialogue in different topics and use that dialogue to train your chatbot.
Below is the conversation with the chatbot I have created-
Download the complete code here
You will see that each time you execute this file it get trained first. But this is only needed when you have a new set of data. But you have fixed set of data so you only need to train once.
Go to the directory where you have saved this file and you will see that there is also another file named db.sqlite3 which contain all the training you have done.
So instead of training, again and again, train only once. To do this run the above file only once and create a new file and write lines given below -
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
bot = ChatBot('Bot')
while True:
message=input('You:')
if message.strip() != 'Bye':
reply=bot.get_response(message)
print('ChatBot:', reply)
if message.strip() == 'Bye':
print('ChatBot : Bye')
break
save this file in the same directory and run this and you will see that chatbot is working without training. See it here -
You can see that chatbt has started working withot training.
If you are facing any problems. Please feel free to comment below.
What is a Chatbot?
A chatbot os computer program that mimics human conversation in the international format using AI technique such as natural language processing.
If you see in today's business world chatbot is the major talking point why it is so relevant in business. We are living in an era where customers are more proficient in using modern technologies and they are always connected to social platforms through their Smart mobile devices. So the arrival of chatbots has opened up a new scope in customer engagement. This new generation of customers always expects the real-time response to their queries and also they demand more personalized suggestion from the brand.
So in order to satisfy these kinds of customer demands and to address the increasing competition in the business space businesses are now focusing on building a chatbot that can manage customer queries round the clock without any delay.
In this tutorial, we will use a Python Package called chatterbot. So you have to install this package chatterbot first. To install chatterbot type the command in terminal or command prompt given below-
pip install chatterbot
and press enter. We are also going to use chatterbot corpus for this chatbot. You can download this from GitHub.
Download chatterbot corpus here.
Download Zip file from above link and extract it in the same folder where you are going to create your chatbot file. You will see inside chatterbot-corpus folder there will be a directory named data and inside this, you will see multiple directories naming multiple languages. Inside these directories, we have data of conversation which we will use to train our chatbot.
Let's start the coding part. Follow step by step to create your own chatbot in Python-
IMPORTANT NOTE: Please write your own code instead of copying the code from here because you may face indentation problem in copying.
1- first of all we need to import chatter bot packages
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer
import os
2 - Next we need to define our bot. You can name it any name you want to. I am going to name it simply as a bot and next I need to set my trainer to train my bot.
bot = ChatBot('Bot') bot.set_trainer(ListTrainer)
3 - Next we need to load chatterbot corpus files that we have already downloaded in our local system. Copy the path of English data files and next, we need to open data so we will use the same part to open data with concatenated files iterator so that we will have the entire path to our files.
and after that, we need to train out bot using bot.train() with the data we have loaded into the script-
path = 'C:/Users/Niteesh Kumar/Desktop/chatbot/chatterbot-corpus-master/chatterbot_corpus/data/english/'
for files in os.listdir(path): data = open(path + files, 'r').readlines() bot.train(data)
4 - When the training is completed we need to get the inputs from the user by using input function and labeling it as you.
Next the reply will be generated from thebot using bot.getresponse() function of the message which the user enters.
Next, we need to print the reply from chatbot.
To end this conversation we will create a condition when the message entered by the user is equal to bye then beak the conversation by printing bye.
we will also create an if condition before chatbot reply. That it only start the conversation if the user's message is not bye.
while True: message = input('You:') if message.strip() != 'Bye': reply = bot.get_response(message) print('ChatBot :', reply) if message.strip() == 'Bye': print('ChatBot : Bye')
break
save the complete code in a file and execute it. you will see that list trainer that we have set to train our bot is now training the bot using the data which have used in the script.
You can also create your own corpus like a collection of files which has a conversation, which has a dialogue in different topics and use that dialogue to train your chatbot.
Below is the conversation with the chatbot I have created-
Download the complete code here
You will see that each time you execute this file it get trained first. But this is only needed when you have a new set of data. But you have fixed set of data so you only need to train once.
Go to the directory where you have saved this file and you will see that there is also another file named db.sqlite3 which contain all the training you have done.
So instead of training, again and again, train only once. To do this run the above file only once and create a new file and write lines given below -
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
import os
bot = ChatBot('Bot')
while True:
message=input('You:')
if message.strip() != 'Bye':
reply=bot.get_response(message)
print('ChatBot:', reply)
if message.strip() == 'Bye':
print('ChatBot : Bye')
break
save this file in the same directory and run this and you will see that chatbot is working without training. See it here -
You can see that chatbt has started working withot training.
If you are facing any problems. Please feel free to comment below.
Nowadays, Chatbot is another medium for your business to grow. People Spend More Time On Messaging Apps Than On Social Media. Over 1 Billion users use messaging apps, your business needs a new dimension. chatbot Malaysia price
ReplyDelete
ReplyDeleteNice post,thanks for posting
Python training course in delhi
Python training course in Noida
This article provided me with a lot of useful information about Coding Institute in Delhi. The material you presented during this post provided me with some excellent information. Continue to post.
ReplyDeleteIts great to see that you not only recovered your investment but made a decent sum out of it.Also, well said about the Chrome extension for the “Gardening Niche” Its all about creativity.I too hope to plan my projects for long haul just as yours instead of mulling over short term chases.Thanks again.
ReplyDeleteJayme Silvestri