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:

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.