Question
You are provided with two Java files that you must use to develop your solution: MarkovModel.java and TextGenerator.java . The constructors of MarkovModel build the
You are provided with two Java files that you must use to develop your solution:
MarkovModel.java
and
TextGenerator.java
.
The constructors of
MarkovModel
build the order-k model of the source text. You are required to
represent the model with the provided HashMap field.
The main method of
TextGenerator
must process the following three command line arguments (in the
args
array):
A non-negative integer
k
A non-negative integer
length
.
The name of an input file
source
that contains more than
k
characters.
Your program must validate the command line arguments by making sure that
k
and
length
are non-
negative and that
source
contains at least
k
characters and can be opened for reading. If any of the
command line arguments are invalid, your program must write an informative error message to
System.out
and terminate. If there are not enough command line arguments, your program must write an informative
error message to
System.out
and terminate.
With valid command line arguments, your program must use the methods of the
MarkovModel
class
to create an order
k
Markov model of the sample text, select the initial kgram, and make each character
selection. You must implement the
MarkovModel
methods according to description of the Markov modeling
process in the section above.
A few sample texts have been provided, but Project Gutenberg (
http://www.gutenberg.org
) maintains
a large collection of public domain literary works that you can use as source texts for fun and practice.
import java.io.File; import java.io.IOException;
/** * TextGenerator.java. Creates an order K Markov model of the supplied source * text, and then outputs M characters generated according to the model. * * @author Taylor Watson (tcw0026@auburn.edu) * @author Dean Hendrix (dh@auburn.edu) * @version 2018-04-17 * */ public class TextGenerator {
/** Drives execution. */ public static void main(String[] args) { if (args.length < 3) { System.out.println("Usage: java TextGenerator k length input"); return; } // No error checking! You may want some, but it's not necessary. int K = Integer.parseInt(args[0]); int M = Integer.parseInt(args[1]); if ((K < 0) || (M < 0)) { System.out.println("Error: Both K and M must be non-negative."); return; } File text; try { text = new File(args[2]); if (!text.canRead()) { throw new Exception(); } } catch (Exception e) { System.out.println("Error: Could not open " + args[2] + "."); return; } // instantiate a MarkovModel with the supplied parameters and // generate sample output text ... } }
import java.io.File; import java.util.HashMap; import java.io.IOException; import java.util.Random; import java.util.Scanner; import java.util.Set;
/** * MarkovModel.java Creates an order K Markov model of the supplied source * text. The value of K determines the size of the "kgrams" used to generate * the model. A kgram is a sequence of k consecutive characters in the source * text. * * @author Taylor Watson (tcw0026@auburn.edu) * @author Dean Hendrix (dh@auburn.edu) * @version 2018-04-17 * */ public class MarkovModel {
// Map of
// add other fields as you need them ...
/** * Reads the contents of the file sourceText into a string, then calls * buildModel to construct the order K model. * * DO NOT CHANGE THIS CONSTRUCTOR. * */ public MarkovModel(int K, File sourceText) { model = new HashMap<>(); try { String text = new Scanner(sourceText).useDelimiter("\\Z").next(); buildModel(K, text); } catch (IOException e) { System.out.println("Error loading source text: " + e); } }
/** * Calls buildModel to construct the order K model of the string sourceText. * * DO NOT CHANGE THIS CONSTRUCTOR. * */ public MarkovModel(int K, String sourceText) { model = new HashMap<>(); buildModel(K, sourceText); }
/** * Builds an order K Markov model of the string sourceText. */ private void buildModel(int K, String sourceText) { }
/** Returns the first kgram found in the source text. */ public String getFirstKgram() { return null; }
/** Returns a kgram chosen at random from the source text. */ public String getRandomKgram() { return null; }
/** * Returns the set of kgrams in the source text. * * DO NOT CHANGE THIS METHOD. * */ public Set
/** * Returns a single character that follows the given kgram in the source * text. This method selects the character according to the probability * distribution of all characters that follow the given kgram in the source * text. */ public char getNextChar(String kgram) { return '\u0000'; }
/** * Returns a string representation of the model. * This is not part of the provided shell for the assignment. * * DO NOT CHANGE THIS METHOD. * */ @Override public String toString() { return model.toString(); }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started