Make a Simple Comand BOT Python
So how many times do you ask Siri, Alexa, or Google for the weather or the time? Several kinds of bots are now in the market, some more complex capable of having a continuous conversation, and other ones that just do some pre-program actions. In this post, I will show you how to build a bot for pre-trained specific actions, you can use to check your stocks, time or weather to even web scrapping. If you are interested in this kind of technology I recommend starting with this simple one.
So now we will create our “dataset” but first, let's define some concepts:
Intents: This is defined as the interaction between the bot and the human. It is formed by patterns, tags, and responses.
Patterns: Are the phrases or keywords the human could say to make a process.
Tags: Is the name to the process we want to be executed
Responses: Can be one or more responses that we want our bot to give.
For example a greetings intent:
So what we need:
1. A yml file with all the intents we want our bot to do. If you don’t know what a yml is please refer here.
2. Python 3 or higher
3. Tensorflow, pickle, and nltk libraries.
I will divide these into 3 main parts o classes. The bot intent reader, the bot model, and the bot loader or production use. These are the import we need:
The Bot Intent Reader
This class is made for reading the yml file with the intents. We are going to need 4 outputs for our model the lemmatizer, the words, the classes, and the intents. Our model is really simple is a close classifier in which each tag is a class, and the input is the words tokenized and lemmatized. Finally, we want to know which of all the commands or actions our bot should do. So our classes are our tags. Then we need the tokenized and cleaned words, for this, we need a lemmatizer and a word tokenizer, both from the nltk library. For this, we create a class name BotIntentsReader.
So we added an extra output called documents, which is only a union of tokenized words (before lemmatized) and the tags. This variable will be the one we iterate to train our model.
The Bot Model
The input for our model will be a list of tokenized words for the pattern, we will lemmatize each word to create a base word, in an attempt to represent related words, then create our bag of words array with 1, if the word match found in current pattern and if not. For the output, we will create a binary matrix to represent each tag. We will do all these in a method call order_data (inside the class BotModelCreator), which will be executed in the init (when the class is created). The second part and many the most interesting part is the model architecture. The input of our model is going to be the bag of words. In the case of the greetings intent is 23 (total of different words). And the output of the possible tags (2 in this case) that we had. For this model, we will use a simple NN, but you can try deeper ones or another architecture of your choice. The architecture is two dense layers 32 and 16 with relu activation and dropout of 0.5
Now that we have everything from loading to training just train our bot. First, we load all the models and create our bot data then we create our model. I left the verbose by default to 0 (if you want to log the training information change it to 1 or 2). So now that we have our model train we need to save all the model files need it to use our bot.
Now we have the files, the model trained but what now? How can we interact with our bot? For this matter, we need to create a new class that can load from files or passes the bot_model object to predict the tag. Finally, we need a method that uses text as input and returns the answer from our bot. I added protection that if you send a sentence that doesn't have a seen word (is in our bag of words) the bot we respond ”I don’t understand you, can you repeat”.
So for testing, we only need to create a bot loader object and pass some text. Now the only thing we need to do is add more intents with their tags, patterns, and responses to make our bot “smarter”.
My name is Sebastian Correa here is my web page if you wanna see more of my projects.