this is the therapist code-

import string
import regex
import whrandom

#----------------------------------------------------------------------
# translate: take a string, replace any words found in dict.keys()
#	with the corresponding dict.values()
#----------------------------------------------------------------------
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)

#----------------------------------------------------------------------
#	respond: take a string, a set of regexps, and a corresponding
#		set of response lists; find a match, and return a randomly
#		chosen response from the corresponding list.
#----------------------------------------------------------------------
def respond(str,keys,values):
	# find a match among keys
	for i in range(0,len(keys)):
		if keys[i].match(str) >= 0:
			# found a match ... stuff with corresponding value
			# chosen randomly from among the available options
			respnum = whrandom.randint(0,len(values[i])-1)
			resp = values[i][respnum]
			# we've got a response... stuff in reflected text where indicated
			pos = string.find(resp,'%')
			while pos > -1:
				num = string.atoi(resp[pos+1:pos+2])
				resp = resp[:pos] + \
					translate(keys[i].group(num),gReflections) + \
					resp[pos+2:]
				pos = string.find(resp,'%')
			# fix munged punctuation at the end
			if resp[-2:] == '?.': resp = resp[:-2] + '.'
			if resp[-2:] == '??': resp = resp[:-2] + '?'
			return resp

#----------------------------------------------------------------------
# gReflections, a translation table used to convert things you say
#		into things the computer says back, e.g. "I am" --> "you are"
#----------------------------------------------------------------------
gReflections = {
	"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" }

