-
Notifications
You must be signed in to change notification settings - Fork 0
/
paip-eliza.el
356 lines (322 loc) · 14 KB
/
paip-eliza.el
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
;;; paip-eliza.el
;; Copyright (C) 2014
;; Yosuke Funahashi <yosuke@funahashi.cc>
;;
;; This file is part of paip-el.
;;
;; paip-el is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; paip-el is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with Foobar. If not, see <http://www.gnu.org/licenses/>.
;; [YF] I will comment out original text, keep them as is, and make comments with [YF] marks.
;; [YF] This is the copyright description about the original code.
;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*-
;;; Code from Paradigms of Artificial Intelligence Programming
;;; Copyright (c) 1991 Peter Norvig
;;;; File eliza.lisp: Advanced version of Eliza.
;;; Has more rules, and accepts input without parens.
;; (requires "eliza1")
(require 'paip-eliza1)
;; ;;; ==============================
;; (defun read-line-no-punct ()
;; "Read an input line, ignoring punctuation."
;; (read-from-string
;; (concatenate 'string "(" (substitute-if #\space #'punctuation-p
;; (read-line))
;; ")")))
(defun paip-eliza-read-line-no-punct (input)
"Read an input line, ignoring punctuation."
(first
(read-from-string
(concat "(" (cl-substitute-if
?\s
'paip-eliza-punctuation-p
input)
")"))))
;; (defun punctuation-p (char) (find char ".,;:`!?#-()\\\""))
(defun paip-eliza-punctuation-p (char)
(cl-find char ".,;:`!?#-()\\\""))
;; ;;; ==============================
;; (defun eliza ()
;; "Respond to user input using pattern matching rules."
;; (loop
;; (print 'eliza>)
;; (let* ((input (read-line-no-punct))
;; (response (flatten (use-eliza-rules input))))
;; (print-with-spaces response)
;; (if (equal response '(good bye)) (RETURN)))))
(defun paip-eliza-eliza (input)
(interactive "sTo Eliza: ")
(paip-eliza-print-with-spaces
(paip-flatten
(paip-eliza1-use-eliza-rules
(paip-eliza-read-line-no-punct input)))))
;; (defun print-with-spaces (list)
;; (mapc #'(lambda (x) (prin1 x) (princ " ")) list))
(defun paip-eliza-print-with-spaces (list)
(let ((output (get-buffer-create "*paip*")))
(mapc (lambda (x)
(prin1 x t)
(princ " " t)) list)))
;; (defun print-with-spaces (list)
;; (format t "~{~a ~}" list))
;;
;; [YF] I go with the above implementation because EL dosen't have any
;; format directives for looping.
;; ;;; ==============================
;; (defun mappend (fn &rest lists)
;; "Apply fn to each element of lists and append the results."
;; (apply #'append (apply #'mapcar fn lists)))
;; [YF] Already in the paip module.
;;; ==============================
;; (defparameter *eliza-rules*
;; '((((?* ?x) hello (?* ?y))
;; (How do you do. Please state your problem.))
;; (((?* ?x) computer (?* ?y))
;; (Do computers worry you?) (What do you think about machines?)
;; (Why do you mention computers?)
;; (What do you think machines have to do with your problem?))
;; (((?* ?x) name (?* ?y))
;; (I am not interested in names))
;; (((?* ?x) sorry (?* ?y))
;; (Please don't apologize) (Apologies are not necessary)
;; (What feelings do you have when you apologize))
;; (((?* ?x) I remember (?* ?y))
;; (Do you often think of ?y)
;; (Does thinking of ?y bring anything else to mind?)
;; (What else do you remember) (Why do you recall ?y right now?)
;; (What in the present situation reminds you of ?y)
;; (What is the connection between me and ?y))
;; (((?* ?x) do you remember (?* ?y))
;; (Did you think I would forget ?y ?)
;; (Why do you think I should recall ?y now)
;; (What about ?y) (You mentioned ?y))
;; (((?* ?x) if (?* ?y))
;; (Do you really think its likely that ?y) (Do you wish that ?y)
;; (What do you think about ?y) (Really-- if ?y))
;; (((?* ?x) I dreamt (?* ?y))
;; (Really-- ?y) (Have you ever fantasized ?y while you were awake?)
;; (Have you dreamt ?y before?))
;; (((?* ?x) dream about (?* ?y))
;; (How do you feel about ?y in reality?))
;; (((?* ?x) dream (?* ?y))
;; (What does this dream suggest to you?) (Do you dream often?)
;; (What persons appear in your dreams?)
;; (Don't you believe that dream has to do with your problem?))
;; (((?* ?x) my mother (?* ?y))
;; (Who else in your family ?y) (Tell me more about your family))
;; (((?* ?x) my father (?* ?y))
;; (Your father) (Does he influence you strongly?)
;; (What else comes to mind when you think of your father?))
;; (((?* ?x) I want (?* ?y))
;; (What would it mean if you got ?y)
;; (Why do you want ?y) (Suppose you got ?y soon))
;; (((?* ?x) I am glad (?* ?y))
;; (How have I helped you to be ?y) (What makes you happy just now)
;; (Can you explain why you are suddenly ?y))
;; (((?* ?x) I am sad (?* ?y))
;; (I am sorry to hear you are depressed)
;; (I'm sure its not pleasant to be sad))
;; (((?* ?x) are like (?* ?y))
;; (What resemblance do you see between ?x and ?y))
;; (((?* ?x) is like (?* ?y))
;; (In what way is it that ?x is like ?y)
;; (What resemblance do you see?)
;; (Could there really be some connection?) (How?))
;; (((?* ?x) alike (?* ?y))
;; (In what way?) (What similarities are there?))
;; (((?* ?x) same (?* ?y))
;; (What other connections do you see?))
;; (((?* ?x) I was (?* ?y))
;; (Were you really?) (Perhaps I already knew you were ?y)
;; (Why do you tell me you were ?y now?))
;; (((?* ?x) was I (?* ?y))
;; (What if you were ?y ?) (Do you thin you were ?y)
;; (What would it mean if you were ?y))
;; (((?* ?x) I am (?* ?y))
;; (In what way are you ?y) (Do you want to be ?y ?))
;; (((?* ?x) am I (?* ?y))
;; (Do you believe you are ?y) (Would you want to be ?y)
;; (You wish I would tell you you are ?y)
;; (What would it mean if you were ?y))
;; (((?* ?x) am (?* ?y))
;; (Why do you say "AM?") (I don't understand that))
;; (((?* ?x) are you (?* ?y))
;; (Why are you interested in whether I am ?y or not?)
;; (Would you prefer if I weren't ?y)
;; (Perhaps I am ?y in your fantasies))
;; (((?* ?x) you are (?* ?y))
;; (What makes you think I am ?y ?))
;; (((?* ?x) because (?* ?y))
;; (Is that the real reason?) (What other reasons might there be?)
;; (Does that reason seem to explain anything else?))
;; (((?* ?x) were you (?* ?y))
;; (Perhaps I was ?y) (What do you think?) (What if I had been ?y))
;; (((?* ?x) I can't (?* ?y))
;; (Maybe you could ?y now) (What if you could ?y ?))
;; (((?* ?x) I feel (?* ?y))
;; (Do you often feel ?y ?))
;; (((?* ?x) I felt (?* ?y))
;; (What other feelings do you have?))
;; (((?* ?x) I (?* ?y) you (?* ?z))
;; (Perhaps in your fantasy we ?y each other))
;; (((?* ?x) why don't you (?* ?y))
;; (Should you ?y yourself?)
;; (Do you believe I don't ?y) (Perhaps I will ?y in good time))
;; (((?* ?x) yes (?* ?y))
;; (You seem quite positive) (You are sure) (I understand))
;; (((?* ?x) no (?* ?y))
;; (Why not?) (You are being a bit negative)
;; (Are you saying "NO" just to be negative?))
;; (((?* ?x) someone (?* ?y))
;; (Can you be more specific?))
;; (((?* ?x) everyone (?* ?y))
;; (surely not everyone) (Can you think of anyone in particular?)
;; (Who for example?) (You are thinking of a special person))
;; (((?* ?x) always (?* ?y))
;; (Can you think of a specific example) (When?)
;; (What incident are you thinking of?) (Really-- always))
;; (((?* ?x) what (?* ?y))
;; (Why do you ask?) (Does that question interest you?)
;; (What is it you really want to know?) (What do you think?)
;; (What comes to your mind when you ask that?))
;; (((?* ?x) perhaps (?* ?y))
;; (You do not seem quite certain))
;; (((?* ?x) are (?* ?y))
;; (Did you think they might not be ?y)
;; (Possibly they are ?y))
;; (((?* ?x))
;; (Very interesting) (I am not sure I understand you fully)
;; (What does that suggest to you?) (Please continue) (Go on)
;; (Do you feel strongly about discussing such things?))))
(defvar paip-eliza-*eliza-rules*
'((((\?* \?x) hello (\?* \?y))
(How do you do. Please state your problem.))
(((\?* \?x) computer (\?* \?y))
(Do computers worry you? ) (What do you think about machines? )
(Why do you mention computers? )
(What do you think machines have to do with your problem? ))
(((\?* \?x) name (\?* \?y))
(I am not interested in names))
(((\?* \?x) sorry (\?* \?y))
(Please don't apologize) (Apologies are not necessary)
(What feelings do you have when you apologize))
(((\?* \?x) I remember (\?* \?y))
(Do you often think of \?y)
(Does thinking of \?y bring anything else to mind? )
(What else do you remember) (Why do you recall \?y right now? )
(What in the present situation reminds you of \?y)
(What is the connection between me and \?y))
(((\?* \?x) do you remember (\?* \?y))
(Did you think I would forget \?y \? )
(Why do you think I should recall \?y now)
(What about \?y) (You mentioned \?y))
(((\?* \?x) if (\?* \?y))
(Do you really think its likely that \?y) (Do you wish that \?y)
(What do you think about \?y) (Really-- if \?y))
(((\?* \?x) I dreamt (\?* \?y))
(Really-- \?y) (Have you ever fantasized \?y while you were awake? )
(Have you dreamt \?y before? ))
(((\?* \?x) dream about (\?* \?y))
(How do you feel about \?y in reality? ))
(((\?* \?x) dream (\?* \?y))
(What does this dream suggest to you? ) (Do you dream often? )
(What persons appear in your dreams? )
(Don't you believe that dream has to do with your problem? ))
(((\?* \?x) my mother (\?* \?y))
(Who else in your family \?y) (Tell me more about your family))
(((\?* \?x) my father (\?* \?y))
(Your father) (Does he influence you strongly? )
(What else comes to mind when you think of your father? ))
(((\?* \?x) I want (\?* \?y))
(What would it mean if you got \?y)
(Why do you want \?y) (Suppose you got \?y soon))
(((\?* \?x) I am glad (\?* \?y))
(How have I helped you to be \?y) (What makes you happy just now)
(Can you explain why you are suddenly \?y))
(((\?* \?x) I am sad (\?* \?y))
(I am sorry to hear you are depressed)
(I'm sure its not pleasant to be sad))
(((\?* \?x) are like (\?* \?y))
(What resemblance do you see between \?x and \?y))
(((\?* \?x) is like (\?* \?y))
(In what way is it that \?x is like \?y)
(What resemblance do you see? )
(Could there really be some connection? ) (How? ))
(((\?* \?x) alike (\?* \?y))
(In what way? ) (What similarities are there? ))
(((\?* \?x) same (\?* \?y))
(What other connections do you see? ))
(((\?* \?x) I was (\?* \?y))
(Were you really? ) (Perhaps I already knew you were \?y)
(Why do you tell me you were \?y now? ))
(((\?* \?x) was I (\?* \?y))
(What if you were \?y \? ) (Do you thin you were \?y)
(What would it mean if you were \?y))
(((\?* \?x) I am (\?* \?y))
(In what way are you \?y) (Do you want to be \?y \? ))
(((\?* \?x) am I (\?* \?y))
(Do you believe you are \?y) (Would you want to be \?y)
(You wish I would tell you you are \?y)
(What would it mean if you were \?y))
(((\?* \?x) am (\?* \?y))
(Why do you say "AM?") (I don't understand that))
(((\?* \?x) are you (\?* \?y))
(Why are you interested in whether I am \?y or not? )
(Would you prefer if I weren't \?y)
(Perhaps I am \?y in your fantasies))
(((\?* \?x) you are (\?* \?y))
(What makes you think I am \?y \? ))
(((\?* \?x) because (\?* \?y))
(Is that the real reason? ) (What other reasons might there be? )
(Does that reason seem to explain anything else? ))
(((\?* \?x) were you (\?* \?y))
(Perhaps I was \?y) (What do you think? ) (What if I had been \?y))
(((\?* \?x) I can't (\?* \?y))
(Maybe you could \?y now) (What if you could \?y \? ))
(((\?* \?x) I feel (\?* \?y))
(Do you often feel \?y \? ))
(((\?* \?x) I felt (\?* \?y))
(What other feelings do you have? ))
(((\?* \?x) I (\?* \?y) you (\?* \?z))
(Perhaps in your fantasy we \?y each other))
(((\?* \?x) why don't you (\?* \?y))
(Should you \?y yourself? )
(Do you believe I don't \?y) (Perhaps I will \?y in good time))
(((\?* \?x) yes (\?* \?y))
(You seem quite positive) (You are sure) (I understand))
(((\?* \?x) no (\?* \?y))
(Why not? ) (You are being a bit negative)
(Are you saying "NO" just to be negative? ))
(((\?* \?x) someone (\?* \?y))
(Can you be more specific? ))
(((\?* \?x) everyone (\?* \?y))
(surely not everyone) (Can you think of anyone in particular? )
(Who for example? ) (You are thinking of a special person))
(((\?* \?x) always (\?* \?y))
(Can you think of a specific example) (When? )
(What incident are you thinking of? ) (Really-- always))
(((\?* \?x) what (\?* \?y))
(Why do you ask? ) (Does that question interest you? )
(What is it you really want to know? ) (What do you think? )
(What comes to your mind when you ask that? ))
(((\?* \?x) perhaps (\?* \?y))
(You do not seem quite certain))
(((\?* \?x) are (\?* \?y))
(Did you think they might not be \?y)
(Possibly they are \?y))
(((\?* \?x))
(Very interesting) (I am not sure I understand you fully)
(What does that suggest to you? ) (Please continue) (Go on)
(Do you feel strongly about discussing such things? ))))
;; ;;; ==============================
(provide 'paip-eliza)
;;; paip-eliza.el ends here