More on Python
In order to do this second assignment in Python there is just a little bit more sytax that we need to introduce. Mainly we need just a few more tools for dealing with lists.
A new list can be created using a pair of square brackets. The list is initially empty. Elements can be added to the list using the function append. Try executing the following
aList = [ ]
aList.append("chris")
aList.append("robin")
aList.append("pat")
print aList
The second bit of syntax we need is to select random numbers.
To do this you need to first import the random library.
Then, within this library there is a function named choice.
If you pass as argument a list to choice, it will return
a randomly selected element. Try this
import random
# ... more of your program
aList = [ ]
aList.append("chris")
aList.append("robin")
aList.append("pat")
print random.choice(aList)
print random.choice(aList)
print random.choice(aList)
print random.choice(aList)
That's all the new syntax you need. Everything else you learned
in the previous programming assignment.
start:person does something: start:person thinks that I am property: start:I do something: start:you think that I am property: activity:dancing: activity:eating: activity:sleeping: object:person: object:life: object:my computer: object:my friends: do:hate: do:am jealous of: do:love: does:hates: does:loves: person:my sister: person:my father: person:my girl friend: person:the man next door: property:creative: property:intelligent: something:activity: something:activity with person: something:object:
Our grammars will always begin with the start symbol ``start''. This particular grammar can be found at /usr/local/classes/cs/cs381/data/grammar.
Your first task is to read the grammar in from the file, placing it into a dictionary named {\sf grammar}. Remember from the last assignment how to read from a file, and how to break a string into parts. Let us call the left part of a grammar rule the head, and the right part the rewriting.
Now notice that there can be more than one rewriting for each head. Thus, the data structure we need to represent the grammar as a rather unusual structure. It is a dictionary, indexed by a string, containing lists of strings.
As you read each line, test to see if the head is already a key for the grammar dictionary. If it is not, initialize the element keyed by the head, assigning it an empty list. Regardless of this test, then append the rewriting to the list given when you index the grammar file by the head.
You may want to just write this part and print the grammar variable before you continue.
The next step is to write a recursive routine, which we will call replace. This routine will take as argument a string. It should check if the argument string is a key in the grammar file. If not, then simply return the argument with one extra space appended to the end. If it is, then random select one of the replacement strings from the list given by the argument key. Then take this string, and split it apart based on spaces. Recursively call replace using each word, and catenate the results. Return the final catenated result.
If in your main program you now call replace("start") you should get one random sentence. Running your program repeatedly should generate different sentences. As the final change to your program, wrap this call in a loop so that you generate ten random sentences each time your program is invoked.
Here are some example sentences produced by the grammar:
the man next door thinks that I am intelligent my girl friend loves eating with my father my father thinks that I am intelligent I hate sleeping with the man next door I am jealous of life I hate my computer the man next door thinks that I am intelligent my father hates dancing with my father you think that I am creative I love eating
As usual, name your file progFive, and hand it in using the COE handin script. Extra totally useless brownie points for posting amusing new grammars to the class mail list.