forked from wadetb/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eliza.py
236 lines (203 loc) · 7.61 KB
/
eliza.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
import logging
import random
import re
from collections import namedtuple
# Fix Python2/Python3 incompatibility
try: input = raw_input
except NameError: pass
log = logging.getLogger(__name__)
class Key:
def __init__(self, word, weight, decomps):
self.word = word
self.weight = weight
self.decomps = decomps
class Decomp:
def __init__(self, parts, save, reasmbs):
self.parts = parts
self.save = save
self.reasmbs = reasmbs
self.next_reasmb_index = 0
class Eliza:
def __init__(self):
self.initials = []
self.finals = []
self.quits = []
self.pres = {}
self.posts = {}
self.synons = {}
self.keys = {}
self.memory = []
def load(self, path):
key = None
decomp = None
with open(path) as file:
for line in file:
if not line.strip():
continue
tag, content = [part.strip() for part in line.split(':')]
if tag == 'initial':
self.initials.append(content)
elif tag == 'final':
self.finals.append(content)
elif tag == 'quit':
self.quits.append(content)
elif tag == 'pre':
parts = content.split(' ')
self.pres[parts[0]] = parts[1:]
elif tag == 'post':
parts = content.split(' ')
self.posts[parts[0]] = parts[1:]
elif tag == 'synon':
parts = content.split(' ')
self.synons[parts[0]] = parts
elif tag == 'key':
parts = content.split(' ')
word = parts[0]
weight = int(parts[1]) if len(parts) > 1 else 1
key = Key(word, weight, [])
self.keys[word] = key
elif tag == 'decomp':
parts = content.split(' ')
save = False
if parts[0] == '$':
save = True
parts = parts[1:]
decomp = Decomp(parts, save, [])
key.decomps.append(decomp)
elif tag == 'reasmb':
parts = content.split(' ')
decomp.reasmbs.append(parts)
def _match_decomp_r(self, parts, words, results):
if not parts and not words:
return True
if not parts or (not words and parts != ['*']):
return False
if parts[0] == '*':
for index in range(len(words), -1, -1):
results.append(words[:index])
if self._match_decomp_r(parts[1:], words[index:], results):
return True
results.pop()
return False
elif parts[0].startswith('@'):
root = parts[0][1:]
if not root in self.synons:
raise ValueError("Unknown synonym root {}".format(root))
if not words[0].lower() in self.synons[root]:
return False
results.append([words[0]])
return self._match_decomp_r(parts[1:], words[1:], results)
elif parts[0].lower() != words[0].lower():
return False
else:
return self._match_decomp_r(parts[1:], words[1:], results)
def _match_decomp(self, parts, words):
results = []
if self._match_decomp_r(parts, words, results):
return results
return None
def _next_reasmb(self, decomp):
index = decomp.next_reasmb_index
result = decomp.reasmbs[index % len(decomp.reasmbs)]
decomp.next_reasmb_index = index + 1
return result
def _reassemble(self, reasmb, results):
output = []
for reword in reasmb:
if not reword:
continue
if reword[0] == '(' and reword[-1] == ')':
index = int(reword[1:-1])
if index < 1 or index > len(results):
raise ValueError("Invalid result index {}".format(index))
insert = results[index - 1]
for punct in [',', '.', ';']:
if punct in insert:
insert = insert[:insert.index(punct)]
output.extend(insert)
else:
output.append(reword)
return output
def _sub(self, words, sub):
output = []
for word in words:
word_lower = word.lower()
if word_lower in sub:
output.extend(sub[word_lower])
else:
output.append(word)
return output
def _match_key(self, words, key):
for decomp in key.decomps:
results = self._match_decomp(decomp.parts, words)
if results is None:
log.debug('Decomp did not match: %s', decomp.parts)
continue
log.debug('Decomp matched: %s', decomp.parts)
log.debug('Decomp results: %s', results)
results = [self._sub(words, self.posts) for words in results]
log.debug('Decomp results after posts: %s', results)
reasmb = self._next_reasmb(decomp)
log.debug('Using reassembly: %s', reasmb)
if reasmb[0] == 'goto':
goto_key = reasmb[1]
if not goto_key in self.keys:
raise ValueError("Invalid goto key {}".format(goto_key))
log.debug('Goto key: %s', goto_key)
return self._match_key(words, self.keys[goto_key])
output = self._reassemble(reasmb, results)
if decomp.save:
self.memory.append(output)
log.debug('Saved to memory: %s', output)
continue
return output
return None
def respond(self, text):
if text.lower() in self.quits:
return None
text = re.sub(r'\s*\.+\s*', ' . ', text)
text = re.sub(r'\s*,+\s*', ' , ', text)
text = re.sub(r'\s*;+\s*', ' ; ', text)
log.debug('After punctuation cleanup: %s', text)
words = [w for w in text.split(' ') if w]
log.debug('Input: %s', words)
words = self._sub(words, self.pres)
log.debug('After pre-substitution: %s', words)
keys = [self.keys[w.lower()] for w in words if w.lower() in self.keys]
keys = sorted(keys, key=lambda k: -k.weight)
log.debug('Sorted keys: %s', [(k.word, k.weight) for k in keys])
output = None
for key in keys:
output = self._match_key(words, key)
if output:
log.debug('Output from key: %s', output)
break
if not output:
if self.memory:
index = random.randrange(len(self.memory))
output = self.memory.pop(index)
log.debug('Output from memory: %s', output)
else:
output = self._next_reasmb(self.keys['xnone'].decomps[0])
log.debug('Output from xnone: %s', output)
return " ".join(output)
def initial(self):
return random.choice(self.initials)
def final(self):
return random.choice(self.finals)
def run(self):
print(self.initial())
while True:
sent = input('> ')
output = self.respond(sent)
if output is None:
break
print(output)
print(self.final())
def main():
eliza = Eliza()
eliza.load('doctor.txt')
eliza.run()
if __name__ == '__main__':
logging.basicConfig()
main()