(Updated:  2005/11/13)

ELIZA:  Everything You've Always Wanted a Therapist to Be


Created by: Kathy Lo 

Updated by: Tanya Ellchuk, Holger H. Hoos

Comments to: Holger H. Hoos






Wouldn't it be great if computers could understand what we say?  What about the ability to have an actual conversation with a computer?  What if a computer could take the place of a psychologist, saving patients hundreds of dollars in fees?  Can computers be taught to think?  In this lab you will observe the behavior of a working model of ELIZA and build your own version as well.

Before the Lab

Read through the lab, and any class notes related to the lab. You should have experience using a text editor, such as Notepad or WordPad, and a web browser, for example, Firefox or Internet Explorer.


ELIZA was invented in the early 1960s at MIT by Joseph Weizenbaum.  The name "Eliza" comes from the play Pygmalion, in which a woman named Eliza Doolittle was taught to act and speak like an aristocrat without truly understanding what she was saying.  ELIZA attempts to emulate a Rogerian psychologist by taking a patient's answers and reformulating them as questions.  When it was first released, many people confided their deepest secrets to this computer program, believing it to be human, and some psychologists believed that ELIZA could be used in place of a real psychologist!  Surprisingly enough, the technology behind ELIZA was not very advanced, as you will see!

ELIZA is a classic example of natural language processing (NLP). This field of computing science / psychology / linguistics studies how computers can be taught to understand natural language (such as English) through text or speech, or even how to translate a text into another language.  There are many versions of ELIZA out there, some more "intelligent" than others, but they all rely on one principle:  pattern matching.

But what is a pattern?  A pattern is a specification of what should appear and in what order.  For example, <letter><number><number><letter> is a pattern.  Strings that match this pattern include strings like "c67r" and "p98t".  Strings that don't match this pattern are strings like "he39" and "699s".  In ELIZA, a pattern might look like <anything><" mother "><anything>.  Strings that match this pattern include strings like "I talked to my mother today" and "My mother bought me a pair of shoes", but not "I find him very mothering".  Why?  Note the space before and after the word "mother" in the pattern.

In this lab you will be able to observe this pattern matching in action, as well as write some patterns of your own.


During the Lab

Part I: Ready-made Eliza


  1. Open a web browser (Firefox or Internet Explorer) and go to



  1. Respond to Eliza by typing your text in the Input field and pressing Enter.  Type the following phrases (pressing Enter after each one) and note the response that Eliza gives you. Based on what you know about pattern matching from above, how do you think Eliza arrived at giving you the response you got?
    1. Hi Eliza.  I am feeling very negative.

Respond to Eliza and continue the conversation for a few question / answer exchanges.  What do you notice?

    1. Address Eliza in the second person. Ask her questions about herself.  (For example, ask how she is, how much she charges, etc.).  What happens?
    2. Complain about something in your life.  How does Eliza react?
    3. Tell Eliza about your family.
    4. Type in nonsense of some sort.  How does Eliza respond?
    5. Use your imagination! This is a "wildcard" question - type in anything you want and note the response as before. Feel free to continue the conversation you start as you would with a person.
    6. Use another "wildcard" question and continue the conversation for a few question / answer exchanges as you did in part (f).


Part II: Simple Eliza


  1. Before you begin
    1. Use Windows Explorer to create a new folder called eliza in your home directory.
    2. Double click on this folder to open it.
    3. Inside of this folder, create another folder called two. Any work associated with this part of the lab should be saved in this directory.


  1. Download the following files into your eliza/two subdirectory:
    1. ElizaApplet.java
    2. MyEliza.java
    3. eliza.html


  1. Compile the files:
    1. Open a Command Prompt window using the Start Menu. (Click on Start, then Programs, then Accessories, then Command Prompt).
    2. Type "cd eliza\two".
    3. Type "javac *.java". (Note there is a space between "javac" and the asterisk.)


  1. Open up Firefox or Internet Explorer and open the file eliza.html. This is the Eliza applet that you are going to modify! Try it out - you'll note that it is very limited.


  1. Now, use your favourite text editor to open MyEliza.java.


  1. Modify the initial greeting:
    1. The first thing you will do is modify the initial greeting when you first open up Eliza. Study the code. Remember the first response you saw when you opened up eliza.html? Can you find it? If you have trouble, use a "search" or "find" command, which you can usually find in the menus of your text editor.
    2. Modify this greeting string. Use your imagination! Remember that your string should be in quotes, and that there should be a "\n" before the last quotation mark and a semicolon after the last quotation mark "like this\n";  (The "\n" is a special character meaning "new line" (it has the same effect as pressing Enter) so that your conversation is easier to read).


  1. In the same part of the file you will see a list of phrases such as "I don't understand what you're getting at". These are the responses that Eliza uses when it doesn't know how to respond to what you say (to hide the fact that it actually didn't know what you were talking about). Add at least three more responses:
    1. To do this, position your cursor at the end of the last response, past the quotation mark.
    2. Add a comma.
    3. Press Enter.
    4. Type in another phrase in quotation marks.
    5. For the second and third phrases and beyond, repeat steps (a) to (d)


  1. Next, you are going to expand Eliza's vocabulary.  MyEliza.java has one hashtable called qa with keywords and some canned responses to those keywords.  For the purpose of this lab, think of a hashtable as a lookup table that contains keywords and associated responses.  When the user enters a phrase, Eliza will take words from that phrase, and search the keywords of the hashtable to see if any of these words is contained in the hashtable.  If the word is found as a keyword in the hashtable, Eliza will use the response that is associated with that keyword.