#----------------------------------------------------------------------
# gPats, the main response table.  Each element of the list is a
#	two-element list; the first is a regexp, and the second is a
#	list of possible responses, with group-macros labelled as
#	%1, %2, etc.	
#----------------------------------------------------------------------
gPats = [
	["I need \(.*\)",
	[	"Why do you need %1?",
		"Would it really help you to get %1?",
		"Are you sure you need %1?"]],
	
	["Why don't you \(.*\)",
	[	"Do you really think I don't %1?",
		"Perhaps eventually I will %1.",
		"Do you really want me to %1?"]],
	
	["Why can't I \(.*\)",
	[	"Do you think you should be able to %1?",
		"If you could %1, what would you do?",
		"I don't know -- why can't you %1?",
		"Have you really tried?"]],
	
	["I can't \(.*\)",
	[	"How do you know you can't %1?",
		"Perhaps you could %1 if you tried.",
		"What would it take for you to %1?"]],
	
	["I am \(.*\)",
	[	"Did you come to me because you are %1?",
		"How long have you been %1?",
		"How do you feel about being %1?"]],
	
	["I'm \(.*\)",
	[	"How does being %1 make you feel?",
		"Do you enjoy being %1?",
		"Why do you tell me you're %1?",
		"Why do you think you're %1?"]],
	
	["Are you \(.*\)",
	[	"Why does it matter whether I am %1?",
		"Would you prefer it if I were not %1?",
		"Perhaps you believe I am %1.",
		"I may be %1 -- what do you think?"]],
	
	["What \(.*\)",
	[	"Why do you ask?",
		"How would an answer to that help you?",
		"What do you think?"]],
	
	["How \(.*\)",
	[	"How do you suppose?",
		"Perhaps you can answer your own question.",
		"What is it you're really asking?"]],
	
	["Because \(.*\)",
	[	"Is that the real reason?",
		"What other reasons come to mind?",
		"Does that reason apply to anything else?",
		"If %1, what else must be true?"]],
	
	["\(.*\) sorry \(.*\)",
	[	"There are many times when no apology is needed.",
		"What feelings do you have when you apologize?"]],
	
	["Hello\(.*\)",
	[	"Hello... I'm glad you could drop by today.",
		"Hi there... how are you today?",
		"Hello, how are you feeling today?"]],
	
	["I think \(.*\)",
	[	"Do you doubt %1?",
		"Do you really think so?",
		"But you're not sure %1?"]],
	
	["\(.*\) friend\(.*\)",
	[	"Tell me more about your friends.",
		"When you think of a friend, what comes to mind?",
		"Why don't you tell me about a childhood friend?"]],
	
	["Yes",
	[	"You seem quite sure.",
		"OK, but can you elaborate a bit?"]],
	
	["\(.*\) computer\(.*\)",
	[	"Are you really talking about me?",
		"Does it seem strange to talk to a computer?",
		"How do computers make you feel?",
		"Do you feel threatened by computers?"]],
	
	["Is it \(.*\)",
	[	"Do you think it is %1?",
		"Perhaps it's %1 -- what do you think?",
		"If it were %1, what would you do?",
		"It could well be that %1."]],
	
	["It is \(.*\)",
	[	"You seem very certain.",
		"If I told you that it probably isn't %1, what would you feel?"]],
	
	["Can you \(.*\)",
	[	"What makes you think I can't %1?",
		"If I could %1, then what?",
		"Why do you ask if I can %1?"]],
	
	["Can I \(.*\)",
	[	"Perhaps you don't want to %1.",
		"Do you want to be able to %1?",
		"If you could %1, would you?"]],
	
	["You are \(.*\)",
	[	"Why do you think I am %1?",
		"Does it please you to think that I'm %1?",
		"Perhaps you would like me to be %1.",
		"Perhaps you're really talking about yourself?"]],
	
	["You're \(.*\)",
	[	"Why do you say I am %1?",
		"Why do you think I am %1?",
		"Are we talking about you, or me?"]],
	
	["I don't \(.*\)",
	[	"Don't you really %1?",
		"Why don't you %1?",
		"Do you want to %1?"]],
	
	["I feel \(.*\)",
	[	"Good, tell me more about these feelings.",
		"Do you often feel %1?",
		"When do you usually feel %1?",
		"When you feel %1, what do you do?"]],
	
	["I have \(.*\)",
	[	"Why do you tell me that you've %1?",
		"Have you really %1?",
		"Now that you have %1, what will you do next?"]],
	
	["I would \(.*\)",
	[	"Could you explain why you would %1?",
		"Why would you %1?",
		"Who else knows that you would %1?"]],
	
	["Is there \(.*\)",
	[	"Do you think there is %1?",
		"It's likely that there is %1.",
		"Would you like there to be %1?"]],
	
	["My \(.*\)",
	[	"I see, your %1.",
		"Why do you say that your %1?",
		"When your %1, how do you feel?"]],
	
	["You \(.*\)",
	[	"We should be discussing you, not me.",
		"Why do you say that about me?",
		"Why do you care whether I %1?"]],
		
	["Why \(.*\)",
	[	"Why don't you tell me the reason why %1?",
		"Why do you think %1?" ]],
		
	["I want \(.*\)",
	[	"What would it mean to you if you got %1?",
		"Why do you want %1?",
		"What would you do if you got %1?",
		"If you got %1, then what would you do?"]],
	
	["\(.*\) mother\(.*\)",
	[	"Tell me more about your mother.",
		"What was your relationship with your mother like?",
		"How do you feel about your mother?",
		"How does this relate to your feelings today?",
		"Good family relations are important."]],
	
	["\(.*\) father\(.*\)",
	[	"Tell me more about your father.",
		"How did your father make you feel?",
		"How do you feel about your father?",
		"Does your relationship with your father relate to your feelings today?",
		"Do you have trouble showing affection with your family?"]],

	["\(.*\) child\(.*\)",
	[	"Did you have close friends as a child?",
		"What is your favorite childhood memory?",
		"Do you remember any dreams or nightmares from childhood?",
		"Did the other children sometimes tease you?",
		"How do you think your childhood experiences relate to your feelings today?"]],
		
	["\(.*\)\?",
	[	"Why do you ask that?",
		"Please consider whether you can answer your own question.",
		"Perhaps the answer lies within yourself?",
		"Why don't you tell me?"]],
	
	["quit",
	[	"Thank you for talking with me.",
		"Good-bye.",
		"Thank you, that will be $150.  Have a good day!"]],
	
	["\(.*\)",
	[	"Please tell me more.",
		"Let's change focus a bit... Tell me about your family.",
		"Can you elaborate on that?",
		"Why do you say that %1?",
		"I see.",
		"Very interesting.",
		"%1.",
		"I see.  And what does that tell you?",
		"How does that make you feel?",
		"How do you feel when you say that?"]]
	]

