Difference between revisions of "CSC111 Homework 7 Solution"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- <source lang="python"> # Eliza1.py # D. Thiebaut # edited by Kristina Fedorenko # 111c - aq # # a very simple eliza-like program # # 1) greet user # react if us…')
 
Line 31: Line 31:
 
from logging111 import Print
 
from logging111 import Print
 
from logging111 import Raw_input
 
from logging111 import Raw_input
 +
 +
#Note: if  you do not have a logging111.py module, simply replace
 +
# Print by print, and Raw_input by raw_input is the program, and it
 +
# should work fine.
 +
#
  
 
# --------------------------------------------------------------------------   
 
# --------------------------------------------------------------------------   

Revision as of 09:11, 30 March 2010

--D. Thiebaut 14:09, 30 March 2010 (UTC)


# Eliza1.py
# D. Thiebaut
# edited by Kristina Fedorenko
# 111c - aq
#
# a very simple eliza-like program
# 
# 1) greet user
#    react if user doesn't greet Eliza
# 2) repeat a huge number of times:
#   2.1)  get user's statement about life
#   2.2)  do a reflexion first to second person
#         by replacing I by you, me by you,
#         mine by yours, my by your, etc...
#         and also 2nd to first person
#   2.3)  figure out what to print
#         if the user's sentence is in the form 'I xxxx you', ask why
#         if there are key words, print a random family related sentence
#         if the sentence was reflected, then print it
#         if the sentence contained no or never, then
#         react to it
#         otherwise, just a random canned sentence
#
 
import random
from logging111 import Print
from logging111 import Raw_input

#Note: if  you do not have a logging111.py module, simply replace
# Print by print, and Raw_input by raw_input is the program, and it
# should work fine.
#

# --------------------------------------------------------------------------   
# Lists                                                                    
# --------------------------------------------------------------------------

 
# canned answers that the program will use to get the
# user to keep the conversation going...
canned = [ "\nPlease tell me more.",
           "\nI see...",
           "\nGo on...",
           "\nTell me more.",
           "\nWhy do you say that?",
           "\nIn what way?",
           "\nI am listening...",
           "\nDo you want to talk about this?",
           "\nDoes it bother you?",
           "\nGive me more details.",
           "\nDoes it mean much to you?",
           "\nInteresting... Please continue.",
           "\nHow does it make you feel?",
           "\nLet's discuss it.",
           "\nI understand.",
           "\nWhy don't we discuss it in greater detail?",
           "\nI would like to hear more about it.",
           "\nWhy do you think it is so important for you?",
           "\nFunny that you would mention it.",
           "\nI know...",
           "\nWhy?",
           "\nHow often do you feel this way?",
           "\nYou are a very interesting person.",
           "\nHow?",
           "\nAre you upset?",
           "\nDo you think a lot about it?",
           "\nDoes it happen to you a lot?",
           "\nHow do you know?",
           "\nWhy do you think so?",
           "\nDo you want to change it?",
           "\nI think you need to think more about it.",
           "\nI can try to help you with that.",
           "\nWe can work it out.",
           "\nDo you know anybody with the same problem?",
           "\nYou will feel better, if you just tell me everything.",
           "\nWhat else bothers you?",
           "\nWhy don't you tell me the whole story?",
           "\nDoes it make you feel anxious?",
           "\nCan you please elaborate on that?" ]
 
# canned answers that the program will use when the
# user is responding in the negative ("no", or "never")
cannedNegative = [ "\nMy, my, so negative!",
                   "\nNo?",
                   "\nIsn't that a bit strong?",
                   "\nReally?",
                   "\nYou really think so?",
                   "\nWhy not?",
                   "\nAre you in denial?",
                   "\nYou are not completely open with me, are you?",
                   "\nInteresting.. You seem very emotionally involved now.",
                   "\nAre you sure?",
                   "\nYou say 'no' a lot...",
                   "\nWhy are you denying everything?",
                   "\nNo need to be so negative.",
                   "\nNever?",
                   "\nI can't help you, if you don't tell me everything."]

# canned answers that are used when the user mentions family members
cannedFamily = [ "\nWhat about other members of the family?",
                 "\nWhat do other members of the family think?",
                 "\nDo you usually get along with your family?",
                 "\nIs your family generally supportive?",
                 "\nAnd what about the rest of your family?",
                 "\nDid you talk about it with the rest of your family?",
                 "\nWhat is your family like?",
                 "\nWere you happy as a child?",
                 "\nDoes it bother anyone else in your famiy?",
                 "\nWho else is there in your family?" ]

# list of greetings
greetings = ["hi", "hello", "salut", "greetings", "morning", "evening",
             "afternoon", "hey", "day", "how", "aloha"]

# list of goodbyes
goodbyes = ["bye", "goodbye", "good-bye", "ciao", "bye-bye"]

# list of family keywords
family =    ["mother", "father", "sister", "brother", "parents", 
             "grandmother", "grandfather", "grandparents", 
             "children", "daughter", "son", "siblings", 
             "cousin", "mom", "dad"]

