Difference between revisions of "CSC111 Eliza Class program"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- <source lang="python"> # eliza.py # # a very simple beginning for an eliza-like program # # 1) greet user # 2) repeat a huge number of times: # 2.1) get user's st...")
 
Line 4: Line 4:
 
# eliza.py
 
# eliza.py
 
#
 
#
# a very simple beginning for an eliza-like program
+
# This program was developed in class. It is poorly documented,
#
+
# and is provided here only to help with the note-taking.
# 1) greet user
+
 
# 2) repeat a huge number of times:
 
#  2.1)  get user's statement about life
 
#   2.2)  respond in a way that seems intelligent
 
#
 
# Addition:
 
#  cycles through a series of canned answers to get
 
#  the user to think the computer is listening
 
 
import random
 
import random
  
Line 34: Line 27:
 
     print( random.choice( canned ) )
 
     print( random.choice( canned ) )
  
def cannedFamily():
+
def cannedFamily( member ):
     canned = [ "tell me more about your family",
+
    """display a random statement about the user's family,
               "is that always the case with your 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 ) )
 
     print( random.choice( canned ) )
 
            
 
            
Line 65: Line 63:
 
             if answer.find( member ) != -1:
 
             if answer.find( member ) != -1:
 
                 found = 1
 
                 found = 1
 +
                break
 
         if found != 0:
 
         if found != 0:
             cannedFamily()
+
             cannedFamily( member )
 
             continue
 
             continue
  

Revision as of 11:17, 27 October 2011

--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()