Chapter 7: Build a Text-Based Dialogue System (Chatbot)
Activity 7: Create a Conversational Agent to Control a Robot
Solution
Open up your Google Colab interface.
Create a folder for the book and download the utils, responses, and training folder from Github and upload it in the folder.
Import drive and mount it as follows:
from google.colab import drive drive.mount('/content/drive')
Note
Every time you use a new collaborator, mount the drive to the desired folder.
Once you have mounted your drive for the first time, you will need to enter the authorization code mentioned by clicking on the URL mentioned by Google and press the Enter key on your keyboard:
Now that you have mounted the drive, you need to set the path of the directory.
cd /content/drive/My Drive/C13550/Lesson07/Activity01
Note
The path mentioned in step 5 may change as per your folder setup on Google Drive. The path will always begin with cd /content/drive/My Drive/
Import the chatbot_intro file:
from chatbot_intro import *
Define the GloVe model:
filename = '../utils/glove.6B.50d.txt.word2vec' model = KeyedVectors.load_word2vec_format(filename, binary=False)
List the responses and training sentences files:
intent_route = 'training/' response_route = 'responses/' intents = listdir(intent_route) responses = listdir(response_route) print("Documents: ", intents)
Figure 7.29: A list of intent documents
Create document vectors:
doc_vectors = np.empty((0,50), dtype='f') for i in intents: with open(intent_route + i) as f: sentences = f.readlines() sentences = [x.strip() for x in sentences] sentences = pre_processing(sentences) doc_vectors= np.append(doc_vectors,doc_vector(sentences,model),axis=0) print("Shape of doc_vectors:",doc_vectors.shape) print(" Vector representation of backward.txt:\n",doc_vectors)
Predict the intent:
user_sentence = "Look to the right" user_sentence = pre_processing([user_sentence]) user_vector = doc_vector(user_sentence,model).reshape(100,) intent = intents[select_intent(user_vector, doc_vectors)] intent
Congratulations! You finished the activity. You can add more intents if you want to and train the GloVe model to achieve better results. By creating a function with all the code, you programmed and developing a movement node in ROS, you can order your robot to make movements and turn around.