CSC111 Eliza Class program

From dftwiki3
Jump to: navigation, search

--D. Thiebaut 11:52, 27 October 2011 (EDT)


# eliza.py
#
# This program was developed in class. It is poorly documented,
# and is provided here only to help with the note-taking.

import random



def cannedAnswer():
    """display a random canned answer.
    """
    canned = [ "\nPlease tell me more",
           "\nI see...",
           "\nI am listening..." ]
    print( random.choice( canned ) )

def cannedNegative():
    """display statement picking up on negativity
    """
    canned = [ "My, my, so negative today!",
               "Really?", "You should be more positive",
               "Maybe you should take a break!" ]
    print( random.choice( canned ) )

def cannedFamily( member ):
    """display a random statement about the user's family,
        or the particular member the user listed in her answer"""

    canned = [ "How do you feel about your " + member,
               "is that always the case in your family",
               "tell me more about your "+member ]

    print( random.choice( canned ) )
           
def main():
 
     # greet user
     print(  "Welcome.  Please tell me of your problems: " )
     print( '(You may quit at any time by answering "bye")' )
 
     # repeat a huge number of times
     for i in range( 100000 ):
 
         # get user's statement about her life
         answer = input( "\n> " )
         if ( answer.lower() == "bye" ):
             break

         # negative answer
         neg = [ "no", "never" ]
         if answer.lower() in neg:
             cannedNegative()
             continue

         # family member
         family = ["mother", "father", "sister", "brother", "dog" ]
         found = 0
         for member in family:
             if answer.find( member ) != -1:
                 found = 1
                 break
         if found != 0:
             cannedFamily( member )
             continue

         # reflection
         # split the answer into a list of words
         words = answer.split()

         # create a new list with the same words
         newWords = []
         for word in words:
             newWords.append( word )

         # go through all the words in the original list
         # and reflect them in the new list.
         for i in range( len( words ) ):
             if words[i] == "I":
                 newWords[i] = "you"
             if words[i] == "you":
                 newWords[i] = "me"

         # if the two lists of words are different, we must have
         # reflected some words...
         if words != newWords:
             print( ' ' . join( newWords ) + '?')
             continue
            
         # respond semi-intelligently if we couldn't detect anything 
         # relating to negativity, family, or reflection
         cannedAnswer()

     # we get here when the user quits.
     print( "it was nice chatting with you" )
     
main()