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