forked from darius/awklisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eliza.lsp
140 lines (126 loc) · 3.62 KB
/
eliza.lsp
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
; Simple Eliza program -- see Norvig, _Paradigms of AI Programming_.
; Uses: startup lists
(define rule-pattern car)
(define rule-answers cdr)
(define rules
'(((hello)
(How do you do -- please state your problem))
((I want)
(What would it mean if you got -R-)
(Why do you want -R-)
(Suppose you got -R- soon))
((if)
(Do you really think its likely that -R-)
(Do you wish that -R-)
(What do you think about -R-)
(Really-- if -R-))
((I was)
(Were you really?)
(Perhaps I already knew you were -R-)
(Why do you tell me you were -R- now?))
((I am)
(In what way are you -R-)
(Do you want to be -R-))
((because)
(Is that the real reason?)
(What other reasons might there be?)
(Does that reason seem to explain anything else?))
((I feel)
(Do you often feel -R-))
((I felt)
(What other feelings do you have?))
((yes)
(You seem quite positive)
(You are sure)
(I understand))
((no)
(Why not?)
(You are being a bit negative)
(Are you saying no just to be negative?))
((someone)
(Can you be more specific?))
((everyone)
(Surely not everyone)
(Can you think of anyone in particular?)
(Who for example?)
(You are thinking of a special person))
((perhaps)
(You do not seem quite certain))
((are)
(Did you think they might not be -R-)
(Possibly they are -R-))
(()
(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?))))
(define eliza
(lambda ()
(say '(Hello-- please state your problem))
(let ((sentence '*))
(while (begin
(write '>)
(not (eq? the-eof-object (set! sentence (read)))))
(say (eliza-answer sentence))))))
(define eliza-answer
(lambda (sentence)
(some
(lambda (rule)
(let ((env (match (rule-pattern rule) sentence)))
(and env
(flatten
(sublis (switch-roles env)
(random-element (rule-answers rule)))))))
rules)))
(define switch-roles
(lambda (words)
(sublis '(('I . 'you) ('you . 'I) ('me . 'you) ('am . 'are))
words)))
; Matching
; If sublist is a sublist of lst , then return an a-list with -R- bound to
; the part of lst after sublist . Else return nil.
(define match
(lambda (sublist lst)
(cond
((matches-head? sublist lst)
(list (cons '-R- (after-head sublist lst))))
((null? lst) nil)
(else (match sublist (cdr lst))))))
(define matches-head?
(lambda (alleged-head lst)
(cond
((null? lst) (null? alleged-head))
((null? alleged-head) t)
(else
(and (eq? (car alleged-head) (car lst))
(matches-head? (cdr alleged-head) (cdr lst)))))))
(define after-head
(lambda (head lst)
(if (null? head)
lst
(after-head (cdr head) (cdr lst)))))
; Help functions
(define some
(lambda (test? lst)
(let ((result nil))
(while (and lst (not (set! result (test? (car lst)))))
(set! lst (cdr lst)))
result)))
(define flatten
(lambda (lst)
(cond
((null? lst) '())
((null? (car lst)) (flatten (cdr lst)))
((atom? (car lst))
(cons (car lst) (flatten (cdr lst))))
(else (append (flatten (car lst))
(flatten (cdr lst)))))))
(define say
(lambda (sentence)
(for-each write sentence)
(newline)))
(define random-element
(lambda (lst)
(list-ref lst (random (length lst)))))