Making Sentences (and poems) with Python revisited

Lab 6

Due Oct 26 moodle link

Phrase structure rules

Part one: Phrase Structure Rules

Phrase structure rules are rewrite rules that generate phrase structure(s). These have the general form of (i), where X is the name of the phrase and Y Z W defines its structure. Y, Z, and W are either phrases, and therefore must themselves occur to the left of the arrow in rules of this type, or non-phrasal (terminal) categories (such as Noun, Verb, or Determiner).

(i)     X   ->   Y Z W ...


The phrase structure rules in (ii) generate the phrase structure of the sentences in (iii).

(ii)  a    S   ->   NP VP
      b    VP   ->   V 
      c    VP   ->   V NP 
      d    VP   ->   V NP PP
      e    VP   ->   V PP
      f    NP   ->   Det N
g NP -> N h NP -> ProperNoun i PP -> P NP

(Round Brackets indicate that a phrase is optional)

(iii) a    John laughs
      b    John eats an apple
      c    The girl walks in the forest
      d    The boy sends flowers to his mother

Thus the phrase structure rule in (ii)a characterizes a sentence (S) as the combination of an NP (the subject) and a VP (the predicate), i.e. as in (iv).

a. S -> NP VP -> ProperNoun V -> John laughs
b. S -> NP VP -> ProperNoun V NP -> ProperNoun V Det N -> John eats an apple
c. S -> NP VP -> Det N V PP -> Det N V P NP -> Det N V P Det N -> The girl walks in the forest
d. S -> NP VP -> Det N V NP PP -> Det N V N P NP ->bo The boy sends flowers to his mother

Below we see the application of the phrase structure rules to sentence (d). Can you draw the trees for (a)-(c)?


Part 2: Python programs for using Phrase Structure Rules

Consider the following program for using rules like the L-Systems Fractel program to generate sentences. The only difference is we are using strings (words) instead of characters to rewrite.

Code: SentenceGenerator1

Try it out. Notice how the code rewrites symbols like NP, VP, Det, and V into the what they consist of, just like the rules above. When we get to word level symbol, such as a V, we pick a random word from the word list for that class of word (e.g. verbs). Once a word is used, it is no longer changed, since there isn't a rule for it. The program merely adds the same word back in to keep it in the sentence. Experiment with it until you understand what is going on.

Notice that we call the createSentence program with 4, so that the rewriting is done 4 times. Experiment with numbers smaller than 4 to see what happens. How big does this number need to be in general?

Part 3: Multiple rules for the same symbol

The problem with this program is it only allows for one grammer rule for each grammer symbol. Can we have multiple rules, and pick a set of rules from a list. For example, can we pick between one of the following three NP rules:

    NP -> Proper
    NP -> Det N
    NP -> Det Adj N

Consider this code: SentenceGenerator2

See how this uses the same method used to randomly select a word to randomly select a NP rule? Play around with this code until you understand it.

Part 4: Add more rules

Modify the program to support the following rules:

    VP -> V
VP -> V NP VP -> V NP PP VP -> V PP VP -> Adv VP VP -> S NP -> Det Adj N

You will need to add a new class or words, adverbs (adv).

See if you an come up with at least three more rules that seem to work.

Part 5: Make a new poetry or story writing program

Assignment: Make a new program to write poems or stories using this new method.

Your program should use at least 20 different rules. Write a program that repeatedly calls the routine to create sentences, and add a period to the end of each sentence.

Added features: Add at least two.

  • The program asks the user for words
  • The program keeps track of words used, like nouns chosen, so that a group of sentences are tied together by a common topic.
  • The program gives the option of saving a poem to a file after generating it.
  • The program gets its word lists from a file(s). You can get word lists from many sources on line: Example.
  • Something else you propose.
Turn in your and best story.

Program generates poems on demand 40
Code is broken up into functions, with one specific task per function 20
Each function is commented at top of function 10
Program has meaningful comments, good variable names, and good formatting 10
Program implements two extra features 10
Instructions on how to use


Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng ParseTree.png r1 manage 7.2 K 2018-10-18 - 17:03 JimSkon  
PNGpng SentTree.png r1 manage 7.1 K 2018-10-18 - 04:38 JimSkon  
GIFgif dogfurtree.gif r1 manage 4.3 K 2018-10-18 - 04:09 JimSkon  
Edit | Attach | Watch | Print version | History: r9 | r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r5 - 2018-10-21 - JimSkon
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback