Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of Programming I Dictionaries redux

Similar presentations


Presentation on theme: "Fundamentals of Programming I Dictionaries redux"— Presentation transcript:

1 Fundamentals of Programming I Dictionaries redux
Computer Science 111 Fundamentals of Programming I Dictionaries redux

2 Another Application: Non-Directive Psychotherapy
Good morning, Ken, how can I help you today? > The therapist opens the session with a leading question, and waits for the patient’s reply

3 Another Application: Non-Directive Psychotherapy
Good morning, Ken, how can I help you today? > My teacher hates me. The therapist lets the patient provide most of the content The patient replies with a statement

4 Another Application: Non-Directive Psychotherapy
Good morning, Ken, how can I help you today? > My teacher hates me. Why do you say that your teacher hates you? The therapist lets the patient provide most of the content She responds by rephrasing the patient’s statement as a request for more information She includes the patient’s content by switching persons in the replies (‘me’ becomes ‘you,’ ‘my’ becomes ‘your’, etc.)

5 Another Application: Non-Directive Psychotherapy
Good morning, Ken, how can I help you today? > My teacher hates me. Why do you say that your teacher hates you? > She always calls on the girls in the class. The therapist lets the patient provide most of the content She responds by rephrasing the patient’s statement as a request for more information She includes the patient’s content by switching persons in the replies (‘I’ becomes ‘you,’ ‘my’ becomes ‘your’, etc.)

6 Another Application: Non-Directive Psychotherapy
Good morning, Ken, how can I help you today? > My teacher hates me. Why do you say that your teacher hates you? > She always calls on the girls in the class. Please tell me more. > Alternatively, the therapist tries to move the conversation forward with a hedge.

7 The Doctor Program The patient inputs a sentence
The doctor replies by either Exploding the patient’s sentence into a list of words Changing the person of each pronoun in the list Converting the list back to a string Prepending a qualifier to the string to create a question Printing the result or Printing a randomly chosen hedge Steps 1 and 2 are repeated until the patient enters ‘quit’

8 Data Structures for the Program
A list of qualifiers to be chosen at random A list of hedges to be chosen at random A dictionary of pronouns and their replacements

9 Data Structures for the Program
qualifiers = ['Why do you say that ', 'You seem to think that ', 'Did I just hear you say that '] replacements = {'I': 'you', 'me': 'you', 'my': 'your', 'we': 'you', 'us': 'you'} The function random.choice returns a randomly selected item from a given sequence import random print(random.choice(qualifiers))

10 The Main Loop The function main handles the interaction with the user
def main(): """Handles user interaction with the program.""" print('Good morning, how can I help you today?') while True: sentence = input('> ') if sentence.upper() == 'QUIT': break print(reply(sentence)) print('Have a nice day!') The function main handles the interaction with the user The function reply transforms the patient’s input into the doctor’s reply and returns it

11 Defining reply def reply(sentence): """Returns a reply to the sentence.""" return random.choice(qualifiers) + changePerson(sentence) reply prepends a randomly chosen qualifier to the result of changing persons in the patient’s input sentence The result returned is in the form of a question that includes most of the content of the patient’s input sentence

12 The Strategy for changePerson
Explode the sentence into a list of words Loop through the list and build a new list as we go If the word in the input list is in the replacements dictionary, add its replacement to the new list Otherwise, just add the original word to the new list When the loop is finished, congeal the new list to a string of words and return it

13 Defining changePerson
def changePerson(sentence): """Returns the sentence with pronouns changed.""" oldlist = sentence.split() newlist = [] for word in oldlist: if word in replacements: newlist.append(replacements[word]) else: newlist.append(word) return " ".join(newlist) Instead of the if-else statement, we could accomplish the replacement more simply using get: newlist.append(replacements.get(word, word))

14 Defining changePerson
def changePerson(sentence): """Returns the sentence with pronouns changed.""" oldlist = sentence.split() newlist = [] for word in oldlist: newlist.append(replacements.get(word, word)) return " ".join(newlist) Instead of using an if-else statement, we could accomplish the replacement more simply by using get

15 Improvements Add hedges
Keep track of earlier patient inputs, so the doctor can refer back to them Spot keywords in a patient’s reply, and respond with associated sentences

16 For Next Monday Start Chapter 6


Download ppt "Fundamentals of Programming I Dictionaries redux"

Similar presentations


Ads by Google