Package nltk :: Package chat :: Module eliza
[hide private]
[frames] | no frames]

Source Code for Module nltk.chat.eliza

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