|
| 1 | +import os |
| 2 | +from sklearn.metrics.pairwise import pairwise_distances_argmin |
| 3 | + |
| 4 | +from chatterbot import ChatBot |
| 5 | +from chatterbot.trainers import ChatterBotCorpusTrainer |
| 6 | +from utils import * |
| 7 | + |
| 8 | + |
| 9 | +class ThreadRanker(object): |
| 10 | + def __init__(self, paths): |
| 11 | + self.word_embeddings, self.embeddings_dim = load_embeddings(paths['WORD_EMBEDDINGS']) |
| 12 | + self.thread_embeddings_folder = paths['THREAD_EMBEDDINGS_FOLDER'] |
| 13 | + |
| 14 | + def __load_embeddings_by_tag(self, tag_name): |
| 15 | + embeddings_path = os.path.join(self.thread_embeddings_folder, tag_name + ".pkl") |
| 16 | + thread_ids, thread_embeddings = unpickle_file(embeddings_path) |
| 17 | + return thread_ids, thread_embeddings |
| 18 | + |
| 19 | + def get_best_thread(self, question, tag_name): |
| 20 | + """ Returns id of the most similar thread for the question. |
| 21 | + The search is performed across the threads with a given tag. |
| 22 | + """ |
| 23 | + thread_ids, thread_embeddings = self.__load_embeddings_by_tag(tag_name) |
| 24 | + |
| 25 | + # HINT: you have already implemented a similar routine in the 3rd assignment. |
| 26 | + |
| 27 | + question_vec = #### YOUR CODE HERE #### |
| 28 | + best_thread = #### YOUR CODE HERE #### |
| 29 | + |
| 30 | + return thread_ids[best_thread] |
| 31 | + |
| 32 | + |
| 33 | +class DialogueManager(object): |
| 34 | + def __init__(self, paths): |
| 35 | + print("Loading resources...") |
| 36 | + |
| 37 | + # Intent recognition: |
| 38 | + self.intent_recognizer = unpickle_file(paths['INTENT_RECOGNIZER']) |
| 39 | + self.tfidf_vectorizer = unpickle_file(paths['TFIDF_VECTORIZER']) |
| 40 | + |
| 41 | + self.ANSWER_TEMPLATE = 'I think its about %s\nThis thread might help you: https://stackoverflow.com/questions/%s' |
| 42 | + |
| 43 | + # Goal-oriented part: |
| 44 | + self.tag_classifier = unpickle_file(paths['TAG_CLASSIFIER']) |
| 45 | + self.thread_ranker = ThreadRanker(paths) |
| 46 | + self.__init_chitchat_bot() |
| 47 | + |
| 48 | + def __init_chitchat_bot(self): |
| 49 | + """Initializes self.chitchat_bot with some conversational model.""" |
| 50 | + |
| 51 | + # Hint: you might want to create and train chatterbot.ChatBot here. |
| 52 | + # Create an instance of the ChatBot class. |
| 53 | + # Create a trainer (chatterbot.trainers.ChatterBotCorpusTrainer) for the ChatBot. |
| 54 | + # Train the ChatBot with "chatterbot.corpus.english" param. |
| 55 | + |
| 56 | + ######################## |
| 57 | + #### YOUR CODE HERE #### |
| 58 | + ######################## |
| 59 | + |
| 60 | + # remove this when you're done |
| 61 | + raise NotImplementedError( |
| 62 | + "Open dialogue_manager.py and fill with your code. In case of Google Colab, download" |
| 63 | + "(https://github.com/hse-aml/natural-language-processing/blob/master/project/dialogue_manager.py), " |
| 64 | + "edit locally and upload using '> arrow on the left edge' -> Files -> UPLOAD") |
| 65 | + |
| 66 | + def generate_answer(self, question): |
| 67 | + """Combines stackoverflow and chitchat parts using intent recognition.""" |
| 68 | + |
| 69 | + # Recognize intent of the question using `intent_recognizer`. |
| 70 | + # Don't forget to prepare question and calculate features for the question. |
| 71 | + |
| 72 | + prepared_question = #### YOUR CODE HERE #### |
| 73 | + features = #### YOUR CODE HERE #### |
| 74 | + intent = #### YOUR CODE HERE #### |
| 75 | + |
| 76 | + # Chit-chat part: |
| 77 | + if intent == 'dialogue': |
| 78 | + # Pass question to chitchat_bot to generate a response. |
| 79 | + response = #### YOUR CODE HERE #### |
| 80 | + return response |
| 81 | + |
| 82 | + # Goal-oriented part: |
| 83 | + else: |
| 84 | + # Pass features to tag_classifier to get predictions. |
| 85 | + tag = #### YOUR CODE HERE #### |
| 86 | + |
| 87 | + # Pass prepared_question to thread_ranker to get predictions. |
| 88 | + thread_id = #### YOUR CODE HERE #### |
| 89 | + |
| 90 | + return self.ANSWER_TEMPLATE % (tag, thread_id) |
0 commit comments