Chat-bot master with artificial neural networks

 Chat-Bot master with neural networks

This project is about, building a chatbot that was useful in different real-world scenarios.

Environment setup/Project Requirements :
  1. Python
  2. Numpy, Keras, Pickle, nltk, Tkinter, JSON, random libraries need to be installed.
  3. Jupyter Notebook
Note: You cannot run this code in Google - colab or in online python interpreter.

Workflow:
  1. Importing Data
  2. Preprocessing Data 
  3. Lemmatizing and removing Duplicates words.
  4. Creating training and testing data.
  5. Neural network Model.
  6. Save model
  7. Initializing 0 or 1 to data.
  8. Getting random output.
  9. GUI
Step 1: Importing Data
    The data used for the project is in JSON format, so we cannot use pandas for reading data. We have to use the file handling technique in Python and iterate through the file.
data_file = open('intents.json').read()
intents = json.loads(data_file)

Step 2: Preprocessing the data.
    We iterate the patterns using nltk.word_tokenize() function and append each word in the list and also created a list of classes for our tags.
w = nltk.word_tokenize(pattern)
classes.append(intent['tag'])

Step 3: Lemmatizing each word.
    Lemmatizing is the process of converting words into their lemma form and we will remove duplicate words from the list. We create a pickle file to store the trained data so that we no need to train the model each and every time we want to run the code.
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
pickle.dump(words,open('words.pkl','wb'))
pickle.dump(classes,open('classes.pkl','wb'))

Step 4: Creating training and testing data.
    Our input will be the pattern and output will be the class of our input. The computer understands only numbers so we convert words into numbers. The output is a 0 for each tag and 1 for the current tag(for each pattern).
training=[]
.
.
.
train_x = list(training[:,0])
train_y = list(training[:,1])


Step 5: Neural network Model.
    We create a neural network with three layers in which the first layer contains 128 neurons, the second layer consisted of 64 neurons and the third layer was the output layer which will be in the size of the output.

model = Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]), activation='softmax'))

Sequential() - It is used to keep the neural network layers in a sequence.
Dense -: Dense layer is a fully connected network.
Dropout - It is used to drop some of the neurons in the particular layer, which helps in removing the overfitting of the model.
Relu - Rectified Linear unit is a non-linear activation function.
softmax - It is an activation function, which is used for the output layer.

Next, we will compile the model with stochastic gradient descent and train the model with the data.

model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
 
Step 6: Save the Model.
    We will save the model in .h5 format so that we can use the trained model in the future without training the entire model again.

model.save('chatbot_model.h5', hist)

Step 7: Initializing 0 and 1
    We initialize 0 or 1 for the words in the bag that exists in the sentence. We assign 1 if the current word is in vocabulary position.

    for s in sentence_words:
        for i,w in enumerate(words):
            if w == s: 
                bag[i] = 1

Now, we initialize some error threshold and filter out predictions below the threshold.

    ERROR_THRESHOLD = 0.25
    results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
    results.sort(key=lambda x: x[1], reverse=True)
    return_list = []

Step 8: Getting random output
    After predicting the class, we get a random response from the list of intents
.
    for i in list_of_intents:
        if(i['tag']== tag):
            result = random.choice(i['responses'])
    return result

Step 9: GUI
    GUI(Graphic user interface) is the most important one in the project, which allows us to communicate with the machine. We create a  GUI with a send button, an entry box, and a chat window.


If you want complete source code, visit my Github.

Resources:
1)Some blogs and Github.

Note: Thanks to everyone who open-sourced their code which helped me a lot in developing the code ad thanks to StackOverflow.

Expected viva:
1)Where can a be chatbot used?
A) Chat bot's a lot of real-world applications, for instance, the TCS website uses a chatbot to take applications and many other websites use the chatbot to answer the most repetitive or basic questions.

2)What is lemmatizing? 
A) To extract the proper lemma, it is necessary to look at the morphological analysis of each word. This requires having dictionaries for every language to provide that kind of analysis.

3)What is tokenization?
A) It splits a piece of text into individual words based on a certain delimiter. The goal is to create a vocabulary.

4)Can you deploy a chatbot? 
A) We can deploy chatbot but, currently I don't know deploying.

5)How chatbot gives a reply?
A)Actually it depends on the model, It may vary according to the model developed. In my case, there are some pre-defined sets of replies for a particular question. I used a random module to give any one of the replies in those.

6)Why did you use artificial neural networks?
A)Basically, I used artificial neural networks in order to develop and test my knowledge, and fortunately it gave 97.5% accuracy. In case if I got less accuracy I may use RNN like LSTM's.

7)Can you elaborate nltk?
A) Natural language toolkit.
(I believe it is not the worst question than what is Fourier in Fourier transform?๐Ÿ˜)

made with ๐Ÿ’“ by G. Sai Dheeraj 






Comments

Popular posts from this blog

Object Detection Using OpenCV and Transfer Learning.

Path Detection based on Bird Eye view with OpenCV module

Traffic signs recognition with convolutional Neural networks