| 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) |
||
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 my singleWord ---2 me ---3 | tell me about your singleWord | |
| ---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. For each of the first four patterns (i.e., all but the default pattern at the end), the response include something from the user input. To expand somewhat and avoid possible misinterpretations,
my singleWord pattern,
the ---2 piece may be empty, because
singleWord guarantees at least one word between
my and me. In all other patterns,
---2 must contain at least one word.
Example 1: These patterns might generate the following "conversation," where the responses are indented and in italics:
well my friend made me come here
tell me about your friend
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
why do you think i would help you
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 |
| ---2 | made |
| 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 --- ).
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.
Write Prolog-based facts and/or rules to solve this Eliza-type pattern-matching problem. For example, if you entered the successive dialog strings from the "Sample Dialog" above, the program would respond as the example indicates.
Notes:
In Prolog, a query needs to have both an initial variable and a result variable. Also, to be consistent with earlier work in the course, the user's text should be entered as a string:
(eliza "My friend said I am nice", Result)
As in past Eliza projects, for simplicity, you may assume that the sentence contains no punctuation.
Although not required, you may choose to use
Prolog's split_string rule:
split_string(String, SepChars, PadChars, SubStrings)
where
String is an initial string of characters.
SepChars is a string that identifies the sequence
of one or more characters used to separate the
original String into pieces
PadChars is a string of characters that are
removed from the front or back of words within the new list.
Substrings is the resulting list of words from the original
string.
For our purposes, we expect a user to enter a string with spaces
between words, and the user is not expected to include additional
characters (so there are no padding characters). That is, in common
usage, SepChars will be a string containing a single space,
and PadChars will be an empty string. For example,
split_string("Hello How Are You Today", " ", "", Split).
returns Split = ["Hello", "How", "Are", "You", "Today"].
Also, although not required, you may choose to use
Prolog's atomics_to_string rule:
atomics_to_string(ListOfStrings, SepChars, Result)
where
ListOfStrings is a list of words.
SepChars consists of one or more characters that
will separate the words in the resulting string.
Result is the resulting string.
In the Eliza application, words likely should be separated by a space. For example,
atomics_to_string(["this", "is", "a", "test"], " ", Result).
returns Result = "this is a test".
Overall, although not required, one appropriate underlying form for a Prolog version of Eliza might utilize the following framework:
eliza(UserInput, Result) :- split_string(UserInput, " ", "", WordList),
eliza_kernel(WordList, ResponseList),
atomics_to_string(ResponseList, " ", Result).
where eliza_kernel involves one or more rule patterns that
convert a list of words into a list of word responses.
The coding for this problem can be done in 6-12 rule
definitions/patterns. (My solution uses 6 rule
patterns—one of which is the eliza rule
above and five of which handle separate patterns within
the eliza_kernel.) Code with more than 20 rule
definitions/patterns may lose some credit for unnecessary
complexity.
The nature of the Eliza problem is to generate one response for each user input. Thus, the program should use cuts, as appropriate, to avoid multiple possible responses to a query.
As with all computing applications, thorough testing is required for this project. Code submitted without thorough testing will not be graded and thus receive a total score of zero.
In submitting your program and testing please submit
Please email both files as attachments to walker@cs.grinnell.edu.