Crossfire Server, Trunk
eliza.py
Go to the documentation of this file.
1 # eliza.py -- Rogerian psychotherapist for Crossfire
2 #
3 # Here is what the original author had to say:
4 # ------------------------------------------------------------------
5 # a cheezy little Eliza knock-off by Joe Strout <joe@strout.net>
6 # with some updates by Jeff Epler <jepler@inetnebr.com>
7 # hacked into a module and updated by Jez Higgins <jez@jezuk.co.uk>
8 # last revised: 28 February 2005
9 # ------------------------------------------------------------------
10 # The author has released this script under the terms of the MIT License.
11 # WWW: http://www.jezuk.co.uk/cgi-bin/view/software/eliza
12 #
13 # Adpated to Crossfire by Kevin Zheng <kevinz5000@gmail.com>
14 
15 import Crossfire
16 import random
17 import re
18 import string
19 
20 class eliza:
21  def __init__(self):
22  self.keys = list(map(lambda x: re.compile(x[0], re.IGNORECASE), gPats))
23  self.values = list(map(lambda x: x[1], gPats))
24 
25  # ----------------------------------------------------------------------
26  # translate: take a string, replace any words found in dict.keys()
27  # with the corresponding dict.values()
28  # ----------------------------------------------------------------------
29  def translate(self, str, dict):
30  words = str.lower().split()
31  keys = dict.keys()
32  for i in range(0, len(words)):
33  if words[i] in keys:
34  words[i] = dict[words[i]]
35  return " ".join(words)
36 
37  # ----------------------------------------------------------------------
38  # respond: take a string, a set of regexps, and a corresponding
39  # set of response lists; find a match, and return a randomly
40  # chosen response from the corresponding list.
41  # ----------------------------------------------------------------------
42  def respond(self, str):
43  # find a match among keys
44  for i in range(0, len(self.keys)):
45  match = self.keys[i].match(str)
46  if match:
47  # found a match ... stuff with corresponding value
48  # chosen randomly from among the available options
49  resp = random.choice(self.values[i])
50 
51  # got a response... stuff in reflected text where indicated
52  pos = resp.find('%')
53  while pos > -1:
54  num = int(resp[pos + 1:pos + 2])
55  resp = resp[:pos] \
56  + self.translate(match.group(num),
57  gReflections) + resp[pos + 2:]
58  pos = resp.find('%')
59 
60  # fix munged punctuation at the end
61  if resp[-2:] == '?.':
62  resp = resp[:-2] + '.'
63  if resp[-2:] == '??':
64  resp = resp[:-2] + '?'
65  return resp
66 
67 
68 # ----------------------------------------------------------------------
69 # gReflections, a translation table used to convert things you say
70 # into things the computer says back, e.g. "I am" --> "you are"
71 # ----------------------------------------------------------------------
72 
73 gReflections = {
74  'am': 'are',
75  'was': 'were',
76  'i': 'you',
77  "i'd": 'you would',
78  "i've": 'you have',
79  "i'll": 'you will',
80  'my': 'your',
81  'are': 'am',
82  "you've": 'I have',
83  "you'll": 'I will',
84  'your': 'my',
85  'yours': 'mine',
86  'you': 'me',
87  'me': 'you',
88  }
89 
90 # ----------------------------------------------------------------------
91 # gPats, the main response table. Each element of the list is a
92 # two-element list; the first is a regexp, and the second is a
93 # list of possible responses, with group-macros labelled as
94 # %1, %2, etc.
95 # ----------------------------------------------------------------------
96 
97 gPats = [
98  [r'I need (.*)', ['Why do you need %1?',
99  'Would it really help you to get %1?', 'Are you sure you need %1?'
100  ]],
101  [r'Why don\'?t you ([^\?]*)\??', ["Do you really think I don't %1?"
102  , 'Perhaps eventually I will %1.', 'Do you really want me to %1?'
103  ]],
104  [r'Why can\'?t I ([^\?]*)\??',
105  ['Do you think you should be able to %1?',
106  'If you could %1, what would you do?',
107  "I don't know -- why can't you %1?", 'Have you really tried?']],
108  [r'I can\'?t (.*)', ["How do you know you can't %1?",
109  'Perhaps you could %1 if you tried.',
110  'What would it take for you to %1?']],
111  [r'I am (.*)', ['Did you come to me because you are %1?',
112  'How long have you been %1?', 'How do you feel about being %1?']],
113  [r'I\'?m (.*)', ['How does being %1 make you feel?',
114  'Do you enjoy being %1?', "Why do you tell me you're %1?",
115  "Why do you think you're %1?"]],
116  [r'Are you ([^\?]*)\??', ['Why does it matter whether I am %1?',
117  'Would you prefer it if I were not %1?',
118  'Perhaps you believe I am %1.', 'I may be %1 -- what do you think?'
119  ]],
120  [r'What (.*)', ['Why do you ask?',
121  'How would an answer to that help you?', 'What do you think?']],
122  [r'How (.*)', ['How do you suppose?',
123  'Perhaps you can answer your own question.',
124  "What is it you're really asking?"]],
125  [r'Because (.*)', ['Is that the real reason?',
126  'What other reasons come to mind?',
127  'Does that reason apply to anything else?',
128  'If %1, what else must be true?']],
129  [r'(.*) sorry (.*)',
130  ['There are many times when no apology is needed.',
131  'What feelings do you have when you apologize?']],
132  [r'(Hi)|(Hello(.*))', ["Hello... I'm glad you could drop by today.",
133  'Hi there... how are you today?',
134  'Greetings, how are you?',
135  'Hello, how are you feeling today?']],
136  [r'I think (.*)', ['Do you doubt %1?', 'Do you really think so?',
137  "But you're not sure %1?"]],
138  [r'(.*) friend (.*)', ['Tell me more about your friends.',
139  'When you think of a friend, what comes to mind?',
140  "Why don't you tell me about a childhood friend?"]],
141  [r'Yes', ['You seem quite sure.', 'OK, but can you elaborate a bit?'
142  ]],
143  [r'(.*) computer(.*)', ['Are you really talking about me?',
144  'Does it seem strange to talk to a computer?',
145  'How do computers make you feel?',
146  'Do you feel threatened by computers?']],
147  [r'Is it (.*)', ['Do you think it is %1?',
148  "Perhaps it's %1 -- what do you think?",
149  'If it were %1, what would you do?', 'It could well be that %1.'
150  ]],
151  [r'It is (.*)', ['You seem very certain.',
152  "If I told you that it probably isn't %1, what would you feel?"]],
153  [r'Can you ([^\?]*)\??', ["What makes you think I can't %1?",
154  'If I could %1, then what?', 'Why do you ask if I can %1?']],
155  [r'Can I ([^\?]*)\??', ["Perhaps you don't want to %1.",
156  'Do you want to be able to %1?', 'If you could %1, would you?']],
157  [r'You are (.*)', ['Why do you think I am %1?',
158  "Does it please you to think that I'm %1?",
159  'Perhaps you would like me to be %1.',
160  "Perhaps you're really talking about yourself?"]],
161  [r'You\'?re (.*)', ['Why do you say I am %1?',
162  'Why do you think I am %1?', 'Are we talking about you, or me?']],
163  [r'I don\'?t (.*)', ["Don't you really %1?", "Why don't you %1?",
164  'Do you want to %1?']],
165  [r'I feel (.*)', ['Good, tell me more about these feelings.',
166  'Do you often feel %1?', 'When do you usually feel %1?',
167  'When you feel %1, what do you do?']],
168  [r'I have (.*)', ["Why do you tell me that you've %1?",
169  'Have you really %1?',
170  'Now that you have %1, what will you do next?']],
171  [r'I would (.*)', ['Could you explain why you would %1?',
172  'Why would you %1?', 'Who else knows that you would %1?']],
173  [r'Is there (.*)', ['Do you think there is %1?',
174  "It's likely that there is %1.", 'Would you like there to be %1?'
175  ]],
176  [r'My (.*)', ['I see, your %1.', 'Why do you say that your %1?',
177  'When your %1, how do you feel?']],
178  [r'You (.*)', ['We should be discussing you, not me.',
179  'Why do you say that about me?', 'Why do you care whether I %1?'
180  ]],
181  [r'Why (.*)', ["Why don't you tell me the reason why %1?",
182  'Why do you think %1?']],
183  [r'I want (.*)', ['What would it mean to you if you got %1?',
184  'Why do you want %1?', 'What would you do if you got %1?',
185  'If you got %1, then what would you do?']],
186  [r'(.*) mother(.*)', ['Tell me more about your mother.',
187  'What was your relationship with your mother like?',
188  'How do you feel about your mother?',
189  'How does this relate to your feelings today?',
190  'Good family relations are important.']],
191  [r'(.*) father(.*)', ['Tell me more about your father.',
192  'How did your father make you feel?',
193  'How do you feel about your father?',
194  'Does your relationship with your father relate to your feelings today?'
195  , 'Do you have trouble showing affection with your family?']],
196  [r'(.*) child(.*)', ['Did you have close friends as a child?',
197  'What is your favorite childhood memory?',
198  'Do you remember any dreams or nightmares from childhood?',
199  'Did the other children sometimes tease you?',
200  'How do you think your childhood experiences relate to your feelings today?'
201  ]],
202  [r'(.*)\?', ['Why do you ask that?',
203  'Please consider whether you can answer your own question.',
204  'Perhaps the answer lies within yourself?',
205  "Why don't you tell me?"]],
206  [r'bye', ['Thank you for talking with me.', 'Good-bye.',
207  'Thank you, that will be $150. Have a good day!']],
208  [r'(.*)', [
209  'Please tell me more.',
210  "Let's change focus a bit... Tell me about your family.",
211  'Can you elaborate on that?',
212  'Why do you say that %1?',
213  'I see.',
214  'Very interesting.',
215  '%1.',
216  'I see. And what does that tell you?',
217  'How does that make you feel?',
218  'How do you feel when you say that?',
219  ]],
220  ]
221 
222 # Crossfire interface to Eliza.
224  whoami = Crossfire.WhoAmI()
225  message = Crossfire.WhatIsMessage()
226 
227  while message[-1] in '!.':
228  message = message[:-1]
229 
230  # Respond to player's message.
231  therapist = eliza()
232  whoami.Say(therapist.respond(message))
233 
234 # Run the Crossfire interface.
235 if Crossfire.WhatIsEvent().Subtype == Crossfire.EventType.SAY:
eliza.eliza.respond
def respond(self, str)
Definition: eliza.py:42
eliza.eliza.translate
def translate(self, str, dict)
Definition: eliza.py:29
guildoracle.list
list
Definition: guildoracle.py:87
eliza.eliza.__init__
def __init__(self)
Definition: eliza.py:21
disinfect.map
map
Definition: disinfect.py:4
eliza.eliza.values
values
Definition: eliza.py:23
eliza.crossfire_interface
def crossfire_interface()
Definition: eliza.py:223
eliza.eliza
Definition: eliza.py:20
push.match
bool match
Definition: push.py:61
make_face_from_files.int
int
Definition: make_face_from_files.py:32
eliza.eliza.keys
keys
Definition: eliza.py:22
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2606