#----------------------------------------------------------------------
#	Main program
#----------------------------------------------------------------------

gKeys = map(lambda x:regex.compile(x[0]),gPats)
gValues = map(lambda x:x[1],gPats)

print "Therapist\n---------"
print "Talk to the program by typing in plain English, using normal upper-"
print 'and lower-case letters and punctuation.  Enter "quit" when done.'
print '='*72
print "Hello.  How are you feeling today?"
s = ""
while s != "quit":
	try: s = raw_input(">")
	except EOFError:
		s = "quit"
		print s
	while s[-1] in "!.": s = s[:-1]
	print respond(s,gKeys,gValues)

when i use it, i get a traceback error, (Traceback (most recent call last):
File "E:\Python Files\Therapist.py", line 2, in <module>
import regex
ImportError: No module named regex)

Could use help. tried messing around with the variables, but it still wouldn't work.

your error was expalined i think.
no module name regex.

import re as regex

thats if you cant do without regex.
;)

if I ever have a module problem, how do I know how to fix it?

This kind of problem comes from old code which used module before it was accepted to distribution, and name changed. Google is your help, you can see how the deprecated modules functionality can be replaced with new module or if you only need name change. Better of course if the original code is well commented so that you get to understand what the missing library is supposed to do. So it is best to include stable Internet address link for the outside module used for your program in comments/docstring.

So now i'm wondering this- if I was to enter a print command in the gPats section where all the print commands are, how would I do so? because I have tried to enter a piece of code in the exact place I'm talking about-

[r'how are you',
  [ "I'm fine, thank you."]],

but it doesn't work correctly. Intead, it prints another command, such as "that's very interesting, please tell me more."

Edited 5 Years Ago by bob24bob: n/a

Maybe something like this:

import string
import re as regex
import random

#----------------------------------------------------------------------
# translate: take a string, replace any words found in dict.keys()
#   with the corresponding dict.values()
#----------------------------------------------------------------------
def translate(str,mydict):
    words = string.split(string.lower(str))
    for i,word in enumerate(words):
        if word in mydict:
            words[i] = mydict[words[i]]
    return ' '.join(words)

#----------------------------------------------------------------------
#   respond: take a string, a set of regexps, and a corresponding
#       set of response lists; find a match, and return a randomly
#       chosen response from the corresponding list.
#----------------------------------------------------------------------
def respond(str,keys,values):
    # find a match among keys
    for i in range(0,len(keys)):
        match = keys[i].search(str)
        if match:
            # print i
            # found a match ... stuff with corresponding value
            # chosen randomly from among the available options
            resp = random.choice(values[i])
            # we've got a response... stuff in reflected text where indicated
            pos = string.find(resp,'%')
            while pos > -1:
                num = int(resp[pos+1:pos+2])
                resp = (resp[:pos] +
                                        translate(match.group(num),gReflections) +
                                        resp[pos+2:])
                pos = string.find(resp,'%')
            # fix munged punctuation at the end
            if resp[-2:] == '?.': resp = resp[:-2] + '.'
            if resp[-2:] == '??': resp = resp[:-2] + '?'
            return resp

#----------------------------------------------------------------------
# gReflections, a translation table used to convert things you say
#       into things the computer says back, e.g. "I am" --> "you are"
#----------------------------------------------------------------------
gReflections = {
    "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" }

