Part 1: In Case Study 5.5, when the patient addresses the therapist personally,
ID: 3535838 • Letter: P
Question
Part 1: In Case Study 5.5, when the patient addresses the
therapist personally, the therapist’s reply does
not change persons appropriately. To see an example of this
problem, test the program with “you are not a
helpful therapist.†Fix this problem by repairing the
dictionary of replacements.
Conversations often shift focus to earlier topics. Modify the
therapist program to support this capability. Add each patient
input to a history list. Then, occasionally choose an element at
random from this list, change persons, and prepend the qualifier
“Earlier you said that†to this
reply. Make sure that this option is triggered only after several
exchanges have occurred.
My instructor told me that this file wasn't working, and offered
this advice:
1. when probability is 1 or 2 just hedge
2. when probability is 3 and history length is greater than 3
the add the phrase, "Earlier you said that..."
3. other cases, transform the current input (code is the
same)
Thank you!
import random
hedges = ("Please tell me more.",
"Many of my patients tell me the same thing.",
"Please continue.")
qualifiers = ("Why do you say that?",
"What makes you think that?",
"Can you explain why?", "Earlier you said that.")
replacements = {"I":"you", "me":"you", "my":"your",
"we":"you", "us":"you", "mine":"yours",
"you":"I", "am":"are", "myself":"yourself",
"was":"were", "your":"my", "you":"me", "yours":"mine",
"you":"us"}
name = input("May I ask your name?")
def reply(sentence):
"""Builds and returns a
reply to the sentence."""
probability =
random.randint(1, 4)
if probability == 1:
return random.choice(hedges)
elif probability ==
2:
return random.choice(hedges)
elif probability ==
3:
return random.choice(len(history)qualifiers)
else:
return random.choice(qualifiers) + changePerson(sentence)
cndtion = input()
history = {}
history['name'] = name
#creates a dictionary to store patients' names
history['condition'] =
cndtion #Creates a dictionary that stores patients'
conditions.
while 1:
print("search name to find details, write 'quit' to exit write
'add' to add a new patient")
option = input(history)
if option in history:
print (history[option], option)
if option == "quit":
break
if option == "add":
name
else:
print("Error patient / command not recognised.")
def changePerson(sentence):
"""Replaces the first
person pronouns with
second person
pronouns."""
words =
sentence.split()
replyWords = []
return "
".join(replyWords)
def main():
"""Handles the interaction
between patient and doctor."""
print("Hello, I hope you
are doing well.")
print("How can I help you
today,", name,"?")
while True:
sentence = input(" >>")
if sentence.upper() == "QUIT":
print("Have a nice day!")
break
print(reply(sentence))
main()
Explanation / Answer
import random
hedges = ("Please tell me more.",
"Many of my patients tell me the same thing.",
"Please coninue.")
qualifiers = ("Why do you say that ",
"You seem to think that ",
"Can you explain why ",
"Earlier you said that")
replacements = {"I":"you", "me":"you", "my":"your",
"we":"you", "us":"you", "mine":"yours"}
def reply(sentence):
"""Implements two different reply strategies."""
probability = random.randint(1, 4)
if probability == 1:
return random.choice(hedges)
else:
return random.choice(qualifiers) + changePerson(sentence)
def changePerson(sentence):
"""Replaces first person pronouns with second person
pronouns."""
words = sentence.split()
replyWords = []
for word in words:
replyWords.append(replacements.get(word, word))
return " ".join(replyWords)
def main():
"""Handles the interaction between patient and doctor."""
print "Good morning, I hope you are well today."
print "What can I do for you?"
while True:
sentence = raw_input(" >> ")
if sentence.upper() == "QUIT":
print "Have a nice day!"
break
print reply(sentence)
main()
Use this part of the code for creating the history list:
def addPatient()
print"enter a name for the patient: "
name = raw_input()
print"enter the age of the patient: "
age = raw_input()
print"enter the condition of the patient: "
cndtion = raw_input()
history = {}
history['name'] = name
history['age'] = age
history['condition'] = cndtion
while 1:
print"search name to find details, write 'quit' to exit, write 'add' to add a new patient"
option = raw_input()
if option in history:
print history[option], option
if option == "quit":
break:
if option == "add":
addPatient()
else:
print"Error patient / command not recognised."
Please let me know if you have any doubts