fork 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. ( instantiate result ( second ( car db ) ) ) )
  46.  
  47. ;; otherwise, keep looking through the DB
  48. ( t ( respond sentence ( cdr db ) ) ) ) )
  49.  
  50. ;;----------------------------------------------------------------------------
  51. ;; match: if there is not a match between this pattern and this data,
  52. ;; returns 'fail;' otherwise, returns the sentence in partitioned
  53. ;; format
  54.  
  55. ( defun match ( data pattern )
  56. ( cond
  57. ;; end of both data and pattern; a match
  58. ( ( and ( null data ) ( null pattern ) ) nil )
  59.  
  60. ;; end of pattern, but not end of data; no match
  61. ( ( null pattern ) fail )
  62.  
  63. ;; end of data, but not end of pattern; if the pattern starts with
  64. ;; a variable, eat it and try and match the rest of the pattern to
  65. ;; the null sentence (will only work if all variables); otherwise,
  66. ;; fail
  67. ( ( null data )
  68. ( cond
  69. ( ( variablep ( car pattern ) )
  70. ( if ( success ( setq result ( match data ( cdr pattern ) ) ) )
  71. result
  72. fail ) )
  73. ( t fail ) ) )
  74.  
  75.  
  76. ;; first item of data and pattern are identical; if the rest of it
  77. ;; matched, return the first item cons'ed with the rest of the
  78. ;; partitioned sentence; otherwise, fail
  79. ( ( equal ( car data ) ( car pattern ) )
  80. ( if ( success ( setq result ( match ( cdr data ) ( cdr pattern ) ) ) )
  81. ( cons ( list ( car data ) ) result )
  82. fail ) )
  83.  
  84. ;; first item of pattern is a variable; if the rest of the data
  85. ;; (minus the first word, matched to the variable) is a match with
  86. ;; all of the pattern, return the appropriate stuff; if all of the
  87. ;; data (variable eats nothing) matches the rest of the pattern,
  88. ;; return appropriate stuff; else, fail.
  89. ( ( variablep ( car pattern ) )
  90. ( cond
  91. ;; variable eats nothing; () is put in partitioned sentence
  92. ( ( success ( setq result ( match data ( cdr pattern ) ) ) )
  93. ( cons () result ) )
  94. ;; variable eats one word; word is cons'ed into the first
  95. ;; element of the partitioned sentence, assuming that the step
  96. ;; before an actual match word would be a ()
  97. ( ( success ( setq result ( match ( cdr data ) pattern ) ) )
  98. ( cons ( cons ( car data ) ( car result ) ) ( cdr result ) ) )
  99. ;; otherwise, fail
  100. ( t fail ) ) )
  101.  
  102. ( t fail ) ) )
  103.  
  104. ;;----------------------------------------------------------------------------
  105. ;; instantiate: takes a partitioned sentence and the response it has
  106. ;; been matched to and generates the appropriated completed response
  107.  
  108. ( defun instantiate ( partitioned response )
  109. ( cond
  110. ( ( null response ) nil )
  111. ;; numbers indicate what part of the partitioned sentence to
  112. ;; insert into the response
  113. ( ( numberp ( car response ) )
  114. ( setq index ( - ( car response ) 1 ) )
  115. ( append ( nth index partitioned )
  116. ( instantiate partitioned ( cdr response ) ) ) )
  117. ( t ( cons ( car response )
  118. ( instantiate partitioned ( cdr response ) ) ) ) ) )
  119.  
  120. ;;---------------------------------------------------------------------------
  121. ;;
  122. ;; helping functions
  123. ;;
  124. ;;---------------------------------------------------------------------------
  125.  
  126. ( setq fail '-1 )
  127.  
  128. ( defun success ( result )
  129. ( not ( equal result fail ) ) )
  130.  
  131. ( defun variablep ( word )
  132. ( equal word '0 ) )
  133.  
  134.  
  135. ;;---------------------------------------------------------------------------
  136. ;;
  137. ;; database
  138. ;;
  139. ;;---------------------------------------------------------------------------
  140.  
  141. ;; CHANGE THIS: add more to this database so that the interaction is
  142. ;; more interesting and communicative and so that Eliza sounds like you
  143. ;; would sound in the same conversation!
  144. ;;---------------------------------------------------------------------------
  145.  
  146. ( setq database
  147. '(
  148. ;; example greetings/farewells -- change them to sound like you
  149. ( (Hello 0)
  150. (Hello - have a seat and tell me how you feel today.) )
  151. ( (0 you came here because 0)
  152. (A lot of people come here for that reason so you are not alone.) )
  153. ( (0 Goodbye 0)
  154. (Goodbye - I hope you enjoyed this session.) )
  155.  
  156. ;; feelings
  157. ( (0 you think 0)
  158. (And just why do you think 4 ?) )
  159.  
  160. ;; the catch-alls
  161. ( (0)
  162. (Could you expand on that?) ) ) )
  163.  
  164. (eliza '(hello))
Success #stdin #stdout #stderr 0.02s 9568KB
stdin
hello
stdout
Standard output is empty
stderr
Warning: reserving address range 0x80000c0000...0x1fffffffffff that contains memory mappings. clisp might crash later!
Memory dump:
  0x8000000000 - 0x80000bffff
  0x14d2ce400000 - 0x14d2ce6e4fff
  0x14d2ce800000 - 0x14d2ce802fff
  0x14d2ce803000 - 0x14d2cea01fff
  0x14d2cea02000 - 0x14d2cea02fff
  0x14d2cea03000 - 0x14d2cea03fff
  0x14d2cea15000 - 0x14d2cea39fff
  0x14d2cea3a000 - 0x14d2cebacfff
  0x14d2cebad000 - 0x14d2cebf5fff
  0x14d2cebf6000 - 0x14d2cebf8fff
  0x14d2cebf9000 - 0x14d2cebfbfff
  0x14d2cebfc000 - 0x14d2cebfffff
  0x14d2cec00000 - 0x14d2cec03fff
  0x14d2cec04000 - 0x14d2cee03fff
  0x14d2cee04000 - 0x14d2cee04fff
  0x14d2cee05000 - 0x14d2cee05fff
  0x14d2cee1e000 - 0x14d2cee1ffff
  0x14d2cee20000 - 0x14d2cee2ffff
  0x14d2cee30000 - 0x14d2cee63fff
  0x14d2cee64000 - 0x14d2cef9afff
  0x14d2cef9b000 - 0x14d2cef9bfff
  0x14d2cef9c000 - 0x14d2cef9efff
  0x14d2cef9f000 - 0x14d2cef9ffff
  0x14d2cefa0000 - 0x14d2cefa1fff
  0x14d2cefa2000 - 0x14d2cefa2fff
  0x14d2cefa3000 - 0x14d2cefa4fff
  0x14d2cefa5000 - 0x14d2cefa5fff
  0x14d2cefa6000 - 0x14d2cefa6fff
  0x14d2cefa7000 - 0x14d2cefa7fff
  0x14d2cefa8000 - 0x14d2cefb5fff
  0x14d2cefb6000 - 0x14d2cefc3fff
  0x14d2cefc4000 - 0x14d2cefd0fff
  0x14d2cefd1000 - 0x14d2cefd4fff
  0x14d2cefd5000 - 0x14d2cefd5fff
  0x14d2cefd6000 - 0x14d2cefd6fff
  0x14d2cefd7000 - 0x14d2cefdcfff
  0x14d2cefdd000 - 0x14d2cefdefff
  0x14d2cefdf000 - 0x14d2cefdffff
  0x14d2cefe0000 - 0x14d2cefe0fff
  0x14d2cefe1000 - 0x14d2cefe1fff
  0x14d2cefe2000 - 0x14d2cf00ffff
  0x14d2cf010000 - 0x14d2cf01efff
  0x14d2cf01f000 - 0x14d2cf0c4fff
  0x14d2cf0c5000 - 0x14d2cf15bfff
  0x14d2cf15c000 - 0x14d2cf15cfff
  0x14d2cf15d000 - 0x14d2cf15dfff
  0x14d2cf15e000 - 0x14d2cf171fff
  0x14d2cf172000 - 0x14d2cf199fff
  0x14d2cf19a000 - 0x14d2cf1a3fff
  0x14d2cf1a4000 - 0x14d2cf1a5fff
  0x14d2cf1a6000 - 0x14d2cf1abfff
  0x14d2cf1ac000 - 0x14d2cf1aefff
  0x14d2cf1b1000 - 0x14d2cf1b1fff
  0x14d2cf1b2000 - 0x14d2cf1b2fff
  0x14d2cf1b3000 - 0x14d2cf1b3fff
  0x14d2cf1b4000 - 0x14d2cf1b4fff
  0x14d2cf1b5000 - 0x14d2cf1b5fff
  0x14d2cf1b6000 - 0x14d2cf1bcfff
  0x14d2cf1bd000 - 0x14d2cf1bffff
  0x14d2cf1c0000 - 0x14d2cf1c0fff
  0x14d2cf1c1000 - 0x14d2cf1e1fff
  0x14d2cf1e2000 - 0x14d2cf1e9fff
  0x14d2cf1ea000 - 0x14d2cf1eafff
  0x14d2cf1eb000 - 0x14d2cf1ebfff
  0x14d2cf1ec000 - 0x14d2cf1ecfff
  0x55ff92e94000 - 0x55ff92f84fff
  0x55ff92f85000 - 0x55ff9308efff
  0x55ff9308f000 - 0x55ff930eefff
  0x55ff930f0000 - 0x55ff9311efff
  0x55ff9311f000 - 0x55ff9314ffff
  0x55ff93150000 - 0x55ff93153fff
  0x55ff94fee000 - 0x55ff9500efff
  0x7ffc024d6000 - 0x7ffc024f6fff
  0x7ffc02516000 - 0x7ffc02519fff
  0x7ffc0251a000 - 0x7ffc0251bfff