CSC111 Eliza Class program
--D. Thiebaut 11:52, 27 October 2011 (EDT)
# 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 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
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():
canned = [ "tell me more about your family",
"is that always the case with your family" ]
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
if found != 0:
cannedFamily()
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()