SUNY Geneseo Department of Mathematics

Homework 2—An “Eliza” Program

Math 230 02
Spring 2017
Prof. Doug Baldwin

Complete by Tuesday, May 2
Grade by Monday, May 8

Purpose

This project develops your ability to work with strings and cell arrays, and to devise ways of representing structured information in a program. It also provides additional practice with many programming constructs discussed earlier in this course.

Background

“Eliza” was an early natural language processing program that simulated a psychiatrist prompting a patient (the user) to discuss his or her concerns. The author of Eliza, Joseph Weizenbaum at MIT, deliberately made no effort to make the program intelligent. His point was that obviously unintelligent programs can display superficially intelligent behavior. All that Eliza does is read a string from the user, search it for certain words or patterns of words, and then print a response corresponding to the pattern that it found. To see for yourself how Eliza acts, you can find an example running on the World Wide Web at http://psych.fullerton.edu/mbirnbaum/psych101/Eliza.htm.

The heart of any Eliza-like program is a list of strings that are the patterns to look for, each paired with one or more string responses to print when that pattern appears in the input. We have recently discussed most of the features you need to do this in Matlab. In particular, we discussed strings in class on April 24, and our textbook discusses them in chapter 7. To store the list of pattern/response pairs you can use a cell array, something we will discuss in class on April 26 and that you can learn more about from sections 14.1 through 14.4 of the text.

There is one new Matlab function you might find useful: strfind. A call to strfind looks like

strfind( text, pattern )

where text is any string, and pattern is a second string to search for in text. The result strfind returns is a vector of numbers, being the positions in text where pattern begins. For example, the expression

strfind( 'this is my string', 'is' )

would return the vector [3,6] because the string ‘is’ appears twice in ‘this is my string,’ once starting at the 3rd character, and again starting at the 6th. If pattern does not appear at all in text, strfind returns an empty vector.

Activity

Write a simple Eliza-like program in Matlab. Your program should not conduct a psychiatrist-patient dialog like the actual Eliza does, but rather should talk with its user about a subject of your own choosing. I find that subjects where the program can respond to the user with superficial comments that prompt for more input work best, although you can use any subject you wish.

Your program should loop reading user input and responding to it until the user enters some set “stop” input (perhaps the string “stop”, or “bye”, etc.)

As described above, Eliza has a fixed number of patterns it looks for in the user’s input, and a single response for each pattern. Although not mentioned explicitly earlier, there should also be a default response in case no pattern matches the input. Having a modest number of patterns with one response for each and a default response would be an acceptable solution to this assignment. However, there are many ways you could extend the basic idea, for example:

There are many other possible extensions to the basic Eliza idea, and you are welcome to explore any you think of.

Follow-Up

I will grade this exercise in a face-to-face meeting with you. During this meeting I will look at your solution, ask you any questions I have about it, answer questions you have, etc. Please bring your solution on your computer, as that will speed the process along.

Sign up for a meeting via Google calendar. Please make the meeting 15 minutes long, and schedule it to finish before the end of the “Grade By” date above.