CSC111 Eliza 1

From dftwiki3
Revision as of 19:09, 10 March 2010 by Thiebaut (talk | contribs) (Created page with '--~~~~ ---- <source lang="python"> # Solution program for lab8 # D. T. # a very simple beginning for an eliza-like program # # 1) greet user # 2) repeat a huge number of times:…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut 00:09, 11 March 2010 (UTC)


# Solution program for lab8
# D. T.
# a very simple beginning for an eliza-like program
# 
# 1) greet user
# 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 sentence was reflected, then print it
#         if the sentence contained no or never, then
#         react to it
#         otherwise, just a random canned sentence
#
import string
import random
from logging111 import Print
from logging111 import Raw_input

# 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..." ]

# 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?" ]
                   
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------                   
def main():

     # 1) greet user
     Print( "Welcome.  Please tell me of your problems: " )
     Print( '(You may quit at any time by answering "bye")' )
     
     # 2) repeat a huge number of times
     for i in range( 10000 ):
         # ---------------------------------------------
         # 2.1) get user's statement about life
         # ---------------------------------------------
         user = Raw_input( "\n> " )
         if ( user.lower() == "bye" ):
             break
            
         # ---------------------------------------------
         # 2.2) do a reflection
         # ---------------------------------------------
         
         words = string.split( user ) # get individual words
         newWords = words[:]          # create new list with the same words
         
         for i in range( len( words ) ):           
             # replace 1st by 2nd person
             if words[i] == "I"   : newWords[i] = "you"
             if words[i] == "I'm" : newWords[i] = "you're"
             if words[i] == "I've": newWords[i] = "you've"
             if words[i] == "am"  : newWords[i] = "are"
             if words[i] == "was" : newWords[i] = "were"
             if words[i] == "my"  : newWords[i] = "your"
             if words[i] == "mine": newWords[i] = "yours"
             if words[i] == "me"  : newWords[i] = "you"

             # replace 2nd by 1st person
             if words[i] == "you"   : newWords[i] = "me"
             if words[i] == "your"  : newWords[i] = "my"
             if words[i] == "yours" : newWords[i]= "mine"
         
         # ---------------------------------------------
         # 2.3) figure out what to print
         # ---------------------------------------------
         if ( words != newWords ):
             # it's a reflection.  Add a question mark and print
             newWords[-1] = newWords[-1]+"?"
             Print( string.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" ):
                 index = random.randrange( len( cannedNegative ) )
                 Print( cannedNegative[ index ] )
             else:
                 index = random.randrange( len( canned ) )
                 Print( canned[ index ] )
         
main()