For example, try to visualize the following associated keywords and phrases:


Keyword                     Phrase

“mother”                       “Tell me more about your family.”

“classes”                       “It appears you have some issues with school.”


Now, assume that the user responds to Eliza with the statement “My mother drives me crazy!”.  Eliza will search the keywords of the hashtable, and recognize the word “mother” as a keyword that the table contains.  As a result, Eliza will respond with the phrase associated with this keyword, which is “Tell me more about your family.”.

(You will notice that if Eliza is unable to find a word in the hashtable’s list of keywords, it will respond with a randomly chosen general phrase, such as “Please elaborate on that a little more." or "Please, do go on.")


Try adding at least five more pairs of keywords and associated responses to the hashtable:

    1. Find the lines of code that begin with qa.put
    2. Follow to this pattern to add more pairs to the hashtable underneath the lines of code that are already there: qa.put("yourkeyword", "your canned response"); . Keywords must not contain spaces and must be in lowercase.

To test your modified version of Eliza, enter any sentence containing the word "mother" into the text field and see what Eliza replies. (In fact, the "mother" on its own will do the same thing.) Do the same for "class". Note how the qa.put(...) statements in the code relate to this behaviour.


  1. Don't forget to save and re-compile to see your results (see step 3). Close your browser window, and reopen the eliza.html page.



Part III: More Complicated Eliza


  1. Create a new folder inside of your eliza folder and name it three. All your work associated with this part of the lab should be saved in this folder.


  1. Download the following files into eliza/three:
    1. ElizaApplet.java
    2. MyEliza.java
    3. eliza.html
    4. Compile the Java files (see part II, step 3).
    5. Use Firefox or Internet Explorer to open eliza.html. Try it out.  This version of Eliza is a little bit more complex - it looks for phrases as keyword instead of single words.  For example, try out "I have a problem with you".


  1. Use your favourite text editor to open MyEliza.java. If you like, you may modify the greeting and the list of "phrases used to feign intelligence" as you did in Part II (step #7). To do this, open eliza/two/MyEliza.java in another window and copy and paste your modifications into this file.


  1. This version of Eliza has two hashtables. The keywords hashtable contains pairs of keywords. For example, "classes" is associated with "school". This table is there so that Eliza seems intelligent by asking about school when you talk about your classes.  Just as you did in Part II, add at least five new keyword pairs to this hashtable (do not use the same additions you used in Part II). You may want to read through the next step as well before adding your keyword pairs.



  1. The second hashtable, startPhrases, is a list of pairs of phrases, sometimes containing the word "*KEYWORD*". 


Rather than search for a keyword and respond with the corresponding phrase, this time Eliza searches through a list of phrases, not words, which we will refer to as patterns.  If the pattern is found in the hashtable, Eliza responds with the corresponding phrase.  We will refer to these pairs of phrases as pattern-response pairs.  Visualizing this table (as in Part II step 8), our Keyword is now a pattern, and the Phrase is now called the Response. 


Pattern                                                Response

“I hate *KEYWORD*”                         “Why do you hate *KEYWORD*?”


So, when a user tells Eliza “I hate you”, Eliza looks up and finds the phrase “I hate *KEYWORD*” in the hashtable, and recognizes that the corresponding response is “Why do you hate *KEYWORD*?”.  Because the user has typed “you” as the *KEYWORD* in the pattern, Eliza refers to first hashtable of keyword pairs, and recognizes that the keyword associated with “you” is “me”, and therefore uses “me” in place of the *KEYWORD* in the Response.  Thus, Eliza responds to “I hate you” with “Why do you hate me?”.


Add at least five more patterns that Eliza should watch out for.  The patterns (that is, the left side of the pattern-response pair) must abide by the following rules. 


    1. If you choose to use any punctuation marks in your pattern, put a space between the punctuation mark and any neighbouring characters like this ! (notice the space between "this" and the exclamation mark).
    2. All patterns must be in lowercase.
    3. Your pattern does not have to contain a "*KEYWORD*".  If you would like it to scan for a specific sentence such as "i feel blah today", go for it.


An example of an acceptable pattern would be startPhrases.put("this part has to be in lowercase with a space around *KEYWORD* ", "This part cAn bE wRiTten HOWEVER you want");


  1. Re-compile (see part II) and Refresh your browser to see your results.  If necessary, close and re-open your browser.




Show your TA:

  1. Your code for Part II.  Open eliza.html (from your eliza/two directory) in a web browser to demonstrate the additions you have made to MyEliza.java to your TA.
  2. Your code for Part III.  Open eliza.html (from your eliza/three directory) in a web browser to demonstrate the additions you have made to MyEliza.java to your TA.

Marking scheme: For Part II: modified greeting as in step 6 (1 mark), three new phrases as in step 7 (1.5 marks), five keyword-response phrases as in step 8 (2.5 marks). For Part III: five additional new keywords as in step 4 (2.5 marks), and 5 pattern-response phrases as in step 5 (2.5 marks)

Be prepared to discuss your observations from Part I.


Reference Links

Check out http://en.wikipedia.org/wiki/Eliza for more information on Eliza and http://en.wikipedia.org/wiki/Loebner_prize for pointers to related, somewhat more advanced efforts.