?he 'ELIZA''Page %'
?fo 'Steve Hardy'-%-'\*(DY'
.ce2

A Mini ELIZA
= ==== =====

.br
This handout describes a design for a simplified
version of Joseph Weizenbaum's ELIZA system.  You should
read his paper:
.bi
ELIZA - A computer program for the study of natural
language communication between man and machine.
.ei
if you want to understand this handout to the full.
(You can borrow a copy from the Cognitive Studies Library).

Consider the following 'conversation':
.br
(1)	GOOD DAY  WHAT IS YOUR PROBLEM
 	: I WORRY ABOUT MY WORK
.br
(2)	YOU WORRY ABOUT YOUR WORK
 	: THIS HANDOUT IS A LOT OF TROUBLE
.br
(3)	SUPPOSE THIS HANDOUT IS NOT A LOT OF TROUBLE
 	: DO YOU THINK THAT POSSIBLE
.br
(4)	WE WERE DISCUSSING YOU NOT ME
 	: OH
.br
(5)	YOU ARE BEING SOMEWHAT SHORT WITH ME
 	: NO I AM NOT
.br
(6)	EARLIER YOU SAID YOU WORRY ABOUT YOUR WORK
 	: IT IS BECAUSE OF THE COMPUTER
.br
(7)	DO MACHINES WORRY YOU
 	: THIS ONE DOES
.br
(8)	PLEASE GO ON
 	: MY FAMILY DOES NOT LIKE MACHINES EITHER
.br
(9)	TELL ME MORE ABOUT YOUR FAMILY
 	: EVERYBODY IN MY FAMILY WORRIES ABOUT MACHINES
.br
(10)	IS THERE SOMEONE IN PARTICULAR
 	: YES - ME
.br
As you will know from your background reading, ELIZA responds by
'twisting' what you say.  This handout describes ways of doing this
twisting.
You can play with a simplified version of ELIZA by typing:
 	: LIB ELIZA;
.br
to compile a library program, then, when you get the prompt, type:
 	: ELIZA();

After that keep on typing whatever you wish until you are fed up.
To stop the program type CTRL and X.

.ne 8
.ce 2
Desiging your own version of ELIZA
----------------------------------

I propose that we have a set of functions each of which produces
a particular type of response, for example:-
 	: FUNCTION FAMILY(SENTENCE);
 	:	IF	CONTAINS([MOTHER], SENTENCE)
 	:	THEN	[TELL ME MORE ABOUT YOUR FAMILY]
 	:	ELSE	FALSE
 	:	CLOSE
 	: END;
 	: FUNCTION YOUTHINK(SENTENCE);
 	:	IF	CONTAINS([YOU THINK],SENTENCE)
 	:	THEN	[WE WERE DISCUSSING YOU NOT ME]
 	:	ELSE	FALSE
 	:	CLOSE
 	: END;
.br
These functions take a sentence (represented by a list of words) and
return either a response to the sentence or FALSE.  I am assuming we
will have a monitoring function to call these functions and to deal
with input and output.
.tp6
.bb
Exercises
.br
=========
.br
The command:
 	: LIB ELIZA;
