Chatterbot: create a chatbot in python

Chatbots are a technology that allows you to automate interaction with users. Leveraging the latest artificial intelligence technologies, conversations turn out to be more and more real. Examples of chatbots evolution are virtual assistants like Alexa, Cortana and Siri. Let's find out how you can develop a simple chatbot in Python using the Chatterbot library.

Share

Reading time: 6 minutes

In recent years, chatbots have become very popular in many industries. For example, many e-commerce sites integrate chatbots to provide information to customers regarding products to enhance their shopping experience. They are also used to provide technical support for issues related to the use of software and/or services.

But what is a chatbot? A chatbot is software designed to interact with humans using natural language. The user, by typing or recording a message, can request some information without having to worry about filling out forms or using complex interfaces. Bots are developed in such a way that they imitate human language and simulate real conversations.

In this article, we will learn how to develop a simple chatbot in Python. However, before we delve into the code, we need to make a small premise about chatbots to understand them better. 

History

The first chatbot dates back to 1966 when Joseph Weizenbaum created ELIZA that could mimic the language of a psychotherapist in just 200 lines of code. It used simple pattern matching and a template-based response mechanism. Its conversational capabilities were limited, but for those days it gave users the impression that they were conversing with another user and not a machine. That first experiment was the beginning of the development of chatbots.

After it we can mention PARRY, developed in 1972, and ALICE that won the Loebner Award in the early 2000s. The latter was based on a simple pattern-matching algorithm developed using Artificial Intelligence Markup Language (AIML), which allowed developers to define the chatbot’s knowledge building blocks.

In recent years, large IT giants have invested considerable resources in the development of increasingly advanced chabots up to the creation of virtual assistants. The latter are not only based on typing text, but also take advantage of voice recognition algorithms to capture user requests. Among the most famous virtual assistants we can’t mention Apple Siri, Microsoft Cortana, Amazon Alexa and Google Assistant.

Types of chatbots

Chatbots are classified based on different parameters including knowledge domain, service provided, goals, and method of input processing and/or response generation.

For example, knowledge domain-based classification distinguishes chatbots based on the data context on which it operates. Chatbots belonging to an open domain may talk about general topics, while closed domain chatbots are focused on a particular knowledge domain.

Differently, if we consider the objective we can identify the following categories:

  • Informative: are designed to provide the user with information that is stored in advance or is available from a fixed source, such as chatbot FAQs.
  • Chat-based/Conversational: they talk to the user, like another human being, and their goal is to respond correctly to the sentence they have been given.
  • Task-based: they perform a specific task, such as booking a flight or helping someone. 

Finally, when considering the method of input processing and response generation, we can identify three models:

  • Rules
  • Query
  • Generative

The rule-based approach instructs a chatbot to answer questions based on a set of rules. The complexity of the rules can vary depending on the context and are based on recognizing the lexical form of the input text. The knowledge used is human-coded by hand and is represented using conversational patterns. 

Since it is a simple model, it can be easily used and implemented for technical support tools.  However, this model has limitations. For example, it is not robust to spelling and grammatical errors in user input. In addition, the response selection usually considers only the last entered message and not the context of the entire conversation.

In contrast, the query-based model offers more flexibility as it analyzes available resources via APIs. In fact, the chatbot retrieves some candidate answers from an index before applying the matching approach to select the most appropriate answer.

Finally, the generative model is based on artificial intelligence and deep learning algorithms. The answers it generates are usually better than the previous models, but it poses some difficulties especially related to the learning phase of the models. 

Chatterbot

ChatterBot is a Python library designed to provide automated responses. It uses a combination of Machine Learning (ML) algorithms to generate different types of responses. In this way, developers can build chatbots to automate conversations with users by leveraging artificial intelligence in order to provide a better user-experience. 

How does it work?

When a user enters a specific input into the chatbot, the bot saves the input along with the response, for future use. This data allows the chatbot to generate automatic responses whenever a new input is entered.

The program chooses the most suitable response from the closest statement that matches the input, and then provides a response from the already known selection of statements and responses. Over time, as the chatbot has more interactions, the accuracy of the response improves. Below is a diagram of the flow of operations underlying the library. 

Chatbot development

The chatbot is usually integrated into a larger application such as a website or an app. In this article we won’t cover the various types of integration, but we’ll just illustrate how to create the chatbot core. For this example, we recommend that you create your own development environment using pipenv. This way, you’ll only install the libraries you need for your project in a dedicated workspace and not at the operating system level.

To build a chatbot, you must remember that you will be analyzing textual data. Therefore, when working with this type of data you need to perform pre-processing before you can use it in any Machine Learning model. For example, tokenization helps fragment text into smaller, more readable chunks, words, while stemming reduces inflected forms of a word to the root.

Installation

As mentioned above, we recommend that you create your own development environment with pipenv. Then you can install the library by typing the following commands.

