#! /usr/bin/env python """ A simple Eliza-like bot adapted and extended from code by Jez Higgins, Jeff Epler and Joe Strout by Tim finin, finin@umbc.edu, September 2009. """ import string import re import random # if the user enters one of these, the bot program exits exit_words = ['quit', 'exit', 'bye', 'done'] # this is the default script for a bot eliza_script = [] default_responses = ['Please go on.', 'Tell me more.'] class bot: def __init__(self, name="Eliza", script=None, dres=default_responses): if not script: script = eliza_script self.name = name self.script = script_compile(script) self.default_responses = dres def translate(self, str, dict): return string.join([dict.get(w,w) for w in string.split(string.lower(str))]) def respond(self, str): """respond takes a string, a set of regexps, and a corresponding set of response lists; find a match, and return a randomly chosen response from the corresponding list. Needs to be rewriten! """ # shuffle the patterns in the script random.shuffle(self.script) for (pat, responses) in self.script: mo = pat.match(str) if mo: resp = random.choice(responses) # stuff in reflected text where indicated pos = string.find(resp,'%') while pos > -1: num = string.atoi(resp[pos+1:pos+2]) resp = resp[:pos] + \ self.translate(mo.group(num), swap_dict) + \ resp[pos+2:] pos = string.find(resp,'%') return resp # a good script should have a default response pattern, but if not return random.choice(self.default_responses) # swap_dict a translation table used to convert things you say into things # the computer says back, e.g. "I am" --> "you are" swap_dict = { "am" : "are", "was" : "were", "i" : "you", "i'd" : "you would", "i've" : "you have", "i'll" : "you will", "my" : "your", "are" : "am", "you've": "I have", "you'll": "I will", "your" : "my", "yours" : "mine", "you" : "me", "me" : "you" } def script_compile (script): """ compile each of the regular exprtessions in a script """ global SYNSETS compiled = [] for (pat, resp) in script: # replace any synonym terms for (token,expansion) in SYNSETS: pat = pat.replace(token, expansion) pat = re.compile(pat, re.IGNORECASE) compiled.append((pat, resp)) return compiled # this regular expression matches letters, numbers, whitespace keep_chars = re.compile(r"[^\w\s]") def simplify_input(s): """ Returns input string s after regularizing it, e.g., removing punctuation, mapping whitespace to single spaces, etc.""" global keep_chars # replace chars we don't like with spaces and trim spaces at end s2 = keep_chars.sub(' ', s).strip() # split and rejoin the string to regularize whitespace and return return(' '.join(s2.split())) def talk(the_bot=None): """ Have the_bot to talk to the user until she says quit """ if not the_bot: the_bot = bot() print "Hello. My name is %s. How are you feeling today?" % the_bot.name s = "" while s not in exit_words: try: s = raw_input("> ") except EOFError: s = "quit" s = simplify_input(s) print the_bot.respond(s) """ eliza_script is the default script. Each element of the list is a two-element tuple with a regular expression that gives the input pattern, and a list of strings representing possible response. Within a response string, %N will be replaced by the corresponding Nth match group from the pattern. """ eliza_script = [ (r'I (need|want) (.+)', ["Why do you need %2?", "Would it really help you to get %2?", "Are you sure you need %2?" "Do you need %2 or just want it?"]), (r'Why dont you (.+)', ["Do you really think I don't %1?", "Perhaps eventually I will %1.", "Do you really want me to %1?"]), (r'Why cant I (.+)', ["Do you think you should be able to %1?", "If you could %1, what would you do?", "I don't know -- why can't you %1?", "Have you really tried?"]), (r'I can\'?t (.+)', ["How do you know you can't %1?", "Perhaps you could %1 if you tried.", "What would it take for you to %1?"]), (r'(I am|Im) (.+)', ["Did you come to me because you are %2?", "How long have you been %2?", "How do you feel about being %2?", "How does being %1 make you feel?", "Do you enjoy being %1?", "Why do you tell me you are %2?", "Why do you think you are %2?"]), (r'Are you (.+)', ["Why does it matter whether I am %1?", "Would you prefer it if I were not %1?", "Perhaps you believe I am %1.", "I may be %1 -- what do you think?"]), (r'What (.+)', ["Why do you ask?", "How would an answer to that help you?", "What do you think?"]), (r'How (.+)', ["How do you suppose?", "Perhaps you can answer your own question.", "What is it you're really asking?"]), (r'Because(.*)', ["Is that the real reason?", "What other reasons come to mind?", "Does that reason apply to anything else?", "If %1, what else must be true?"]), (r'(.*) sorry (.*)', ["There are many times when no apology is needed.", "What feelings do you have when you apologize?"]), (r'(Hi|Hello)(.*)', ["Hello... I'm glad you could drop by today.", "Hi there... how are you today?", "Hello, how are you feeling today?"]), (r'I think (.*)', ["Do you doubt %1?", "Do you really think so?", "But you're not sure %1?"]), (r'(.*) friend (.*)', ["Tell me more about your friends.", "When you think of a friend, what comes to mind?", "Why don't you tell me about a childhood friend?"]), (r'Yes', ["You seem quite sure.", "OK, but can you elaborate a bit?"]), (r'(.*) computer(.*)', ["Are you really talking about me?", "Does it seem strange to talk to a computer?", "How do computers make you feel?", "Do you feel threatened by computers?"]), (r'Is it (.*)', ["Do you think it is %1?", "Perhaps it's %1 -- what do you think?", "If it were %1, what would you do?", "It could well be that %1."]), (r'It is (.*)', ["You seem very certain.", "If I told you that it probably isn't %1, what would you feel?"]), (r'Can you(.+)', ["What makes you think I can't %1?", "If I could %1, then what?", "Why do you ask if I can %1?"]), (r'Can I (.+)', ["Perhaps you don't want to %1.", "Do you want to be able to %1?", "If you could %1, would you?"]), (r'You are (.*)', ["Why do you think I am %1?", "Does it please you to think that I'm %1?", "Perhaps you would like me to be %1.", "Perhaps you're really talking about yourself?"]), (r'You\'?re (.*)', ["Why do you say I am %1?", "Why do you think I am %1?", "Are we talking about you, or me?"]), (r'I don\'?t (.*)', ["Don't you really %1?", "Why don't you %1?", "Do you want to %1?"]), (r'I feel (.*)', ["Good, tell me more about these feelings.", "Do you often feel %1?", "When do you usually feel %1?", "When you feel %1, what do you do?"]), (r'I have (.*)', ["Why do you tell me that you've %1?", "Have you really %1?", "Now that you have %1, what will you do next?"]), (r'I would (.*)', ["Could you explain why you would %1?", "Why would you %1?", "Who else knows that you would %1?"]), (r'Is there (.*)', ["Do you think there is %1?", "It's likely that there is %1.", "Would you like there to be %1?"]), (r'My (.*)', ["I see, your %1.", "Why do you say that your %1?", "When your %1, how do you feel?"]), (r'You (.*)', ["We should be discussing you, not me.", "Why do you say that about me?", "Why do you care whether I %1?"]), (r'Why (.*)', ["Why don't you tell me the reason why %1?", "Why do you think %1?" ]), (r'I want (.*)', ["What would it mean to you if you got %1?", "Why do you want %1?", "What would you do if you got %1?", "If you got %1, then what would you do?"]), (r'(.*) mother(.*)', ["Tell me more about your mother.", "What was your relationship with your mother like?", "How do you feel about your mother?", "How does this relate to your feelings today?", "Good family relations are important."]), (r'(.*) father(.*)', ["Tell me more about your father.", "How did your father make you feel?", "How do you feel about your father?", "Does your relationship with your father relate to your feelings today?", "Do you have trouble showing affection with your family?"]), (r'(.*) child(.*)', ["Did you have close friends as a child?", "What is your favorite childhood memory?", "Do you remember any dreams or nightmares from childhood?", "Did the other children sometimes tease you?", "How do you think your childhood experiences relate to your feelings today?"]), (r'.*$profanity.*', ["Please, let's not use words like %1", "Let's be civil", "Do you often talk like that?", "You seem upset" ]), (r'(I ($like|$hate)(.+))', ["Have you aways felt this way about %2?", "Tell me more aobut your feelings", "Do you think a lot about %1?"]), (r'I .* $hurt (.*)', ["Why do you feel hostile toward %1", "Do you often fantisize about hurting people?"]), (r'quit', ["Thank you for talking with me.", "Good-bye.", "Thank you, that will be $175. Have a good day!"]), (r'.* name .*', ["Do you think names are important?", "We are not concerned with names here.", "Nomen est omen"]), (r'(.*)', ["Please tell me more.", "Let's change focus a bit... Tell me about your family.", "Can you elaborate on that?", "Why do you say that %1?", "I see.", "Very interesting.", "%1.", "I see. And what does that tell you?", "How does that make you feel?", "How do you feel when you say that?"]) ] # SYNSETS is a list of synonum sets, where each is a tuple like ('$kill', # (kill|murder|shoot|stab|strangle)). The first element should begine with $ # and is a token to use in a script pattern. The second element is a regular # expression that will replace the token. Thus, the pattern "I want .* $kill # (.*)" will be compiled as ""I want .* (kill|murder|shoot|stab|strangle) (.*)" SYNSETS = [ ('$profanity', '(hell|damn|shit|crap|fuck)'), ('$like', '(like|likes|liked|love|loves|loved)'), ('$hate', "(hate|hates|dislike|dislikes|despise|don't like|not like)"), ('$hurt', '(kill|murder|shoot|stab|strangle)') ] # if called from the cammand line, start talking if __name__ == "__main__": talk()