fork download
  1. ;gnu clisp 2.49.60
  2.  
  3. ;;
  4. ;; STARTER FILE FOR CSC 4240/5240 PROGRAM #1: Eliza
  5. ;;==========================================================================
  6.  
  7. ;;----------------------------------------------------------------------------
  8. ;; eliza: top-level function which, when given a sentence (no
  9. ;; punctuation, please!), comes back with a response like you would.
  10.  
  11. ( defun eliza ( sentence )
  12. ;(format t "Input type: ~a, value: ~a~%" (type-of sentence) sentence)
  13. ( respond ( change-pros sentence ) database ) )
  14.  
  15. ;;----------------------------------------------------------------------------
  16. ;; change-pros: changes the pronouns of the sentence so that Eliza can
  17. ;; come back with the appropriately switched first and second person
  18. ;; references.
  19.  
  20. ( defun change-pros ( sentence )
  21. ( cond
  22. ( ( null sentence ) nil )
  23.  
  24. ( ( equal ( car sentence ) 'you )
  25. ( cons 'I ( change-pros ( cdr sentence ) ) ) )
  26. ( ( equal ( car sentence ) 'I )
  27. ( cons 'you ( change-pros ( cdr sentence ) ) ) )
  28.  
  29. ( ( equal ( car sentence ) 'am )
  30. ( cons 'are ( change-pros ( cdr sentence ) ) ) )
  31.  
  32. ( ( equal ( car sentence ) 'my )
  33. ( cons 'your ( change-pros ( cdr sentence ) ) ) )
  34. ( ( equal ( car sentence ) 'your )
  35. ( cons 'my ( change-pros ( cdr sentence ) ) ) )
  36.  
  37. ( ( equal ( car sentence ) 'mine )
  38. ( cons 'yours ( change-pros ( cdr sentence ) ) ) )
  39. ( ( equal ( car sentence ) 'yours )
  40. ( cons 'mine ( change-pros ( cdr sentence ) ) ) )
  41.  
  42. ( ( equal ( car sentence ) 'he )
  43. ( cons 'him ( change-pros ( cdr sentence ) ) ) )
  44. ( ( equal ( car sentence ) 'him )
  45. ( cons 'he ( change-pros ( cdr sentence ) ) ) )
  46.  
  47. ( ( equal ( car sentence ) 'she )
  48. ( cons 'she ( change-pros ( cdr sentence ) ) ) )
  49. ( ( equal ( car sentence ) 'her )
  50. ( cons 'hers ( change-pros ( cdr sentence ) ) ) )
  51. ( ( equal ( car sentence ) 'hers )
  52. ( cons 'her ( change-pros ( cdr sentence ) ) ) )
  53.  
  54. ( ( equal ( car sentence ) 'it )
  55. ( cons 'it ( change-pros ( cdr sentence ) ) ) )
  56.  
  57. ;; CHANGE THIS: add more cases here of pronouns or other words
  58. ;; that should flip in order for this to work well
  59.  
  60. ( t ( cons ( car sentence ) ( change-pros ( cdr sentence ) ) ) ) ) )
  61.  
  62. ;;----------------------------------------------------------------------------
  63. ( defun get-random-index ( len )
  64.  
  65. ;; random-idx is the variable that will store the random index we will return to the caller function. Initially is = 0
  66. (setq random-idx 0)
  67.  
  68. ;; We keep iterating until we have a value for our index which does not equal to 0 (it will be > 0 and < len)
  69. ;; We apply mod operation to get an index from 0 to (len - 1) to choose a response to return
  70. (loop while (= random-idx 0) do
  71.  
  72. ;; I thought of an easy way how to get a random value to act as our seed and the timestamp would work perfect for this case
  73. ;; get current timestamp as if its our seed, and we are sure it is a dynamic value and it will always lead to random results
  74. (setq modu (get-universal-time))
  75.  
  76. ;; We apply the mod (remainder) operation to get an index that lies within the possible range: [1, len - 1] (inclusive)
  77. (setq random-idx (mod modu len))
  78. )
  79.  
  80. ;; This is a safe-check. If for any reason the random index had value of 0, we return the first response we had
  81. ;; since index 0 does not contain a response, it's just a placeholder for pattern-matching
  82. (if (= random-idx 0)
  83. (setq random-idx 1)
  84. (princ ""))
  85.  
  86. ;; We return the random index value we got to the caller function to use the response we have at our database at that index
  87. random-idx)
  88.  
  89.  
  90. ;; respond: given a sentence, looks through the database in search of
  91. ;; a matching pattern and the response; given the database response,
  92. ;; uses 'instantiate' to fill in the blanks, and returns the completed
  93. ;; response
  94.  
  95. ( defun respond ( sentence db )
  96. ( cond
  97. ;; end of DB, return nil - should never really happen
  98. ( ( null db ) nil )
  99.  
  100. ;; if the result of matching the sentence against the current
  101. ;; pattern is a success, produce this response
  102. (
  103. ( success ( setq result ( match sentence ( first ( car db ) ) ) ) )
  104.  
  105. (setq random-index (get-random-index (length (car db))))
  106.  
  107. ( instantiate result ( nth random-index ( car db ) ) )
  108. )
  109.  
  110. ;; otherwise, keep looking through the DB
  111. ( t ( respond sentence ( cdr db ) ) ) ) )
  112.  
  113. ;;----------------------------------------------------------------------------
  114. ;; match: if there is not a match between this pattern and this data,
  115. ;; returns 'fail;' otherwise, returns the sentence in partitioned
  116. ;; format
  117.  
  118. ( defun match ( data pattern )
  119. ( cond
  120. ;; end of both data and pattern; a match
  121. ( ( and ( null data ) ( null pattern ) ) nil )
  122.  
  123. ;; end of pattern, but not end of data; no match
  124. ( ( null pattern ) fail )
  125.  
  126. ;; end of data, but not end of pattern; if the pattern starts with
  127. ;; a variable, eat it and try and match the rest of the pattern to
  128. ;; the null sentence (will only work if all variables); otherwise,
  129. ;; fail
  130. ( ( null data )
  131. ( cond
  132. ( ( variablep ( car pattern ) )
  133. ( if ( success ( setq result ( match data ( cdr pattern ) ) ) )
  134. result
  135. fail ) )
  136. ( t fail ) ) )
  137.  
  138.  
  139. ;; first item of data and pattern are identical; if the rest of it
  140. ;; matched, return the first item cons'ed with the rest of the
  141. ;; partitioned sentence; otherwise, fail
  142. ( ( equal ( car data ) ( car pattern ) )
  143. ( if ( success ( setq result ( match ( cdr data ) ( cdr pattern ) ) ) )
  144. ( cons ( list ( car data ) ) result )
  145. fail ) )
  146.  
  147. ;; first item of pattern is a variable; if the rest of the data
  148. ;; (minus the first word, matched to the variable) is a match with
  149. ;; all of the pattern, return the appropriate stuff; if all of the
  150. ;; data (variable eats nothing) matches the rest of the pattern,
  151. ;; return appropriate stuff; else, fail.
  152. ( ( variablep ( car pattern ) )
  153. ( cond
  154. ;; variable eats nothing; () is put in partitioned sentence
  155. ( ( success ( setq result ( match data ( cdr pattern ) ) ) )
  156. ( cons () result ) )
  157. ;; variable eats one word; word is cons'ed into the first
  158. ;; element of the partitioned sentence, assuming that the step
  159. ;; before an actual match word would be a ()
  160. ( ( success ( setq result ( match ( cdr data ) pattern ) ) )
  161. ( cons ( cons ( car data ) ( car result ) ) ( cdr result ) ) )
  162. ;; otherwise, fail
  163. ( t fail ) ) )
  164.  
  165. ( t fail ) ) )
  166.  
  167. ;;----------------------------------------------------------------------------
  168. ;; instantiate: takes a partitioned sentence and the response it has
  169. ;; been matched to and generates the appropriated completed response
  170.  
  171. ( defun instantiate ( partitioned response )
  172. ( cond
  173. ( ( null response ) nil )
  174. ;; numbers indicate what part of the partitioned sentence to
  175. ;; insert into the response
  176. ( ( numberp ( car response ) )
  177. ( setq index ( - ( car response ) 1 ) )
  178. ( append ( nth index partitioned )
  179. ( instantiate partitioned ( cdr response ) ) ) )
  180. ( t ( cons ( car response )
  181. ( instantiate partitioned ( cdr response ) ) ) ) ) )
  182.  
  183. ;;---------------------------------------------------------------------------
  184. ;;
  185. ;; helping functions
  186. ;;
  187. ;;---------------------------------------------------------------------------
  188.  
  189. ( setq fail '-1 )
  190.  
  191. ( defun success ( result )
  192. ( not ( equal result fail ) ) )
  193.  
  194. ( defun variablep ( word )
  195. ( equal word '0 ) )
  196.  
  197.  
  198. ;;---------------------------------------------------------------------------
  199. ;;
  200. ;; database
  201. ;;
  202. ;;---------------------------------------------------------------------------
  203.  
  204. ;; CHANGE THIS: add more to this database so that the interaction is
  205. ;; more interesting and communicative and so that Eliza sounds like you
  206. ;; would sound in the same conversation!
  207. ;;---------------------------------------------------------------------------
  208.  
  209. ;; Here I show the extra rules I added to the previous rules we already had at the starter file.
  210. ;; I tried adding different rules covering different cases, scenarios, and different emotions.
  211. ;; For the generic response, I added multiple ones in order to have multiple random ones that can be used needed
  212.  
  213. ( setq database
  214. '(
  215. ;; example greetings/farewells -- change them to sound like you
  216. (
  217. (Hello 0)
  218. ("Hello - have a seat and tell me how you feel today.")
  219. )
  220.  
  221. ( (0 Goodbye 0)
  222. ("Goodbye - I hope you enjoyed this session.") )
  223.  
  224. ( (0 You came here because 0)
  225. (A lot of people come here for that reason so you are not alone.) )
  226.  
  227. ;; normal open questions
  228. ((0 your day 0)
  229. ("Great! I would love to hear that, tell me how was your day?")
  230. )
  231.  
  232. ;; personal information about favourite things
  233. (
  234. (0 my favourite team won 0)
  235. ("I'm glad to hear that! Tell me how's your health?" )
  236. )
  237.  
  238. ;; feelings
  239. ( (0 you think 0)
  240. (And just why do you think 4 ? ) )
  241.  
  242. ( (0 you are happy 0)
  243. (That's wonderful! What’s making you happy today? ) )
  244.  
  245. (
  246. (0 you feel joyful 0)
  247. ("I'm glad to hear that! Tell me more what makes you feel joyful?" )
  248. )
  249.  
  250. ((0 feel excited 0)
  251. ("I'm glad to hear that! Tell me more what makes you feel excited?")
  252. )
  253.  
  254.  
  255. ((0 too much work 0)
  256. ("Try getting some rest please, maybe get tomorrow as vacation")
  257. )
  258.  
  259. ((0 go to the gym 0)
  260. ("Oh that is great! This is really very important to maintain a good health.")
  261. )
  262.  
  263. ((0 feel tired 0)
  264. ("Sad to hear that. What happened?")
  265. )
  266.  
  267. ((0 you feel sad 0)
  268. ("I'm sorry to hear that. Why do you think you are sad?"
  269. "Tell me more about what’s troubling you."))
  270.  
  271. ((0 you are angry 0)
  272. ("Why do you think you’re feeling so angry?" )
  273. )
  274.  
  275. ;; recognize certain keywords and respond
  276. ((0 try rescheduling 0)
  277. ("That would be great! I hope you find another appointement soon")
  278. )
  279.  
  280. ((0 you are late 0)
  281. ("It is never too late. Try to catch up.")
  282. )
  283.  
  284. ((0 laptop not working 0)
  285. ("It is frustrating to hear that.. do you have a backup of your data?")
  286. )
  287.  
  288. ;; actions/acts
  289. ((0 password 0)
  290. ("You can reset your password if you forgot it")
  291. )
  292.  
  293. ((0 got lost 0)
  294. ("Try calling 911 or go to nearest police station! Take care!")
  295. )
  296.  
  297. ((0 have breakfast 0)
  298. ("Bon appétit! What will you eat?")
  299. )
  300.  
  301. ((0 will eat 0)
  302. ("I do not know this food. What is it about?")
  303. )
  304.  
  305. ((0 have lunch 0)
  306. ("Bon appétit! What will you eat?")
  307. )
  308.  
  309. ((0 have dinner 0)
  310. ("Bon appétit! What will you eat?")
  311. )
  312.  
  313. ((0 delicious food 0)
  314. ("Aha! That sounds tasty, enjoy your meal!")
  315. )
  316.  
  317. ((0 missed your appointment 0)
  318. ("Uhh.. Can you try rescheduling it?")
  319. )
  320.  
  321. ((0 information 0)
  322. ("Thanks for the new information. Glad that you feel happy. So how is everything else?")
  323. )
  324.  
  325. ;; the catch-alls
  326. (
  327. (0)
  328. ("Could you expand on that?")
  329. ("Hmmm.. Is it possible to elaborate more on that please?")
  330. ("Hmmm.. I feel I didn't get what you mean. Can you explain again in other terms?")
  331. ("Uhh.. I fear I don't understand what are you talking about..")
  332. ("Ops, didn't get it, please expand on that.")
  333. ("Sorry, I could not understand that. Can you elaborate more?")
  334. )
  335. )
  336. )
  337.  
  338. (princ (eliza '(I feel sad)))
Success #stdin #stdout #stderr 0.64s 9752KB
stdin
Standard input is empty
stdout
(I'm sorry to hear that. Why do you think you are sad?
 Tell me more about what’s troubling you.)
stderr
Warning: reserving address range 0x80000c0000...0x1fffffffffff that contains memory mappings. clisp might crash later!
Memory dump:
  0x8000000000 - 0x80000bffff
  0x147517000000 - 0x1475172e4fff
  0x147517415000 - 0x147517439fff
  0x14751743a000 - 0x1475175acfff
  0x1475175ad000 - 0x1475175f5fff
  0x1475175f6000 - 0x1475175f8fff
  0x1475175f9000 - 0x1475175fbfff
  0x1475175fc000 - 0x1475175fffff
  0x147517600000 - 0x147517602fff
  0x147517603000 - 0x147517801fff
  0x147517802000 - 0x147517802fff
  0x147517803000 - 0x147517803fff
  0x147517880000 - 0x14751788ffff
  0x147517890000 - 0x1475178c3fff
  0x1475178c4000 - 0x1475179fafff
  0x1475179fb000 - 0x1475179fbfff
  0x1475179fc000 - 0x1475179fefff
  0x1475179ff000 - 0x1475179fffff
  0x147517a00000 - 0x147517a03fff
  0x147517a04000 - 0x147517c03fff
  0x147517c04000 - 0x147517c04fff
  0x147517c05000 - 0x147517c05fff
  0x147517ccf000 - 0x147517cd2fff
  0x147517cd3000 - 0x147517cd3fff
  0x147517cd4000 - 0x147517cd5fff
  0x147517cd6000 - 0x147517cd6fff
  0x147517cd7000 - 0x147517cd7fff
  0x147517cd8000 - 0x147517cd8fff
  0x147517cd9000 - 0x147517ce6fff
  0x147517ce7000 - 0x147517cf4fff
  0x147517cf5000 - 0x147517d01fff
  0x147517d02000 - 0x147517d05fff
  0x147517d06000 - 0x147517d06fff
  0x147517d07000 - 0x147517d07fff
  0x147517d08000 - 0x147517d0dfff
  0x147517d0e000 - 0x147517d0ffff
  0x147517d10000 - 0x147517d10fff
  0x147517d11000 - 0x147517d11fff
  0x147517d12000 - 0x147517d12fff
  0x147517d13000 - 0x147517d40fff
  0x147517d41000 - 0x147517d4ffff
  0x147517d50000 - 0x147517df5fff
  0x147517df6000 - 0x147517e8cfff
  0x147517e8d000 - 0x147517e8dfff
  0x147517e8e000 - 0x147517e8efff
  0x147517e8f000 - 0x147517ea2fff
  0x147517ea3000 - 0x147517ecafff
  0x147517ecb000 - 0x147517ed4fff
  0x147517ed5000 - 0x147517ed6fff
  0x147517ed7000 - 0x147517edcfff
  0x147517edd000 - 0x147517edffff
  0x147517ee2000 - 0x147517ee2fff
  0x147517ee3000 - 0x147517ee3fff
  0x147517ee4000 - 0x147517ee4fff
  0x147517ee5000 - 0x147517ee5fff
  0x147517ee6000 - 0x147517ee6fff
  0x147517ee7000 - 0x147517eedfff
  0x147517eee000 - 0x147517ef0fff
  0x147517ef1000 - 0x147517ef1fff
  0x147517ef2000 - 0x147517f12fff
  0x147517f13000 - 0x147517f1afff
  0x147517f1b000 - 0x147517f1bfff
  0x147517f1c000 - 0x147517f1cfff
  0x147517f1d000 - 0x147517f1dfff
  0x556f5be4a000 - 0x556f5bf3afff
  0x556f5bf3b000 - 0x556f5c044fff
  0x556f5c045000 - 0x556f5c0a4fff
  0x556f5c0a6000 - 0x556f5c0d4fff
  0x556f5c0d5000 - 0x556f5c105fff
  0x556f5c106000 - 0x556f5c109fff
  0x556f5d26a000 - 0x556f5d28afff
  0x7ffe39204000 - 0x7ffe39224fff
  0x7ffe392a9000 - 0x7ffe392acfff
  0x7ffe392ad000 - 0x7ffe392aefff