#----------------------------------------------------------------------
# gPats, the main response table.  Each element of the list is a
#   two-element list; the first is a regexp, and the second is a
#   list of possible responses, with group-macros labelled as
#   %1, %2, etc.
#----------------------------------------------------------------------
gPats = [
   ['How are you?',
      [ "I'm fine, thank you."]],
    ["I need (.*)",
    [   "Why do you need %1?",
        "Would it really help you to get %1?",
        "Are you sure you need %1?"]],

    ["Why don't you (.*)",
    [   "Do you really think I don't %1?",
        "Perhaps eventually I will %1.",
        "Do you really want me to %1?"]],

    ["Why can't I (.*)",
    [   "Do you think you should be able to %1?",
        "If you could %1, what would you do?",
        "I don't know -- why can't you %1?",
        "Have you really tried?"]],

    ["I can't (.*)",
    [   "How do you know you can't %1?",
        "Perhaps you could %1 if you tried.",
        "What would it take for you to %1?"]],

    ["I am (.*)",
    [   "Did you come to me because you are %1?",
        "How long have you been %1?",
        "How do you feel about being %1?"]],

    ["I'm (.*)",
    [   "How does being %1 make you feel?",
        "Do you enjoy being %1?",
        "Why do you tell me you're %1?",
        "Why do you think you're %1?"]],

    ["Are you (.*)",
    [   "Why does it matter whether I am %1?",
        "Would you prefer it if I were not %1?",
        "Perhaps you believe I am %1.",
        "I may be %1 -- what do you think?"]],

    ["What (.*)",
    [   "Why do you ask?",
        "How would an answer to that help you?",
        "What do you think?"]],

    ["How (.*)",
    [   "How do you suppose?",
        "Perhaps you can answer your own question.",
        "What is it you're really asking?"]],

    ["Because (.*)",
    [   "Is that the real reason?",
        "What other reasons come to mind?",
        "Does that reason apply to anything else?",
        "If %1, what else must be true?"]],

    ["(.*) sorry (.*)",
    [   "There are many times when no apology is needed.",
        "What feelings do you have when you apologize?"]],

    ["Hello(.*)",
    [   "Hello... I'm glad you could drop by today.",
        "Hi there... how are you today?",
        "Hello, how are you feeling today?"]],

    ["I think (.*)",
    [   "Do you doubt %1?",
        "Do you really think so?",
        "But you're not sure %1?"]],

    ["(.*) friend(.*)",
    [   "Tell me more about your friends.",
        "When you think of a friend, what comes to mind?",
        "Why don't you tell me about a childhood friend?"]],

    ["Yes",
    [   "You seem quite sure.",
        "OK, but can you elaborate a bit?"]],

    ["(.*) computer(.*)",
    [   "Are you really talking about me?",
        "Does it seem strange to talk to a computer?",
        "How do computers make you feel?",
        "Do you feel threatened by computers?"]],

    ["Is it (.*)",
    [   "Do you think it is %1?",
        "Perhaps it's %1 -- what do you think?",
        "If it were %1, what would you do?",
        "It could well be that %1."]],

    ["It is (.*)",
    [   "You seem very certain.",
        "If I told you that it probably isn't %1, what would you feel?"]],

    ["Can you (.*)",
    [   "What makes you think I can't %1?",
        "If I could %1, then what?",
        "Why do you ask if I can %1?"]],

    ["Can I (.*)",
    [   "Perhaps you don't want to %1.",
        "Do you want to be able to %1?",
        "If you could %1, would you?"]],

    ["You are (.*)",
    [   "Why do you think I am %1?",
        "Does it please you to think that I'm %1?",
        "Perhaps you would like me to be %1.",
        "Perhaps you're really talking about yourself?"]],

    ["You're (.*)",
    [   "Why do you say I am %1?",
        "Why do you think I am %1?",
        "Are we talking about you, or me?"]],

    ["I don't (.*)",
    [   "Don't you really %1?",
        "Why don't you %1?",
        "Do you want to %1?"]],

    ["I feel (.*)",
    [   "Good, tell me more about these feelings.",
        "Do you often feel %1?",
        "When do you usually feel %1?",
        "When you feel %1, what do you do?"]],

    ["I have (.*)",
    [   "Why do you tell me that you've %1?",
        "Have you really %1?",
        "Now that you have %1, what will you do next?"]],

    ["I would (.*)",
    [   "Could you explain why you would %1?",
        "Why would you %1?",
        "Who else knows that you would %1?"]],

    ["Is there (.*)",
    [   "Do you think there is %1?",
        "It's likely that there is %1.",
        "Would you like there to be %1?"]],

    ["My (.*)",
    [   "I see, your %1.",
        "Why do you say that your %1?",
        "When your %1, how do you feel?"]],

    ["You (.*)",
    [   "We should be discussing you, not me.",
        "Why do you say that about me?",
        "Why do you care whether I %1?"]],

    ["Why (.*)",
    [   "Why don't you tell me the reason why %1?",
        "Why do you think %1?" ]],

    ["I want (.*)",
    [   "What would it mean to you if you got %1?",
        "Why do you want %1?",
        "What would you do if you got %1?",
        "If you got %1, then what would you do?"]],

    ["(.*) mother(.*)",
    [   "Tell me more about your mother.",
        "What was your relationship with your mother like?",
        "How do you feel about your mother?",
        "How does this relate to your feelings today?",
        "Good family relations are important."]],

    ["(.*) father(.*)",
    [   "Tell me more about your father.",
        "How did your father make you feel?",
        "How do you feel about your father?",
        "Does your relationship with your father relate to your feelings today?",
        "Do you have trouble showing affection with your family?"]],

    ["(.*) child(.*)",
    [   "Did you have close friends as a child?",
        "What is your favorite childhood memory?",
        "Do you remember any dreams or nightmares from childhood?",
        "Did the other children sometimes tease you?",
        "How do you think your childhood experiences relate to your feelings today?"]],

    ["(.*)\?",
    [   "Why do you ask that?",
        "Please consider whether you can answer your own question.",
        "Perhaps the answer lies within yourself?",
        "Why don't you tell me?"]],

    ["quit",
    [   "Thank you for talking with me.",
        "Good-bye.",
        "Thank you, that will be $150.  Have a good day!"]],

  ["(.*)",
  [   "Please tell me more.",
      "Let's change focus a bit... Tell me about your family.",
      "Can you elaborate on that?",
      "Why do you say that %1?",
      "I see.",
      "Very interesting.",
      "%1.",
      "I see.  And what does that tell you?",
      "How does that make you feel?",
      "How do you feel when you say that?"]]
    ]

