eliza.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # Natural Language Toolkit: Eliza
  2. #
  3. # Copyright (C) 2001-2019 NLTK Project
  4. # Authors: Steven Bird <stevenbird1@gmail.com>
  5. # Edward Loper <edloper@gmail.com>
  6. # URL: <http://nltk.org/>
  7. # For license information, see LICENSE.TXT
  8. # Based on an Eliza implementation by Joe Strout <joe@strout.net>,
  9. # Jeff Epler <jepler@inetnebr.com> and Jez Higgins <mailto:jez@jezuk.co.uk>.
  10. # a translation table used to convert things you say into things the
  11. # computer says back, e.g. "I am" --> "you are"
  12. from __future__ import print_function
  13. from nltk.chat.util import Chat, reflections
  14. # a table of response pairs, where each pair consists of a
  15. # regular expression, and a list of possible responses,
  16. # with group-macros labelled as %1, %2.
  17. pairs = (
  18. (
  19. r'I need (.*)',
  20. (
  21. "Why do you need %1?",
  22. "Would it really help you to get %1?",
  23. "Are you sure you need %1?",
  24. ),
  25. ),
  26. (
  27. r'Why don\'t you (.*)',
  28. (
  29. "Do you really think I don't %1?",
  30. "Perhaps eventually I will %1.",
  31. "Do you really want me to %1?",
  32. ),
  33. ),
  34. (
  35. r'Why can\'t I (.*)',
  36. (
  37. "Do you think you should be able to %1?",
  38. "If you could %1, what would you do?",
  39. "I don't know -- why can't you %1?",
  40. "Have you really tried?",
  41. ),
  42. ),
  43. (
  44. r'I can\'t (.*)',
  45. (
  46. "How do you know you can't %1?",
  47. "Perhaps you could %1 if you tried.",
  48. "What would it take for you to %1?",
  49. ),
  50. ),
  51. (
  52. r'I am (.*)',
  53. (
  54. "Did you come to me because you are %1?",
  55. "How long have you been %1?",
  56. "How do you feel about being %1?",
  57. ),
  58. ),
  59. (
  60. r'I\'m (.*)',
  61. (
  62. "How does being %1 make you feel?",
  63. "Do you enjoy being %1?",
  64. "Why do you tell me you're %1?",
  65. "Why do you think you're %1?",
  66. ),
  67. ),
  68. (
  69. r'Are you (.*)',
  70. (
  71. "Why does it matter whether I am %1?",
  72. "Would you prefer it if I were not %1?",
  73. "Perhaps you believe I am %1.",
  74. "I may be %1 -- what do you think?",
  75. ),
  76. ),
  77. (
  78. r'What (.*)',
  79. (
  80. "Why do you ask?",
  81. "How would an answer to that help you?",
  82. "What do you think?",
  83. ),
  84. ),
  85. (
  86. r'How (.*)',
  87. (
  88. "How do you suppose?",
  89. "Perhaps you can answer your own question.",
  90. "What is it you're really asking?",
  91. ),
  92. ),
  93. (
  94. r'Because (.*)',
  95. (
  96. "Is that the real reason?",
  97. "What other reasons come to mind?",
  98. "Does that reason apply to anything else?",
  99. "If %1, what else must be true?",
  100. ),
  101. ),
  102. (
  103. r'(.*) sorry (.*)',
  104. (
  105. "There are many times when no apology is needed.",
  106. "What feelings do you have when you apologize?",
  107. ),
  108. ),
  109. (
  110. r'Hello(.*)',
  111. (
  112. "Hello... I'm glad you could drop by today.",
  113. "Hi there... how are you today?",
  114. "Hello, how are you feeling today?",
  115. ),
  116. ),
  117. (
  118. r'I think (.*)',
  119. ("Do you doubt %1?", "Do you really think so?", "But you're not sure %1?"),
  120. ),
  121. (
  122. r'(.*) friend (.*)',
  123. (
  124. "Tell me more about your friends.",
  125. "When you think of a friend, what comes to mind?",
  126. "Why don't you tell me about a childhood friend?",
  127. ),
  128. ),
  129. (r'Yes', ("You seem quite sure.", "OK, but can you elaborate a bit?")),
  130. (
  131. r'(.*) computer(.*)',
  132. (
  133. "Are you really talking about me?",
  134. "Does it seem strange to talk to a computer?",
  135. "How do computers make you feel?",
  136. "Do you feel threatened by computers?",
  137. ),
  138. ),
  139. (
  140. r'Is it (.*)',
  141. (
  142. "Do you think it is %1?",
  143. "Perhaps it's %1 -- what do you think?",
  144. "If it were %1, what would you do?",
  145. "It could well be that %1.",
  146. ),
  147. ),
  148. (
  149. r'It is (.*)',
  150. (
  151. "You seem very certain.",
  152. "If I told you that it probably isn't %1, what would you feel?",
  153. ),
  154. ),
  155. (
  156. r'Can you (.*)',
  157. (
  158. "What makes you think I can't %1?",
  159. "If I could %1, then what?",
  160. "Why do you ask if I can %1?",
  161. ),
  162. ),
  163. (
  164. r'Can I (.*)',
  165. (
  166. "Perhaps you don't want to %1.",
  167. "Do you want to be able to %1?",
  168. "If you could %1, would you?",
  169. ),
  170. ),
  171. (
  172. r'You are (.*)',
  173. (
  174. "Why do you think I am %1?",
  175. "Does it please you to think that I'm %1?",
  176. "Perhaps you would like me to be %1.",
  177. "Perhaps you're really talking about yourself?",
  178. ),
  179. ),
  180. (
  181. r'You\'re (.*)',
  182. (
  183. "Why do you say I am %1?",
  184. "Why do you think I am %1?",
  185. "Are we talking about you, or me?",
  186. ),
  187. ),
  188. (
  189. r'I don\'t (.*)',
  190. ("Don't you really %1?", "Why don't you %1?", "Do you want to %1?"),
  191. ),
  192. (
  193. r'I feel (.*)',
  194. (
  195. "Good, tell me more about these feelings.",
  196. "Do you often feel %1?",
  197. "When do you usually feel %1?",
  198. "When you feel %1, what do you do?",
  199. ),
  200. ),
  201. (
  202. r'I have (.*)',
  203. (
  204. "Why do you tell me that you've %1?",
  205. "Have you really %1?",
  206. "Now that you have %1, what will you do next?",
  207. ),
  208. ),
  209. (
  210. r'I would (.*)',
  211. (
  212. "Could you explain why you would %1?",
  213. "Why would you %1?",
  214. "Who else knows that you would %1?",
  215. ),
  216. ),
  217. (
  218. r'Is there (.*)',
  219. (
  220. "Do you think there is %1?",
  221. "It's likely that there is %1.",
  222. "Would you like there to be %1?",
  223. ),
  224. ),
  225. (
  226. r'My (.*)',
  227. (
  228. "I see, your %1.",
  229. "Why do you say that your %1?",
  230. "When your %1, how do you feel?",
  231. ),
  232. ),
  233. (
  234. r'You (.*)',
  235. (
  236. "We should be discussing you, not me.",
  237. "Why do you say that about me?",
  238. "Why do you care whether I %1?",
  239. ),
  240. ),
  241. (r'Why (.*)', ("Why don't you tell me the reason why %1?", "Why do you think %1?")),
  242. (
  243. r'I want (.*)',
  244. (
  245. "What would it mean to you if you got %1?",
  246. "Why do you want %1?",
  247. "What would you do if you got %1?",
  248. "If you got %1, then what would you do?",
  249. ),
  250. ),
  251. (
  252. r'(.*) mother(.*)',
  253. (
  254. "Tell me more about your mother.",
  255. "What was your relationship with your mother like?",
  256. "How do you feel about your mother?",
  257. "How does this relate to your feelings today?",
  258. "Good family relations are important.",
  259. ),
  260. ),
  261. (
  262. r'(.*) father(.*)',
  263. (
  264. "Tell me more about your father.",
  265. "How did your father make you feel?",
  266. "How do you feel about your father?",
  267. "Does your relationship with your father relate to your feelings today?",
  268. "Do you have trouble showing affection with your family?",
  269. ),
  270. ),
  271. (
  272. r'(.*) child(.*)',
  273. (
  274. "Did you have close friends as a child?",
  275. "What is your favorite childhood memory?",
  276. "Do you remember any dreams or nightmares from childhood?",
  277. "Did the other children sometimes tease you?",
  278. "How do you think your childhood experiences relate to your feelings today?",
  279. ),
  280. ),
  281. (
  282. r'(.*)\?',
  283. (
  284. "Why do you ask that?",
  285. "Please consider whether you can answer your own question.",
  286. "Perhaps the answer lies within yourself?",
  287. "Why don't you tell me?",
  288. ),
  289. ),
  290. (
  291. r'quit',
  292. (
  293. "Thank you for talking with me.",
  294. "Good-bye.",
  295. "Thank you, that will be $150. Have a good day!",
  296. ),
  297. ),
  298. (
  299. r'(.*)',
  300. (
  301. "Please tell me more.",
  302. "Let's change focus a bit... Tell me about your family.",
  303. "Can you elaborate on that?",
  304. "Why do you say that %1?",
  305. "I see.",
  306. "Very interesting.",
  307. "%1.",
  308. "I see. And what does that tell you?",
  309. "How does that make you feel?",
  310. "How do you feel when you say that?",
  311. ),
  312. ),
  313. )
  314. eliza_chatbot = Chat(pairs, reflections)
  315. def eliza_chat():
  316. print("Therapist\n---------")
  317. print("Talk to the program by typing in plain English, using normal upper-")
  318. print('and lower-case letters and punctuation. Enter "quit" when done.')
  319. print('=' * 72)
  320. print("Hello. How are you feeling today?")
  321. eliza_chatbot.converse()
  322. def demo():
  323. eliza_chat()
  324. if __name__ == "__main__":
  325. demo()