Fundamentals of Programming I Dictionaries redux

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Fundamentals of Python: From First Programs Through Data Structures
Python Magic Select a Lesson: Why Learn to Code? Basic Python Syntax
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Python.
Turing Test and other amusements. Read this! The Actual Article by Turing.
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
Computer Science 111 Fundamentals of Programming I Dictionaries redux.
Python Mini-Course University of Oklahoma Department of Psychology Day 2 – Lesson 7 Conditionals and Loops 4/18/09 Python Mini-Course: Day 2 - Lesson 7.
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Computer Science 111 Fundamentals of Programming I Default and Optional Parameters Higher-Order Functions.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Microsoft® Small Basic Conditions and Loops Estimated time to complete this lesson: 2 hours.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
How To Work In Small Groups. Your role as the leader To have each child feel as though he/she has contributed something special To follow teacher directive.
Fundamentals of Programming I Higher-Order Functions
CMSC201 Computer Science I for Majors Lecture 07 – While Loops
GCSE COMPUTER SCIENCE Practical Programming using Python
Topic 6 Recursion.
AP Java Enhancing the Chatbot.
Week of 12/12/16 Test Review.
Lesson 4 - Challenges.
Repetition (While-Loop) version]
The switch Statement, and Introduction to Looping
Working with Dictionaries
Lesson 05: Iterations Class Chat: Attendance: Participation
ECS10 10/10
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Fundamentals of Programming I Design with Functions
CS1371 Introduction to Computing for Engineers
Chapter Topics 11.1 Introduction to Menu-Driven Programs
Fundamentals of Programming I Managing the Namespace
Phrases For Business English
While Loops in Python.
REPORTED SPEECH Unit 11 – English 12.
Iterations Programming Condition Controlled Loops (WHILE Loop)
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
Python Primer 2: Functions and Control Flow
File Handling Programming Guides.
Haskell Strings and Tuples
Validations and Error Handling
In Class Program: Today in History
CS190/295 Programming in Python for Life Sciences: Lecture 6
Alice Variables Pepper.
AP Magpie Chatbox "If a computer could think, how could we tell?"
Topic 1: Problem Solving
Introduction to TouchDevelop
More Looping Structures
CMSC201 Computer Science I for Majors Lecture 09 – While Loops
AP Java Enhancing the Chatbot.
Python programming exercise
Looping III (do … while statement)
Computer Science 111 Fundamentals of Programming I
A LESSON IN LOOPING What is a loop?
Program Flow.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Making decisions with code
Another Example Problem
Text Files and Random Numbers
Using C++ Arithmetic Operators and Control Structures
Chapter 3: Selection Structures: Making Decisions
More Looping Structures
CSE 231 Lab 2.
While Loops in Python.
CS Problem Solving and Object Oriented Programming Spring 2019
CMPT 120 Lecture 6 – Unit 1 – Chatbots
How to allow the program to know when to stop a loop.
Presentation transcript:

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

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

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

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.)

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.)

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.

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’

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

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))

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

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

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

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))

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

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

For Next Monday Start Chapter 6