213k views
2 votes
The Doctor program described in Chapter 5 combines the data model of a doctor and the operations for handling user interaction. Restructure this program according to the model/view pattern so that these areas of responsibility are assigned to separate sets of classes.

The program should include a Doctor class with an interface that allows one to obtain a greeting, a signoff message, and a reply to a patient’s string.

To implement the greeting, define a method named greeting for the Doctor class. To implement the signoff message, define a method named farewell for the Doctor class. Both greeting and farewell should return a string with a greeting or farewell message respectively. The reply function is defined for you, it should be added as a method for the Doctor class.

The rest of the program, in a separate main program module, handles the user’s interactions with the Doctor object. Develop this program with a terminal-based user interface.

Note: The program should output in the following format:

Hello, how can I help you today?
> I am sick
You seem to think that you are sick?
> Yes
And what do you think about this?
> I am not feeling good
Did I just hear you say that you are not feeling good?
> Yes
Why do you believe that Yes?
> My nose is running
I would like to hear more about that.
> I have a headache too
You seem to think that you have a headache too?
> Correct
Go on.
> It doesn't stop
Did I just hear you say that It doesn't stop?
> Correct
Go on.
> That's it
And what do you think about this?
> quit
Have a nice day!

Cengage Code Sample:
"""
File: doctor.py
Project 5
Conducts an interactive session of nondirective psychotherapy.
Adds a history list of earlier patient sentences, which can
be chosen for replies to shift the conversation to an earlier topic.
"""

import random

history = []

# All doctors share the same qualifiers, replacements, and hedges.

QUALIFIERS = ['Why do you say that ', 'You seem to think that ',
'Did I just hear you say that ',
'Why do you believe that ']

REPLACEMENTS = {'i': 'you', 'me': 'you', 'my': 'your',
'we': 'you', 'us': 'you', 'am': 'are',
'you': 'i', 'you': 'I'}

HEDGES = ['Go on.', 'I would like to hear more about that.',
'And what do you think about this?', 'Please continue.']

def reply(sentence):
"""Implements three different reply strategies."""
probability = random.randint(1, 5)
if probability in (1, 2):
# Just hedge
answer = random.choice(hedges)
elif probability == 3 and len(history) > 3:
# Go back to an earlier topic
answer = "Earlier you said that " + \
changePerson(random.choice(history))
else:
# Transform the current input
answer = random.choice(qualifiers) + changePerson(sentence)
# Always add the current sentence to the history list
history.append(sentence)
return answer

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 = input("\\>> ")
if sentence.upper() == "QUIT":
print("Have a nice day!")
break
print(reply(sentence))

# The entry point for program execution
if __name__ == "__main__":
main()

1 Answer

3 votes

Final answer:

The Doctor program can be restructured according to the model/view pattern by creating a Doctor class for data logic, which includes methods for greeting, farewell, and reply. The view component is separated into the main program module for user interaction.

Step-by-step explanation:

To restructure the Doctor program according to the model/view pattern, we create a distinct Doctor class to manage the data model, which encompasses the logic of generating responses to the patient. The user interaction is then managed separately in the main program module, following the view component of the pattern.

Doctor Class Implementation

The Doctor class should include three methods as interfaces:


  • greeting - Returns a greeting message to start the session.

  • farewell - Provides a signoff message to end the session.

  • reply - Generates a reply to the patient's statements based on given strategies.



Main Program Module

In the main program module, the user interaction workflow is implemented. It creates an instance of the Doctor class and handles the input/output loop where the patient interacts with the instance through the terminal.

This segregation enables an organized structure where the data logic and user interface are neatly compartmentalized, adhering to the model/view design principle.

User Woolstar
by
7.9k points