/
Chat.py
291 lines (253 loc) · 9.66 KB
/
Chat.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import pickle
import nltk
from nltk.corpus import nps_chat
from nltk.corpus import stopwords
from statistics import mean
from random import choice, choices
import pandas as pd
from scipy.spatial.distance import cosine
from spell import correct
from augmented_markov import AugmentedChain
import matplotlib.pyplot as plt
# change nltk data path
nltk.data.path = ['nltk_data']
# load up markov model if found
try:
f = open('markov_model.pickle', 'rb')
m_model = pickle.load(f)
f.close()
except FileNotFoundError:
m_model = None
# load up word weights if found
try:
f = open('word_weights.pickle', 'rb')
word_weights = pickle.load(f)
f.close()
except FileNotFoundError:
word_weights = {}
# load up categorized sentences if found
try:
f = open('categorized_sentences.pickle', 'rb')
categorized_sentences = pickle.load(f)
f.close()
except FileNotFoundError:
categorized_sentences = []
# load up categorized sentences if found
try:
f = open('sentence_clusters.pickle', 'rb')
sentence_clusters= pickle.load(f)
f.close()
except FileNotFoundError:
sentence_clusters = []
# preprocessing nps chat corpus for sentence classification
all_words = nltk.FreqDist(w.lower() for w in nps_chat.words())
word_features = [a[0] for a in all_words.most_common()[:2000]]
sentences = [(nltk.word_tokenize(a.text.lower()), a.attrib['class']) for a in nps_chat.xml_posts()]
# logical response types for each input sentence type
response_types = {
'Accept': ['Statement', 'Emotion', 'Emphasis'],
'Bye': ['Bye'],
'Clarify': ['Accept', 'Reject', 'Statement', 'Emphasis'],
'Emotion': ['Accept', 'Reject', 'Statement', 'Emotion', 'Emphasis'],
'Continuer': ['Accept', 'Reject', 'Statement', 'Emphasis'],
'Emphasis': ['Accept', 'Reject', 'Statement', 'Emotion', 'Emphasis'],
'Greet': ['Greet'],
'Other': ['Statement'],
'Reject': ['Statement', 'Emotion', 'Emphasis'],
'Statement': ['Accept', 'Reject', 'Statement', 'Emotion', 'Emphasis'],
'System': ['Statement'],
'nAnswer': ['Statement', 'Emotion', 'Emphasis'],
'whQuestion': ['Statement'],
'yAnswer': ['Accept', 'Reject', 'Statement', 'Emotion', 'Emphasis'],
'ynQuestion': ['nAnswer', 'yAnswer']
}
# print and return, handy for use in list comps
def printr(p):
print(p)
return p
# average out dict of weights
def unpack_weights(d):
unpacked = {}
for ws in d:
for w in d[ws]:
if w not in unpacked:
unpacked[w] = [d[ws][w]]
else:
unpacked[w].append(d[ws][w])
return {word: mean(unpacked[word]) for word in unpacked}
def topic_vectorize_sent(sent, weights):
# gets a dict of word weights
sweights = {w: weights[w] for w in weights if w in sent}
# averages out word
stopics = unpack_weights(sweights)
# adds unrelated words back in
return {w: 0 if w not in stopics else stopics[w] for w in weights}
# returns the similarity of two sentences
def sent_similarity(sent1, sent2, weights):
# vectorize if not already
s1vector = topic_vectorize_sent(sent1, weights) if not type(sent1) == dict else sent1
s2vector = topic_vectorize_sent(sent2, weights) if not type(sent2) == dict else sent2
# return similarity
return 1 - cosine(pd.Series(s1vector), pd.Series(s2vector))
# vectorizes sentences for classifier
def sentence_features(sentence):
sentence_words = set(sentence)
features = {}
for word in word_features:
features['contains({})'.format(word)] = (word in sentence_words)
return features
# trains sentence classifier
def train_classifier():
featuresets = [(sentence_features(s), c) for (s, c) in sentences]
train_set, test_set = featuresets[100:], featuresets[:100]
clas = nltk.NaiveBayesClassifier.train(train_set)
print(nltk.classify.accuracy(clas, test_set))
clas.show_most_informative_features(5)
return clas
# loads classifier if it exists
try:
f = open('classifier.pickle', 'rb')
classifier = pickle.load(f)
f.close()
# generates classifier if it does not exist, and dumps it to file
except FileNotFoundError:
classifier = train_classifier()
f = open('classifier.pickle', 'wb')
pickle.dump(classifier, f)
f.close()
def show_clusters(sents):
l = [x[0] for x in choices(sents, k=1000)]
s = nltk.word_tokenize(choice(sents)[0])
d = []
for a in l:
sim = sent_similarity(nltk.word_tokenize(a), s, word_weights)
d.append(sim)
plt.hist(d, bins=20)
plt.show()
def generate_clusters(sents=categorized_sentences, threshold=0.2, weights=word_weights):
clusters = []
while sents:
s = topic_vectorize_sent(nltk.word_tokenize(choice(sents)[0]), weights)
clusters.append((s, {}))
for sent, clas in sents:
svec = topic_vectorize_sent(nltk.word_tokenize(sent), weights)
sim = sent_similarity(s, svec, weights)
if sim >= threshold:
clusters[-1][1][sent] = (svec, clas)
sents.remove((sent, clas))
print('s', len(sents))
print('c', len(clusters))
return clusters
# gets a list of valid responses
# TODO: make this faster
def get_responses(message, weights=word_weights, sents=categorized_sentences, clas=classifier, threshold=0.4, n=10):
# pre tokenize and spelling correct message
tkn_message = nltk.word_tokenize(correct(message))
print(tkn_message)
has_non_stop = False
# check if message has any words that are in the dictionary
for word in tkn_message:
if word in word_weights:
has_non_stop = True
# get response types
typs = response_types[clas.classify(sentence_features(tkn_message))]
print(typs)
relevant = []
i = 0
# if some words are in the dictionary, look for a sentence with sufficient similarity
if has_non_stop:
for s in sents:
# decrement threshold every 500 words to speed up search
if i > 500 and threshold >= 0.1:
threshold -= 0.05
i = 0
i += 1
sm = sent_similarity(tkn_message, nltk.word_tokenize(s[0]), weights)
if sm >= threshold and s[1] in typs:
relevant.append((s, sm))
if len(relevant) >= n:
break
# if no dictionary words are found, give up and just look for a sentence with the right type
else:
for s in sents:
if s[1] in typs:
relevant.append((s, 0))
if len(relevant) >= n:
break
# sort by relevance
relevant.sort(key=lambda x: x[1])
if relevant:
return relevant[:10]
else:
return []
# gets a single response
def get_response(message, weights=word_weights, model=m_model):
v = topic_vectorize_sent(message, weights)
print({a:v[a] for a in v if v[a]>0})
return model.make_sentece(v)
# generates a dict of probabilities that any given word will appear in a sentence with another word
def generate_word_weights(hist):
# get all words that aren't stopwords
stop_words = stopwords.words('english')
filtered_sentences = [[b for b in nltk.word_tokenize(a.lower()) if b not in stop_words] for a in hist]
non_stop_words = list(set([b for a in filtered_sentences for b in a]))
word_counts = {a: {b: [0, 0] for b in non_stop_words} for a in non_stop_words}
# get counts for each word pair
count = 0
for sent in filtered_sentences:
count += 1 # I don't know what this does but I refuse to touch it
for word in sent:
for wword in non_stop_words:
word_counts[word][wword][0] += 1
if wword in sent:
word_counts[word][wword][1] += 1
weights = {a: {b: word_counts[a][b][1] / word_counts[a][b][0] for b in word_counts[a] if word_counts[a][b][1] > 0} for a in word_counts}
return weights
# generates word probability weights and categorized sentences
# dumps word weights and categorized sentences to file
def generate_model(hist):
global word_weights
global categorized_sentences
global m_model
m_model = AugmentedChain(hist)
f = open('markov_model.pickle', 'wb')
pickle.dump(m_model, f)
f.close()
word_weights = generate_word_weights(hist)
f = open('word_weights.pickle', 'wb')
pickle.dump(word_weights, f)
f.close()
categorized_sentences = [(a, classifier.classify(sentence_features(nltk.word_tokenize(a)))) for a in hist]
f = open('categorized_sentences.pickle', 'wb')
pickle.dump(categorized_sentences, f)
f.close()
# simple main function for testing
if __name__ == '__main__':
# using nps chat for testing
h = [a.text for a in nps_chat.xml_posts()]
# generate markov model if not loaded
if not m_model:
print('generating markov model')
m_model = AugmentedChain(h[:10000])
f = open('markov_model.pickle', 'wb')
pickle.dump(m_model, f)
f.close()
# get word weights if they weren't loaded
if not word_weights:
print('weighting words')
word_weights = generate_word_weights(h)
f = open('word_weights.pickle', 'wb')
pickle.dump(word_weights, f)
f.close()
# get categorized sentences if they weren't loaded
if not categorized_sentences:
print('categorizing sentences')
categorized_sentences = [(a.text, a.attrib['class']) for a in nps_chat.xml_posts()]
f = open('categorized_sentences.pickle', 'wb')
pickle.dump(categorized_sentences, f)
f.close()
# converse!
while True:
m = input('>>> ')
v = topic_vectorize_sent(nltk.word_tokenize(m), word_weights)
print(m_model.make_sentece(v))