All of you will be familiar with chatbot. Today we will learn about how to design chatbots in python. Making chatbots are very amazing.So welcome in Python Chatbot Tutorial. So let’s start without wasting time.
Now-a-days various companies,industries or individuals are using chatbots.Chatbots are very helpful tool for today’s business world.They are providing great business opportunities for small and large scale industries.It reduces the response time and increases the availability of services.So now the question is- what are chatbots,how they work and why we use them?
Contents
- 1 What Are Chatbots
- 2 Python Chatbot Tutorial – Getting Started
- 2.1 7 steps to building a chatbot
- 2.2 Creating a project
- 2.3 Creating chatbot text file
- 2.4 Installing ChatterBot package
- 2.5 Importing ChatterBot modules
- 2.6 Creating Chatbot
- 2.7 Setting Trainer to train bot
- 2.8 Opening the Conversation file
- 2.9 Train the bot
- 2.10 Taking input from the user and replying by the bot
- 3 Let’s get the Code together
What Are Chatbots
- A chatbot is a service,powered by rules and sometimes artificial intelligence,that you interact with via a chat interface.
- Chatbots are softwares agents that converse trough a chat interface,that means the softwares programs that are able to have a conversation which provides some kinds of value to the end users.
- The user can interact with the chatbot by typing in their end of the conversation or simply by voice depending upon the type of chatbot is provided.
- Chatbots are revolutionizing the way businesses interact with their clients. Using AI and sophisticated natural language processing, modern chatbots offer a deeper level of interaction than ever before.
Some examples of chatbots
- Liveperson
- LiveChat
- Amazon Lex
- Dialogflow
- IBM Watson
- bold360
What can chatbots do
- Deliver personalized content experiences.
- Answer common customer service questions.
- Streamline product purchases.
- Cultivate connection via entertainments.
- Offer specialized services.
How can chatbots improve business
Chatbots can help the business in many ways ,like –
- Drive sales
- Immediate customer services
- Lower Acquisition costs
- Automation+Efficient
- Better Interaction with consumers
- Gather data faster
Some use cases of chatbots
- YouTube celebrity/Artists
- Restaurants/cafe
- Ecommerce
- Newsletter
- Business card
- Customer service
- Quiz
- Campaign etc.
Read Also-Python Rest API Example using Bottle Framework
Python Chatbot Tutorial – Getting Started
Building chatbots in python is very easy and funny task.
7 steps to building a chatbot
- Decides on an application area
- Design conversations
- List intents, entities , actions, responses, contexts
- Train AI engines
- Write code for actions
- Create and update knowledge base
- Test scenarios and incrementally improve
Creating a project
First of all, create a new project , named it as ChatterBot or as you like. I am using PyCharm IDE , you can use anything.
Now, create a new python file by following the path – ChatterBot->Right click->New->Python File and named it as you wish.
Creating chatbot text file
Now create a text file by following the path – ChatterBot->Right click->New->File
This file contains a list of conversations but the way this file need to be created or organized by saying simple row that is each conversation must be relied on the last conversation.
Imagine a person communicate with another person like this way-
chats.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
Good morning, how are you? - I am doing well, how about you? - I'm also good. - That's good to hear. - Yes it is. - Hello - Hi - How are you doing? - I am doing well. - That is good to hear - Yes it is. - Can I help you with anything? - Yes, I have a question. - What is your question? - Could I borrow a cup of sugar? - I'm sorry, but I don't have any. - Thank you anyway - No problem - - How are you doing? - I am doing well, how about you? - I am also good. - That's good. - - Have you heard the news? - What good news? - - What is your favorite book? - I can't read. - So what's your favorite color? -Blue Who are you? - - I am working on a project - What are you working on? - I am baking a cake. - - The cake is a lie. - No it is not. The cake is delicious. - What else is delicious? - Nothing - Or something - Tell me about your self. - What do you want to know? - Are you a robot? - Yes I am. - What is it like? - What is it that you want to know? - How do you work? - Its complicated. - Complex is better than complicated. - - Complex is better than complicated. - Simple is better than complex. - In the face of ambiguity, refuse the temptation to guess. - It seems your familiar with the Zen of Python - I am. - Do you know all of it? - Beautiful is better than ugly. - Explicit is better than implicit. - Simple is better than complex. - Complex is better than complicated. - Flat is better than nested. - Sparse is better than dense. - Readability counts. - Namespaces are one honking great idea. Let's do more of those! - I agree. - - Are you a programmer? - Of course I am a programmer. - I am indeed. - What languages do you like to use? - I use Python, Java and C++ quite often. - I use Python quite a bit myself. - I'm not incredibly fond of Java |
Installing ChatterBot package
Furthermore , in your project go to File->Setting->Python Interpreter.
Search ChatterBot package and click on Install Package button.Now the package is successfully installed.
Also Read : Python Simple HTTP Server : A Simple HTTP Web Server With Python
Importing ChatterBot modules
ChatterBot is a Python library that makes it easy to generate automated responses to a user’s input.As a result, ChatterBot uses a selection of machine learning algorithms to produce different types of responses.
ChatBot
1 2 3 |
from chatterbot import ChatBot #this imports chatbot |
ListTrainer
1 2 3 |
from chatterbot.trainers import ListTrainer # method to train the chatbot |
Creating Chatbot
Now, create the chatbot.Here i have given the name of chatbot as MyChatBot.
1 2 3 |
bot = ChatBot('MyChatBot') |
- ChatBot() method is used to create the chatbot.
- the chatbot is store in the variable bot.
Setting Trainer to train bot
So,now we have to set the trainer to train the bot.
1 2 3 |
bot.set_trainer(ListTrainer) #set the trainer |
- set_trainer() method is used to set trainer to the bot.
Opening the Conversation file
Now, we have to open the file where the conversations are stored.For this we write the following code.
1 2 3 |
conversation = open('chats.txt','r').readlines() |
- open() method open the conversation file.It takes two arguments file name and file mode.
- Here we are using reading mode that is ‘r’. It opens the file in read mode.
- readlines() reads until EOF using readline() and returns a list containing the lines.
Train the bot
And now we need to train the bot with the data i have loaded into this script.
1 2 3 |
bot.train(conversation) # train the bot |
- train() method is used to train the bot along with loaded data.
- Here we need to pass the conversation as an argument.
Taking input from the user and replying by the bot
Now we have to code for taking input from user and the reply by the bot.For this we write the following code.
1 2 3 4 5 6 7 8 9 10 |
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 |
Now we understand the code line-by-line
- while True: means the training of the bot have been completed.
- message = input(‘You:’) statement is used to take input from the user.input() function takes input from the user and store it in message variable.
- Now we have to include a condition that is if message.strip()!= ‘Bye’: . It means if user don’t enter Bye message till then bot will reply the user messages.
- The reply will be generated by the bot using reply =bot.get_response(message) statement.get_response() is used to reply to the user.This method takes an argument message which is entered by the user.
- print(‘ChatBot:’,reply) statement print the reply of the chatbot.
- Now, if the user enters Bye message then chatbot also reply Bye message and the process will end.
Let’s get the Code together
Hence,the final code for building chatbot in python will be as follows-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
from chatterbot import ChatBot from chatterbot.trainers import ListTrainer bot = ChatBot('MyChatBot') bot.set_trainer(ListTrainer) conversation = open('chats.txt','r').readlines() bot.train(conversation) 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 |
Finally, now run the code and start conversation with chatbot. As a result we see the output like this-
Finally, Chatbot is working well. Creating chatbots is amazing and lots of fun.
Also Read – Speech Recognition Python – Converting Speech to Text
So, friends it was all about Python Chatbot Tutorial.I hope it will help you very much. And please comment me-have you enjoyed creating this chatbot or not.And if you are getting any difficulties then leave your comment. If you have benefited from it then must shares with your fellows.Thanks
Ok works fine, tanks.
need change this line.
bot.set_trainer(ListTrainer)
for
trainer = ListTrainer(bot)
Yes you’re right and, in addition this one too
# Train the bot
trainer.train(conversation)
i need where we create which file please explained step by step .i am not understand whats it will work ,i am new on python. i have install python and pycharm IDE.
please provide step by step explanation for simple chat bot project.