ComputerSysAndProgramming 8
ComputerSysAndProgramming 8
article noun
article noun
the Similar
girl to the behavior of strings so far
the Similar
girl to the behavior of strings so far
A verb phrase is a verb followed by a noun phrase and a
prepositional phrase
The Structure of a Sentence
sentence
the Similarthrew
girl to the behavior of strings so far
article noun
the Similarthrew
girl to the behavior of strings so far
article noun
the Similarthrew
girl to the behavior
the of
pie strings so far
the Similarthrew
girl to the behavior
the of
pie strings so far
A prepositional phrase is a preposition followed by a noun
phrase
The Structure of a Sentence
sentence
the Similarthrew
girl to the behavior
the of
pie stringsatso far
article noun
the Similarthrew
girl to the behavior
the of
pie stringsatso far
article noun
the Similarthrew
girl to the behavior
the of
pie stringsatso far a boy
import random
Etc., etc.
Define a Function for Each Rule
# sentence = nounphrase verbphrase
def sentence():
return nounphrase() + ' ' + verbphrase()
an_empty_one = {}
Syntax:
{<key> : <value>, … , <key> : <value>}
Acessing a Value with a Key
>>> database = {'name':'Ken', 'age':67,
'gender':'M', 'occupation':'teacher'}
>>> database['name']
'Ken'
>>> database['age']
67
If the key is not already in the dictionary, Python creates one and
inserts it with the associated value
Inserting a New Key/Value
hexdigits = {'A':10, 'B':11, 'C':12,
'D':13, 'E':14, 'F':15}
>>> hexdigits
{'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15,
'1': 1, '0': 0, '3': 3, '2': 2, '5': 5, '4': 4,
'7': 7, '6': 6, '9': 9, '8': 8}
The pop method removes the key and its associated value and
returns this value
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?
import random
print(random.choice(qualifiers))
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!')
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)