1
0

eliza.lisp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. (in-package :cl-user)
  2. (defpackage chatikbot.eliza
  3. (:use :cl
  4. :chatikbot.patmatch
  5. :chatikbot.utils
  6. :alexandria)
  7. (:export :?is
  8. :?or
  9. :?and
  10. :?not
  11. :?*
  12. :?+
  13. :??
  14. :?if
  15. :eliza
  16. :read-from-string-no-punct))
  17. (in-package :chatikbot.eliza)
  18. (defun punctuation-p (char)
  19. (find char ".,;:'!?#-()\\\""))
  20. (defun read-from-string-no-punct (input)
  21. "Read from an input string, ignoring punctuation."
  22. (read-from-string
  23. (concatenate 'string "(" (substitute-if #\space #'punctuation-p input) ")")))
  24. (defun switch-viewpoint (words)
  25. "Change I to you and vice versa, and so on."
  26. (sublis '((I . you) (you . I) (me . you) (am . are)
  27. (я ты) (ты я) (меня тебя) (тебя меня))
  28. words))
  29. (defun use-eliza-rules (input rules)
  30. "Find some rule with which to transform the input."
  31. (rule-based-translator input rules
  32. :action #'(lambda (bindings responses)
  33. (sublis (switch-viewpoint bindings)
  34. (random-elt responses)))))
  35. (defun eliza (input rules)
  36. (let ((r (use-eliza-rules input rules)))
  37. (cond
  38. ((null r) nil)
  39. ((and (consp (car r)) (eq 'function (caar r)))
  40. (apply (cadar r) (cdr r)))
  41. ((keywordp (car r)) r)
  42. (t (print-with-spaces (flatten r))))))