.br
makes functions like CONTAINS, FAMILY and YOUTHINK available.
Make sure you understand what these functions do (don't worry
about how CONTAINS works - that's exercise 2).  Write some more functions
like FAMILY and YOUTHINK.
If you are familiar with the function MATCH (see the MATCH demo) or the library
function MEMBER, you may use them (type HELP MEMBER).

The library package ELIZA also makes available a REPLYTO function.  This
takes two arguments - a sentence and a list of function
names for example:-
 	: REPLYTO(
 	: 	[I WORRY ABOUT MY MOTHER],
 	: 	[YOUTHINK  FAMILY]) =>
.br
REPLYTO tries the functions in turn on the sentence until one
returns a non-FALSE result.  Use REPLYTO to test your response functions.
(You may find it helpful to write a function which repeatedly reads a list,
calls REPLYTO and prints REPLYTO's result.) To print the result, you can use either the print arrow =>, or the
function PPR, which prints a list without the brackets.

To read in a line of text, you can use the function READLINE, thus:
 	: READLINE() -> SENTENCE;

.bb
Can you write the CONTAINS function?  Here is a possible schema: 
 	: FUNCTION CONTAINS(LITTLE,BIG);
 	:	IF	< BIG is empty >
 	:	THEN	FALSE
 	:	ELSEIF	< BIG begins with the element LITTLE >
 	:	THEN	TRUE
 	:	ELSE	< see if LITTLE is in TL of BIG >
 	: 	CLOSE
 	: END;
.br

You will find it useful to have a function called
BEGINSWITH of two arguments LITTLE and BIG, both lists.
It returns TRUE if LITTLE
is equivalent to an initial sublist of BIG, that is, there is some list, L such that:
 	: BIG = LITTLE <> L
.br
.bb
Write the REPLYTO function.  Hint: what does the following mean?
 	: VALOF(HD(LIST))(SENTENCE)
.br
Bear in mind that:
 	:  <EXPR> (<ARGS>)
.br
means 'apply the value of <EXPR> to <ARGS>'.
In this case <EXPR>
is VALOF(HD(LIST)) and <ARGS> is SENTENCE.
.bb
You may find it useful to define a function called HASONEOF which takes
a list of items, and a second list, e.g. SENTENCE, and returns true if
any one of the words in the first list occurs in the second.
.bb
Not all the ELIZA program's output can be explained by functions
as simple as FAMILY and YOUTHINK.  Notice three points:
 	a) The program changes "I" to "YOU" etc. at points 2 and 6.
 	b) The program 'supposes' the reverse of the input at point 3.
 	c) The program refers back to the problem at point 6.

To accomplish this behaviour we will need at least two functions:
.in +8
a) CHANGEPERSON which changes every word in a list from
'second person' (e.g. I to YOU, MY to YOUR, AM to ARE).
.br
b) DENY which 'denies' a statement, for example 
given [THIS\ HANDOUT\ IS\ A\ LOT\ OF\ TROUBLE]
it returns
[THIS\ HANDOUT\ IS\ NOT\ A\ LOT\ OF\ TROUBLE].
.in -8
.br
Write these functions yourself.

We also require that the 'current problem' be saved somewhere, perhaps
by:
 	: FUNCTION CONVERSE();
 	:	VARS PROBLEM;
 	:	PPR(GOOD DAY WHAT IS YOUR PROBLEM);
 	:	PR(NEWLINE);
 	:	READLINE() -> PROBLEM;
 	:	< CONTINUE WITH REST OF CONVERSATION>
 	: END;
We could then have a function which refers back to the problem, for
example:
 	: FUNCTION EARLIER(SENTENCE);
 	:	[EARLIER YOU SAID]
 	:		<> CHANGEPERSON (PROBLEM)
 	: END;
.br

What about making it occasionally alter the value of PROBLEM, e.g.
by assigning SENTENCE to PROBLEM, so that when EARLIER is called
several times it does not always print out the same thing?

Some possible refinements to this system are:
.br
(1) Delete a function after it has been applied successfully.
.br
(2) Shuffle the list of response functions before generating a response.
To do this you will need a function to shuffle a list, try the following
schema:-
.br
.tp9
 	: FUNCTION SHUFFLE(LIST);
 	:	IF	LIST=[]
 	:	THEN	[]
 	:	ELSE	VARS TEMP;
 	:		ONEOF(LIST) -> TEMP;
 	:		[^TEMP] <> SHUFFLE(DELETE(TEMP,LIST))
 	:	CLOSE
 	: END;
.bb
After reading Weizanbaum's paper, you may feel the need for a 
'pattern matcher' - see the MATCH demo.
.sp
The PFUNCTION demo describes a library program which enormously
simplifies the writing of programs like ELIZA.
See the PARRY demo for a development of the ideas embedded in ELIZA.
