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.
*
*
*/
public class MarkovModel {
// Map of <kgram, chars following> pairs that stores the Markov model.
private HashMap<String, String> model;
private String kgram;
private int size;
private String charAft;
private Random kgramChooser;
private Random charChooser;
private String firstKgram;
// 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) {
if(K < 0 || sourceText.length() < K) {
throw new IOException();
}
for (int i = 0; i < sourceText.length() - K; i++) {
String kgram = sourceText.substring(i, i + K) + "";
String charAfter = model.get(kgram);
if (charAfter == null) {
charAfter = "";
}
char charAft;
if (i < sourceText.length() - K) {
charAft = sourceText.charAt(i + K);
}
model.put(kgram, charAfter + charAft);
}
int size = model.size();
String[] kgram = model.keySet().toArray(new String[0]);
String firstKgram = sourceText.substring(0, K);
Random kgramChooser = new Random();
Random charChooser = new Random();
}
/** Returns the first kgram found in the source text. */
public String getFirstKgram() {
return firstKgram;
}
/** Returns a kgram chosen at random from the source text. */
public String getRandomKgram() {
return kgram[kgramChooser.nextInt(size)];
}
/**
* Returns the set of kgrams in the source text.
*
* DO NOT CHANGE THIS METHOD.
*
*/
public Set<String> 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) {
String charAfter = model.get(kgram);
if (charAfter == null) {
return '\u0000';
}
return charAfter.charAt(charChooser.nextInt(charAft.length()));
}
/**
* 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();
}
}
I'm truly an amateur at coding so please excuse the terminology that I may use but I'm getting the error listed below. The above code is from an assignment that I've been working on but why is this error appearing if kgram is just an array of strings? What would also be the best way to go about instantiating a MarkovModel in a separate file? Please don't post just an answer I need to understand why as well.
MarkovModel.java:104: error: array required, but String found return
return kgram[kgramChooser.nextInt(size)];
kgram. This is very confusing.kgram. this throws the error. it should work if you use unique names for each variable