Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a markov model: You are provided with two Java files that you must use to develop your solution: MarkovModel.java and TextGenerator.java. The constructors of

Create a markov model:

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 nonnegative and that source contains more than 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.

Provided resources:

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. * * @version 2016-04-19 * */ public class MarkovModel { // Map of  pairs that stores the Markov model. private HashMap model; // 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 getAllKgrams() { return model.keySet(); } /** * 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(); } } 
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. * * @version 2016-04-19 * */ 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! 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 ... } } 
 
** * HandoutExamples.java * Generates examples from the assignment handout. * * @version 2016-04-20 * */ public class HandoutExamples { /** Drives execution. */ public static void main(String[] args) { String sourceText = "agggcagcgggcg"; int k = 2; MarkovModel model = new MarkovModel(k, sourceText); System.out.println("k = " + k + ", source text: " + sourceText); System.out.println("The first kgram: " + model.getFirstKgram()); System.out.println("A random kgram: " + model.getRandomKgram()); System.out.println("All kgrams: " + model.getAllKgrams()); System.out.println("The Markov model: "); System.out.println(model); } } /* RUNTIME OUTPUT: k = 2, source text: agggcagcgggcg The first kgram: ag A random kgram: cg All kgrams: [gg, cg, ag, gc, ca] The Markov model: {gg=gcgc, cg=g, ag=gc, gc=agg, ca=g} */ 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

1 . Television News channels importantance of our Life pattern ?

Answered: 1 week ago