#!/usr/bin/python # Elizalike --- Ling/CSE 472 Assignment I # Simple automated conversation agent, in the style of # Weizenbaum's Eliza. Invoke by typing: # python elizalike.py # from a command prompt or unix shell. # The basic script provided as part of the assignment takes care # of input and output, gives one example regular expression, and # provides a series of comments (preceded by #) indicating what # you should add. import re import sys print "Welcome to Elizalike. Talk to me! (Or type \"bye\" to quit.)\n" # Start an infinite loop while True: # Read in the user's input string = raw_input("Patient: ") # Allow user to quit if re.search(string,r'bye|Bye|BYE'): print "Elizalike: Well, it was nice talking to you!\n" sys.exit() # Insert a tag at the beginning of the line to identify # it as Eliza's reponse, and to make finding word boundaries # easier. string = re.sub('^','Elizalike: ',string) # Replace all instances of "you are" with "---Eliza-is---": # Note how (\W) and \1, etc are used to mark word boundaries # and keep whatever non-word character was in the input in the # output. string = re.sub(r'(\W)[Yy]ou are(\W)','\g<1>---Eliza-is---\g<2>',string) # A brief note about syntax: The function re.sub takes three # arguments: a regular expression to search for in the string, # a replacement string to replace it with, and the string to # operate on. It returns a new string, which the above expression # is storing in the variable 'string'. # Remaining statements for changing second person reference # in the input to third person reference (temporarily): # Other transformations on the input that don't have to do with # person reference: # Statements for changing first person reference to second # person reference: # Statements for changing third person Eliza reference to # first person reference: # Print the reply print string