Difference: PythonLab6 (6 vs. 7)

Revision 72019-02-12 - JimSkon

Line: 1 to 1

Making Sentences (and poems) with Python revisited

Line: 37 to 41
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.
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
Line: 66 to 72
  VP -> S NP -> Det Adj N
You will need to add a new class or words, adverbs (adv).
You will need to add a new class of words, adverbs (adv).
See if you an come up with at least three more rules that seem to work.
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.


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.


  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.

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