pip install chatterbot
pip install chatterbot_corpus 

In case you want to update them you can use the commands:

pip install --upgrade chatterbot_corpus
pip install --upgrade chatterbot 

Import library

You can use the python or ipython shel for the next steps. Otherwise you can create a .py file where you will write the code of your chatbot.

In any case, you need to import two classes: ChatBot from chatterbot and ListTrainer from chatterbot.trainers. To do this, you can run the following command:

from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer 

Chatbot creation and training

The chatbot will be an instance of the ChatBot class. After creating a new ChatterBot instance, you can train the bot to improve its performance. Training ensures that the bot has enough knowledge to start with specific responses to specific inputs. The commands to execute are as follows.

my_bot = ChatBot(name='PyBot', read_only=True,
                 logic_adapters=['chatterbot.logic.MathematicalEvaluation',
                                 'chatterbot.logic.BestMatch']) 

The only argument required is the name. If you want to disable the bot’s ability to learn after training (i.e. from real conversations) you need to enter the read_only=True parameter. The logic_adapters parameter indicates the list of adapters used to train the bot. There are several provided by chatterbot, including chatterbot.logic.MathematicalEvaluation which allows the bot to solve mathematical problems, and chatterbot.logic.BestMatch which chooses the best match between the answers already provided.

Since we want to use BestMatch, we need to provide a list of answers. You can do this by specifying lists of strings that will later be used both to train the chatbot and to find the best match for each query. Here is an example of answers:

small_talk = ['hi there!',
              'hi!',
              'how do you do?',
              'how are you?',
              'i\'m cool.',
              'fine, you?',
              'always cool.',
              'i\'m ok',
              'glad to hear that.',
              'i\'m fine',
              'glad to hear that.',
              'i feel awesome',
              'excellent, glad to hear that.',
              'not so good',
              'sorry to hear that.',
              'what\'s your name?',
              'i\'m pybot. ask me a math question, please.']
math_talk_1 = ['pythagorean theorem',
               'a squared plus b squared equals c squared.']
math_talk_2 = ['law of cosines',
               'c**2 = a**2 + b**2 - 2 * a * b * cos(gamma)'] 

We can now create and train the bot using an instance of ListTrainer and providing it with the lists of strings defined earlier.

list_trainer = ListTrainer(my_bot)
for item in (small_talk, math_talk_1, math_talk_2):
    list_trainer.train(item) 

At this point, the bot is trained and ready to communicate.

Communicating with the bot

To interact with the chatbot, you can use the .get_response() function. Below is an example of communication in the python shell.

>>> print(my_bot.get_response("hi"))
how do you do?

>>> print(my_bot.get_response("i feel awesome today"))
excellent, glad to hear that.

>>> print(my_bot.get_response("what's your name?"))
i'm pybot. ask me a math question, please.

>>> print(my_bot.get_response("show me the pythagorean theorem"))
a squared plus b squared equals c squared.

>>> print(my_bot.get_response("do you know the law of cosines?"))
c**2 = a**2 + b**2 - 2 * a * b * cos(gamma) 

However, it is essential to understand that the chatbot may not know how to answer all your questions. In fact, its knowledge is very limited. You need to give it time and provide more data to train it and improve its performance accordingly.

Train the chatbot with a corpus

To provide more data to the chatbot, you can either use your own data or an existing corpus. In the latter case, you can use the corpus provided directly by chatterbot. ChatterBot offers this functionality in several languages. So, you can also specify a subset of a corpus in a language you prefer. Here’s how to train the chatbot using the existing corpus.

from chatterbot.trainers import ChatterBotCorpusTrainer

corpus_trainer = ChatterBotCorpusTrainer(my_bot)
corpus_trainer.train('chatterbot.corpus.english') 

Conclusions

What we have outlined in this article is just one of the many ways to make a chatbot in Python. You can also use NLTK, another resourceful Python library to create a Python chatbot. There are several other solutions. If you want to learn more about chatbots I recommend reading the articles on chatbotslife.

Recommended Readings

More To Explore

Artificial intelligence

Gradio: web applications in python for AI [part1]

Writing web applications for our machine learning and/or artificial intelligence models can take a lot of time and skills that we do not possess. To streamline and speed up this task we are helped by Gradio, a Python library designed to create web applications with just a few lines of code. Let’s discover its basic functionality with some examples.

Artificial intelligence

AI: the best prompt techniques for leveraging LLMs

Prompt techniques are the basis for the use of LLMs. There are several studies and guidelines for obtaining the best results from these models. Let us analyze some of them to extract the basic principles that will allow us to obtain the desired answers according to our task.

Leave a Reply

Your email address will not be published. Required fields are marked *

Design with MongoDB

Design with MongoDB!!!

Buy the new book that will help you to use MongoDB correctly for your applications. Available now on Amazon!