CSC 153 Grinnell College Spring, 2005 Computer Science Fundamentals Laboratory Exercise

# Eliza: An Example of Object-Oriented Design

## Goal

This laboratory exercise explores an object-oriented solution to a problem based on a famous porgram called Eliza, developed by Joseph Weizenbaum.

## Problem

Consider the following problem statement:

Pattern matching and transformation rules occur frequently in symbolic computation. The following patterns and corresponding response templates can be used to generate "conversations".

Pattern     Response Template
---1 i am ---2 i am sorry to hear you are ---2
---1 am i ---2 do you believe you are ---2
---1 you ---2 me --- 3 why do you think i ---2 you
---1 in what way

In this table, the blank spaces in a pattern, denoted by subscripted dashes, can be filled in with any sequence of words. Similarly, singleWord represents exactly one word in a sentence. For example, these patterns might generate the following "conversation," where the responses are indented and in italics:

### Sample Dialogue

well my friend made me come here
he says i am depressed
i am sorry to hear you are depressed
i think i need help
in what way
i wonder if you would help me learning to program Scheme
when i registered for the course i wondered am i crazy
do you believe you are crazy

Each of these statements and responses follows the template/response patterns given.

The reading for this lab shows how the first sentence in the sample dialogue maps to the first pattern and response template.

1. Identify the patterns for each of the other sentences in the above "conversation". Then, for each pattern, write out which words correspond to which pattern elements, and use this analysis to explain each result printed by the Eliza program.

2. Brainstorm to determine another pattern template and response. Show two examples to illustrate how your template and response might fit into a conversation.

Also, it seems reasonable for the user to quit by typing "exit". The program then should respond program terminated" and stop. Within the current context, it seems natural to consider this interaction as another pattern/response pair, with a special final action.

### An Object-Oriented Solution

The reading for this lab identifies the following major objects in an object-oriented solution to this problem:

 input line - with 1 line for each user's input 6 patterns - each of which which includes a template with text/words, wildcards, and singleword designators 6 responses - corresponding to the patterns, each includes a template with text/words, wildcards, and singleword designators directory/table - which matches text/words with wildcards and singleword designators

The reading also discusses the following diagram shows a sequence of messages that might arise if an input line matches pattern 2.

1. The diagram shows 13 steps involved in processing a line that matches pattern 2. Write a sentence or two that explains what processing happens for each of these steps.

2. Now consider processing that occurs in processing the line "i wonder if you would help me learning to program Scheme". Outline each of the steps involved.

3. For each class in the solution of the problem, make a list of all operations or methods used in processing (at least following the above processing diagram).

### Programing the Solution

The reading for this lab describes both Scheme and C++ programs that follow this object-oriented approach to the Eliza problem:

• Scheme:
• A full Scheme program may be found in ~walker/oop/eliza/eliza.ss
• As Scheme allows incremental development and testing of procedures, a series of test cases were created as each class was written. These test cases may be found in ~walker/oop/eliza/eliza-tests.ss
• The original program development placed code and test cases together in a single file. This integrated file may be found in ~walker/oop/eliza/eliza--with-tests.ss
• The complete program is 228 lines long, and the test-case file contains another 77 lines.

• C++:
• A full C++ program may be found in ~walker/oop/eliza/eliza.cc
• This program uses the apstring class, developed for the Advanced Placement Computer Science Examination. The required files are available at ~walker/oop/eliza/apstring.h and ~walker/oop/eliza/apstring.cpp.
• The complete program is 359 lines long.
1. Review in some detail the Scheme version of the solution of this problem, copying the program to your account, loading it into Scheme, and running it with a conversation including the dialog at the beginning of this lab.

2. Review the structure of the Scheme program.

1. Identify various sections of the code, giving line numbers and description for each section. In xemacs, one can determine the line number of any line by pressing <control> and the lower-case letter x simultaneously, followed by a lower-case letter l.
2. Note that the defintion of each class is accompanied by a brief comment of what the class does and by a sequence of test cases. The tests allow each class to be tested individually!
3. Using your list of operations required for class lineDirectory (from step 5 above), use the test cases described in the file to describe how each class responds to each method. In other words, describe what is returned by the insert and find methods. Also, describe what the clear method does?
4. Similarly, use test cases to describe how the other classes respond when each method is involked.

3. Examine the structure of the main Eliza procedure to understand the main flow of processing. Then, expand the procedure to include the addtional pattern and response you suggested in step 2 above. Run the revised program, including tests of both the original user statements and the examples you suggested in step 2.

This document is available on the World Wide Web as

```http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/lab-oo-eliza.shtml
```