15-111/200 Final Exam
Random Sentence Generation

Problem Statement (Including Input and Output)

First, read a file of words one line at a time, building a Map whose key is the first word on the line and whose value is a List of all subsequent words on that line: from each category word to a List of all its exemplar words. All words are separated by one semi-colon character. For example, the input file substitutions.txt contains the following lines:
  name;Fred;Bob;Mary
  verb;throws;eats;chases;breaks
  article;a;the;this
  adj;big;red;small;blue
  do;wagon;ball;hot-dog
Here, for example, the category word verb is associated with the List of exemplar words throws, eats, chases, and breaks.

Second, print a sorted version of this Map: the category words must be printed in alphabetical order, one per line, each followed -on the same line- by its List of exemplar words, which must also printed in alphabetical order. For the input file shown above, the program should print the following lines:

  Map (sorted alphabetically by Category and by Exemplars)
    adj can be replaced by [big, blue, red, small]
    article can be replaced by [a, the, this]
    do can be replaced by [ball, hot-dog, wagon]
    name can be replaced by [Bob, Fred, Mary]
    verb can be replaced by [breaks, chases, eats, throws]

If you finish this part correctly, you will at least be awarded 4 points of the total 8 points that this entire problem is worth.

Third, read a line of category words from a file and build a sentence (store it in a List) by selecting one random exemplar word for each category word that is read. All words are separated by one semi-colon character. For example, the input file sentence.txt contains the following line:

  name;verb;article;adj;do
It indicates a name is to be followed by a verb, which is followed by an article, etc.

Finally, print the generated sentence that was stored in the List of exemplar words. For example, with these input files the program might print the following line:

  [Fred, chases, this, big, ball]
Of course, other sentences are just as likely to occur. Running the program more than once should produce different results (try it), but always using appropriate exemplar words selected from these five category words. Note that all the input files will be perfect: the right size, with no misspellings, etc.

Sample Interaction

The program, as specified, will have the following interaction: user-typed information appears in italics. Your output should "match" this one exactly. Notice that the category words are sorted, as are the exemplars in each List.
  Enter Category Word file name: substitutions.txt
  Map (sorted alphabetically by Category then Word)
    adj can be replaced by [big, blue, red, small]
    article can be replaced by [a, the, this]
    do can be replaced by [ball, hot-dog, wagon]
    name can be replaced by [Bob, Fred, Mary]
    verb can be replaced by [breaks, chases, eats, throws]

  Enter sentence generation file name: sentence.txt

  Generated Sentence using the pattern: name;verb;article;adj;do
    [Fred, chases, this, big, ball]
Of course, other sentences are just as likely to occur. Running the program more than once should produce different results (try it), but always using exemplar words selected from these five category words.