CSC111 Eliza Class program

From dftwiki3
Revision as of 11:17, 27 October 2011 by Thiebaut (talk | contribs)
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",
               "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
         words = answer.split()
         newWords = []
         for word in words:
             newWords.append( word )

         for i in range( len( words ) ):
             if words[i] == "I":
                 newWords[i] = "you"
             if words[i] == "you":
                 newWords[i] = "me"
         if words != newWords:
             print( ' '.join( newWords ) + '?')
             continue
            
         # respond semi-intelligently
         # cycle through the list of canned sentences...
         cannedAnswer()

     print( "it was nice chatting with you" )
     
main()