A chatbot enables businesses to put a layer of automation or self-service in front of customers in a friendly and familiar way. A chatbot can work alongside a knowledge base, deliver personalized responses, and help customers complete tasks.
Currently, chatbots, or digital assistants, use natural language processing to communicate with humans. Known as NLP, this technology focuses on understanding how humans communicate with each other and how we can get a computer to understand and replicate that behavior. It is expected that in a few years chatbots will power 85% of all customer service interactions.
There are generally two types of bots: Artificial Intelligence (AI) and Machine Learning (ML) chatbots.
Rule-based chatbot: A rule-based bot can only comprehend a limited range of choices that it has been programmed with.
AI-based chatbot: It is trained using machine-learning algorithms and can understand open-ended queries.
Chatbots work by adopting three classification methods:
The bot uses pattern matching to classify the text and produce a response for the customers. A standard structure of these patterns is “AI Markup Language”.
Algorithms reduce the number of classifiers and create a more manageable structure. Some of the examples are naïve Bayes, decision trees, support vector machines, Recurrent Neural Networks (RNN), Markov chains, etc.
Neural networks calculate the output from the input using weighted connections. They are computed from reputed iterations while training the data.
ChatterBot is a Python library used to create chatbots that generate automated responses to users’ input by using machine learning algorithms.
You can install ChatterBot using the pip command.
1 2
pip install chatterbot pip install chatterbot_corpus
We import the modules which we will be using in our chatbot.
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot import ChatBot
The next step is to give a name to our chatbot. You can use any name of your choice. For example, I am giving the name “My Bot”.
1 2 3
from chatterbot import ChatBot chatbot = ChatBot("My Bot")
The main step is to train the chatbot. TheChatterBot Corpus contains data that can be used to train chatbots to communicate. I am giving the language as “English”.
1 2 3 4
bot_trainer = ChatterBotCorpusTrainer(chatbot) bot_trainer.train( "chatterbot.corpus.english" )
The last step is to test the bot. For doing so, we are creating a while loop that takes the input from the user and quits when the user enters ‘quit’. We then get the response using the method get_response and print it.
1 2 3 4 5 6
print("enter 'quit' to stop") while True: user_input = input("You: ") if user_input == 'quit': break print("Bot:", chatbot.get_response(user_input))
Output:
The list trainer takes a list of statements that represent a conversation. The following example uses the list trainer.
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
from chatterbot.trainers import ListTrainer from chatterbot import ChatBot chatbot = ChatBot("New_Bot") conversation = [ "Hello", "Hi, there!", "How are you doing?", "I'm doing great.", "That is good to hear", "how can i help you?", "Thank you.", "You're welcome." ] trainer = ListTrainer(chatbot) trainer.train(conversation) print("enter 'quit' to stop") while True: text_input = input("You: ") if text_input == 'quit': break print("Bot:", chatbot.get_response(text_input))
Output:
Example 2:
In this example, I am using a text file that I have taken as a sample from a website. The data set can be used to create a conversational chatbot.
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
!pip install chatterbot !pip install chatterbot_corpus from chatterbot import ChatBot from chatterbot.trainers import ListTrainer from chatterbot.trainers import ChatterBotCorpusTrainer bot = ChatBot( 'AIBot', storage_adapter = 'chatterbot.storage.SQLStorageAdapter', database_uri = 'sqlite:///database.sqlite3' ) #opening the text file example1 = "/content/dataset2.txt" file1 = open(example1, "r") training_data = file1.read() .splitlines() #training the bot trainer = ListTrainer(bot) trainer.train(training_data) #testing the bot print("enter 'quit' to stop") while True: user_txt = input("You: ") if user_txt == 'quit': break print("Bot:", bot.get_response(user_txt))
Output:
That was the basic knowledge of how to create a chatbot using ChatterBot. We have learned how to create a new chatbot by creating an instance of ChatBot. Finally, we test if our chatbot is working properly. In summary, ChatterBot is a library of Python that helps us create software that can engage in a conversation. The simple process is shown below in the diagram: