Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 111 Fundamentals of Programming I Dictionaries redux.

Similar presentations


Presentation on theme: "Computer Science 111 Fundamentals of Programming I Dictionaries redux."— Presentation transcript:

1 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

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 (‘I’ 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 continue the conversation with a hedge.

7 The Doctor Program 1.The patient inputs a sentence 2.The doctor replies by either a.Exploding the patient’s sentence into a list of words b.Changing the person of each pronoun in the list c.Converting the list back to a string d.Prepending a qualifier to the string to create a question e.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 element from a given sequence import random print(random.choice(qualifiers))

10 The Main Loop 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 1.Explode the sentence into a list of words 2.Loop through the list and build a new list as we go 3.If the word in the input list is in the replacements dictionary, add its replacement to the new list 4.Otherwise, just add the original word to the new list 5.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) newlist.append(replacements.get(word, word)) Instead of the if-else statement, we could accomplish the replacement more simply using get :

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 using get

15 For Wednesday Start Chapter 6


Download ppt "Computer Science 111 Fundamentals of Programming I Dictionaries redux."

Similar presentations


Ads by Google