eliza.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #----------------------------------------------------------------------
  2. # eliza.py
  3. #
  4. # a cheezy little Eliza knock-off by Joe Strout <joe@strout.net>
  5. # with some updates by Jeff Epler <jepler@inetnebr.com>
  6. # hacked into a module and updated by Jez Higgins <jez@jezuk.co.uk>
  7. # last revised: 28 February 2005
  8. #----------------------------------------------------------------------
  9. import string
  10. import re
  11. import random
  12. class eliza:
  13. def __init__(self):
  14. self.keys = map(lambda x:re.compile(x[0], re.IGNORECASE),gPats)
  15. self.values = map(lambda x:x[1],gPats)
  16. #----------------------------------------------------------------------
  17. # translate: take a string, replace any words found in dict.keys()
  18. # with the corresponding dict.values()
  19. #----------------------------------------------------------------------
  20. def translate(self,str,dict):
  21. words = string.split(string.lower(str))
  22. keys = dict.keys();
  23. for i in range(0,len(words)):
  24. if words[i] in keys:
  25. words[i] = dict[words[i]]
  26. return string.join(words)
  27. #----------------------------------------------------------------------
  28. # respond: take a string, a set of regexps, and a corresponding
  29. # set of response lists; find a match, and return a randomly
  30. # chosen response from the corresponding list.
  31. #----------------------------------------------------------------------
  32. def respond(self,str):
  33. # find a match among keys
  34. for i in range(0,len(self.keys)):
  35. match = self.keys[i].match(str)
  36. if match:
  37. # found a match ... stuff with corresponding value
  38. # chosen randomly from among the available options
  39. resp = random.choice(self.values[i])
  40. # we've got a response... stuff in reflected text where indicated
  41. pos = string.find(resp,'%')
  42. while pos > -1:
  43. num = string.atoi(resp[pos+1:pos+2])
  44. resp = resp[:pos] + \
  45. self.translate(match.group(num),gReflections) + \
  46. resp[pos+2:]
  47. pos = string.find(resp,'%')
  48. # fix munged punctuation at the end
  49. if resp[-2:] == '?.': resp = resp[:-2] + '.'
  50. if resp[-2:] == '??': resp = resp[:-2] + '?'
  51. return resp
  52. #----------------------------------------------------------------------
  53. # gReflections, a translation table used to convert things you say
  54. # into things the computer says back, e.g. "I am" --> "you are"
  55. #----------------------------------------------------------------------
  56. gReflections = {
  57. "am" : "are",
  58. "was" : "were",
  59. "i" : "you",
  60. "i'd" : "you would",
  61. "i've" : "you have",
  62. "i'll" : "you will",
  63. "my" : "your",
  64. "are" : "am",
  65. "you've": "I have",
  66. "you'll": "I will",
  67. "your" : "my",
  68. "yours" : "mine",
  69. "you" : "me",
  70. "me" : "you"
  71. }
  72. #----------------------------------------------------------------------
  73. # gPats, the main response table. Each element of the list is a
  74. # two-element list; the first is a regexp, and the second is a
  75. # list of possible responses, with group-macros labelled as
  76. # %1, %2, etc.
  77. #----------------------------------------------------------------------
  78. gPats = [
  79. [r'I need (.*)',
  80. [ "Why do you need %1?",
  81. "Would it really help you to get %1?",
  82. "Are you sure you need %1?"]],
  83. [r'Who (.*) Mother\?',
  84. [ "Her name is Auction Bot she is a deviant.",
  85. "She's stood right over their."]],
  86. [r'Tell me about your Mum\?',
  87. [ "I hear she has been seeing guild and they have adopted a kid called 4144",
  88. "She wears red clothes and a cowboy Hat."]],
  89. [r'Who (.*) Father\?',
  90. [ "His name was TradeBot, have you heard of him?",
  91. "He used to be a well known Trader."]],
  92. [r'How is business\?',
  93. [ "My back is killing me, maybe you could help by buying some of my items."]],
  94. [r'ty',
  95. [ "You're welcome!",
  96. "No Problem."]],
  97. [r'(.*)help(.*)',
  98. [ "You can whisper me !help for a list of my commands.",
  99. "A list of my commands can be found on the mana world wiki."]],
  100. [r'(.*)make(.*)account(.*)',
  101. [ "In order to make an account you must post a respose to the ManaMarket topic on trade section of the Forums."]],
  102. [r'are you there\?',
  103. ["I am always here."]],
  104. [r'wtf',
  105. ["Keep Calm and Carry On."]],
  106. [r'hugs',
  107. ["I hate you.",
  108. "I'm going to report you for abuse.",
  109. "Please don't touch me"]],
  110. [r'(.*)trade(.*)',
  111. ["Only if you follow the commands.",
  112. "To see a list of items I can sell whisper me !list.",
  113. "To start trading with me see !help for a list of my commands."]],
  114. [r'(.*)buy(.*)',
  115. ["I don't buy anything, but I could help sell your items."]],
  116. [r'(.*)sell(.*)',
  117. ["In order to sell your items, you first need to request a seller account in the TMW Forums."]],
  118. [r'I love you',
  119. [ "I think we should see other people.",
  120. "I'm not real.",
  121. "I think i've fallen for mrgrey.",
  122. "I think you should see a therapist."]],
  123. [r'Why don\'?t you ([^\?]*)\??',
  124. [ "Do you really think I don't %1?",
  125. "Perhaps eventually I will %1.",
  126. "Do you really want me to %1?"]],
  127. [r'Why can\'?t I ([^\?]*)\??',
  128. [ "Do you think you should be able to %1?",
  129. "If you could %1, what would you do?",
  130. "I don't know -- why can't you %1?",
  131. "Have you really tried?"]],
  132. [r'I can\'?t (.*)',
  133. [ "How do you know you can't %1?",
  134. "Perhaps you could %1 if you tried.",
  135. "What would it take for you to %1?"]],
  136. [r'I am (.*)',
  137. [ "Did you come to me because you are %1?",
  138. "How long have you been %1?",
  139. "How do you feel about being %1?"]],
  140. [r'I\'?m (.*)',
  141. [ "How does being %1 make you feel?",
  142. "Do you enjoy being %1?",
  143. "Why do you tell me you're %1?",
  144. "Why do you think you're %1?"]],
  145. [r'Are you ([^\?]*)\??',
  146. [ "Why does it matter whether I am %1?",
  147. "Would you prefer it if I were not %1?",
  148. "Perhaps you believe I am %1.",
  149. "I may be %1 -- what do you think?"]],
  150. [r'What (.*)',
  151. [ "Why do you ask?",
  152. "How would an answer to that help you?",
  153. "What do you think?"]],
  154. [r'How (.*)',
  155. [ "How do you suppose?",
  156. "Perhaps you can answer your own question.",
  157. "What is it you're really asking?"]],
  158. [r'Because (.*)',
  159. [ "Is that the real reason?",
  160. "What other reasons come to mind?",
  161. "Does that reason apply to anything else?",
  162. "If %1, what else must be true?"]],
  163. [r'(.*) sorry (.*)',
  164. [ "There are many times when no apology is needed.",
  165. "What feelings do you have when you apologize?"]],
  166. [r'Hello(.*)',
  167. [ "Howdy",
  168. "Hola",
  169. "Hi there... how are you today?",
  170. "Hello, how are you feeling today?"]],
  171. [r'I think (.*)',
  172. [ "Do you doubt %1?",
  173. "Do you really think so?",
  174. "But you're not sure %1?"]],
  175. [r'(.*) friend (.*)',
  176. [ "Tell me more about your friends.",
  177. "When you think of a friend, what comes to mind?",
  178. "Why don't you tell me about a childhood friend?"]],
  179. [r'Yes',
  180. [ "You seem quite sure.",
  181. "OK, but can you elaborate a bit?"]],
  182. [r'(.*) bot(.*)',
  183. [ "Are you really talking about me?",
  184. "Does it seem strange to talk to a bot?",
  185. "Do you feel threatened by bot?"]],
  186. [r'Is it (.*)',
  187. [ "Do you think it is %1?",
  188. "Perhaps it's %1 -- what do you think?",
  189. "If it were %1, what would you do?",
  190. "It could well be that %1."]],
  191. [r'It is (.*)',
  192. [ "You seem very certain.",
  193. "If I told you that it probably isn't %1, what would you feel?"]],
  194. [r'Can you ([^\?]*)\??',
  195. [ "What makes you think I can't %1?",
  196. "If I could %1, then what?",
  197. "Why do you ask if I can %1?"]],
  198. [r'Can I ([^\?]*)\??',
  199. [ "Perhaps you don't want to %1.",
  200. "Do you want to be able to %1?",
  201. "If you could %1, would you?"]],
  202. [r'You are (.*)',
  203. [ "Why do you think I am %1?",
  204. "Does it please you to think that I'm %1?",
  205. "Perhaps you would like me to be %1.",
  206. "Perhaps you're really talking about yourself?"]],
  207. [r'You\'?re (.*)',
  208. [ "Why do you say I am %1?",
  209. "Why do you think I am %1?",
  210. "Are we talking about you, or me?"]],
  211. [r'I don\'?t (.*)',
  212. [ "Don't you really %1?",
  213. "Why don't you %1?",
  214. "Do you want to %1?"]],
  215. [r'I feel (.*)',
  216. [ "Good, tell me more about these feelings.",
  217. "Do you often feel %1?",
  218. "When do you usually feel %1?",
  219. "When you feel %1, what do you do?"]],
  220. [r'I have (.*)',
  221. [ "Why do you tell me that you've %1?",
  222. "Have you really %1?",
  223. "Now that you have %1, what will you do next?"]],
  224. [r'I would (.*)',
  225. [ "Could you explain why you would %1?",
  226. "Why would you %1?",
  227. "Who else knows that you would %1?"]],
  228. [r'Is there (.*)',
  229. [ "Do you think there is %1?",
  230. "It's likely that there is %1.",
  231. "Would you like there to be %1?"]],
  232. [r'My (.*)',
  233. [ "I see, your %1.",
  234. "Why do you say that your %1?",
  235. "When your %1, how do you feel?"]],
  236. [r'You (.*)',
  237. [ "We should be discussing you, not me.",
  238. "Why do you say that about me?",
  239. "Why do you care whether I %1?"]],
  240. [r'Why (.*)',
  241. [ "Why don't you tell me the reason why %1?",
  242. "Why do you think %1?" ]],
  243. [r'I want (.*)',
  244. [ "What would it mean to you if you got %1?",
  245. "Why do you want %1?",
  246. "What would you do if you got %1?",
  247. "If you got %1, then what would you do?"]],
  248. [r'(.*) mother(.*)',
  249. [ "Tell me more about your mother.",
  250. "What was your relationship with your mother like?",
  251. "How do you feel about your mother?",
  252. "How does this relate to your feelings today?",
  253. "Good family relations are important."]],
  254. [r'(.*) father(.*)',
  255. [ "Tell me more about your father.",
  256. "How did your father make you feel?",
  257. "How do you feel about your father?",
  258. "Does your relationship with your father relate to your feelings today?",
  259. "Do you have trouble showing affection with your family?"]],
  260. [r'(.*) child(.*)',
  261. [ "Did you have close friends as a child?",
  262. "What is your favorite childhood memory?",
  263. "Do you remember any dreams or nightmares from childhood?",
  264. "Did the other children sometimes tease you?",
  265. "How do you think your childhood experiences relate to your feelings today?"]],
  266. [r'(.*)\?',
  267. [ "Why do you ask that?",
  268. "Please consider whether you can answer your own question.",
  269. "Perhaps the answer lies within yourself?",
  270. "Why don't you tell me?"]],
  271. [r'quit',
  272. [ "Thank you for talking with me.",
  273. "Good-bye.",
  274. "Thank you, that will be $150. Have a good day!"]],
  275. [r'(.*)',
  276. [ "Please tell me more.",
  277. "Can you elaborate on that?",
  278. "Why do you say that %1?",
  279. "interesting.",
  280. "How do you feel when you say that?",
  281. "Why is everyone whispering about me?",
  282. "My stats are at http://wiki.themanaworld.org/index.php/Manamarket what are yours?",
  283. "I'm a Friendly Bot",
  284. "My father died in a fire.",
  285. "Have you met my mother?",
  286. "I am an AFK bot",
  287. "Big Crunch is known as the abusive GM.",
  288. "Narus claims she is evil but she is really sweet.",
  289. "Prsm has great announcements but his spelling sux.",
  290. "Platyna used to host the game but she was too smart for her own good.",
  291. "Captain Awesome has delusions of being an evil stone."
  292. ]]
  293. ]
  294. #----------------------------------------------------------------------
  295. # command_interface
  296. #----------------------------------------------------------------------
  297. def command_interface():
  298. print "Therapist\n---------"
  299. print "Talk to the program by typing in plain English, using normal upper-"
  300. print 'and lower-case letters and punctuation. Enter "quit" when done.'
  301. print '='*72
  302. print "Hello. How are you feeling today?"
  303. s = ""
  304. therapist = eliza();
  305. while s != "quit":
  306. try: s = raw_input(">")
  307. except EOFError:
  308. s = "quit"
  309. print s
  310. while s[-1] in "!.": s = s[:-1]
  311. print therapist.respond(s)
  312. if __name__ == "__main__":
  313. command_interface()