Difference between revisions of "CSC111 Eliza 1"
Line 24: | Line 24: | ||
# | # | ||
− | import | + | from random import choice |
# canned answers that the program will use to get the | # canned answers that the program will use to get the | ||
Line 53: | Line 53: | ||
def isGoodBye( reply ): | def isGoodBye( reply ): | ||
− | if reply.lower().strip() in ["bye", " | + | if reply.lower().strip() in ["bye", |
+ | "ciao", | ||
+ | "au revoir", | ||
+ | "i'm done" ]: | ||
return True | return True | ||
return False | return False | ||
Line 62: | Line 65: | ||
# -------------------------------------------------------------------------- | # -------------------------------------------------------------------------- | ||
def doReflection( words, newWords ): | def doReflection( words, newWords ): | ||
− | + | for i in range( len( words ) ): | |
# replace 1st by 2nd person | # replace 1st by 2nd person | ||
if words[i] == "I" : newWords[i] = "you" | if words[i] == "I" : newWords[i] = "you" | ||
Line 101: | Line 104: | ||
Greetings() | Greetings() | ||
− | # 2) | + | # 2) conversation: get user input, and respond |
− | + | while True: | |
# --------------------------------------------- | # --------------------------------------------- | ||
# 2.1) get user's statement about life | # 2.1) get user's statement about life | ||
Line 124: | Line 127: | ||
if isAReflection: | if isAReflection: | ||
# it's a reflection. Add a question mark and print | # it's a reflection. Add a question mark and print | ||
− | + | print( ' '.join( newWords ), '?', sep="" ) | |
− | print( ' '.join( newWords ) ) | ||
else: | else: | ||
# figure out if it's a regular canned answer or a negative canned answer | # figure out if it's a regular canned answer or a negative canned answer | ||
if isNegativeStatement( words ): | if isNegativeStatement( words ): | ||
− | + | print( choice( cannedNegative ) ) | |
− | |||
else: | else: | ||
− | + | print( choice( canned ) ) | |
− | |||
main() | main() | ||
− | |||
</source> | </source> | ||
<br /> | <br /> |
Revision as of 13:08, 26 March 2014
--D. Thiebaut 00:09, 11 March 2010 (UTC)
Newer Version (with functions)
# Eliza1.py
# D. Thiebaut
#
# 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
#
from random import choice
# 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?" ]
# --------------------------------------------------------------------------
# Greetings(): greets the user and explains how to stop program
# --------------------------------------------------------------------------
def Greetings():
print( "Welcome. Please tell me of your problems: " )
print( '(You may quit at any time by answering "bye")' )
def isGoodBye( reply ):
if reply.lower().strip() in ["bye",
"ciao",
"au revoir",
"i'm done" ]:
return True
return False
# --------------------------------------------------------------------------
# doReflection( words, newWords ). replaces I by you and you by I, including
# pronouns such as mine and yours. The reflected words are stored in newWords
# --------------------------------------------------------------------------
def doReflection( words, newWords ):
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"
# if we changed something, return True, otherwise return False
return words != newWords
# --------------------------------------------------------------------------
# isNegativeStatement( words )
# return True if one of the words is a negative word, such as no, not, never.
# --------------------------------------------------------------------------
def isNegativeStatement( words ):
if len( words ) < 1:
return False
for word in words:
if word.lower() in [ "no", "not", "never", "nein", "jamais" ]:
return True
return False
# --------------------------------------------------------------------------
# main function
# --------------------------------------------------------------------------
def main():
# 1) greet user
Greetings()
# 2) conversation: get user input, and respond
while True:
# ---------------------------------------------
# 2.1) get user's statement about life
# ---------------------------------------------
user = input( "\n> " )
if isGoodBye( user ):
break
# ---------------------------------------------
# 2.2) do a reflection
# ---------------------------------------------
words = user.split( ) # get individual words
newWords = words[:] # create new list with the same words
isAReflection = doReflection( words, newWords )
# ---------------------------------------------
# 2.3) figure out what to print
# ---------------------------------------------
if isAReflection:
# it's a reflection. Add a question mark and print
print( ' '.join( newWords ), '?', sep="" )
else:
# figure out if it's a regular canned answer or a negative canned answer
if isNegativeStatement( words ):
print( choice( cannedNegative ) )
else:
print( choice( canned ) )
main()
Old Version (no functions)
# Eliza1.py
# D. Thiebaut
#
# 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 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 = user.split( ) # 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( ' '.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()