1
2
3
4
5
6
7
8
9 import string
10 import re
11 import random
12
13
14
15
16
18 words = string.split(string.lower(str))
19 keys = dict.keys();
20 for i in range(0,len(words)):
21 if words[i] in keys:
22 words[i] = dict[words[i]]
23 return string.join(words)
24
25
26
27
28
29
31
32 for i in range(0,len(keys)):
33 if re.match(keys[i],(str)):
34
35
36 respnum = random.randint(0,len(values[i])-1)
37 resp = values[i][respnum]
38
39 pos = string.find(resp,'%')
40 while pos > -1:
41 num = string.atoi(resp[pos+1:pos+2])
42 resp = resp[:pos] + \
43 translate(keys[i].group(num),gReflections) + \
44 resp[pos+2:]
45 pos = string.find(resp,'%')
46
47 if resp[-2:] == '?.': resp = resp[:-2] + '.'
48 if resp[-2:] == '??': resp = resp[:-2] + '?'
49 return resp
50
51
52
53
54
55 gReflections = {
56 "am" : "are",
57 "was" : "were",
58 "i" : "you",
59 "i'd" : "you would",
60 "i've" : "you have",
61 "i'll" : "you will",
62 "my" : "your",
63 "are" : "am",
64 "you've": "I have",
65 "you'll": "I will",
66 "your" : "my",
67 "yours" : "mine",
68 "you" : "me",
69 "me" : "you" }
70
71
72
73
74
75
76
77 gPats = [
78 ["I need .*",
79 [ "Why do you need %1?",
80 "Would it really help you to get %1?",
81 "Are you sure you need %1?"]],
82
83 ["Why don't you .*",
84 [ "Do you really think I don't %1?",
85 "Perhaps eventually I will %1.",
86 "Do you really want me to %1?"]],
87
88 ["Why can't I .*",
89 [ "Do you think you should be able to %1?",
90 "If you could %1, what would you do?",
91 "I don't know -- why can't you %1?",
92 "Have you really tried?"]],
93
94 ["I can't .*",
95 [ "How do you know you can't %1?",
96 "Perhaps you could %1 if you tried.",
97 "What would it take for you to %1?"]],
98
99 ["I am .*",
100 [ "Did you come to me because you are %1?",
101 "How long have you been %1?",
102 "How do you feel about being %1?"]],
103
104 ["I'm .*",
105 [ "How does being %1 make you feel?",
106 "Do you enjoy being %1?",
107 "Why do you tell me you're %1?",
108 "Why do you think you're %1?"]],
109
110 ["Are you .*",
111 [ "Why does it matter whether I am %1?",
112 "Would you prefer it if I were not %1?",
113 "Perhaps you believe I am %1.",
114 "I may be %1 -- what do you think?"]],
115
116 ["What .*",
117 [ "Why do you ask?",
118 "How would an answer to that help you?",
119 "What do you think?"]],
120
121 ["How .*",
122 [ "How do you suppose?",
123 "Perhaps you can answer your own question.",
124 "What is it you're really asking?"]],
125
126 ["Because .*",
127 [ "Is that the real reason?",
128 "What other reasons come to mind?",
129 "Does that reason apply to anything else?",
130 "If %1, what else must be true?"]],
131
132 [".* sorry .*",
133 [ "There are many times when no apology is needed.",
134 "What feelings do you have when you apologize?"]],
135
136 ["Hello.*",
137 [ "Hello... I'm glad you could drop by today.",
138 "Hi there... how are you today?",
139 "Hello, how are you feeling today?"]],
140
141 ["I think .*",
142 [ "Do you doubt %1?",
143 "Do you really think so?",
144 "But you're not sure %1?"]],
145
146 [".* friend.*",
147 [ "Tell me more about your friends.",
148 "When you think of a friend, what comes to mind?",
149 "Why don't you tell me about a childhood friend?"]],
150
151 ["Yes",
152 [ "You seem quite sure.",
153 "OK, but can you elaborate a bit?"]],
154
155 [".* computer.*",
156 [ "Are you really talking about me?",
157 "Does it seem strange to talk to a computer?",
158 "How do computers make you feel?",
159 "Do you feel threatened by computers?"]],
160
161 ["Is it .*",
162 [ "Do you think it is %1?",
163 "Perhaps it's %1 -- what do you think?",
164 "If it were %1, what would you do?",
165 "It could well be that %1."]],
166
167 ["It is .*",
168 [ "You seem very certain.",
169 "If I told you that it probably isn't %1, what would you feel?"]],
170
171 ["Can you .*",
172 [ "What makes you think I can't %1?",
173 "If I could %1, then what?",
174 "Why do you ask if I can %1?"]],
175
176 ["Can I .*",
177 [ "Perhaps you don't want to %1.",
178 "Do you want to be able to %1?",
179 "If you could %1, would you?"]],
180
181 ["You are .*",
182 [ "Why do you think I am %1?",
183 "Does it please you to think that I'm %1?",
184 "Perhaps you would like me to be %1.",
185 "Perhaps you're really talking about yourself?"]],
186
187 ["You're .*",
188 [ "Why do you say I am %1?",
189 "Why do you think I am %1?",
190 "Are we talking about you, or me?"]],
191
192 ["I don't .*",
193 [ "Don't you really %1?",
194 "Why don't you %1?",
195 "Do you want to %1?"]],
196
197 ["I feel .*",
198 [ "Good, tell me more about these feelings.",
199 "Do you often feel %1?",
200 "When do you usually feel %1?",
201 "When you feel %1, what do you do?"]],
202
203 ["I have .*",
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 ["I would .*",
209 [ "Could you explain why you would %1?",
210 "Why would you %1?",
211 "Who else knows that you would %1?"]],
212
213 ["Is there .*",
214 [ "Do you think there is %1?",
215 "It's likely that there is %1.",
216 "Would you like there to be %1?"]],
217
218 ["My .*",
219 [ "I see, your %1.",
220 "Why do you say that your %1?",
221 "When your %1, how do you feel?"]],
222
223 ["You .*",
224 [ "We should be discussing you, not me.",
225 "Why do you say that about me?",
226 "Why do you care whether I %1?"]],
227
228 ["Why .*",
229 [ "Why don't you tell me the reason why %1?",
230 "Why do you think %1?" ]],
231
232 ["I want .*",
233 [ "What would it mean to you if you got %1?",
234 "Why do you want %1?",
235 "What would you do if you got %1?",
236 "If you got %1, then what would you do?"]],
237
238 [".* mother.*",
239 [ "Tell me more about your mother.",
240 "What was your relationship with your mother like?",
241 "How do you feel about your mother?",
242 "How does this relate to your feelings today?",
243 "Good family relations are important."]],
244
245 [".* father.*",
246 [ "Tell me more about your father.",
247 "How did your father make you feel?",
248 "How do you feel about your father?",
249 "Does your relationship with your father relate to your feelings today?",
250 "Do you have trouble showing affection with your family?"]],
251
252 [".* child.*",
253 [ "Did you have close friends as a child?",
254 "What is your favorite childhood memory?",
255 "Do you remember any dreams or nightmares from childhood?",
256 "Did the other children sometimes tease you?",
257 "How do you think your childhood experiences relate to your feelings today?"]],
258
259 [".*\?",
260 [ "Why do you ask that?",
261 "Please consider whether you can answer your own question.",
262 "Perhaps the answer lies within yourself?",
263 "Why don't you tell me?"]],
264
265 ["quit",
266 [ "Thank you for talking with me.",
267 "Good-bye.",
268 "Thank you, that will be $150. Have a good day!"]],
269
270 [".*",
271 [ "Please tell me more.",
272 "Let's change focus a bit... Tell me about your family.",
273 "Can you elaborate on that?",
274 "Why do you say that %1?",
275 "I see.",
276 "Very interesting.",
277 "%1.",
278 "I see. And what does that tell you?",
279 "How does that make you feel?",
280 "How do you feel when you say that?"]]
281 ]
282
283
284
285
286
287 if __name__ == '__main__':
288 gKeys = map(lambda x:re.compile(x[0]),gPats)
289 gValues = map(lambda x:x[1],gPats)
290
291 print "Therapist\n---------"
292 print "Talk to the program by typing in plain English, using normal upper-"
293 print 'and lower-case letters and punctuation. Enter "quit" when done.'
294 print '='*72
295 print "Hello. How are you feeling today?"
296 s = ""
297 while s != "quit":
298 try: s = raw_input(">")
299 except EOFError:
300 s = "quit"
301 print s
302 while s[-1] in "!.": s = s[:-1]
303 print respond(s,gKeys,gValues)
304