-
Notifications
You must be signed in to change notification settings - Fork 74
/
PatternMgr.py
340 lines (305 loc) · 13.2 KB
/
PatternMgr.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# This class implements the AIML pattern-matching algorithm described
# by Dr. Richard Wallace at the following site:
# http://www.alicebot.org/documentation/matching.html
from six import print_
import marshal
import pprint
import re
import string
import sys
class PatternMgr:
# special dictionary keys
_UNDERSCORE = 0
_STAR = 1
_TEMPLATE = 2
_THAT = 3
_TOPIC = 4
_BOT_NAME = 5
def __init__(self):
self._root = {}
self._templateCount = 0
self._botName = "Nameless"
punctuation = "\"`~!@#$%^&*()-_=+[{]}\|;:',<.>/?"
self._puncStripRE = re.compile("[" + re.escape(punctuation) + "]")
self._whitespaceRE = re.compile("\s+", re.LOCALE | re.UNICODE)
def numTemplates(self):
"""Return the number of templates currently stored."""
return self._templateCount
def setBotName(self, name):
"""Set the name of the bot, used to match <bot name="name"> tags in
patterns. The name must be a single word!
"""
# Collapse a multi-word name into a single word
self._botName = str(string.join(name.split()))
def dump(self):
"""Print_ all learned patterns, for debugging purposes."""
pprint.pprint(self._root)
def save(self, filename):
"""Dump the current patterns to the file specified by filename. To
restore later, use restore().
"""
try:
outFile = open(filename, "wb")
marshal.dump(self._templateCount, outFile)
marshal.dump(self._botName, outFile)
marshal.dump(self._root, outFile)
outFile.close()
except Exception as e:
print_("Error saving PatternMgr to file %s:" % filename)
raise Exception(e)
def restore(self, filename):
"""Restore a previously save()d collection of patterns."""
try:
inFile = open(filename, "rb")
self._templateCount = marshal.load(inFile)
self._botName = marshal.load(inFile)
self._root = marshal.load(inFile)
inFile.close()
except Exception as e:
print_("Error restoring PatternMgr from file %s:" % filename)
raise Exception(e)
def add(self, xxx_todo_changeme, template):
"""Add a [pattern/that/topic] tuple and its corresponding template
to the node tree.
"""
(pattern, that, topic) = xxx_todo_changeme
node = self._root
for word in string.split(pattern):
key = word
if key == "_":
key = self._UNDERSCORE
elif key == "*":
key = self._STAR
elif key == "BOT_NAME":
key = self._BOT_NAME
if key not in node:
node[key] = {}
node = node[key]
# navigate further down, if a non-empty "that" pattern was included
if len(that) > 0:
if self._THAT not in node:
node[self._THAT] = {}
node = node[self._THAT]
for word in string.split(that):
key = word
if key == "_":
key = self._UNDERSCORE
elif key == "*":
key = self._STAR
if key not in node:
node[key] = {}
node = node[key]
# navigate yet further down, if a non-empty "topic" string was included
if len(topic) > 0:
if self._TOPIC not in node:
node[self._TOPIC] = {}
node = node[self._TOPIC]
for word in string.split(topic):
key = word
if key == "_":
key = self._UNDERSCORE
elif key == "*":
key = self._STAR
if key not in node:
node[key] = {}
node = node[key]
# add the template.
if self._TEMPLATE not in node:
self._templateCount += 1
node[self._TEMPLATE] = template
def match(self, pattern, that, topic):
"""Return the template which is the closest match to pattern. The
'that' parameter contains the bot's previous response. The 'topic'
parameter contains the current topic of conversation.
Returns None if no template is found.
"""
if len(pattern) == 0:
return None
# Mutilate the input. Remove all punctuation and convert the
# text to all caps.
input = string.upper(pattern)
input = re.sub(self._puncStripRE, " ", input)
if that.strip() == "":
that = "ULTRABOGUSDUMMYTHAT" # 'that' must never be empty
thatInput = string.upper(that)
thatInput = re.sub(self._puncStripRE, " ", thatInput)
thatInput = re.sub(self._whitespaceRE, " ", thatInput)
if topic.strip() == "":
topic = "ULTRABOGUSDUMMYTOPIC" # 'topic' must never be empty
topicInput = string.upper(topic)
topicInput = re.sub(self._puncStripRE, " ", topicInput)
# Pass the input off to the recursive call
patMatch, template = self._match(
input.split(), thatInput.split(), topicInput.split(), self._root)
return template
def star(self, starType, pattern, that, topic, index):
"""Returns a string, the portion of pattern that was matched by a *.
The 'starType' parameter specifies which type of star to find.
Legal values are:
- 'star': matches a star in the main pattern.
- 'thatstar': matches a star in the that pattern.
- 'topicstar': matches a star in the topic pattern.
"""
# Mutilate the input. Remove all punctuation and convert the
# text to all caps.
input = string.upper(pattern)
input = re.sub(self._puncStripRE, " ", input)
input = re.sub(self._whitespaceRE, " ", input)
if that.strip() == "":
that = "ULTRABOGUSDUMMYTHAT" # 'that' must never be empty
thatInput = string.upper(that)
thatInput = re.sub(self._puncStripRE, " ", thatInput)
thatInput = re.sub(self._whitespaceRE, " ", thatInput)
if topic.strip() == "":
topic = "ULTRABOGUSDUMMYTOPIC" # 'topic' must never be empty
topicInput = string.upper(topic)
topicInput = re.sub(self._puncStripRE, " ", topicInput)
topicInput = re.sub(self._whitespaceRE, " ", topicInput)
# Pass the input off to the recursive pattern-matcher
patMatch, template = self._match(
input.split(), thatInput.split(), topicInput.split(), self._root)
if template == None:
return ""
# Extract the appropriate portion of the pattern, based on the
# starType argument.
words = None
if starType == 'star':
patMatch = patMatch[:patMatch.index(self._THAT)]
words = input.split()
elif starType == 'thatstar':
patMatch = patMatch[patMatch.index(self._THAT) + 1: patMatch.index(self._TOPIC)]
words = thatInput.split()
elif starType == 'topicstar':
patMatch = patMatch[patMatch.index(self._TOPIC) + 1:]
words = topicInput.split()
else:
# unknown value
raise ValueError("starType must be in ['star', 'thatstar', 'topicstar']")
# compare the input string to the matched pattern, word by word.
# At the end of this loop, if foundTheRightStar is true, start and
# end will contain the start and end indices (in "words") of
# the substring that the desired star matched.
foundTheRightStar = False
start = end = j = numStars = k = 0
for i in range(len(words)):
# This condition is true after processing a star
# that ISN'T the one we're looking for.
if i < k:
continue
# If we're reached the end of the pattern, we're done.
if j == len(patMatch):
break
if not foundTheRightStar:
if patMatch[j] in [self._STAR, self._UNDERSCORE]: # we got a star
numStars += 1
if numStars == index:
# This is the star we care about.
foundTheRightStar = True
start = i
# Iterate through the rest of the string.
for k in range(i, len(words)):
# If the star is at the end of the pattern,
# we know exactly where it ends.
if j + 1 == len(patMatch):
end = len(words)
break
# If the words have started matching the
# pattern again, the star has ended.
if patMatch[j + 1] == words[k]:
end = k - 1
i = k
break
# If we just finished processing the star we cared
# about, we exit the loop early.
if foundTheRightStar:
break
# Move to the next element of the pattern.
j += 1
# extract the star words from the original, unmutilated input.
if foundTheRightStar:
# print_ string.join(pattern.split()[start:end+1])
if starType == 'star':
return string.join(pattern.split()[start:end + 1])
elif starType == 'thatstar':
return string.join(that.split()[start:end + 1])
elif starType == 'topicstar':
return string.join(topic.split()[start:end + 1])
else:
return ""
def _match(self, words, thatWords, topicWords, root):
"""Return a tuple (pat, tem) where pat is a list of nodes, starting
at the root and leading to the matching pattern, and tem is the
matched template.
"""
# base-case: if the word list is empty, return the current node's
# template.
if len(words) == 0:
# we're out of words.
pattern = []
template = None
if len(thatWords) > 0:
# If thatWords isn't empty, recursively
# pattern-match on the _THAT node with thatWords as words.
try:
pattern, template = self._match(thatWords, [], topicWords, root[self._THAT])
if pattern != None:
pattern = [self._THAT] + pattern
except KeyError:
pattern = []
template = None
elif len(topicWords) > 0:
# If thatWords is empty and topicWords isn't, recursively pattern
# on the _TOPIC node with topicWords as words.
try:
pattern, template = self._match(topicWords, [], [], root[self._TOPIC])
if pattern != None:
pattern = [self._TOPIC] + pattern
except KeyError:
pattern = []
template = None
if template == None:
# we're totally out of input. Grab the template at this node.
pattern = []
try:
template = root[self._TEMPLATE]
except KeyError:
template = None
return (pattern, template)
first = words[0]
suffix = words[1:]
# Check underscore.
# Note: this is causing problems in the standard AIML set, and is
# currently disabled.
if self._UNDERSCORE in root:
# Must include the case where suf is [] in order to handle the case
# where a * or _ is at the end of the pattern.
for j in range(len(suffix) + 1):
suf = suffix[j:]
pattern, template = self._match(suf, thatWords, topicWords, root[self._UNDERSCORE])
if template is not None:
newPattern = [self._UNDERSCORE] + pattern
return (newPattern, template)
# Check first
if first in root:
pattern, template = self._match(suffix, thatWords, topicWords, root[first])
if template is not None:
newPattern = [first] + pattern
return (newPattern, template)
# check bot name
if self._BOT_NAME in root and first == self._botName:
pattern, template = self._match(suffix, thatWords, topicWords, root[self._BOT_NAME])
if template is not None:
newPattern = [first] + pattern
return (newPattern, template)
# check star
if self._STAR in root:
# Must include the case where suf is [] in order to handle the case
# where a * or _ is at the end of the pattern.
for j in range(len(suffix) + 1):
suf = suffix[j:]
pattern, template = self._match(suf, thatWords, topicWords, root[self._STAR])
if template is not None:
newPattern = [self._STAR] + pattern
return (newPattern, template)
# No matches were found.
return (None, None)