Difference between revisions of "CSC111 Eliza 1"
Line 21: | Line 21: | ||
# otherwise, just a random canned sentence | # otherwise, just a random canned sentence | ||
# | # | ||
− | + | ||
import random | import random | ||
from logging111 import Print | from logging111 import Print | ||
Line 67: | Line 67: | ||
# --------------------------------------------- | # --------------------------------------------- | ||
− | words = | + | words = user.split( ) # get individual words |
newWords = words[:] # create new list with the same words | newWords = words[:] # create new list with the same words | ||
Line 92: | Line 92: | ||
# it's a reflection. Add a question mark and print | # it's a reflection. Add a question mark and print | ||
newWords[-1] = newWords[-1]+"?" | newWords[-1] = newWords[-1]+"?" | ||
− | Print( | + | Print( ' '.join( newWords ) ) |
else: | else: |
Revision as of 19:14, 10 March 2010
--D. Thiebaut 00:09, 11 March 2010 (UTC)
# 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()