C Sc 227 28-April Section Activity Probabilistic Text Generation with Orderedmap<K, V>

C Sc 227 28-April Section Activity Probabilistic Text Generation with Orderedmap<K, V>

C Sc 227 28-April Section Activity Probabilistic Text Generation with OrderedMap<K, V>

Here is a class that uses our familiar OrderedMap<K, V> class. It is intended to provide a start to the current Project: RandomWriterWithOrderedMap. 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. Feel free to use this code as a start (also at

// The beginning of the probabalistic text generation

public class RandomWriterWithOrderedMap {

public static void main(String[] args) {

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

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

rw.printRandom(100);

}

private OrderedMap<String, ArrayList<Character> all;

private int seedLength;

private String fileName;

private StringBuilder theText;

private static Random generator;

private String seed;

public RandomWriterWithOrderedMap(String fileName, int seedLength) {

this.fileName = fileName;

this.seedLength = seedLength;

generator = new Random();

makeTheText();

setRandomSeed();

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

}

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);

}

}

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

private void setUpMap()

Example text: "Alice likes icy olives"

Using "Alice likes icy olives", an OrderedMap<K, V> of seed/list mappings would look like the following when viewed "sideways". In this example, the seed has length 2 and the value mapped to each key is an ArrayList<Character> of ALL characters that follow the many seeds in the input file. Here is the tree printed sideways with a height of 5 and size of 17:

// From OrderedMap<K, V>
public String sideways() {
return sideways(root, 0);
}
private String sideways(MapNode t, int level) {
if (t == null)
return"";
else {
String pad = "";
for (int i = 0; i < level; i++)
pad += " ";
return sideways(t.right, level + 1)
+ pad + "'" + t.key + "' ->" + t.value + "\n"
+ sideways(t.left, level + 1);
}
} / 'y ' ->[o]
've' ->[s]
's ' ->[i]
'ol' ->[i]
'li' ->[c, k, v]
'ke' ->[s]
'iv' ->[e]
'ik' ->[e]
'ic' ->[e, y]
'es' ->[ ]
'e ' ->[l]
'cy' ->[ ]
'ce' ->[ ]
'Al' ->[i]
' o' ->[l]
' l' ->[i]
' i' ->[c]

Sample output (does it look like a bit like the original text?):

cy olikes ice lice lives ice likes icy olice lice

lives icy olives ice lice lives icy olives icy oli

As a section, altogether now

1) Build an OrderedMap from the root down of the following text when the seedLength is 3. Do not print sideways, but keep the keys/value mappings in order by key in a BST. The first two seeds and their first followers are given. Remember, the Map is built first and only once to store the list of all followers (take more memory, but runs much faster).

the true try the truths

"the" [ ' '

"he " [ 't'

void printRandom(int n)

2) Walk through generating 8 (or fewer if no time) probabilistic characters of code with an initial seed of "e t"