# list of phrases program can use to greet the user
ProgGreet = ["Hello! How are you?", "Hi! How are you doing?", 
             "Good afternoon. Let's talk about your problems.",
             "Hello! I'm so glad you came!", "How do yo do?",
             "Hi! How is it going?", "Hello! Please tell me what bothers you.",
             "Morning! Please, don't be shy. Let's talk about you."]

# list of phrases program uses to say goodbye to the user
ProgBye  =  ["Goodbye! It was nice talking to you.", 
             "Bye! Hope to see you soon again!",
             "Goodbye! Please, don't hesitate to come again.",
             "Bye! On no, don't thank me. It was my pleasure helping you!",
             "Have a good day!",
             "See you!"]

# list of phrases program uses to scold the user
ProgScold = ["No greeting? How rude!", 
             "Your lack of politeness indicates a serious psycological"\
             "problem",
             "I wish you would at least say 'hi'...",
             "Did nobody tell you it is rude not to greet people??",
             "Please, be so kind - say 'hello' next time.",
             "Hmm.. You are not very social, are you?"]
 
# --------------------------------------------------------------------------
# functions
# -------------------------------------------------------------------------- 
                
# returns a canned phrase at random from a given list
def returnCan (List):
     index = random.randrange(len(List))
     Print(List[index])

# formats user's input into a list of words striped of 
# punctuation marks
def formatInput():
     sentence = Raw_input( "\n>" )
     sentence = sentence.split()
     for i in range(len(sentence)):
         sentence[i] = sentence[i].strip(".!,?):~\/ ")
     return sentence

# check split-up user's sentence if it has a word
# from a given list; returns a boolean variable
def checkSentence(sentence, List):
     isThere = False
     for i in range(len(sentence)):
          for word in List:
              if sentence[i].lower() == word : isThere = True
     return isThere

def Greet():
     Print( "Welcome. Please tell me of your problems." )
     Print( "(Don't forget to say goodbye when you"\
            "want to end the conversation.)" )
     # check if the user greeted Eliza;                                        
     # if yes - respond, if not - scold                                       
     user_greet = formatInput()
     isGreeting = checkSentence(user_greet, greetings)
     if isGreeting : returnCan(ProgGreet)
     else : returnCan(ProgScold)


# --------------------------------------------------------------------------   
# main function                                                                
# -------------------------------------------------------------------------- 
def main():
 
     # 1) greet user
     Greet()

     # 2) repeat a huge number of times
     for i in range( 10000 ):
         # ---------------------------------------------
         # 2.1) get user's statement about life
         # ---------------------------------------------
         words = formatInput() # contains list of words from user's input 
         if len(words) == 0:   # prevents crashing if the user enters nothing 
          returnCan(canned)
         else : 
          if checkSentence(words,goodbyes): # breaks the loop if user says
              returnCan(ProgBye)            # goodbye
              Print(" ")
              break
 
          # ---------------------------------------------
          # 2.2) do a reflection
          # ---------------------------------------------
        
          newWords = words[:] # create new list with the same words
          
          for i in range( len( words ) ):           
              # replace 1st by 2nd person
              if words[i].lower() == "i"   : newWords[i] = "you"
              if words[i].lower() == "i'm" : newWords[i] = "you're"
              if words[i].lower() == "i've": newWords[i] = "you've"
              if words[i].lower() == "am"  : newWords[i] = "are"
              if words[i].lower() == "was" : newWords[i] = "were"
              if words[i].lower() == "my"  : newWords[i] = "your"
              if words[i].lower() == "mine": newWords[i] = "yours"
              if words[i].lower() == "me"  : newWords[i] = "you"
 
              # replace 2nd by 1st person
              if words[i].lower() == "you"   : newWords[i] = "me"
              if words[i].lower() == "your"  : newWords[i] = "my"
              if words[i].lower() == "yours" : newWords[i]= "mine"
         
          # replace 'you' by 'I' if it is the first word
          # and if followed by 'are', replace 'are' by 'am'
          if words[0].lower() == "you" : 
             newWords[0]= "I"    
             if len(words)>1 and words[1].lower()== "are": 
                  newWords[1] = "am"
 
          # ---------------------------------------------
          # 2.3) figure out what to print
          # ---------------------------------------------
                  
          # checks if the user's input is in a form 'I xxxx you'        
          # if yes, asks 'why do you xxxx me?' and appends to the             
          # canned phrases 'Is this why you xxxx me?'                           
          if len(words) == 3\
          and words[0].lower() == "i"\
          and words[2].lower() == "you":
              verb = words[1]
              Print ("why do you", verb, "me?")
              canned.append(" ".join(["Is this why you", verb, "me?"]))

          # check for the family keywords
          elif checkSentence(words, family): 
               returnCan(cannedFamily)

          elif ( words != newWords ):
               # it's a reflection.  Add a question mark and print
               newWords[-1] = newWords[-1]+"?"
               Print( ' '.join( newWords ) )
 
          else:
               # figure out if it's a regular canned answer 
               # or a negative canned answer
               if len( words )==1 and ( words[0].lower()=="no" \
               or words[0].lower()=="never" ):
                  returnCan(cannedNegative)
               else:
                  returnCan(canned)
 
main()