CS 216 - Data Structures
Homework #5


Due: Thursday 10/23 at the beginning of class

NOTE: This work is to be done in groups of 2-3.

Hash Tables: the Markov Word Processor

(Mind you, this word processor will process words as a food processor processes food!)

1.  Implement a chained hash table using Java generics according to the ChainedHashTable interface.  You may, but are not required to, make use of your linked list implementation.

2.  Using your hash table, implement a word-level order-1 Markov text generator according to section 15.3 of Programming Pearls, 2nd ed. by Jon Bentley, and chapter 3 of The Practice of Programming by Brian Kernighan and Rob Pike.  The order-2 Markov chain algorithm is described by Kernighan and Pike as follows:

set w1 and w2 to the first two words in the text
print w1 and w2
loop:
    randomly choose w3, one of the successors of prefix w1 w2 in the text
    print w3
    replace w1 and w2 by w2 and w3
    repeat loop

You may implement this order-2 algorithm if you wish, but the order-1 algorithm is given as follows:

set w1 to the first word in the text
print w1
loop:
    randomly choose w2, one of the successors of prefix w1 in the text
    print w2
    replace w1 by w2
    repeat loop

To implement this algorithm simply, let "word" here be interpreted as "token".  Read all words of a text from the standard input and build a hash table that associates strings with lists of strings.  For an order-1 implementation, each word is associated with a list of words that follow it in the text (including repeats).  For an order-2 implementation, a string with a space-separated pair of words is associated with a list of words that follow them in the text. (including repeats).  Once this table is built, it may be used to look up a list of successors for the random generation.

For example, consider the possible output (1, 2, 3) for Alice in Wonderland