-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatbot.py
61 lines (46 loc) · 2.12 KB
/
chatbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random
import models
import trainingdataset
from intents import Intents
from user_input import UserInput
def is_context_setter(intent):
return 'context_set' in intent
def is_contextual(intent):
return 'context_filter' in intent
def choose_response_for_intent(intent):
return random.choice(intent['responses'])
class Chatbot:
ERROR_THRESHOLD = 0.25
# contains state for each user
context = {}
def __init__(self, file):
self.intents = Intents(file)
self.training_dataset = trainingdataset.TrainingDataset(self.intents)
self.intents_dict = Intents().intents_dict
self.model = models.TfModel(self.training_dataset.x_train, self.training_dataset.y_train)
self.model.fit(self.training_dataset.x_train, self.training_dataset.y_train)
def classify(self, sentence):
inp = UserInput(sentence, lexicon=self.intents.lexicon)
predictions = self.model.model.predict(inp.bag.reshape(1, len(self.intents.lexicon)))[0]
# filter out predictions below a threshold
predictions = [[i, p] for i, p in enumerate(predictions) if p > self.ERROR_THRESHOLD]
prediction = max(predictions)
# return tuple of prediction and probability
return self.intents.classes[prediction[0]], prediction[1]
def respond(self, sentence, userID=None):
results = self.classify(sentence)
pred_class = results[0]
pred_intent = self.intents_dict[pred_class]
self.set_context_for_further_conversation(pred_intent, userID)
answer = self.apply_context(pred_intent, userID)
return answer
def set_context_for_further_conversation(self, intent, userID):
if is_context_setter(intent):
self.context[userID] = intent['context_set']
def apply_context(self, intent, userID):
if not is_contextual(intent):
return choose_response_for_intent(intent)
elif self.context_already_set(userID) and intent['context_filter'] == self.context[userID]:
return choose_response_for_intent(intent)
def context_already_set(self, userID):
return userID in self.context