1
+ import os
2
+ from sklearn .metrics .pairwise import pairwise_distances_argmin
3
+
4
+ from chatterbot import ChatBot
5
+ from utils import *
6
+ paths = RESOURCE_PATH
7
+
8
+ class ThreadRanker (object ):
9
+ def __init__ (self , paths ):
10
+ self .word_embeddings , self .embeddings_dim = load_embeddings (paths ['WORD_EMBEDDINGS' ])
11
+ self .thread_embeddings_folder = paths ['THREAD_EMBEDDINGS_FOLDER' ]
12
+
13
+ def __load_embeddings_by_tag (self , tag_name ):
14
+ embeddings_path = os .path .join (self .thread_embeddings_folder , tag_name + ".pkl" )
15
+ thread_ids , thread_embeddings = unpickle_file (embeddings_path )
16
+ return thread_ids , thread_embeddings
17
+
18
+ def get_best_thread (self , question , tag_name ):
19
+ """ Returns id of the most similar thread for the question.
20
+ The search is performed across the threads with a given tag.
21
+ """
22
+ thread_ids , thread_embeddings = self .__load_embeddings_by_tag (tag_name )
23
+
24
+ # HINT: you have already implemented a similar routine in the 3rd assignment.
25
+
26
+ question_vec = question_to_vec (question , self .word_embeddings , self .embeddings_dim )#### YOUR CODE HERE ####
27
+ from sklearn .metrics .pairwise import cosine_similarity
28
+ best_thread = np .argmax (cosine_similarity ([question_vec ],thread_embeddings )[0 ])#### 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\n This 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
+
47
+ def create_chitchat_bot (self ):
48
+ """Initializes self.chitchat_bot with some conversational model."""
49
+
50
+ # Hint: you might want to create and train chatterbot.ChatBot here.
51
+ # It could be done by creating ChatBot with the *trainer* parameter equals
52
+ # "chatterbot.trainers.ChatterBotCorpusTrainer"
53
+ # and then calling *train* function with "chatterbot.corpus.english" param
54
+
55
+ ########################
56
+ #### YOUR CODE HERE ####
57
+ from chatterbot .trainers import ChatterBotCorpusTrainer
58
+
59
+ mybot = ChatBot ("Training Example" )
60
+ mybot .set_trainer (ChatterBotCorpusTrainer )
61
+
62
+ mybot .train ("chatterbot.corpus.english" )
63
+
64
+ return mybot
65
+
66
+ ########################
67
+
68
+ def generate_answer (self , question ):
69
+ """Combines stackoverflow and chitchat parts using intent recognition."""
70
+
71
+ # Recognize intent of the question using `intent_recognizer`.
72
+ # Don't forget to prepare question and calculate features for the question.
73
+
74
+ prepared_question = text_prepare (question )#### YOUR CODE HERE ####
75
+ features = self .tfidf_vectorizer .transform ([prepared_question ])#### YOUR CODE HERE ####
76
+ intent = self .intent_recognizer .predict (features )#### YOUR CODE HERE ####
77
+
78
+ # Chit-chat part:
79
+ if intent == 'dialogue' :
80
+ # Pass question to chitchat_bot to generate a response.
81
+ mybot = self .create_chitchat_bot ()
82
+ response = mybot .get_response (question )#### YOUR CODE HERE ####
83
+ return response
84
+
85
+ # Goal-oriented part:
86
+ else :
87
+ # Pass features to tag_classifier to get predictions.
88
+ tag = self .tag_classifier .predict (features )#### YOUR CODE HERE ####
89
+
90
+ # Pass prepared_question to thread_ranker to get predictions.
91
+ thread_id = self .thread_ranker .get_best_thread (prepared_question ,tag [0 ])#### YOUR CODE HERE ####
92
+
93
+ return self .ANSWER_TEMPLATE % (tag , thread_id )
0 commit comments