(Based on an example in Brian W. Kernighan and Rob Pike, The Practice of Programming, Addison-Wesley, 1999) We want to generate "random text" in the style of another author. Your first task is to...


(Based on an example in Brian W. Kernighan and Rob Pike, The Practice of Programming, Addison-Wesley, 1999) We want to generate "random text" in the style of another author. Your first task is to collect a group of prefix strings of two words that occur in a text file and associate them with a list of suffiX strings using a Map. For example, the text for Charles Dickens' A Christmas Carol contains the four phrases:


The prefix string "Marley was" would be associated with the Arrayl i st containing the four suffix strings "dead : ", "as", "as", "dead . ". You must go through the text and examine each successive pair of two-word strings to see whether that pair is already in the map as a key. H so, add the next word to the Arrayl i st that is the value for that prefix string. For example, in examining the first two sentences shown, you would first add to the entry ("Marley was", Arrayli st "dead : "). Next you would add the entry ("was dead", Arrayl i st "as"). Next you would add the entry ("dead as", Arrayl i st "a"), and so on. When you retrieve the prefix "Marley was" again, you would modify the Arrayl i st that is its value, and the entry would become ("Marley was", Arrayli st "dead : ", "as"). When you are all finished, add the entry "THLEND" to the suffix list for the last prefix placed in the Map.


Once you have scanned the complete text, it is time to use the Map to begin generating new text that is in the same style as the old text. Output the first prefix you placed in the Map : "Marley was". Then retrieve the Arrayli st that is the value for this prefix. Randomly select one of the suffixes and then output the suffix. For example, the output text so far might be "Marley was dead" if the suffix "dead" was selected from the Arrayl i st of suffixes for "Marley was". Now continue with the two-word sequence consisting of the second word from the previous prefix and the suffix (that would be the string "was dead"). Look it up in the map, randomly select one of the suffixes, and output it. Continue this process until the suffix "THE_END" is selected.

Dec 05, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here