Building a simple NLP based Chatbot in Python

0
68
NLP based Chatbot

As per a Gartner research, 80% of the CEOs believe that customer service is going to be the most important factor that will differentiate a brand from its competitors. Basically, this means that the top CEOs are betting that in order to get ahead of their competitors, they need to improve their customer service. In this blog post, we will take a look at a NLP based Chatbot built from scratch in Python.

Currently, the way customer service is done is via call centers – customers can call up the call center, get on the IVR and then eventually reach a human after waiting for a long time. This is not only irritating for the customers (because they have to wait), but also expensive for the organizations because the cost of running a call center is just too high.

Companies therefore are looking for ways to improve their customer service. One of the ways they are achieving this is to go digital – driving the customers to website rather than the call center. Maintaining website is easier and far more economical as compared to management of a call center. To ensure that the customers are easily able to navigate across the pages of a website, companies have come up with an interesting tool called as a ChatBot.

ChatBot is an extension of a “chat box” where instead of a customer chatting with a human (call center or customer service representative), the customer is rather chatting with a computer that tends to understand what the customer is typing and based on that, send out a response. ChatBots are widely used by organizations like banks, insurance companies, etc owing to the fact that they have a huge number of customers and therefore, spending money on call center becomes uneconomical.

There are 2 most popular Machine Learning libraries in Python:

Keras: it is an excellent library for building powerful Neural Networks in Python
Scikit Learn: it is a general purpose Machine Learning library in Python

We will be using Keras for our purpose. Let us start writing actual code now.

Let’s get started and write actual code to build a simple NLP based Chatbot.

Here, we’ve uploaded some numpy packages as well. For instance, the argmax function in numpy helps in finding the index of the largest number in a vector.

Let us now build some dummy training data for our ChatBot. We have some sentences in the vector X. It looks like the following.

Observe that the first 3 sentences are sentences that are in greeting mood. The next 2 talk about studying mood. Finally, the last 3 talk about a bye mood. greeting, studying and bye are essentially the “intents” of these sentences. An intent indicates the “meaning” of the sentence. Corresponding to each of the above sentence in X we have an intent vector Y as follows:

The idea of a ChatBot is fairly simple – from the sentence, we identify the intent using Machine Learning. Later, for each intent, we have a predefined answer. For instance, for the intent greeting, we may have an answer – hey, what’s up? such that whenever someone mentions things related to greeting, this is the answer that is sent.

Now, the question to be answered is as follows – given a sentence, how do we identify its intent? This is a classic Machine Learning problem – sentence classification. The way it works is as follows:

  • We create a training data (X and Y as above) which contains a list of sentences along with their intents.
  • Now, a classifier is fit on this training data. This classifier could be as simple as a multi-class logistic classifier or as complex as a deep Neural Network.
  • To classify a sentence, we first convert each sentence into a vector of numbers. This is achieved by creating a frequency based encoding of the sentence. Basically, we first create a vocabulary and then map each sentence to a vector which talks about the frequency of each word.

To give you an example on the last point – suppose we have the following vocabulary: {a, the, hi, hello, you, are, hey, it, how}. In such a situation, for the sentence “hi, how are you”, the frequency based encoding would be: {0, 0, 1, 0, 1, 1, 0, 0, 1}

This indicates that the first word “a” is present 0 times, the second word “the” is present 0 times, the 3rd word “hi” is present 1 time and so on.

However, before we get into frequency based encoding, let us first do some preprocessing on the sentence by removing non alphanumeric characters:

Many other preprocessing mechanisms are applied on each sentence. For instance:

  • Convert it to all lower case.
  • Remove leading and trailing spaces.
  • Remove multiple spaces from the sentence.

Take a look at the preprocess_data function that does all of this.

Here, let us create some vocabulary for the frequency based encoding. As a simple example, we can take the vocabulary as the union of all words present across all the sentences in the training data.

Now, let us encode each of the sentence present in the training data as per the frequency based encoding.

Similarly, let us also encode the labels (the intents). For instance, let us call greeting as [1, 0, 0], studying as [0, 1, 0] and bye as [0, 0, 1].

Let us now prepare our data for training by a simple Neural Network written in keras. The train and the test data can (should) be separate.

Here, we are building a simple sequential Neural Network model with sigmoid activation. The hidden layer here comprises of 64 nodes. We are using categorical cross entropy as the cost function.

Let us now generate the predictions of the model.

Finally, let us measure the accuracy of the predictions on the test data.

Finally, let us create a ChatBot prompt where the user will be able to type in a sentence which the ChatBot understands and prints the intent. Eventually, we can map the intent to a ChatBot reply which can be sent out to the user.

The above simple code for ChatBot gives an accuracy of over 90%. The following activities could be performed to increase the number:

  • Training on more data: this is by far the best method to increase accuracy of a ChatBot. the more the data the ChatBot sees, the better is it able to learn and generalize, resulting in higher accuracy.
  • Using a deeper Neural Network: the deeper the network is, the more the features that can be extracted. This generally results in a better performance. In some cases, performance degradation could also be seen. However, in most cases, accuracy increases.

LEAVE A REPLY

Please enter your comment!
Please enter your name here