;;==========================================================================
;;
;; STARTER FILE FOR CSC 4240/5240 PROGRAM #1: Eliza
;;==========================================================================
;;----------------------------------------------------------------------------
;; eliza: top-level function which, when given a sentence (no
;; punctuation, please!), comes back with a response like you would.
( defun eliza ( sentence )
( respond ( change-pros sentence ) database ) )
;;----------------------------------------------------------------------------
;; change-pros: changes the pronouns of the sentence so that Eliza can
;; come back with the appropriately switched first and second person
;; references.
( defun change-pros ( sentence )
( cond
( ( null sentence ) nil )
( ( equal ( car sentence ) 'you )
( cons 'I ( change-pros ( cdr sentence ) ) ) )
( ( equal ( car sentence ) 'I )
( cons 'you ( change-pros ( cdr sentence ) ) ) )
;; CHANGE THIS: add more cases here of pronouns or other words
;; that should flip in order for this to work well
((equal ( car sentence ) 'my)
(cons 'your (change-pros (cdr sentence))))
((equal ( car sentence ) 'your)
(cons 'my (change-pros (cdr sentence))))
((equal (car sentence) 'im)
(cons 'you (cons 'are (cdr sentence))))
((equal ( car sentence ) 'you)
(cons 'im (change-pros (cdr sentence))))
((equal ( car sentence ) 'mine)
(cons 'yours (change-pros (cdr sentence))))
((equal ( car sentence ) 'yours)
(cons 'mine (change-pros (cdr sentence))))
((equal ( car sentence ) 'he)
(cons 'they (change-pros (cdr sentence))))
((equal ( car sentence ) 'she)
(cons 'they (change-pros (cdr sentence))))
((equal ( car sentence ) 'her)
(cons 'them (change-pros (cdr sentence))))
((equal ( car sentence ) 'his)
(cons 'them (change-pros (cdr sentence))))
((equal ( car sentence ) 'me)
(cons 'you (change-pros (cdr sentence))))
((equal ( car sentence ) 'you)
(cons 'me (change-pros (cdr sentence))))
((equal ( car sentence ) 'am)
(cons 'are (change-pros (cdr sentence))))
((equal ( car sentence ) 'are)
(cons 'am (change-pros (cdr sentence))))
( t ( cons ( car sentence ) ( change-pros ( cdr sentence ) ) ) ) ) )
;;----------------------------------------------------------------------------
;; respond: given a sentence, looks through the database in search of
;; a matching pattern and the response; given the database response,
;; uses 'instantiate' to fill in the blanks, and returns the completed
;; response
( defun respond ( sentence db )
( cond
;; end of DB, return nil - should never really happen
( ( null db ) nil )
;; if the result of matching the sentence against the current
;; pattern is a success, produce this response
( ( success ( setq result ( match sentence ( first ( car db ) ) ) ) )
( instantiate result ( second ( car db ) ) ) )
;; otherwise, keep looking through the DB
( t ( respond sentence ( cdr db ) ) ) ) )
;;----------------------------------------------------------------------------
;; match: if there is not a match between this pattern and this data,
;; returns 'fail;' otherwise, returns the sentence in partitioned
;; format
( defun match ( data pattern )
( cond
;; end of both data and pattern; a match
( ( and ( null data ) ( null pattern ) ) nil )
;; end of pattern, but not end of data; no match
( ( null pattern ) fail )
;; end of data, but not end of pattern; if the pattern starts with
;; a variable, eat it and try and match the rest of the pattern to
;; the null sentence (will only work if all variables); otherwise,
;; fail
( ( null data )
( cond
( ( variablep ( car pattern ) )
( if ( success ( setq result ( match data ( cdr pattern ) ) ) )
result
fail ) )
( t fail ) ) )
;; first item of data and pattern are identical; if the rest of it
;; matched, return the first item cons'ed with the rest of the
;; partitioned sentence; otherwise, fail
( ( equal ( car data ) ( car pattern ) )
( if ( success ( setq result ( match ( cdr data ) ( cdr pattern ) ) ) )
( cons ( list ( car data ) ) result )
fail ) )
;; first item of pattern is a variable; if the rest of the data
;; (minus the first word, matched to the variable) is a match with
;; all of the pattern, return the appropriate stuff; if all of the
;; data (variable eats nothing) matches the rest of the pattern,
;; return appropriate stuff; else, fail.
( ( variablep ( car pattern ) )
( cond
;; variable eats nothing; () is put in partitioned sentence
( ( success ( setq result ( match data ( cdr pattern ) ) ) )
( cons () result ) )
;; variable eats one word; word is cons'ed into the first
;; element of the partitioned sentence, assuming that the step
;; before an actual match word would be a ()
( ( success ( setq result ( match ( cdr data ) pattern ) ) )
( cons ( cons ( car data ) ( car result ) ) ( cdr result ) ) )
;; otherwise, fail
( t fail ) ) )
( t fail ) ) )
;;----------------------------------------------------------------------------
;; instantiate: takes a partitioned sentence and the response it has
;; been matched to and generates the appropriated completed response
( defun instantiate ( partitioned response )
( cond
( ( null response ) nil )
;; numbers indicate what part of the partitioned sentence to
;; insert into the response
( ( numberp ( car response ) )
( setq index ( - ( car response ) 1 ) )
( append ( nth index partitioned )
( instantiate partitioned ( cdr response ) ) ) )
( t ( cons ( car response )
( instantiate partitioned ( cdr response ) ) ) ) ) )
;;---------------------------------------------------------------------------
;;
;; helping functions
;;
;;---------------------------------------------------------------------------
( setq fail '-1 )
( defun success ( result )
( not ( equal result fail ) ) )
( defun variablep ( word )
( equal word '0 ) )
(defun random-response-from (responses)
(nth (random (length responses)) responses))
;;---------------------------------------------------------------------------
;;
;; database
;;
;;---------------------------------------------------------------------------
;; CHANGE THIS: add more to this database so that the interaction is
;; more interesting and communicative and so that Eliza sounds like you
;; would sound in the same conversation!
;;---------------------------------------------------------------------------
( setq database
'(
;; example greetings/farewells -- change them to sound like you
((Hello 0)
("Whats up? Everything been good today?"))
((Goodbye 0)
("Ill see you later, take it easy!"))
((Whats up 0)
("Not much, just waiting for you to ask me a question..."))
((Hi 0)
("Hey, what can I help you with today?"))
((See you later 0)
("Absolutely! Till next time."))
((Ill see ya 0)
("Later!"))
((Bye 0)
("Alright, have a good one!"))
;; feelings
((0 you think 0)
("And why do you think" 4 "?"))
((0 Im happy 0)
(How long have you been happy?))
((0 Im sad 0)
(Why are you feeling sad?))
((0 im stressed 0)
(What is causing you stress?))
((0 I need advice 0)
(What do you need advice for?))
((0 school 0)
(What about school is on your mind?))
((0 work 0)
(What about work is on your mind?))
;; the catch-alls
( (0)
("Could you expand on that?")
("Can you reword that? I don't think I'm getting it.")
("It sounds like you need to take a break, do you agree?")
("What do you think your mom would say about this?"))))
(setq response (eliza '(Yeah I have two projects that i need advice on to do right now)))
(print response)