-
Notifications
You must be signed in to change notification settings - Fork 54
/
eliza.m
683 lines (578 loc) · 20.1 KB
/
eliza.m
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
%-----------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
%
% File: eliza.m.
% Main author: bromage.
%
% This source file is hereby placed in the public domain. -bromage.
%
% Eliza, the famous psychotherapist.
%
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- module eliza.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module assoc_list.
:- import_module bool.
:- import_module char.
:- import_module list.
:- import_module map.
:- import_module maybe.
:- import_module pair.
:- import_module string.
:- import_module require.
%-----------------------------------------------------------------------------%
% Print the opening banner, initialise the response state, run the main
% loop.
%
main(!IO) :-
io.write_string("\nHi! I'm Eliza. Please tell me your problem.\n", !IO),
initial_state(State),
main_loop([], State, !IO),
io.write_string("\nGoodbye.\n", !IO).
:- pred main_loop(list(string)::in, eliza_state::in, io::di, io::uo) is det.
main_loop(Prev, StateIn, !IO) :-
eliza.read_line(MaybeLine, !IO),
(
MaybeLine = yes(Line0),
parse(Line0, Line1),
(
Line1 = [],
main_loop(Prev, StateIn, !IO)
;
Line1 = [_ | _],
( if Line1 = Prev then
generate_repeat(StateIn, StateOut, !IO)
else
generate_response(Line1, StateIn, StateOut, !IO)
),
main_loop(Line1, StateOut, !IO)
)
;
MaybeLine = no
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% Response state processing functions
% The response state consists of two parts: a set of
% (type - messages) pairs and a list of repeat messages.
% The response messages are used in response to a context,
% and the repeat messages are used in response to a repeated
% input line.
:- type response_state == assoc_list(message_type, list(message)).
:- type repeat_state == list(string).
:- type eliza_state
---> state(response_state, repeat_state).
% Initialise the state by reading in the initial message
% database.
%
:- pred initial_state(eliza_state::out) is det.
initial_state(state(ResMsg,RepMsg)) :-
repeat_messages(RepMsg),
response_messages(ResMsg).
% Get a repeat message, and then cycle the list so that
% a new one will come up next time.
%
:- pred get_repeat(string::out, eliza_state::in, eliza_state::out) is det.
get_repeat(MsgOut, state(Res, RepIn), state(Res, RepOut)) :-
(
RepIn = [Msg | Rest],
MsgOut = Msg,
list.append(Rest, [Msg], RepOut)
;
RepIn = [],
error("Error: No repeat messages.\n")
).
% Get a response message, and then cycle the list so that
% a new one will come up next time.
%
:- pred get_response(message_type::in, message::out,
eliza_state::in, eliza_state::out) is det.
get_response(Type, MsgOut, state(ResIn, Rep), state(ResOut, Rep)) :-
get_response2(Type, MsgOut, ResIn, ResOut).
:- pred get_response2(message_type::in, message::out,
response_state::in, response_state::out) is det.
get_response2(_Type, _MsgOut, [], []) :-
error("Error: Cannot match message type.\n").
get_response2(Type, MsgOut,
[Type2 - Msgs2 | RestIn],
[Type2 - Msgs3 | RestOut]) :-
( if Type = Type2 then
( if Msgs2 = [MsgOut1 | MsgOutRest] then
MsgOut = MsgOut1,
RestOut = RestIn,
list.append(MsgOutRest, [MsgOut], Msgs3)
else
error("Error: Empty response list.\n")
)
else
Msgs2 = Msgs3,
get_response2(Type, MsgOut, RestIn, RestOut)
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% Write a prompt, then read a line.
%
:- pred read_line(maybe(list(char))::out, io::di, io::uo) is det.
read_line(MaybeLine, !IO) :-
io.write_string("\n> ", !IO),
io.flush_output(!IO),
io.input_stream(Stdin, !IO),
io.read_line(Stdin, Result, !IO),
io.write_string("\n", !IO),
(
Result = ok(Line1),
MaybeLine = yes(Line1)
;
( Result = eof
; Result = error(_)
),
MaybeLine = no
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% These are the characters that we must strip from a line during parsing.
%
:- pred is_punct(char::in) is semidet.
is_punct('.').
is_punct(',').
is_punct('!').
is_punct('?').
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% Parse the input string into words. This is
% achieved in three phases:
%
% - Strip leading whitespace and intermediate
% punctuation symbols.
%
% - Turn the resulting line into a list of words (each
% word stored as a list of characters).
%
% - Turn each word into a string.
%
:- pred parse(list(char)::in, list(string)::out) is det.
parse -->
strip,
form_words,
words_to_strings.
:- pred strip(list(char)::in, list(char)::out) is det.
strip([], []).
strip([X | Xs], Ys) :-
( if char.is_whitespace(X) then
strip(Xs, Ys)
else
strip2([X | Xs], Ys)
).
:- pred strip2(list(char)::in, list(char)::out) is det.
strip2([], []).
strip2([X | Xs], Ys) :-
( if is_punct(X) then
strip2([' ' | Xs], Ys)
else
strip2(Xs, Ys1),
( if char.is_whitespace(X), Ys1 = [] then
Ys = []
else
Ys = [X | Ys1]
)
).
:- pred form_words(list(char)::in, list(list(char))::out) is det.
form_words([], []).
form_words([X | Xs], Ys) :-
( if char.is_whitespace(X) then
form_words(Xs, Ys)
else
form_word(Xs, [X], Word, Rest),
form_words(Rest, Words),
Ys = [Word | Words]
).
:- pred form_word(list(char)::in, list(char)::in,
list(char)::out, list(char)::out) is det.
form_word([], Word1, Word2, []) :-
list.reverse(Word1, Word2).
form_word([X | Xs], WordIn, WordOut, Rest) :-
( if char.is_whitespace(X) then
list.reverse(WordIn, WordOut), Rest = Xs
else
form_word(Xs, [X | WordIn], WordOut, Rest)
).
:- pred words_to_strings(list(list(char))::in, list(string)::out) is det.
words_to_strings([], []).
words_to_strings([X | Xs], [Y | Ys]) :-
string.from_char_list(X, Y),
words_to_strings(Xs, Ys).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
% Generate and display a repeat message
%
:- pred generate_repeat(eliza_state::in, eliza_state::out,
io::di, io::uo) is det.
generate_repeat(!State, !IO) :-
get_repeat(Msg, !State),
io.write_string(Msg, !IO),
io.write_string("\n", !IO).
% Generate and display a repeat message
%
:- pred generate_response(list(string)::in,
eliza_state::in, eliza_state::out, io::di, io::uo) is det.
generate_response(Words, !State, !IO) :-
% Find out what sort of message we are dealing with.
find_handle(Words, MsgType, Rest),
get_response(MsgType, Maybe - String, !State),
io.write_string(String, !IO),
% If we must parrot back part of the original message,
% resolve conjugates, write that string and then add
% a trailing punctuation mark.
(
Maybe = yes(C),
perform_conjugate(Rest, Postfix),
eliza_write_strings(Postfix, !IO),
io.write_char(C, !IO)
;
Maybe = no
),
io.write_string("\n", !IO).
% Write a list of strings to the output stream with
% words thrown in between.
%
:- pred eliza_write_strings(list(string)::in, io::di, io::uo) is det.
eliza_write_strings([], !IO).
eliza_write_strings([X | Xs], !IO) :-
io.write_char(' ', !IO),
io.write_string(X, !IO),
eliza_write_strings(Xs, !IO).
:- pred match_prefix(list(string)::in,
list(string)::in, list(string)::out) is semidet.
match_prefix([], Ys, Ys).
match_prefix([X | Xs], [Y | Ys], Zs) :-
string.to_upper(Y, X),
match_prefix(Xs,Ys,Zs).
:- pred find_handle(list(string)::in, message_type::out,
list(string)::out) is det.
find_handle(In, MsgType, Out) :-
response_handles(Handles),
find_handle2(In, MsgType, Out, Handles).
:- pred find_handle2(list(string)::in, message_type::out,
list(string)::out, assoc_list(list(string), message_type)::in) is det.
find_handle2(In, no_key_message, In, []).
find_handle2(In, Type, Out, [Prefix - Type2 | Handles]) :-
( if find_handle3(In, Prefix, Rest) then
Out = Rest, Type = Type2
else
find_handle2(In, Type, Out, Handles)
).
:- pred find_handle3(list(string)::in, list(string)::in, list(string)::out)
is semidet.
find_handle3([X | Xs], Prefix, Rest) :-
( if match_prefix(Prefix, [X | Xs], Rest2) then
Rest = Rest2
else
find_handle3(Xs, Prefix, Rest)
).
:- pred perform_conjugate(list(string)::in, list(string)::out) is det.
perform_conjugate([], []).
perform_conjugate([X | Xs], [Y | Ys]) :-
( if ( X = "I", Xs = [] ) then
Y = "me", Ys = []
else
conjugate_map(Map),
string.to_upper(X, Xupp),
( if map.search(Map, Xupp, Result) then
Y = Result
else
Y = X
),
perform_conjugate(Xs, Ys)
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- type message == pair(maybe(char), string).
:- type message_type
---> can_you
; can_i
; you_are
; i_dont
; i_feel
; why_dont
; why_cant
; are_you
; i_cant
; i_am
; you
; yes
; no
; computer
; i_want
; question
; name
; because
; sorry
; dream
; hello
; maybe
; your
; always
; think
; alike
; friend
; no_key_message.
:- pred conjugate_map(map(string, string)::out) is det.
conjugate_map(MapOut) :-
one_way_conjugates(AL1),
two_way_conjugates(AL2),
assoc_list.reverse_members(AL2, AL3),
list.append(AL1, AL2, AL12),
list.append(AL12, AL3, AL123),
prepare_conj(AL123, ALFinal),
map.from_assoc_list(ALFinal, MapOut).
:- pred prepare_conj(assoc_list(string, string)::in,
assoc_list(string, string)::out) is det.
prepare_conj([], []).
prepare_conj([X - V | Xs], [Y - V | Ys]) :-
string.to_upper(X,Y),
prepare_conj(Xs, Ys).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- pred response_handles(assoc_list(list(string), message_type)::out) is det.
response_handles([
["CAN","YOU"] - can_you,
["CAN","I"] - can_i,
["YOU","ARE"] - you_are,
["YOU'RE"] - you_are,
["I","DON'T"] - i_dont,
["I","FEEL"] - i_feel,
["WHY","DON'T","YOU"] - why_dont,
["WHY","CAN'T","I"] - why_cant,
["ARE","YOU"] - are_you,
["I","CAN'T"] - i_cant,
["I","AM"] - i_am,
["I'M"] - i_am,
["YES"] - yes,
["NO"] - no,
["COMPUTER"] - computer,
["COMPUTERS"] - computer,
["I","WANT"] - i_want,
["WHAT"] - question,
["HOW"] - question,
["WHO"] - question,
["WHERE"] - question,
["WHEN"] - question,
["WHY"] - question,
["NAME"] - name,
["BECAUSE"] - because,
["CAUSE"] - because,
["SORRY"] - sorry,
["DREAM"] - dream,
["DREAMS"] - dream,
["HI"] - hello,
["HELLO"] - hello,
["MAYBE"] - maybe,
["YOUR"] - your,
["ALWAYS"] - always,
["THINK"] - think,
["ALIKE"] - alike,
["FRIEND"] - friend,
["FRIENDS"] - friend,
["YOU"] - you
]).
:- pred one_way_conjugates(assoc_list(string, string)::out) is det.
one_way_conjugates([
"me" - "you"
]).
:- pred two_way_conjugates(assoc_list(string, string)::out) is det.
two_way_conjugates([
"are" - "am",
"were" - "was",
"you" - "I",
"your" - "my",
"I've" - "you've",
"I'm" - "you're"
]).
:- pred repeat_messages(repeat_state::out) is det.
repeat_messages([
"Why did you repeat yourself?",
"Do you expect a different answer by repeating yourself?",
"Come, come, elucidate your thoughts.",
"Please don't repeat yourself!"
]).
:- pred response_messages(response_state::out) is det.
response_messages(
[
can_you - [
yes('?') - "Don't you believe that I can",
yes('?') - "Perhaps you would like to be able to",
yes('?') - "You want me to be able to"
],
can_i - [
yes('?') - "Perhaps you don't want to",
yes('?') - "Do you want to be able to"
],
you_are - [
yes('?') - "What makes you think I am",
yes('?') - "Does it please you to believe I am",
yes('?') - "Perhaps you would like to be",
yes('?') - "Do you sometimes wish you were"
],
i_dont - [
yes('?') - "Don't you really",
yes('?') - "Why don't you",
yes('?') - "Do you wish to be able to",
no - "Does that trouble you?"
],
i_feel - [
no - "Tell me more about such feelings.",
yes('?') - "Do you often feel",
yes('?') - "Do you enjoy feeling"
],
why_dont - [
yes('?') - "Do you really believe I don't",
yes('.') - "Perhaps in good time I will",
yes('?') - "Do you want me to"
],
why_cant - [
yes('?') - "Do you think you should be able to",
yes('?') - "Why can't you"
],
are_you - [
yes('?') - "Why are you interested in whether or not I am",
yes('?') - "Would you prefer if I were not",
yes('?') - "Perhaps in your fantasies I am"
],
i_cant - [
yes('?') - "How do you know you can't",
no - "Have you tried?",
yes('?') - "Perhaps you can now"
],
i_am - [
yes('?') - "Did you come to me because you are",
yes('?') - "How long have you been",
yes('?') - "Do you believe it is normal to be",
yes('?') - "Do you enjoy being"
],
you - [
no - "We were discussing you --not me.",
yes('?') - "Oh,",
no - "You're not really talking about me, are you?"
],
yes - [
no - "You seem quite positive.",
no - "Are you sure?",
no - "I see.",
no - "I understand."
],
no - [
no - "Are you saying no just to be negative?",
no - "You are being a bit negative.",
no - "Why not?",
no - "Are you sure?",
no - "Why no?"
],
computer - [
no - "Do computers worry you?",
no - "Are you talking about me in particular?",
no - "Are you frightened by machines?",
no - "Why do you mention computers?",
no - "What do you think machines have to do with your problems?",
no - "Don't you think computers can help people?",
no - "What is it about machines that worries you?"
],
i_want - [
yes('?') - "Why do you want",
yes('?') - "What would it mean to you if you got",
yes('?') - "Suppose you got",
yes('?') - "What if you never got",
yes('.') - "I sometimes also want"
],
question - [
no - "Why do you ask?",
no - "Does that question interest you?",
no - "What answer would please you the most?",
no - "What do you think?",
no - "Are such questions on your mind often?",
no - "What is it that you really want to know?",
no - "Have you asked anyone else?",
no - "Have you asked such questions before?",
no - "What else comes to mind when you ask that?"
],
name - [
no - "Names don't interest me.",
no - "I don't care about names --please go on."
],
because - [
no - "Is that the real reason?",
no - "Don't any other reasons come to mind?",
no - "Does that reason explain anything else?",
no - "What other reasons might there be?"
],
sorry - [
no - "Please don't apologise!",
no - "Apologies are not necessary.",
no - "What feelings do you have when you apologise?",
no - "Don't be so defensive!"
],
dream - [
no - "What does that dream suggest to you?",
no - "Do you dream often?",
no - "What persons appear in your dreams?",
no - "Are you disturbed by your dreams?"
],
hello - [
no - "How do you...please state your problem."
],
maybe - [
no - "You don't seem quite certain.",
no - "Why the uncertain tone?",
no - "Can't you be more positive?",
no - "You aren't sure?",
no - "Don't you know?"
],
your - [
yes('?') - "Why are you concerned about my",
yes('?') - "What about your own"
],
always - [
no - "Can you think of a specific example?",
no - "When?",
no - "What are you thinking of?",
no - "Really, always?"
],
think - [
no - "Do you really think so?",
yes('?') - "But you are not sure you",
yes('?') - "Do you doubt you"
],
alike - [
no - "In what way?",
no - "What resemblance do you see?",
no - "What does the similarity suggest to you?",
no - "What other connections do you see?",
no - "Could there really be some connection?",
no - "How?"
],
friend - [
no - "Why do you bring up the topic of friends?",
no - "Do your friends worry you?",
no - "Do your friends pick on you?",
no - "Are you sure you have any friends?",
no - "Do you impose on your friends?",
no - "Perhaps your love for friends worries you."
],
no_key_message - [
no - "I'm not sure I understand you fully.",
no - "What does that suggest to you?",
no - "I see.",
no - "Can you elaborate on that?",
no - "Say, do you have any psychological problems?"
]
]).
%-----------------------------------------------------------------------------%
:- end_module eliza.
%-----------------------------------------------------------------------------%