Probabilistic Text Generation with HashMap<K, V> and java.util.ArrayList<E>

Begin with this Eclipse project: RandomWriterStart.zip

You are to implement class RandomWriterWithHashMap.java that provides a random writing application using java.util.HashMap to store all possible seeds with its list of all possible following characters.This file must be turned into D2L dropbox RandomWrite. While testing your code, use any very small input file you want or a large one from Project Gutenberg. You must use java.util.HashMap<K, V> class, java.util.ArrayList, and the following algorithm.

  1. Done: Read all file input into one big string (already done in RandomWriterWithHashMap.java, actually a StringBuilder object to save time--StringBuilder has all the methods of String plus a fast append method)
  2. You do this in method setUpMap(): Initializethe HashMapinstance variable all to have all possible seeds of the given length as the key and an ArrayList<Character> as the value. These ArrayLists must contain all single characters that follow each key in the given text input.
  3. Done: Pick a random seed from the original text. This is known as seed
  4. You do this in method printRandom(int): For each character you need to print

Get the list of all the characters that follow the seed (the instance variable seed is set for you initially) from all (the HashMap instance variable you built in setUpMap.

Randomly select one of the characters that list of followers

Print that random character

Change the seed so the first character is gone and the just printed random character is appended (seed must be the same length at the end)

Here is a sample HashMap built in setUpMap (stop at the 2nd occurrence of "ic"):

Example text: "Alice likes icy olives"

Using "Alice likes icy olives", create a HashMap<K, V> of seed/list mappings where the seed is a string of length 2 and the value mapped to each key is a list of ALL characters that follow that seed in the original text.

Seed / Value: ArrayList<Character>()
"Al" / ['i']
"li" / ['c', 'k', 'v'] 'k' and 'v' added later
"ic" / ['e', 'y']
"ce" / [' ']
"e " / ['l']
" l" / ['i']
"li" / "li" is already a key, add follower 'k' to the list already mapped to the key "li"
"ik" / ['e']
"ke" / ['s']
"es" / [' '] This ArrayList has two elements: a space ' '. Another ' ' will be added
"s " / [i]
" i" / [c]
"ic" / "ic" is already a key, add follower 'k' to the list already mapped to the key "ic"
"cy" / [' ']
"y " / ['o']
" o" / ['l']
"ol" / ['o']
"li" / "li" is already a key, add follower 'v' to the list already mapped to the key "li"
"ve" / ['s'] Stop one possible seed short to avoid a StrinvIndexOutOfBoundException

Warmup Activity: Probabilistic Text Generation with HashMap<K, V>

Here is a class that uses our familiar HashMap<K, V> class. It is intended to provide a start to the second part of the Project: RandomWriterWithHashMap. Method makeTheText reads all of the text from an input file into one big StringBuilder object. The StringBuilder class has the methods of the String class with a very efficient append method that we recommend you use. It also has code to get a random seed initially setRandomSeed

// The beginning of the probabalistic text generation

public class RandomWriterWithHashMap {

public static void main(String[] args) {

// Assume there is a file named alice with the text: "Alice likes icy olives"

RandomWriterWithHashMap rw = new RandomWriterWithHashMap("alice", 2);

rw.printRandom(100);

}

private HashMap<String, ArrayList<Character> all;

private int seedLength;

private String fileName;

private StringBuilder theText;

private static Random generator;

private String seed;

public RandomWriterWithHashMap(String fileName, int seedLength) {

this.fileName = fileName;

this.seedLength = seedLength;

generator = new Random();

makeTheText();

setRandomSeed();

setUpMap(); // Algorithm to be considered during class

}

private void makeTheText() {

Scanner inFile = null;

try {

inFile = new Scanner(new File(fileName));

}

catch (FileNotFoundException e) {

e.printStackTrace();

}

theText = new StringBuilder();

while (inFile.hasNextLine()) {

theText = theText.append(inFile.nextLine().trim());

theText = theText.append(' ');

}

}

public void setRandomSeed() {

generator = new Random();

int start = generator.nextInt(theText.length() - seedLength);

seed = theText.substring(start, start + seedLength);

}

// Read theText char by char to build a OrderedMaps where every possible seed exists with the

// list of followers. You need these instance variables here

// seedLength, theText, all

privatevoid setUpMap() {

// TODO Implement this method

// See Part B screencast for algorithm and sample output

}

// Print chars random characters. Please insert line breaks to make your

// output readable to the poor grader :-)

void printRandom(int chars) {

// TODO Implement this method

// See Part B screencast for algorithm and sample output

}

}

Consider algorithms for these two methods that use the put and get methods of HashMap

privatevoid setUpMap()

1) Build a HashMap from the following text when the seedLength is 3. The first two seeds and their first followers are given. Remember, the HashMap is built first and only once to store the list of all followers (takes more memory, but runs faster). Sample text: "the true try the truths"

Seed / Value: ArrayList<Character>()
"the" / [' '
"he " / ['t'

void printRandom(int chars)

Walk through generating 8 probabilistic characters of code with an initial seed of "e t"

Grading Criteria 50 pts max

___/ +50 Generates text that is gets closer to the original as the seed increases (subjective). For example,
when seed length = 2, a few words may appear; but when 12, some sentences appear close to the original text

-50 If no text is generated with a printRandom(400) message

-40 If you did not use a HashMap<K, V> and the algorithm presented in the project that uses a Map to

set up all seeds and the list of followers for each

-40 If text has no apparent difference with different seed lengths or file input