-
Notifications
You must be signed in to change notification settings - Fork 0
/
doctor.rkt
267 lines (239 loc) · 6.26 KB
/
doctor.rkt
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
#lang scheme/base
(define (visit-doctor name)
(define (doctor-driver-loop name memories)
(define (reply user-response)
(define (change-person phrase)
(many-replace '((i you) (me you) (am are) (my your) (you i) (are am) (your my)) phrase)
)
(define (qualifier)
(pick-random
'(
(you seem to think)
(you feel that)
(why do you believe)
(why do you say)
(please speak louder, it seemed to me you've said that)
(you should pay no attention to the fact that)
(is it truth that)
)
)
)
(define (get-response-from-predicates response)
(let ( (f (pick-func (check-predicates (triples) response))) )
(if (null? f)
'()
(f response)
)
)
)
(define (check-predicates triples response)
(let ( (right-triples (filter (lambda (x) ((car x) response)) triples)) )
(map (lambda (x) (cdr x)) right-triples)
)
)
(define (pick-func func-weight-pairs)
(define (rand)
(/ (random 1000001) 1000000)
)
(define (normalize-weights)
(define (get-sum)
(let ( (weights (map (lambda (x) (cadr x)) func-weight-pairs)) )
(foldl + 0 weights)
)
)
(let ( (sum (get-sum)))
(map
(lambda (x) (list (car x) (/ (cadr x) sum)))
func-weight-pairs
)
)
)
(define (pick-func-rec func-p-pairs outcome checked)
(if (null? func-p-pairs)
'()
(let ( (next (+ checked (cadar func-p-pairs))) )
(if (<= outcome next)
(caar func-p-pairs)
(pick-func-rec (cdr func-p-pairs) outcome next)
)
)
)
)
(pick-func-rec (normalize-weights) (rand) 0)
)
(define (triples)
(list
(list
(lambda (response)
(let ( (keyword-answer (get-keywords-answer response (keywords-answers))) )
(not (null? keyword-answer))
)
)
(lambda (response)
(get-keywords-answer response (keywords-answers))
)
1
)
(list
(lambda (user-response) (< (length user-response) 3))
(lambda (response) '(Could you say more?))
1
)
(list
(lambda (response) #t)
(lambda (response) (append (qualifier) (change-person response)))
100
)
(list
(lambda (response) (> (length memories) 0))
(lambda (response) (append '(earlier you said that) (pick-random memories)))
1000
)
(list
(lambda (response) #t)
(lambda (response) (hedge))
1
)
)
)
(define (hedge)
(pick-random
'(
(please go on)
(many people have the same sorts of feelings)
(many of my patients have told me the same thing)
(please continue)
(by the way, what do you think about last United States House of Representatives elections?)
(don't you mind listening some Portnoy? It helps)
(would you like some soft French buns and tea?)
)
)
)
(get-response-from-predicates user-response)
)
(newline)
(print '**)
(let ( (user-response (read)) )
(cond
((equal? user-response '(goodbye))
(printf "Goodbye, ~a!\n" name)
(printf "see you next week\n")
(let ( (new-name (ask-patient-name)) )
(if (equal? new-name 'enough!)
(print '(Time to go!))
(visit-doctor new-name)
)
)
)
(else
(print (reply user-response))
(doctor-driver-loop name (cons user-response memories))
)
)
)
)
(printf "Hello, ~a!\n" name)
(print '(what seems to be the trouble?))
(doctor-driver-loop name '())
)
(define (contains-word? lst word)
(if (null? lst)
#f
(if (equal? (car lst) word)
#t
(contains-word? (cdr lst) word)
)
)
)
(define (search-in-map phrase map)
(if (null? phrase)
'()
(let ( (word (car phrase)) )
(if (contains-word? (car map) word)
(many-replace (list (list '__ word)) (pick-random (cadr map)))
(search-in-map (cdr phrase) map)
)
)
)
)
(define (get-keywords-answer phrase answers)
(if (null? answers)
'()
(let ( (search-in-map-res (search-in-map phrase (car answers))) )
(if (not (null? search-in-map-res))
search-in-map-res
(get-keywords-answer phrase (cdr answers))
)
)
)
)
(define (keywords-answers)
'(
(
(depressed suicide)
(
(when you feel depressed, go out for an ice cream)
(depression is a disease that can be treated)
)
)
(
(mother father parents)
(
(tell me more about your __)
(why do you feel that way about your __ ?)
)
)
(
(tired lazy)
(
(get back to work, you sluggard fool!)
(you certainly should take a vacation)
)
)
(
(loneliness depression desolation bore)
(
(a career in politics will releive you from the __)
(maybe cannabis can suppress the feeling of __)
(to get rid of __, try to marry someone)
)
)
)
)
;p(true) = n1 / n2
(define (prob n1 n2)
(< (random n2) n1)
)
(define (strategy)
(random 4)
)
(define (ask-patient-name)
(printf "NEXT!\n")
(printf "Who are you?\n")
(car (read))
)
(define (pick-random lst)
(list-ref lst (random (length lst)))
)
(define (replace-word replacement-pairs word)
(if (null? replacement-pairs)
word
(let ((pair (car replacement-pairs)))
(if (equal? (car pair) word)
(cadr pair)
(replace-word (cdr replacement-pairs) word)
)
)
)
)
(define (many-replace replacement-pairs lst)
(cond
((null? lst) '())
(else
(cons
(replace-word replacement-pairs (car lst))
(many-replace replacement-pairs (cdr lst))
)
)
)
)