- commit
- de09b1b7370cd5ab73136db6a77ac28c67ecfc69
- parent
- 0260d39375b8da57d2593fc5c1d9d5010c23ba7e
- Author
- Tobias Bengfort <tobias.bengfort@gmx.net>
- Date
- 2016-05-24 06:26
refactor eliza
Diffstat
| M | eliza.py | 575 | +++++++++++++++++++++++++++++++------------------------------ |
| M | elizaBot.py | 6 | ++---- |
2 files changed, 290 insertions, 291 deletions
diff --git a/eliza.py b/eliza.py
@@ -1,286 +1,288 @@1 -1 #----------------------------------------------------------------------2 -1 # eliza.py3 -1 #4 -1 # a cheezy little Eliza knock-off by Joe Strout <joe@strout.net>5 -1 # with some updates by Jeff Epler <jepler@inetnebr.com>6 -1 # hacked into a module and updated by Jez Higgins <jez@jezuk.co.uk>7 -1 # last revised: 28 February 20058 -1 #----------------------------------------------------------------------9 -110 -1 import re11 -1 import random12 -113 -114 -1 class Eliza:15 -1 def __init__(self):16 -1 self.keys = [re.compile(x[0], re.IGNORECASE) for x in gPats]17 -1 self.values = [x[1] for x in gPats]18 -119 -1 #----------------------------------------------------------------------20 -1 # translate: take a string, replace any words found in dict.keys()21 -1 # with the corresponding dict.values()22 -1 #----------------------------------------------------------------------23 -1 def translate(self, str, dict):24 -1 words = str.lower().split()25 -1 keys = dict.keys()26 -1 for i in range(0, len(words)):27 -1 if words[i] in keys:28 -1 words[i] = dict[words[i]]29 -1 return ''.join(words)30 -131 -1 #----------------------------------------------------------------------32 -1 # respond: take a string, a set of regexps, and a corresponding33 -1 # set of response lists; find a match, and return a randomly34 -1 # chosen response from the corresponding list.35 -1 #----------------------------------------------------------------------36 -1 def respond(self, str):37 -1 # find a match among keys38 -1 for i in range(0, len(self.keys)):39 -1 match = self.keys[i].match(str)40 -1 if match:41 -1 # found a match ... stuff with corresponding value42 -1 # chosen randomly from among the available options43 -1 resp = random.choice(self.values[i])44 -1 # we've got a response... stuff in reflected text where indicated45 -1 pos = resp.find('%')46 -1 while pos > -1:47 -1 num = int(resp[pos + 1:pos + 2])48 -1 resp = resp[:pos] + self.translate(match.group(num), gReflections) + resp[pos + 2:]49 -1 pos = resp.find('%')50 -1 # fix munged punctuation at the end51 -1 if resp[-2:] == '?.': resp = resp[:-2] + '.'52 -1 if resp[-2:] == '??': resp = resp[:-2] + '?'53 -1 return resp54 -155 -1 #----------------------------------------------------------------------56 -1 # gReflections, a translation table used to convert things you say57 -1 # into things the computer says back, e.g. "I am" --> "you are"58 -1 #----------------------------------------------------------------------59 -1 gReflections = {60 -1 "am": "are",61 -1 "was": "were",62 -1 "i": "you",63 -1 "i'd": "you would",64 -1 "i've": "you have",65 -1 "i'll": "you will",66 -1 "my": "your",67 -1 "are": "am",68 -1 "you've": "I have",69 -1 "you'll": "I will",70 -1 "your": "my",71 -1 "yours": "mine",72 -1 "you": "me",73 -1 "me": "you"74 -1 }75 -176 -1 #----------------------------------------------------------------------77 -1 # gPats, the main response table. Each element of the list is a78 -1 # two-element list; the first is a regexp, and the second is a79 -1 # list of possible responses, with group-macros labelled as80 -1 # %1, %2, etc.81 -1 #----------------------------------------------------------------------82 -1 gPats = [83 -1 [r'I need (.*)',84 -1 ["Why do you need %1?",85 -1 "Would it really help you to get %1?",86 -1 "Are you sure you need %1?"]],87 -188 -1 [r'Why don\'?t you ([^\?]*)\??',89 -1 ["Do you really think I don't %1?",90 -1 "Perhaps eventually I will %1.",91 -1 "Do you really want me to %1?"]],92 -193 -1 [r'Why can\'?t I ([^\?]*)\??',94 -1 ["Do you think you should be able to %1?",95 -1 "If you could %1, what would you do?",96 -1 "I don't know -- why can't you %1?",97 -1 "Have you really tried?"]],98 -199 -1 [r'I can\'?t (.*)',100 -1 ["How do you know you can't %1?",101 -1 "Perhaps you could %1 if you tried.",102 -1 "What would it take for you to %1?"]],103 -1104 -1 [r'I am (.*)',105 -1 ["Did you come to me because you are %1?",106 -1 "How long have you been %1?",107 -1 "How do you feel about being %1?"]],108 -1109 -1 [r'I\'?m (.*)',110 -1 ["How does being %1 make you feel?",111 -1 "Do you enjoy being %1?",112 -1 "Why do you tell me you're %1?",113 -1 "Why do you think you're %1?"]],114 -1115 -1 [r'Are you ([^\?]*)\??',116 -1 ["Why does it matter whether I am %1?",117 -1 "Would you prefer it if I were not %1?",118 -1 "Perhaps you believe I am %1.",119 -1 "I may be %1 -- what do you think?"]],120 -1121 -1 [r'What (.*)',122 -1 ["Why do you ask?",123 -1 "How would an answer to that help you?",124 -1 "What do you think?"]],125 -1126 -1 [r'How (.*)',127 -1 ["How do you suppose?",128 -1 "Perhaps you can answer your own question.",129 -1 "What is it you're really asking?"]],130 -1131 -1 [r'Because (.*)',132 -1 ["Is that the real reason?",133 -1 "What other reasons come to mind?",134 -1 "Does that reason apply to anything else?",135 -1 "If %1, what else must be true?"]],136 -1137 -1 [r'(.*) sorry (.*)',138 -1 ["There are many times when no apology is needed.",139 -1 "What feelings do you have when you apologize?"]],140 -1141 -1 [r'Hello(.*)',142 -1 ["Hello... I'm glad you could drop by today.",143 -1 "Hi there... how are you today?",144 -1 "Hello, how are you feeling today?"]],145 -1146 -1 [r'I think (.*)',147 -1 ["Do you doubt %1?",148 -1 "Do you really think so?",149 -1 "But you're not sure %1?"]],150 -1151 -1 [r'(.*) friend (.*)',152 -1 ["Tell me more about your friends.",153 -1 "When you think of a friend, what comes to mind?",154 -1 "Why don't you tell me about a childhood friend?"]],155 -1156 -1 [r'Yes',157 -1 ["You seem quite sure.",158 -1 "OK, but can you elaborate a bit?"]],159 -1160 -1 [r'(.*) computer(.*)',161 -1 ["Are you really talking about me?",162 -1 "Does it seem strange to talk to a computer?",163 -1 "How do computers make you feel?",164 -1 "Do you feel threatened by computers?"]],165 -1166 -1 [r'Is it (.*)',167 -1 ["Do you think it is %1?",168 -1 "Perhaps it's %1 -- what do you think?",169 -1 "If it were %1, what would you do?",170 -1 "It could well be that %1."]],171 -1172 -1 [r'It is (.*)',173 -1 ["You seem very certain.",174 -1 "If I told you that it probably isn't %1, what would you feel?"]],175 -1176 -1 [r'Can you ([^\?]*)\??',177 -1 ["What makes you think I can't %1?",178 -1 "If I could %1, then what?",179 -1 "Why do you ask if I can %1?"]],180 -1181 -1 [r'Can I ([^\?]*)\??',182 -1 ["Perhaps you don't want to %1.",183 -1 "Do you want to be able to %1?",184 -1 "If you could %1, would you?"]],185 -1186 -1 [r'You are (.*)',187 -1 ["Why do you think I am %1?",188 -1 "Does it please you to think that I'm %1?",189 -1 "Perhaps you would like me to be %1.",190 -1 "Perhaps you're really talking about yourself?"]],191 -1192 -1 [r'You\'?re (.*)',193 -1 ["Why do you say I am %1?",194 -1 "Why do you think I am %1?",195 -1 "Are we talking about you, or me?"]],196 -1197 -1 [r'I don\'?t (.*)',198 -1 ["Don't you really %1?",199 -1 "Why don't you %1?",200 -1 "Do you want to %1?"]],201 -1202 -1 [r'I feel (.*)',203 -1 ["Good, tell me more about these feelings.",204 -1 "Do you often feel %1?",205 -1 "When do you usually feel %1?",206 -1 "When you feel %1, what do you do?"]],207 -1208 -1 [r'I have (.*)',209 -1 ["Why do you tell me that you've %1?",210 -1 "Have you really %1?",211 -1 "Now that you have %1, what will you do next?"]],212 -1213 -1 [r'I would (.*)',214 -1 ["Could you explain why you would %1?",215 -1 "Why would you %1?",216 -1 "Who else knows that you would %1?"]],217 -1218 -1 [r'Is there (.*)',219 -1 ["Do you think there is %1?",220 -1 "It's likely that there is %1.",221 -1 "Would you like there to be %1?"]],222 -1223 -1 [r'My (.*)',224 -1 ["I see, your %1.",225 -1 "Why do you say that your %1?",226 -1 "When your %1, how do you feel?"]],227 -1228 -1 [r'You (.*)',229 -1 ["We should be discussing you, not me.",230 -1 "Why do you say that about me?",231 -1 "Why do you care whether I %1?"]],232 -1233 -1 [r'Why (.*)',234 -1 ["Why don't you tell me the reason why %1?",235 -1 "Why do you think %1?"]],236 -1237 -1 [r'I want (.*)',238 -1 ["What would it mean to you if you got %1?",239 -1 "Why do you want %1?",240 -1 "What would you do if you got %1?",241 -1 "If you got %1, then what would you do?"]],242 -1243 -1 [r'(.*) mother(.*)',244 -1 ["Tell me more about your mother.",245 -1 "What was your relationship with your mother like?",246 -1 "How do you feel about your mother?",247 -1 "How does this relate to your feelings today?",248 -1 "Good family relations are important."]],249 -1250 -1 [r'(.*) father(.*)',251 -1 ["Tell me more about your father.",252 -1 "How did your father make you feel?",253 -1 "How do you feel about your father?",254 -1 "Does your relationship with your father relate to your feelings today?",255 -1 "Do you have trouble showing affection with your family?"]],256 -1257 -1 [r'(.*) child(.*)',258 -1 ["Did you have close friends as a child?",259 -1 "What is your favorite childhood memory?",260 -1 "Do you remember any dreams or nightmares from childhood?",261 -1 "Did the other children sometimes tease you?",262 -1 "How do you think your childhood experiences relate to your feelings today?"]],263 -1264 -1 [r'(.*)\?',265 -1 ["Why do you ask that?",266 -1 "Please consider whether you can answer your own question.",267 -1 "Perhaps the answer lies within yourself?",268 -1 "Why don't you tell me?"]],269 -1270 -1 [r'quit',271 -1 ["Thank you for talking with me.",272 -1 "Good-bye.",273 -1 "Thank you, that will be $150. Have a good day!"]],274 -1275 -1 [r'(.*)',276 -1 ["Please tell me more.",277 -1 "Let's change focus a bit... Tell me about your family.",278 -1 "Can you elaborate on that?",279 -1 "Why do you say that %1?",280 -1 "I see.",281 -1 "Very interesting.",282 -1 "%1.",283 -1 "I see. And what does that tell you?",284 -1 "How does that make you feel?",285 -1 "How do you feel when you say that?"]]286 -1286 -1 \ No newline at end of file-1 1 #!/usr/bin/env python -1 2 -1 3 """eliza.py -1 4 -1 5 a cheezy little Eliza knock-off by Joe Strout <joe@strout.net> -1 6 with some updates by Jeff Epler <jepler@inetnebr.com> -1 7 hacked into a module and updated by Jez Higgins <jez@jezuk.co.uk> -1 8 last revised: 28 February 2005""" -1 9 -1 10 import re -1 11 import random -1 12 -1 13 REFLECTIONS = { -1 14 'am': 'are', -1 15 'was': 'were', -1 16 'i': 'you', -1 17 'i\'d': 'you would', -1 18 'i\'ve': 'you have', -1 19 'i\'ll': 'you will', -1 20 'my': 'your', -1 21 'are': 'am', -1 22 'you\'ve': 'I have', -1 23 'you\'ll': 'I will', -1 24 'your': 'my', -1 25 'yours': 'mine', -1 26 'you': 'me', -1 27 'me': 'you', -1 28 'yourself': 'myself', -1 29 } -1 30 -1 31 PATS = [ -1 32 (r'I need (.*)', [ -1 33 'Why do you need {0}?', -1 34 'Would it really help you to get {0}?', -1 35 'Are you sure you need {0}?', -1 36 ]), -1 37 (r'Why don\'?t you ([^\?]*)\??', [ -1 38 'Do you really think I don\'t {0}?', -1 39 'Perhaps eventually I will {0}.', -1 40 'Do you really want me to {0}?', -1 41 ]), -1 42 (r'Why can\'?t I ([^\?]*)\??', [ -1 43 'Do you think you should be able to {0}?', -1 44 'If you could {0}, what would you do?', -1 45 'I don\'t know -- why can\'t you {0}?', -1 46 'Have you really tried?', -1 47 ]), -1 48 (r'I can\'?t (.*)', [ -1 49 'How do you know you can\'t {0}?', -1 50 'Perhaps you could {0} if you tried.', -1 51 'What would it take for you to {0}?', -1 52 ]), -1 53 (r'I am (.*)', [ -1 54 'Did you come to me because you are {0}?', -1 55 'How long have you been {0}?', -1 56 'How do you feel about being {0}?', -1 57 ]), -1 58 (r'I\'?m (.*)', [ -1 59 'How does being {0} make you feel?', -1 60 'Do you enjoy being {0}?', -1 61 'Why do you tell me you\'re {0}?', -1 62 'Why do you think you\'re {0}?', -1 63 ]), -1 64 (r'Are you ([^\?]*)\??', [ -1 65 'Why does it matter whether I am {0}?', -1 66 'Would you prefer it if I were not {0}?', -1 67 'Perhaps you believe I am {0}.', -1 68 'I may be {0} -- what do you think?', -1 69 ]), -1 70 (r'What (.*)', [ -1 71 'Why do you ask?', -1 72 'How would an answer to that help you?', -1 73 'What do you think?', -1 74 ]), -1 75 (r'How (.*)', [ -1 76 'How do you suppose?', -1 77 'Perhaps you can answer your own question.', -1 78 'What is it you\'re really asking?', -1 79 ]), -1 80 (r'Because (.*)', [ -1 81 'Is that the real reason?', -1 82 'What other reasons come to mind?', -1 83 'Does that reason apply to anything else?', -1 84 'If {0}, what else must be true?', -1 85 ]), -1 86 (r'(.*) sorry (.*)', [ -1 87 'There are many times when no apology is needed.', -1 88 'What feelings do you have when you apologize?', -1 89 ]), -1 90 (r'Hello(.*)', [ -1 91 'Hello... I\'m glad you could drop by today.', -1 92 'Hi there... how are you today?', -1 93 'Hello, how are you feeling today?', -1 94 ]), -1 95 (r'I think (.*)', [ -1 96 'Do you doubt {0}?', -1 97 'Do you really think so?', -1 98 'But you\'re not sure {0}?', -1 99 ]), -1 100 (r'(.*) friend (.*)', [ -1 101 'Tell me more about your friends.', -1 102 'When you think of a friend, what comes to mind?', -1 103 'Why don\'t you tell me about a childhood friend?', -1 104 ]), -1 105 (r'Yes', [ -1 106 'You seem quite sure.', -1 107 'OK, but can you elaborate a bit?', -1 108 ]), -1 109 (r'(.*) computer(.*)', [ -1 110 'Are you really talking about me?', -1 111 'Does it seem strange to talk to a computer?', -1 112 'How do computers make you feel?', -1 113 'Do you feel threatened by computers?', -1 114 ]), -1 115 (r'Is it (.*)', [ -1 116 'Do you think it is {0}?', -1 117 'Perhaps it\'s {0} -- what do you think?', -1 118 'If it were {0}, what would you do?', -1 119 'It could well be that {0}.', -1 120 ]), -1 121 (r'It is (.*)', [ -1 122 'You seem very certain.', -1 123 'If I told you that it probably isn\'t {0}, what would you feel?', -1 124 ]), -1 125 (r'Can you ([^\?]*)\??', [ -1 126 'What makes you think I can\'t {0}?', -1 127 'If I could {0}, then what?', -1 128 'Why do you ask if I can {0}?', -1 129 ]), -1 130 (r'Can I ([^\?]*)\??', [ -1 131 'Perhaps you don\'t want to {0}.', -1 132 'Do you want to be able to {0}?', -1 133 'If you could {0}, would you?', -1 134 ]), -1 135 (r'You are (.*)', [ -1 136 'Why do you think I am {0}?', -1 137 'Does it please you to think that I\'m {0}?', -1 138 'Perhaps you would like me to be {0}.', -1 139 'Perhaps you\'re really talking about yourself?', -1 140 ]), -1 141 (r'You\'?re (.*)', [ -1 142 'Why do you say I am {0}?', -1 143 'Why do you think I am {0}?', -1 144 'Are we talking about you, or me?', -1 145 ]), -1 146 (r'I don\'?t (.*)', [ -1 147 'Don\'t you really {0}?', -1 148 'Why don\'t you {0}?', -1 149 'Do you want to {0}?', -1 150 ]), -1 151 (r'I feel (.*)', [ -1 152 'Good, tell me more about these feelings.', -1 153 'Do you often feel {0}?', -1 154 'When do you usually feel {0}?', -1 155 'When you feel {0}, what do you do?', -1 156 ]), -1 157 (r'I have (.*)', [ -1 158 'Why do you tell me that you\'ve {0}?', -1 159 'Have you really {0}?', -1 160 'Now that you have {0}, what will you do next?', -1 161 ]), -1 162 (r'I would (.*)', [ -1 163 'Could you explain why you would {0}?', -1 164 'Why would you {0}?', -1 165 'Who else knows that you would {0}?', -1 166 ]), -1 167 (r'Is there (.*)', [ -1 168 'Do you think there is {0}?', -1 169 'It\'s likely that there is {0}.', -1 170 'Would you like there to be {0}?', -1 171 ]), -1 172 (r'My (.*)', [ -1 173 'I see, your {0}.', -1 174 'Why do you say that your {0}?', -1 175 'When your {0}, how do you feel?', -1 176 ]), -1 177 (r'You (.*)', [ -1 178 'We should be discussing you, not me.', -1 179 'Why do you say that about me?', -1 180 'Why do you care whether I {0}?', -1 181 ]), -1 182 (r'Why (.*)', [ -1 183 'Why don\'t you tell me the reason why {0}?', -1 184 'Why do you think {0}?', -1 185 ]), -1 186 (r'I want (.*)', [ -1 187 'What would it mean to you if you got {0}?', -1 188 'Why do you want {0}?', -1 189 'What would you do if you got {0}?', -1 190 'If you got {0}, then what would you do?', -1 191 ]), -1 192 (r'(.*) mother(.*)', [ -1 193 'Tell me more about your mother.', -1 194 'What was your relationship with your mother like?', -1 195 'How do you feel about your mother?', -1 196 'How does this relate to your feelings today?', -1 197 'Good family relations are important.', -1 198 ]), -1 199 (r'(.*) father(.*)', [ -1 200 'Tell me more about your father.', -1 201 'How did your father make you feel?', -1 202 'How do you feel about your father?', -1 203 'Does your relationship with your father relate to your feelings today?', -1 204 'Do you have trouble showing affection with your family?', -1 205 ]), -1 206 (r'(.*) child(.*)', [ -1 207 'Did you have close friends as a child?', -1 208 'What is your favorite childhood memory?', -1 209 'Do you remember any dreams or nightmares from childhood?', -1 210 'Did the other children sometimes tease you?', -1 211 'How do you think your childhood experiences relate to your feelings today?', -1 212 ]), -1 213 (r'(.*)\?', [ -1 214 'Why do you ask that?', -1 215 'Please consider whether you can answer your own question.', -1 216 'Perhaps the answer lies within yourself?', -1 217 'Why don\'t you tell me?', -1 218 ]), -1 219 (r'quit', [ -1 220 'Thank you for talking with me.', -1 221 'Good-bye.', -1 222 'Thank you, that will be $150. Have a good day!', -1 223 ]), -1 224 (r'(.*)', [ -1 225 'Please tell me more.', -1 226 'Let\'s change focus a bit... Tell me about your family.', -1 227 'Can you elaborate on that?', -1 228 'Why do you say that {0}?', -1 229 'I see.', -1 230 'Very interesting.', -1 231 '{0}.', -1 232 'I see. And what does that tell you?', -1 233 'How does that make you feel?', -1 234 'How do you feel when you say that?', -1 235 ]), -1 236 ] -1 237 -1 238 REGEXPS = [re.compile(p[0], re.IGNORECASE) for p in PATS] -1 239 RESPONSES = [p[1] for p in PATS] -1 240 -1 241 -1 242 def reflect(sentence): -1 243 words = sentence.split() -1 244 -1 245 for i in range(0, len(words)): -1 246 words[i] = REFLECTIONS.get(words[i].lower(), words[i]) -1 247 -1 248 return ' '.join(words) -1 249 -1 250 -1 251 def respond(sentence): -1 252 for i in range(len(REGEXPS)): -1 253 regexp = REGEXPS[i] -1 254 responses = RESPONSES[i] -1 255 -1 256 match = regexp.match(sentence) -1 257 if match: -1 258 response = random.choice(responses) -1 259 groups = [reflect(s) for s in match.groups()] -1 260 response = response.format(*groups) -1 261 -1 262 if response[-2:] == '?.': response = response[:-2] + '.' -1 263 if response[-2:] == '??': response = response[:-2] + '?' -1 264 -1 265 return response -1 266 -1 267 -1 268 def main(): -1 269 print('Therapist\n---------') -1 270 print('Talk to the program by typing in plain English, using normal upper-') -1 271 print('and lower-case letters and punctuation. Enter "quit" when done.') -1 272 print('=' * 72) -1 273 print('Hello. How are you feeling today?') -1 274 -1 275 s = '' -1 276 while s != 'quit': -1 277 try: -1 278 s = raw_input('>') -1 279 except EOFError: -1 280 s = 'quit' -1 281 print(s) -1 282 -1 283 s = s.rstrip('!.') -1 284 print(respond(s)) -1 285 -1 286 -1 287 if __name__ == '__main__': -1 288 main()
diff --git a/elizaBot.py b/elizaBot.py
@@ -1,12 +1,10 @@1 -1 from eliza import Eliza-1 1 import eliza 2 2 from errbot import botcmd, BotPlugin 3 3 4 4 5 5 class ElizaBot(BotPlugin):6 -1 eliza_daemon = Eliza()7 -18 6 @botcmd 9 7 def eliza(self, _, args): 10 8 """ El'cheapo shrink for you """ 11 9 args = args.strip()12 -1 return self.eliza_daemon.respond(args)-1 10 return eliza.respond(args)