-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathprediction.py
More file actions
104 lines (88 loc) · 3.95 KB
/
Copy pathprediction.py
File metadata and controls
104 lines (88 loc) · 3.95 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import nltk
import numpy
import tflearn
import tensorflow
import random
import json
import pickle
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
nltk.download('punkt')
ERROR_THRESHOLD = 0.25
class Prediction:
# This class is mainly used to predict the answer from user's question.
# It need below informations:
# - data.pickle file which store all preprocessing data from training step
# - input file or intent file (its name is stored as part of data.pickle file)
# - model.tflearn - training model from training step
def load_model(self):
if hasattr(self, 'data'):
return
with open("{}/data.pickle".format(self.MODEL_DIR), "rb") as f:
self.words, self.labels, self.training, self.output, self.input_file = pickle.load(f)
with open(self.input_file) as file:
self.data = json.load(file)
tensorflow.reset_default_graph()
tflearn.init_graph(num_cores=1)
net = tflearn.input_data(shape=[None, len(self.training[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(self.output[0]), activation="softmax")
net = tflearn.regression(net)
self.model = tflearn.DNN(net)
self.model.load("{}/model.tflearn".format(self.MODEL_DIR))
def __init__(self, model_dir = '.'):
self.MODEL_DIR = model_dir if model_dir else '.'
self.words = []
self.labels = []
self.docs_x = []
self.docs_y = []
self.context = {}
try:
self.load_model()
except Exception as e:
print('Failed to load model, please train it first. Error {}'.format(e))
def bag_of_words(self, s):
bag = [0 for _ in range(len(self.words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
print('questions: {}'.format(s_words))
for se in s_words:
for i, w in enumerate(self.words):
if w == se:
bag[i] = 1
return numpy.array(bag)
def classify(self, sentence):
# generate probabilities from the model
results = self.model.predict([self.bag_of_words(sentence)])[0]
# filter out predictions below a threshold
results = [[i,r] for i,r in enumerate(results) if r>ERROR_THRESHOLD]
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append((self.labels[r[0]], r[1]))
# return tuple of intent and probability
return return_list
def response(self, sentence, userID='123', show_details=False):
results = self.classify(sentence)
# if we have a classification then find the matching intent tag
if results:
# loop as long as there are matches to process
while results:
for i in self.data["intents"]:
# find a tag matching the first result
if i['tag'] == results[0][0]:
# set context for this intent if necessary
if 'context_set' in i:
if show_details: print ('context:', i['context_set'])
self.context[userID] = i['context_set']
print (self.context)
# check if this intent is contextual and applies to this user's conversation
if not 'context_filter' in i or \
(userID in self.context and 'context_filter' in i and i['context_filter'] == self.context[userID]):
if show_details: print ('tag:', i['tag'])
# a random response from the intent
return random.choice(i['responses'])
results.pop(0)