How to create a chatbot in Python
Natural language processing (NLP) is one of the most promising fields of artificial intelligence that uses natural languages to enable human interactions with machines.
There are two main approaches to NLP:
– rule-based methods,
– statistical methods, i.e., methods related to machine learning.
There are several exciting Python libraries for NLP, such as Natural Language Toolkit (NLTK), spaCy, TextBlob, etc.
A chatbot is a computer software able to interact with humans using a natural language. They usually rely on machine learning, especially on NLP. Apple’s Siri, Amazon’s Alexa, Google Assitant, and Microsoft’s Cortana are some well-known examples of software able to process natural languages.
This article shows how to create a simple chatbot in Python using the library ChatterBot. Our bot will be used for small talk, as well as to answer some math questions. Here, we’ll scratch the surface of what’s possible in building custom chatbots and NLP in general.
You’re only going to install the library ChatterBot for now. I recommend creating and using a new Python virtual environment for this purpose. Execute the following commands in your Python (or Anaconda) terminal:
You can also try upgrading them:
That’s it. We’re ready to go.
You’ll need to import two classes for this purpose: ChatBot from chatterbot and ListTrainer from chatterbot.trainers:
We’re now prepared to create and train our math bot.
Creating and Training a Bot
Our bot will be an instance of the class ChatBot:
The only required argument corresponds to the parameter name. It represents the name of the bot. You can provide read_only=True if you want to disable the bot’s ability to learn after the training (i.e. from actual conversations). logic_adapters is the list of adapters used to train the bot. There are several of them provided by chatterbot, like the two from our example. chatterbot.logic.MathematicalEvaluation enables the bot to solve math problems, while chatterbot.logic.BestMatch chooses the best match from the already provided responses.
So, we have to provide responses. We do that by specifying the lists of strings later used to train the bot and find the best match for each question. This is what I want our bot to learn for now:
We can create and train the bot by creating an instance of ListTrainer and supplying it with the lists of strings:
The bot should now be trained and ready to communicate.
Communicating with a Bot
You can communicate with your bot using its method .get_response(). Here’s an example of how that might look like:
Don’t expect the bot to answer each question well! Its knowledge is limited to the stuff similar to what it has learned. Many times, you’ll find it answering nonsense, especially if you don’t provide comprehensive training.
Training a Bot with a Corpus of Data
You can use your own or an existing corpus of data to train a bot. For example, you can use some corpus provided by chatterbot:
chatterbot offers this functionality in several languages. You can also specify a subset of a corpus you’d like to use.
Now you know how to create and use a simple chat bot.
This is just a small illustration of what you can do with natural language processing and chatbots. There are many more possibilities out there. If you’re interested in exploring them, you can start by getting familiar with NLTK and ChatterBot.
Thank you for reading!
The article was prepared by our teammate Mirko.