fork(1) download
  1. ;;==========================================================================
  2. ;;
  3. ;; STARTER FILE FOR CSC 4240/5240 PROGRAM #1: Eliza
  4. ;;==========================================================================
  5.  
  6. ;;----------------------------------------------------------------------------
  7. ;; eliza: top-level function which, when given a sentence (no
  8. ;; punctuation, please!), comes back with a response like you would.
  9.  
  10. ( defun eliza ( sentence )
  11. ( respond ( change-pros sentence ) database ) )
  12.  
  13. ;;----------------------------------------------------------------------------
  14. ;; change-pros: changes the pronouns of the sentence so that Eliza can
  15. ;; come back with the appropriately switched first and second person
  16. ;; references.
  17.  
  18. ( defun change-pros ( sentence )
  19. ( cond
  20. ( ( null sentence ) nil )
  21. ( ( equal ( car sentence ) 'you )
  22. ( cons 'I ( change-pros ( cdr sentence ) ) ) )
  23. ( ( equal ( car sentence ) 'I )
  24. ( cons 'you ( change-pros ( cdr sentence ) ) ) )
  25.  
  26. ;; CHANGE THIS: add more cases here of pronouns or other words
  27. ;; that should flip in order for this to work well
  28.  
  29. ( t ( cons ( car sentence ) ( change-pros ( cdr sentence ) ) ) ) ) )
  30.  
  31. ;;----------------------------------------------------------------------------
  32. ;; respond: given a sentence, looks through the database in search of
  33. ;; a matching pattern and the response; given the database response,
  34. ;; uses 'instantiate' to fill in the blanks, and returns the completed
  35. ;; response
  36.  
  37. (defun respond (sentence db)
  38. (cond
  39. ;; end of DB, return nil - should never really happen
  40. ((null db) nil)
  41.  
  42. ;; if the result of matching the sentence against the current
  43. ;; pattern is a success, produce this response
  44. ((success (setq result (match sentence (first (car db)))))
  45. (let ((response (instantiate result (second (car db)))))
  46. (format t "~a~%" response)
  47. response))
  48.  
  49. ;; otherwise, keep looking through the DB
  50. (t (respond sentence (cdr db)))))
  51.  
  52.  
  53. ;;----------------------------------------------------------------------------
  54. ;; match: if there is not a match between this pattern and this data,
  55. ;; returns 'fail;' otherwise, returns the sentence in partitioned
  56. ;; format
  57.  
  58. ( defun match ( data pattern )
  59. ( cond
  60. ;; end of both data and pattern; a match
  61. ( ( and ( null data ) ( null pattern ) ) nil )
  62.  
  63. ;; end of pattern, but not end of data; no match
  64. ( ( null pattern ) fail )
  65.  
  66. ;; end of data, but not end of pattern; if the pattern starts with
  67. ;; a variable, eat it and try and match the rest of the pattern to
  68. ;; the null sentence (will only work if all variables); otherwise,
  69. ;; fail
  70. ( ( null data )
  71. ( cond
  72. ( ( variablep ( car pattern ) )
  73. ( if ( success ( setq result ( match data ( cdr pattern ) ) ) )
  74. result
  75. fail ) )
  76. ( t fail ) ) )
  77.  
  78.  
  79. ;; first item of data and pattern are identical; if the rest of it
  80. ;; matched, return the first item cons'ed with the rest of the
  81. ;; partitioned sentence; otherwise, fail
  82. ( ( equal ( car data ) ( car pattern ) )
  83. ( if ( success ( setq result ( match ( cdr data ) ( cdr pattern ) ) ) )
  84. ( cons ( list ( car data ) ) result )
  85. fail ) )
  86.  
  87. ;; first item of pattern is a variable; if the rest of the data
  88. ;; (minus the first word, matched to the variable) is a match with
  89. ;; all of the pattern, return the appropriate stuff; if all of the
  90. ;; data (variable eats nothing) matches the rest of the pattern,
  91. ;; return appropriate stuff; else, fail.
  92. ( ( variablep ( car pattern ) )
  93. ( cond
  94. ;; variable eats nothing; () is put in partitioned sentence
  95. ( ( success ( setq result ( match data ( cdr pattern ) ) ) )
  96. ( cons () result ) )
  97. ;; variable eats one word; word is cons'ed into the first
  98. ;; element of the partitioned sentence, assuming that the step
  99. ;; before an actual match word would be a ()
  100. ( ( success ( setq result ( match ( cdr data ) pattern ) ) )
  101. ( cons ( cons ( car data ) ( car result ) ) ( cdr result ) ) )
  102. ;; otherwise, fail
  103. ( t fail ) ) )
  104.  
  105. ( t fail ) ) )
  106.  
  107. ;;----------------------------------------------------------------------------
  108. ;; instantiate: takes a partitioned sentence and the response it has
  109. ;; been matched to and generates the appropriated completed response
  110.  
  111. ( defun instantiate ( partitioned response )
  112. ( cond
  113. ( ( null response ) nil )
  114. ;; numbers indicate what part of the partitioned sentence to
  115. ;; insert into the response
  116. ( ( numberp ( car response ) )
  117. ( setq index ( - ( car response ) 1 ) )
  118. ( append ( nth index partitioned )
  119. ( instantiate partitioned ( cdr response ) ) ) )
  120. ( t ( cons ( car response )
  121. ( instantiate partitioned ( cdr response ) ) ) ) ) )
  122.  
  123. ;;---------------------------------------------------------------------------
  124. ;;
  125. ;; helping functions
  126. ;;
  127. ;;---------------------------------------------------------------------------
  128.  
  129. ( setq fail '-1 )
  130.  
  131. ( defun success ( result )
  132. ( not ( equal result fail ) ) )
  133.  
  134. ( defun variablep ( word )
  135. ( equal word '0 ) )
  136.  
  137.  
  138. ;;---------------------------------------------------------------------------
  139. ;;
  140. ;; database
  141. ;;
  142. ;;---------------------------------------------------------------------------
  143.  
  144. ;; CHANGE THIS: add more to this database so that the interaction is
  145. ;; more interesting and communicative and so that Eliza sounds like you
  146. ;; would sound in the same conversation!
  147. ;;---------------------------------------------------------------------------
  148.  
  149. ( setq database
  150. '(
  151. ;; example greetings/farewells -- change them to sound like you
  152. ( (Hello 0)
  153. (Hello - have a seat and tell me how you feel today.) )
  154. ( (0 you came here because 0)
  155. (A lot of people come here for that reason so you are not alone.) )
  156. ( (0 Goodbye 0)
  157. (Goodbye - I hope you enjoyed this session.) )
  158.  
  159. ;; feelings
  160. ( (0 you think 0)
  161. (And just why do you think 4 ?) )
  162.  
  163. ;; the catch-alls
  164. ( (0)
  165. (Could you expand on that?) ) ) )
  166. (eliza '(i feel quite bad))
Success #stdin #stdout #stderr 0.01s 9548KB
stdin
Standard input is empty
stdout
(COULD YOU EXPAND ON THAT?)
stderr
Warning: reserving address range 0x80000c0000...0x1fffffffffff that contains memory mappings. clisp might crash later!
Memory dump:
  0x8000000000 - 0x80000bffff
  0x154e4f000000 - 0x154e4f2e4fff
  0x154e4f415000 - 0x154e4f439fff
  0x154e4f43a000 - 0x154e4f5acfff
  0x154e4f5ad000 - 0x154e4f5f5fff
  0x154e4f5f6000 - 0x154e4f5f8fff
  0x154e4f5f9000 - 0x154e4f5fbfff
  0x154e4f5fc000 - 0x154e4f5fffff
  0x154e4f600000 - 0x154e4f602fff
  0x154e4f603000 - 0x154e4f801fff
  0x154e4f802000 - 0x154e4f802fff
  0x154e4f803000 - 0x154e4f803fff
  0x154e4f880000 - 0x154e4f88ffff
  0x154e4f890000 - 0x154e4f8c3fff
  0x154e4f8c4000 - 0x154e4f9fafff
  0x154e4f9fb000 - 0x154e4f9fbfff
  0x154e4f9fc000 - 0x154e4f9fefff
  0x154e4f9ff000 - 0x154e4f9fffff
  0x154e4fa00000 - 0x154e4fa03fff
  0x154e4fa04000 - 0x154e4fc03fff
  0x154e4fc04000 - 0x154e4fc04fff
  0x154e4fc05000 - 0x154e4fc05fff
  0x154e4fc4b000 - 0x154e4fc4efff
  0x154e4fc4f000 - 0x154e4fc4ffff
  0x154e4fc50000 - 0x154e4fc51fff
  0x154e4fc52000 - 0x154e4fc52fff
  0x154e4fc53000 - 0x154e4fc53fff
  0x154e4fc54000 - 0x154e4fc54fff
  0x154e4fc55000 - 0x154e4fc62fff
  0x154e4fc63000 - 0x154e4fc70fff
  0x154e4fc71000 - 0x154e4fc7dfff
  0x154e4fc7e000 - 0x154e4fc81fff
  0x154e4fc82000 - 0x154e4fc82fff
  0x154e4fc83000 - 0x154e4fc83fff
  0x154e4fc84000 - 0x154e4fc89fff
  0x154e4fc8a000 - 0x154e4fc8bfff
  0x154e4fc8c000 - 0x154e4fc8cfff
  0x154e4fc8d000 - 0x154e4fc8dfff
  0x154e4fc8e000 - 0x154e4fc8efff
  0x154e4fc8f000 - 0x154e4fcbcfff
  0x154e4fcbd000 - 0x154e4fccbfff
  0x154e4fccc000 - 0x154e4fd71fff
  0x154e4fd72000 - 0x154e4fe08fff
  0x154e4fe09000 - 0x154e4fe09fff
  0x154e4fe0a000 - 0x154e4fe0afff
  0x154e4fe0b000 - 0x154e4fe1efff
  0x154e4fe1f000 - 0x154e4fe46fff
  0x154e4fe47000 - 0x154e4fe50fff
  0x154e4fe51000 - 0x154e4fe52fff
  0x154e4fe53000 - 0x154e4fe58fff
  0x154e4fe59000 - 0x154e4fe5bfff
  0x154e4fe5e000 - 0x154e4fe5efff
  0x154e4fe5f000 - 0x154e4fe5ffff
  0x154e4fe60000 - 0x154e4fe60fff
  0x154e4fe61000 - 0x154e4fe61fff
  0x154e4fe62000 - 0x154e4fe62fff
  0x154e4fe63000 - 0x154e4fe69fff
  0x154e4fe6a000 - 0x154e4fe6cfff
  0x154e4fe6d000 - 0x154e4fe6dfff
  0x154e4fe6e000 - 0x154e4fe8efff
  0x154e4fe8f000 - 0x154e4fe96fff
  0x154e4fe97000 - 0x154e4fe97fff
  0x154e4fe98000 - 0x154e4fe98fff
  0x154e4fe99000 - 0x154e4fe99fff
  0x562a426bd000 - 0x562a427adfff
  0x562a427ae000 - 0x562a428b7fff
  0x562a428b8000 - 0x562a42917fff
  0x562a42919000 - 0x562a42947fff
  0x562a42948000 - 0x562a42978fff
  0x562a42979000 - 0x562a4297cfff
  0x562a43ec0000 - 0x562a43ee0fff
  0x7ffeee71f000 - 0x7ffeee73ffff
  0x7ffeee797000 - 0x7ffeee79afff
  0x7ffeee79b000 - 0x7ffeee79cfff