Making Sentences (and poems) with Python revisited

Lab 6

Due March 26 moodle link
dogfurtree.gif

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

Example:

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

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

SentTree.png

Part 2: Python programs for using Phrase Structure Rules

Consider how much this is 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
NP -> NP PP

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

Experiment with at least five different sentence structures. English has plenty.

  • Can you write a rule for a passive sentence, e.g.:
    1. The student was using her phone. (Active)
    2. The phone was being used by the student. (passive)
  • Can you find a way to deal with verb agreement, e.g.:
    1. The woman swims for the finish line. (singular subject)
    2. The four woman swim for the finish line. (plural subject)
  • Can you write a rule with an embedded clause?
    1. The girl watched the puppies playing in the yard. (Active)
    2. The puppies playing in the yard were watched by the girl. (Passive)
For each sentence structure write a difference function to generate that type of sentence. The function should return a sentence as a string.

Part 5: Make a new poetry or story writing program

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

2. Pick a poetry style, or make one up, and make a program that generates that type of poem. You can pick words that represent a domain that you wish the poems to focus on.

Example:

Traditional Haiku Structure

Traditional haiku are always the same, including the following features:

  1. There are only three lines, totaling 17 syllables.
  2. The first line is 5 syllables.
  3. The second line is 7 syllables.
  4. The third line is 5 syllables like the first.
  5. Punctuation and capitalization are up to the poet, and need not follow rigid rules used in structuring sentences.
  6. A haiku does not have to rhyme, in fact usually it does not rhyme at all.
  7. Some haiku can include the repetition of words or sounds

Over the wintry forest,
winds howl in rage
with no leaves to blow.

Others:

  1. Limerick
  2. Types of Poems
  3. Introduction to Types of Poems
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 repl.it and best story or poem.

RequirementPointsCommentsScore
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

10

Total
Topic attachments
I Attachment History Action Size Date WhoSorted ascending 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 < r8 < r7 < r6 < r5 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r9 - 2019-03-18 - JimSkon
 
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