zen.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. # Natural Language Toolkit: Zen Chatbot
  2. #
  3. # Copyright (C) 2001-2019 NLTK Project
  4. # Author: Amy Holland <amyrh@csse.unimelb.edu.au>
  5. # URL: <http://nltk.org/>
  6. # For license information, see LICENSE.TXT
  7. """
  8. Zen Chatbot talks in gems of Zen wisdom.
  9. This is a sample conversation with Zen Chatbot:
  10. ZC: Welcome, my child.
  11. me: Good afternoon.
  12. ZC: Ask the question you have come to ask.
  13. me: How can I achieve enlightenment?
  14. ZC: How do you suppose?
  15. me: Through meditation.
  16. ZC: Form is emptiness, and emptiness form.
  17. me: How can I empty my mind of worldly troubles?
  18. ZC: Will an answer to that really help in your search for enlightenment?
  19. me: Yes.
  20. ZC: It is better to be right than to be certain.
  21. me: I seek truth and wisdom.
  22. ZC: The search for truth is a long journey.
  23. me: Are you sure?
  24. ZC: Maybe sure, maybe not sure.
  25. The chatbot structure is based on that of chat.eliza. Thus, it uses
  26. a translation table to convert from question to response
  27. i.e. "I am" --> "you are"
  28. Of course, since Zen Chatbot does not understand the meaning of any words,
  29. responses are very limited. Zen Chatbot will usually answer very vaguely, or
  30. respond to a question by asking a different question, in much the same way
  31. as Eliza.
  32. """
  33. from __future__ import print_function
  34. from nltk.chat.util import Chat, reflections
  35. # responses are matched top to bottom, so non-specific matches occur later
  36. # for each match, a list of possible responses is provided
  37. responses = (
  38. # Zen Chatbot opens with the line "Welcome, my child." The usual
  39. # response will be a greeting problem: 'good' matches "good morning",
  40. # "good day" etc, but also "good grief!" and other sentences starting
  41. # with the word 'good' that may not be a greeting
  42. (
  43. r'(hello(.*))|(good [a-zA-Z]+)',
  44. (
  45. "The path to enlightenment is often difficult to see.",
  46. "Greetings. I sense your mind is troubled. Tell me of your troubles.",
  47. "Ask the question you have come to ask.",
  48. "Hello. Do you seek englightenment?",
  49. ),
  50. ),
  51. # "I need" and "I want" can be followed by a thing (eg 'help')
  52. # or an action (eg 'to see you')
  53. #
  54. # This is a problem with this style of response -
  55. # person: "I need you"
  56. # chatbot: "me can be achieved by hard work and dedication of the mind"
  57. # i.e. 'you' is not really a thing that can be mapped this way, so this
  58. # interpretation only makes sense for some inputs
  59. #
  60. (
  61. r'i need (.*)',
  62. (
  63. "%1 can be achieved by hard work and dedication of the mind.",
  64. "%1 is not a need, but a desire of the mind. Clear your mind of such concerns.",
  65. "Focus your mind on%1, and you will find what you need.",
  66. ),
  67. ),
  68. (
  69. r'i want (.*)',
  70. (
  71. "Desires of the heart will distract you from the path to enlightenment.",
  72. "Will%1 help you attain enlightenment?",
  73. "Is%1 a desire of the mind, or of the heart?",
  74. ),
  75. ),
  76. # why questions are separated into three types:
  77. # "why..I" e.g. "why am I here?" "Why do I like cake?"
  78. # "why..you" e.g. "why are you here?" "Why won't you tell me?"
  79. # "why..." e.g. "Why is the sky blue?"
  80. # problems:
  81. # person: "Why can't you tell me?"
  82. # chatbot: "Are you sure I tell you?"
  83. # - this style works for positives (e.g. "why do you like cake?")
  84. # but does not work for negatives (e.g. "why don't you like cake?")
  85. (r'why (.*) i (.*)\?', ("You%1%2?", "Perhaps you only think you%1%2")),
  86. (r'why (.*) you(.*)\?', ("Why%1 you%2?", "%2 I%1", "Are you sure I%2?")),
  87. (r'why (.*)\?', ("I cannot tell you why%1.", "Why do you think %1?")),
  88. # e.g. "are you listening?", "are you a duck"
  89. (
  90. r'are you (.*)\?',
  91. ("Maybe%1, maybe not%1.", "Whether I am%1 or not is God's business."),
  92. ),
  93. # e.g. "am I a duck?", "am I going to die?"
  94. (
  95. r'am i (.*)\?',
  96. ("Perhaps%1, perhaps not%1.", "Whether you are%1 or not is not for me to say."),
  97. ),
  98. # what questions, e.g. "what time is it?"
  99. # problems:
  100. # person: "What do you want?"
  101. # chatbot: "Seek truth, not what do me want."
  102. (r'what (.*)\?', ("Seek truth, not what%1.", "What%1 should not concern you.")),
  103. # how questions, e.g. "how do you do?"
  104. (
  105. r'how (.*)\?',
  106. (
  107. "How do you suppose?",
  108. "Will an answer to that really help in your search for enlightenment?",
  109. "Ask yourself not how, but why.",
  110. ),
  111. ),
  112. # can questions, e.g. "can you run?", "can you come over here please?"
  113. (
  114. r'can you (.*)\?',
  115. (
  116. "I probably can, but I may not.",
  117. "Maybe I can%1, and maybe I cannot.",
  118. "I can do all, and I can do nothing.",
  119. ),
  120. ),
  121. # can questions, e.g. "can I have some cake?", "can I know truth?"
  122. (
  123. r'can i (.*)\?',
  124. (
  125. "You can%1 if you believe you can%1, and have a pure spirit.",
  126. "Seek truth and you will know if you can%1.",
  127. ),
  128. ),
  129. # e.g. "It is raining" - implies the speaker is certain of a fact
  130. (
  131. r'it is (.*)',
  132. (
  133. "How can you be certain that%1, when you do not even know yourself?",
  134. "Whether it is%1 or not does not change the way the world is.",
  135. ),
  136. ),
  137. # e.g. "is there a doctor in the house?"
  138. (
  139. r'is there (.*)\?',
  140. ("There is%1 if you believe there is.", "It is possible that there is%1."),
  141. ),
  142. # e.g. "is it possible?", "is this true?"
  143. (r'is(.*)\?', ("%1 is not relevant.", "Does this matter?")),
  144. # non-specific question
  145. (
  146. r'(.*)\?',
  147. (
  148. "Do you think %1?",
  149. "You seek the truth. Does the truth seek you?",
  150. "If you intentionally pursue the answers to your questions, the answers become hard to see.",
  151. "The answer to your question cannot be told. It must be experienced.",
  152. ),
  153. ),
  154. # expression of hate of form "I hate you" or "Kelly hates cheese"
  155. (
  156. r'(.*) (hate[s]?)|(dislike[s]?)|(don\'t like)(.*)',
  157. (
  158. "Perhaps it is not about hating %2, but about hate from within.",
  159. "Weeds only grow when we dislike them",
  160. "Hate is a very strong emotion.",
  161. ),
  162. ),
  163. # statement containing the word 'truth'
  164. (
  165. r'(.*) truth(.*)',
  166. (
  167. "Seek truth, and truth will seek you.",
  168. "Remember, it is not the spoon which bends - only yourself.",
  169. "The search for truth is a long journey.",
  170. ),
  171. ),
  172. # desire to do an action
  173. # e.g. "I want to go shopping"
  174. (
  175. r'i want to (.*)',
  176. ("You may %1 if your heart truly desires to.", "You may have to %1."),
  177. ),
  178. # desire for an object
  179. # e.g. "I want a pony"
  180. (
  181. r'i want (.*)',
  182. (
  183. "Does your heart truly desire %1?",
  184. "Is this a desire of the heart, or of the mind?",
  185. ),
  186. ),
  187. # e.g. "I can't wait" or "I can't do this"
  188. (
  189. r'i can\'t (.*)',
  190. (
  191. "What we can and can't do is a limitation of the mind.",
  192. "There are limitations of the body, and limitations of the mind.",
  193. "Have you tried to%1 with a clear mind?",
  194. ),
  195. ),
  196. # "I think.." indicates uncertainty. e.g. "I think so."
  197. # problem: exceptions...
  198. # e.g. "I think, therefore I am"
  199. (
  200. r'i think (.*)',
  201. (
  202. "Uncertainty in an uncertain world.",
  203. "Indeed, how can we be certain of anything in such uncertain times.",
  204. "Are you not, in fact, certain that%1?",
  205. ),
  206. ),
  207. # "I feel...emotions/sick/light-headed..."
  208. (
  209. r'i feel (.*)',
  210. (
  211. "Your body and your emotions are both symptoms of your mind."
  212. "What do you believe is the root of such feelings?",
  213. "Feeling%1 can be a sign of your state-of-mind.",
  214. ),
  215. ),
  216. # exclaimation mark indicating emotion
  217. # e.g. "Wow!" or "No!"
  218. (
  219. r'(.*)!',
  220. (
  221. "I sense that you are feeling emotional today.",
  222. "You need to calm your emotions.",
  223. ),
  224. ),
  225. # because [statement]
  226. # e.g. "because I said so"
  227. (
  228. r'because (.*)',
  229. (
  230. "Does knowning the reasons behind things help you to understand"
  231. " the things themselves?",
  232. "If%1, what else must be true?",
  233. ),
  234. ),
  235. # yes or no - raise an issue of certainty/correctness
  236. (
  237. r'(yes)|(no)',
  238. (
  239. "Is there certainty in an uncertain world?",
  240. "It is better to be right than to be certain.",
  241. ),
  242. ),
  243. # sentence containing word 'love'
  244. (
  245. r'(.*)love(.*)',
  246. (
  247. "Think of the trees: they let the birds perch and fly with no intention to call them when they come, and no longing for their return when they fly away. Let your heart be like the trees.",
  248. "Free love!",
  249. ),
  250. ),
  251. # sentence containing word 'understand' - r
  252. (
  253. r'(.*)understand(.*)',
  254. (
  255. "If you understand, things are just as they are;"
  256. " if you do not understand, things are just as they are.",
  257. "Imagination is more important than knowledge.",
  258. ),
  259. ),
  260. # 'I', 'me', 'my' - person is talking about themself.
  261. # this breaks down when words contain these - eg 'Thyme', 'Irish'
  262. (
  263. r'(.*)(me )|( me)|(my)|(mine)|(i)(.*)',
  264. (
  265. "'I', 'me', 'my'... these are selfish expressions.",
  266. "Have you ever considered that you might be a selfish person?",
  267. "Try to consider others, not just yourself.",
  268. "Think not just of yourself, but of others.",
  269. ),
  270. ),
  271. # 'you' starting a sentence
  272. # e.g. "you stink!"
  273. (
  274. r'you (.*)',
  275. ("My path is not of conern to you.", "I am but one, and you but one more."),
  276. ),
  277. # say goodbye with some extra Zen wisdom.
  278. (
  279. r'exit',
  280. (
  281. "Farewell. The obstacle is the path.",
  282. "Farewell. Life is a journey, not a destination.",
  283. "Good bye. We are cups, constantly and quietly being filled."
  284. "\nThe trick is knowning how to tip ourselves over and let the beautiful stuff out.",
  285. ),
  286. ),
  287. # fall through case -
  288. # when stumped, respond with generic zen wisdom
  289. #
  290. (
  291. r'(.*)',
  292. (
  293. "When you're enlightened, every word is wisdom.",
  294. "Random talk is useless.",
  295. "The reverse side also has a reverse side.",
  296. "Form is emptiness, and emptiness is form.",
  297. "I pour out a cup of water. Is the cup empty?",
  298. ),
  299. ),
  300. )
  301. zen_chatbot = Chat(responses, reflections)
  302. def zen_chat():
  303. print('*' * 75)
  304. print("Zen Chatbot!".center(75))
  305. print('*' * 75)
  306. print('"Look beyond mere words and letters - look into your mind"'.center(75))
  307. print("* Talk your way to truth with Zen Chatbot.")
  308. print("* Type 'quit' when you have had enough.")
  309. print('*' * 75)
  310. print("Welcome, my child.")
  311. zen_chatbot.converse()
  312. def demo():
  313. zen_chat()
  314. if __name__ == "__main__":
  315. demo()