# elizabot.py a cheezy Eliza knock-off by Joe Strout <joe@strout.net>,
# updated by Jeff Epler <jepler@inetnebr.com> and landonb@douglas.bc.ca
import string

# translate a string by replacing any words found in dict.keys()
def translate(str,dict):
	words = string.split(string.lower(str))
	keys = dict.keys();
	for i in range(0,len(words)):
		if words[i] in keys:
			words[i] = dict[words[i]]
	return string.join(words)


# dict converts what you say into repsonses e.g. "I am" --> "you are"
reflections = {
	"am" 	: "are",
	"was"	: "were",
	"i"	: "you",
	"i'd"	: "you would",
	"i've"	: "you have",
	"i'll"	: "you will",
	"my"	: "your",
	"are"	: "am",
	"you've": "I have",
	"you'll": "I will",
	"your"	: "my",
	"yours"	: "mine",
	"you"	: "me",
	"me"	: "you" }


def need(s): # for handling things you say starting with "I need"
	r = s[7:]
	if r[-1] == ".": r = r[:-1]
	r = translate(r, reflections)
	try: return ineed.popitem()[1] + r + '?'
	except: return saymore(s)


# dict of I need --> responses possible
ineed = {
	"1" : "Why do you need ",
	"2" : "Would it really help you to get ",
	"3" : "Are you sure you need "}


def saymore(s): # for encouraging more conversation - backchannel
	if s != "quit" :
		try: r = nomatch.popitem()[1]
		except: r = "That is all I can say today, Good-bye." 
		return r


# dict of responses when there is no match - just prompting more conversation	
nomatch = {
	"1" : "Please tell me more.",
	"2" : "Let's change focus a bit... Tell me about your family.",
	"3" : "Can you elaborate on that?",
	"4" : "I see.",
	"5" : "Very interesting.",
	"6" : "I see.  And what does that tell you?",
	"7" : "How does that make you feel?",
	"8" : "How do you feel when you say that?"}


# Main program for elizabot as updated 12/14/02 by landonb@douglas.bc.ca
print 'Talk to Elizabot by typing in plain English.  Enter "quit" when done.'
print "Hello.  What do you need to feel good today?"
s = ""
while s != "quit":
	try: s = raw_input(":> ")
	except EOFError:
		s = "quit"
		print s
	if s != "": # avoids crashing on blank input line
		if s.lower()[:7] == "i need " : r = need(s)
		else: r = saymore(s)
		# change to introduce yourself
		if s.lower() == "who is bruce landon?" : 
		    r = "He is the Cognitive Psychology course instructor."
	if r != None : print r #response to the typed input string


