OrderedMapRandomWriter.doc

advertisement
Project: OrderedMap<K extends Comparable<K>, V> and Random writing
Collaboration Complete this by yourself. You may get help from Section Leaders and Rick.
Add these three methods to the OrderedMap class.
public boolean put(K key, V value) Add a mapping to the BST
public V get(K key, V value) Get the value mapped to the give key
public boolean containsKey(K key) Return true if key exists in the
OrderedMap object, or false if it doesn't
First get OrderedMap.java and OrderedMapTest.java into an Eclipse project
2) Probabilistic Text Generation with OrderedMap<K, V>
You are to implement class RandomWriterWithOrderedMap.java that provides a random writing application sing a
different collection to store the seeds and list of followers. Email class RandomWriterWithOrderedMap.java to your
section leader by 21:00 4-May While testing your code, use any input file you want from Project Gutenberg. Generate
Probabilistic text using java.util.OrderedMap and java.util.ArrayList with the following algorithm.
1. Read all file input into one big string (already done in RandomWriterWithOrderedMap.java)
2. Create a OrderedMap object that has all possible seeds of the given length as the key and an ArrayList of
followers as the value. (You do this)
3. Pick a random seed from the original text. (already done in RandomWriterWithOrderedMap.java)
4. For each character you need to print (You do this)
 Randomly select one of the characters in the list of followers
 Print that random character
 Change the seed so the first character is gone and the just printed random character is appended
Example text: "Alice likes icy olives"
Using "Alice likes icy olives", create a OrderedMap<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
list toString()
"Al"
[i]
"li"
[c, k ] // 'v' to be added
"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"
[ ]
"s "
[i]
" i"
[c]
"ic"
"ic" is already a key, add follower 'k' to the list already mapped to the key "ic"
...
...
This ArrayList has one element (that you cannot see): a space ' '
This ArrayList has one element: a space ' '. Another ' ' should be added later
8 more possible seeds to map
Warmup 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 second part of the
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 http://www.cs.arizona.edu/people/mercer/Projects/RandomWriterWithOrderedMap.java
// 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
private
private
private
private
private
OrderedMap<String, ArrayList<Character>> all;
int seedLength;
String fileName;
StringBuilder theText;
static Random generator;
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
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 probabilistic characters of code with an initial seed of "e t"
This is somehow the text box from page 1, ignore it (but don't delete it)
Download