CS 291 University of Puget Sound Spring, 2020 Programming Language Paradigms: Explorations with Functional Problem Solving (supported by Scheme/Haskell) and Declarative Prblem Solving (supported by Prolog)

# A Simplified Eliza Program

## Haskell Version (minor editing: March 14, 2020)

One of the early, well-known programs within the field of artificial intelligence was called Eliza, a program by Joseph Weizenbaum. The idea was to simulate conversation between a patient and psycho-therapist, based on a series of patterns and corresponding responses. Although the original program had an extensive sequence of patterns, this assignment focuses on the following five rules:

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.

Example 1: 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. For example, in the first statement:

well my friend made me come here

The presence of the word `my` near the start of the sentence with the word `me` later on corresponds to the first pattern, with the following matches:

Pattern Element     Words from this Sentence
---1 well
my my
singleWord friend
me me
---3 come here

After identifying these pattern elements for the first pattern, the Eliza program uses the corresponding response template. In this case, the program prints "Tell me about your friend". The first four of these words come directly from the template. For the final word, singleWord in the template was matched with the word "friend" in the above analysis.

Example 2: Consider the statement:

My friend said i am nice.

Although this sentence starts with the word my, the word me does not appear later in the text, so the first pattern (my --- me) does not apply. Rather, the response should be based on the second pattern (i am --- ).

#### Historical Note

Although this approach may seem simple today, Joseph Weizenbaum used this approach as the basis for his widely heralded Eliza in 1966. In 1976, Weizenbaum noted he "was startled to see how quickly and how deeply people conversing with [ Eliza] became emotionally involved" in the conversation. People shared their hopes and secrets, and they became annoyed if other people looked over their shoulders or otherwise interrupted. Even when they knew Eliza was a program, they often talked as if it were a close personal friend.

#### Exercise

Write a Haskell-based program to solve this Eliza-type pattern-matching problem. For example, if you entered any of the dialog strings from the "Sample Dialog" above, the program would respond as the example indicates.

Notes:

The very beginning of of every project for this course must contain the following lines (in the language used by the program—e.g., Scheme)

```{- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Name
Assignment Name:  eliza project in Haskell
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Written/online surces used:
[include book(s), course labs or readings, complete Web citations, etc.
or write "none" if no sources used]
Help obtained
[include names of instructor, class members, other students
or write "none" if no others were consulted]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-}
```
• In this program, for consistency with other projects in the course, the user's text is to be entered as a string, perhaps using the following line:

eliza "My friend said I am nice"
• For simplicity, you may assume that the sentence contains no punctuation.

• Since Haskell provides capabilities for processing with both guards and patterns, you should consider the use of one or both of these capabilities for this project.

• In writing your eliza function, all helper functions should be defined locally.

• Since both patterns 1 and 4 search for the word "me" after the second word of a string, this code could be reasonably efficient if that search were done only once—not for each condition and possible response. With that in mind, your solution should perform that search just once—perhaps binding a local variable to the result of the search.