I am reading many articles to make a chatbot in python but all of the ways are too difficult that’s why I write this article to everyone build a chatbot in python with a very easy method.
Why we make chatbot using python?
past few years, chatbots in python have becomes wildly popular in the tech and business sectors because 50% of the tasks are fulfilled by chatbots. These intelligent chatbots adapt the human’s natural languages and converse with humans. Nowadays all companies use chatbots to provides the services like customer support, information, etc. Now you see examples like Siri, Alexa becomes clear chatbots are important in our daily lives and the various industrial sectors companies adopting them. In this article, we will learn how to make a chatbot using python with Chatterbot library which has many Machine learning algorithms to generate a response like NLP (Natural language Processing) for building a bot or chatbot. Now you can see the chatbot are everywhere like banking website, every store website, e-commerce shopping stores.
What is a Chatbot?
A chatbot is an AI-based software designed to interact with humans in their NLP (Natural language Processing). it is looked upon as a virtual assistant that communicates with users via text messages or speech and helps businesses in getting close to their customers. The Famous examples I have already told you to include Siri, Alexa and chatbots are doing many tasks like making a transaction, booking a hotel, form submissions, etc. Almost 30% of tasks are performed by chatbots in the company Nowadays many companies provide chatbots services to integrate with your website.
How To Install ChatterBot In Python?
The Most recommended: we make a virtualenv to remove the confliction of packages. Here is a command to install and make an environment. Now I am using Python3.6
pip install virtualenv python3.6 -m virtualenv myenv
After installation activate a environment.
#ubuntu,liunx source env/bin/activate #window venv\Scripts\activate
The first step creating a chatbot in python with the python chatbot library ChatterBot see the PyPI package and install the ChatterBot library in your system. After that install a Chatterbot corpus. the corpus is a collection of words. This contains a corpus of data that is included in the ChatterBot module. These corpus are used by bots to train themselves. After installing the chatterbot and chatterbot corpus. otherwise, you can get the complete python chatbot source code on Github but the source code of GitHub is too difficult to understand for beginners.
How to make a chatbot in python?
Here is a installation command of Chatterbot and chatterbot_corpus.
pip install chatterbot pip install chatterbot_corpus
Now your setup is ready we can move on to the next setup import the Chatbot class of the Chatterbot module and create a chatbot instance and train a chatbot.
There are many types of logical adapters in chatbot object. the MathematicalEvaluation adapter helps the chatbot to solve the math problems and BestMatch helps to choose the best match from the list of responses already provided. Since you have to provide a list of responses using ListTrainer to train your chatbot and find the best match of each query. it is a simple chatbot in python.
Read More : Model queries work in Django
Read More : Django Admin Full Customization step by step
A) Chatbot with Single ListTrainer
List Trainer used for when you train a List data.
from chatterbot import Chatbot from chatterbot.trainers import ListTrainer bot = ChatBot( 'Jarvis', logic_adapters=[ 'chatterbot.logic.MathematicalEvaluation', 'chatterbot.logic.BestMatch', 'chatterbot.logic.TimeLogicAdapter'], ) trainer = ListTrainer(bot) trainer.train([ 'hi there', 'hi', 'how are you', 'i am cool', 'fine you?', 'always cool', 'what is your name', 'I am AI chatbot and my name is Jarvis', 'Hi', 'Hello', 'I need your assistance regarding my order', 'Please, Provide me with your order id', 'I have a complaint.', 'Please elaborate, your concern', 'How long it will take to receive an order ?', 'An order takes 3-5 Business days to get delivered.', 'Okay Thanks', 'No Problem! Have a Good Day!' ]) response = bot.get_response('I have a complaint.') print("Bot Response:", response) #Bot Response: Please elaborate, your concern
when I run the chat.py file then the spacy python package error comes. these types of errors depend on your OS. Right now I am working on Linux. If an error comes on your side then run this command otherwise no need for this.
command to install spacy:
pip install spacy
After the spacy package installation runs again chat.py the new error comes ( Can’t find model ‘en’ ). there are two types of errors.
- en error
- en_core_web_sm error
The error depend on python and chatterbot version. if your chatterbot version is old then en error comes and version is latest then en_core_web_sm error comes.
I found many solutions everywhere like Git, Stackoverflow but no one fixes this. after that I fixed the error.
if error is en : python -m spacy download en
if error is en_core_web_sm : python -m spacy download en_core_web_sm
In my case error is en_core_web_sm. how will fixed this see below:
- python -m spacy download en_core_web_sm
- go to env/lib/python3.6/site-packages/chatterbot/languages.py
- change ISO_639_1 = ‘en’ to ISO_639_1 = ‘en_core_web_sm’
- Install nltk (pip install nltk) for TimeLogicAdapter
- Run chat.py successfully and return a response.
B) Chatbot with multiple List Trainer:
In this case we train a multiple lists .
from chatterbot import Chatbot from chatterbot.trainers import ListTrainer bot = ChatBot( 'Jarvis', logic_adapters=[ 'chatterbot.logic.MathematicalEvaluation', 'chatterbot.logic.BestMatch', 'chatterbot.logic.TimeLogicAdapter'], ) list_1 = ['hi there','hi'] list_2 = ['are you stupid','No I am Intelligent'] list_3 = ['what you eat','People Mind !'] trainer = ListTrainer(bot) for item in (list_1,list_2,list_3): trainer.train(item) response = bot.get_response('what you eat') print("Bot Response:", response) #Bot Response: People Mind !
No need to train your chatbot again and again with the same data train only one time if you need to add more new data then train again see below train your chatbot the first time and communicate with a chatbot.
from chatterbot import Chatbot from chatterbot.trainers import ListTrainer bot = ChatBot( 'Jarvis', logic_adapters=[ 'chatterbot.logic.MathematicalEvaluation', 'chatterbot.logic.BestMatch', 'chatterbot.logic.TimeLogicAdapter'], ) trainer = ListTrainer(bot) trainer.train([ 'hi there', 'hi', 'how are you', 'i am cool', 'fine you?', 'always cool', 'what is your name', 'I am AI chatbot and my name is Jarvis', 'Hi', 'Hello', 'I need your assistance regarding my order', 'Please, Provide me with your order id', 'I have a complaint.', 'Please elaborate, your concern', 'How long it will take to receive an order ?', 'An order takes 3-5 Business days to get delivered.', 'Okay Thanks', 'No Problem! Have a Good Day!' ]) name=input("Enter Your Name: ") print("Welcome to the Bot Service! Let me know how can I help you?") while True: request=input(name+':') if request=='Bye' or request =='bye': print('Bot: Bye') break else: response=bot.get_response(request) print('Bot:',response)
However, the chatbot using python might not know how to answer all your questions. Chatbot knowledge and training are still very limited, you have to give it time and provide more training data to train it further.
C) Train your Python Chatbot with a Corpus of Data using ChatterBotCorpusTrainer
I have already told you what is chatbot corpus. the corpus is a collection of data. In ChatterBot have different languages corpus.
from chatterbot import ChatBot from chatterbot.trainers import ChatterBotCorpusTrainer bot = ChatBot("Jarvis", storage_adapter="chatterbot.storage.SQLStorageAdapter") trainer = ChatterBotCorpusTrainer(bot) trainer.train("chatterbot.corpus.english") userText = "What is AI?" print(bot.get_response(userText)) #Artificial Intelligence is the branch of engineering and science devoted to constructing machines that think.
if you want to see what is in the English corpus go to your environment path like env/lib/python3.6/site-packages/chatterbot_corpus/data/ and here many folders see in the screenshot below:
When you write trainer.train(“chatterbot.corpus.english”) then ChatterBot go to data inside English folder and train every file they exist in a folder and all file extension is yml that means .yml is a better file extension for python chatbot.
Mostly we use a chatbot in the website and website make in a framework like Django. if you want to train your service-related question-answer custom file in chatbot you have many ways to train your file but is the simple way is to go data inside any language folder and paste your custom.yml file and train your chatbot again and the custom.yml file look like see:
for example my custom.yml file in data/english folder then I train my chatbot with trainer.train(“chatterbot.corpus.english”).
If you want to brief description about how to train your custom file in python chatbot, how to connect MySQL DB in Chatbot, how to customize Django ChatterBot, how to return the custom response from the chatbot package. all of the things I will cover in the Django chatterbot article the article is coming soon.
Congratulations, you have made it to the end of this tutorial!
This article was based on learning how to make a chatbot in Python by getting python chatbot source code but Chatbot with ChatterBot was not only simple but in this article, you learn advanced topic very easily, the results were accurate. With Artificial Intelligence and Machine Learning, in advancement, everything and anything is possible to achieve whether it is creating bots with conversational skills like humans or be it anything else.
Chabot in Python is completed if you like the article share it with your friends. if you have any question or query related to the article and have any suggestions please leave in the comment box.
Thanks for reading the Article.