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

Source Code for Module nltk.chat.zen

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