The Chatbot works based on DNN(Deep Neural Network) to identify the patterns of sentences given by the user as input and pick a random response related to that query. The NLTK Library in Python has functions that help to figure out the most relevant words from a sentence or paragraph and stem the words into their root meaning and can reduce them,(for instance, the root meaning or stem of the word 'going' is 'go'). This process is known as Stemming. The words are then converted into their corresponding numerical values since the Neural Networks only understand numbers. The process of converting text into numerical values is known as One-Hot Encoding. When the data preprocessing is completed we'll create Neural Networks using 'TFlearn' and then fit the training data into it. After the successful training, the model is able to predict the tags that are related to the user's query.
Installing Libraries using pip
First, you need to install NLTK, TFlearn, and Tensorflow.
$ pip install nltk tensorflow tflearn
After the installation, you may want to download the 'Punkt' model from NLTK corpora.
Then we need a file 'intents.json' which is the data used to train our Neural Network.
Here each intent contains a tag, patterns, responses, and context. Patterns are the data that the user is more likely to type and responses are the results from the chatbot. This data file above only contains a very little amount of data. So to alter this chatbot as you like, provide more tags, patterns, and responses for the way how you want it to do.
Now we can load the 'intents.json' file and start the process.
Here we loaded the 'intents.json' file and retrieved some data. Now it's time to start the data preprocessing.
We stemmed the words and also removed the duplicate words from the list of words. Here the Lancaster Stemmer algorithm is used to reduce words into their stem.
One-Hot Encoding and preparing training data-
The resulting training and output data are One-Hot encoded. Literally, the words are converted into a form of ones and zeros which are then appended to the training list as well as the output list and then converted to NumPy arrays.
Training the Neural Network
First, we need to create our model using Neural Networks.
The first layer is the input layer with the parameter of the equal-sized input data. Then the middle three are the hidden layers that are responsible for all the processing of the input data. The output layer gives the probabilities of different words there in the training data.
The training data is fitted into the model and set the epochs to 500 in which the training will continue until it reaches 500 iterations. Lastly, we saved the model with the TFlearn extension.
Remember, we trained the model with a list of words or we can say a bag of words, so to make predictions we need to do the same as well. Now we can create a function that provides us a bag of words for our model prediction.
This function helps to create a bag of words for our model, Now let's create a chat function that ties all this together.
In this chat function, we have only a few things to notice. First, the model predicts the results using the bag of words and the user input, Then it returns a list of probabilities. Among the probabilities, the highest number is more likely to be the result the user is expecting. So we are selecting the index of highest probability and finding the tag and responses of that particular index. Then we can pick some random responses from the list of responses.
Here the chatbot can actually identify the pattern of the user input and can respond according to that. You can add more tags, patterns, responses, and intents to make the bot more user-friendly.
The final version of the bot
Great! we have created our own AI chatbot. Now we can make some changes in the code since whenever you run this code it will always train the model continuously. We don't want to do that, So using some try & except blocks and saving our trained model and training data in a pickle file will help us to load the trained model instead of training it again. This avoids frequent training of our Neural Network.
You may also like: Build your own Spam Classifier using Naive Bayes - Machine Learning.