#----------------------------------------------------------------------
#   Main program
#----------------------------------------------------------------------

gKeys = map(lambda x:regex.compile(x[0]),gPats)
gValues = map(lambda x:x[1],gPats)

print "Therapist\n---------"
print "Talk to the program by typing in plain English, using normal upper-"
print 'and lower-case letters and punctuation.  Enter "quit" when done.'
print '='*72
print "Hello.  How are you feeling today?"
s = ""
while s != "quit":
    try: s = raw_input(">")
    except EOFError:
        s = "quit"
        print s
    while s[-1] in "!.": s = s[:-1]
    print respond(s,gKeys,gValues)

Edited 5 Years Ago by pyTony: n/a

Thank you. The code now works with the command in. However, now I am trying to put in a new command into the gPats for "No"

gPats = [
   ['How are you?',
      [ "I'm fine, thank you. What have you come here for?"]],
    ["I need (.*)",
    [   "Why do you need %1?",
        "Would it really help you to get %1?",
        "Are you sure you need %1?"]],

    ["No",
    [    "Why not?"]]

    ["Why don't you (.*)",
    [   "Do you really think I don't %1?",
        "Perhaps eventually I will %1.",
        "Do you really want me to %1?"]],

And I get a new Traceback error
Traceback (most recent call last):
File "C:\Python25\Therapy.py", line 61, in <module>
"Do you really want me to %1?"]],
TypeError: list indices must be integers

So now, I'm left with two questions. One; How do I fix this? and Two; Whenever I want to put in a new command for the gPats section, how do I go about doing that?

I put the No response in my program and it works without problem, I do not know what problem you have.

["Yes",
    [   "You seem quite sure.",
        "OK, but can you elaborate a bit?"]],

    ["No",
    [ "Why not?"]],
This article has been dead for over six months. Start a new discussion instead.