Java Markov Chain Text Generater Prefix/Suffix class Map of prefixes to suffix p
ID: 3607109 • Letter: J
Question
Java Markov Chain Text Generater Prefix/Suffix class
Map of prefixes to suffix probabilities. This map will be filled based on the sequence given to the addItems method. Remember, addItems may be called more than once.
Create private nested classes for the prefixes and suffix probability objects. It is up to you whether they should be static or non-static classes:
– Prefix key class – A simple implementation would wrap a list of Strings. Since this going to be used as a Map key, you’ll need to override the equals and hashCode methods, at least.
– Suffix probabilities class – A simple implementation might just wrap a list of Strings, with more common values appearing more often in the list. This does have the disadvantage of taking up more space than a fancier implementation.
A more complicated implementation might wrap a map of unique suffix strings to frequency or probability values. This implementation would save space, but would be trickier to implement correctly.
However you choose to implement this, you should have a way of taking a random number generator and selecting a suffix, with more common frequencies being more likely.
Write the classes:
Explanation / Answer
public class MarkovChain { public static void main(String[] args) { // the state transition matrix double[][] transition = { { 0.386, 0.147, 0.202, 0.062, 0.140, 0.047, 0.016}, { 0.107, 0.267, 0.227, 0.120, 0.207, 0.052, 0.020}, { 0.035, 0.101, 0.188, 0.191, 0.357, 0.067, 0.061}, { 0.021, 0.039, 0.112, 0.212, 0.431, 0.124, 0.061}, { 0.009, 0.024, 0.075, 0.123, 0.473, 0.171, 0.125}, { 0.000, 0.103, 0.041, 0.088, 0.391, 0.312, 0.155}, { 0.000, 0.008, 0.036, 0.083, 0.364, 0.235, 0.274} }; int N = 7; // number of states int state = N - 1; // current state int steps = 0; // number of transitions // run Markov chain while (state > 0) { StdOut.println(state); steps++; double r = Math.random(); double sum = 0.0; // determine next state for (int j = 0; jRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.