| 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. 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:
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 a Scheme-based program to solve this Eliza-type pattern-matching problem; that is, a user will type eliza as a procedure name, followed by string containing a sequence of words. The program then would respond with the responce indicated by the above.
For example, following the first pattern above, the command
(eliza "well my friend made me come here")
would respond with the result:
"tell me about your friend"
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 ;;; Campus Mailbox ;;; Assignment Name ;;;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ;;; Academic Honesty Certification ;;; 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] ; ;;; Signature: ;;;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Signature must be handwritten in pen—typed signatures not accepted!
A simple review of the patterns and response templates indicates consider commonality for processing. With this in mind, your program must use at least 2 higher-order procedures that capture common processing (perhaps with different words, phrases, or other details.) These higher-order procedures may be used to identify at least 2 patterns and/or respnses. When defined, the higher-order procedures should be used to define at least two separate procedures—each used for a specific pattern or response.
In this program, for consistency with later work 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.
Internally, you may proceed as you wish. If you wish to work with symbols rather than strings, the following procedures (edited from https://stackoverflow.com/questions/47028798/scheme-convert-a-string-to-a-list-without-characters-and-with-spaces) convert a string of words into a list of symbols:
(define (tokenize letter-list)
(let loop ((word-letter-list '())
(ls letter-list))
(if (pair? ls)
(let ((ch (car ls)))
(if (char=? ch #\space)
(cons (reverse word-letter-list) (loop '() (cdr ls)))
(loop (cons ch word-letter-list) (cdr ls))
) ; end if (char=?
) ; end let ((ch
(if (null? word-letter-list)
'()
(list (reverse word-letter-list))
) ; end if (null?
) ; end if (pair?
) ; end let loop
) ; end define
(define (string-split str)
(map string->symbol (map list->string (tokenize (string->list str)))))
As a test case, (string-split "hi there friend")
returns (hi there friend)
Since every programming task should yield readable and carefully tested code, the grading of all programs for this course will begin with the following algorithm (expressed in C/Java format):
if ( (no_comments)
|| (missing pre- or post-conditions)
|| (formatting_does_not_show_structure)
|| (long_procedures_not_divided_into_sections_with_clarifying_comments)
|| (no_evidence_of_compilation)
|| (no_test_plan,__no_listing_of_circumstances__OR__no_listing_of_test_cases)
|| (no_test_runs)
|| (no_commentary_on_correctness)
|| (no_signed_certification_regarding_sources_and_help)
|| (use of Bubble Sort or other non-approved sorting algorithm)
return (no_grade);