CS 12 - Assignment 1 - Sentences
CS 12 Homepage
Due Sunday October 12th, 8pm
Imagine, if you will, that
you wanted to have your computer generate truly atrocious haiku
poetry. A haiku is a Japanese poem with no rhyme scheme or meter
composed of three lines: one of 5 syllables, followed by one of
7 syllables, ended by one of 5. How would you do it?
One way to do it is to generate 7 lists of words corresponding
to words with 1, 2, 3, 4, 5, 6, or 7 syllables respectively.
Then have 7 helper functions which (randomly) decide whether to
return 1 word of the appropriate length, or call two smaller
helpers which produce the same total number of syllables.
This is a bit complicated. Let us walk through an example:
-
We want to generate a 5 syllable line. We randomly choose a
number between 1 and 5 to represent the number of syllables the
first word on our line will be.
-
We choose 3
-
We randomly choose a word from our 3 syllable word list.
-
We still have 2 syllables, which could be either 2 words of
one syllable each, or 1 word of two syllables. We call our
helper that generates two syllable lines to finish off.
-
The two syllable helper has two options available to it. It
chooses (randomly) that we will have a 2 syllable word. We pick
a word from our 2 syllable word list.
-
Finally, we return the two chosen words
A similar technique can be used breaking things down into word
types (noun, verb, adjective, etc) to generate simple sentences.
You could even go further to have helpers that generated
"subjects" (either a noun or an adjective and a noun, or several
adjectives and a noun, etc). This technique is a simplification
of a general idea known as a "context-free grammar", and has a
strong influence in computational theory as well as modern
linguistics. Specs
In this assignment, you will create
a program that generates random sentences or poems given a
pattern (grammar) of your creation. You will write a series of
helper functions that return values of type string, many
of which will want to call helpers of their own.
Your program must have no fewer than 7 such helpers with an
average of 4 possibilities in each helper. Your subject matter
can be whatever you like. Bonus credit is available for those
that have many rules and produce more natural-seeming output.
A simple example follows:
string bobsName()
{
if (rand() % 2 == 0)
return "Bob";
else
return "Mr. Smith";
}
string greeting()
{
int num = rand() % 3;
if (num == 0)
return "Hello";
else if (num == 1)
return "Greetings";
else
return "Welcome";
}
string sayHi()
{
if (rand() % 2 == 0)
return greeting();
else
return greeting() + ", " + bobsName();
}
Turn this assignment in as "sentences.cc".
© 2003 UC
Riverside Department of
Computer Science & Engineering. All rights
reserved.