User:Andre Castro/2.1/bots

From XPUB & Lens-Based wiki

Sean Talking Bot

I have been working o an adaption of Eliza (from python's NLTK package) to make a bot that responds to spam emails.

There are several changes/addition like to make:

  • crafting more carefully the patterns that the bot (Sean) responds to and answers give TODO
  • changing the pronouns of the input to the proper pronoun in the output: I <-> you; mine <-> yours, etc
eg:
IN: I need your help.
OUT: Would it really help you to get my help?
  • include salutations and farewells TODO


Sean's script

<sounce lang="python">

import re, random, sys from vocabulary_Sean import pairs


def change_subject(found):

   """
   changes the text subject when answering  you --> me; my ---> your; etc
   """    
   you_ptrn=re.compile(r"\byou\b|\byour\b|\byours\b|\bi\b|\bme\b|\bmy\b|\bmine\b|\myself\b|\byourself\b", re.IGNORECASE) #\W is to prevent a word such as Industry or idiot to be matched
   subjects =  you_ptrn.findall(found)
   if len(subjects) > 0:
       comp_subjects = []
       persons = { "I":"you", "me":"you", "you": "me", "your":"my", "my":"your", "yours":"mine", "mine":"yours", "yourself":"myself", "myself":"yourself" }
       print subjects
   # matching items to its complementary in the answer
       for subject in subjects:
           if (subject in persons.keys()) == 1: #make sure it is it dict persons
               comp_subjects.append( persons[subject])
           
       subject_pair =  zip( subjects, comp_subjects) #a list of tuple pairs: (old subject, new subject)
       for subject in subject_pair:
           print subject     
           found = re.sub(subject[0], subject[1], found)
   return found    


txt_input = sys.argv[1] found_l = [] found_s = [] possible_answers=[] found = []

found_pairs=[]

  1. Feed to Bot

for i, pair in enumerate(pairs): #loop through the vocabulary (pairs)

   pattern = re.compile(pair[0]) #compile pattern from pairs
   found =  (pattern.findall(txt_input)) #found any match??
   if len(found) > 0: #if pattern is matched
       found_pairs.append([i, found[0]])
       found_l.append(i) #append index of the matched pair


if len(found_pairs) > 0: # pattern was matched

   if len(found_pairs) > 1 : # pattern was matched : choose one
       found_pairs = random.choice(found_pairs)
   elif len(found_pairs) == 1:
       found_pairs = found_pairs[0]


   possible_answers =  pairs[ found_pairs[0] ][1]
   print possible_answers
   rnd_str = random.choice(possible_answers)
   if '%1' in rnd_str: #has the answer terms to replace or not??
       chg_sentence = change_subject( found_pairs[1] ) # SWAP THE SENTENCE SUBJECTS 
       
       output_txt = rnd_str.replace('%1', chg_sentence)  #and replace variable
   else:
       output_txt =  rnd_str
   print "BOT OUTPUT: " +  output_txt

</source>

Seans vocabulary=

<sounce lang="python">

  1. save it in the same dir as the previous script under the name of vocabulary_Sean.py
  2. you

pairs = (

 (r'I need (.*)',
 ( "Why do you need %1?",
   "Would it really help you to get %1?",
   "Are you sure you need %1?")),
 (r'Why don\'t you (.*)',
 ( "Do you really think I don't %1?",
   "Perhaps eventually I will %1.",
   "Do you really want me to %1?")),
 (r'Why can\'t 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 (.*)',
 ( "Are you approaching me because you are %1?",
   "How long have you been %1?",
   "How do you feel about being %1?",
   "What made you %1 ?")),
 (r'I\'m (.*)',
 ( "How does being %1 make you feel?",
   "Do you enjoy being %1?",
   "Why do you tell me you're %1?",
   "Why do you think you're %1?")),
 (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'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'(.*)\?',
 ( "Why do you ask that?",
   "Please consider whether you can answer your own question.",
   "Perhaps the answer lies within yourself?",
   "Why don't you tell me?"))

)

</source>

How is it working?

the vocabulary file contains tuple pairs: (regex, possible replies)

eg:  (r'Why (.*)', #regex will select from 'Why are you happy?' the string 'are you happy?'
( "Why don't you tell me the reason why %1?", "Why do you think %1?" )),
  • a loop cycles through those tuples trying to find the which regex yield matchs; and adds them to found_pairs list
  • from the results 1 is selected
  • from that 1 result 1 possible answer is chosen
  • if it allows substitution ( %1 ):
    • the gramatical person of the input is changed (in the change_subject function)
    • the found match (found_pairs[1]) replaces 1%
  • we get sean's reply