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();
}
}
我真的是一个编码业余爱好者,请原谅我可能会使用的术语,但我遇到下面列出的错误。上面的代码来自我一直在处理的任务,但为什么当kgram只是一个字符串数组时出现这个错误?在单独的文件中实例化MarkovModel的最好方法是什么?请不要张贴我只需要理解为什么的答案。了解为什么“需要阵列,但发现字符串”出现错误
MarkovModel.java:104: error: array required, but String found return
return kgram[kgramChooser.nextInt(size)];
您至少有三个变量(两个局部变量和一个属性),它们具有不同的类型,但名为'kgram'。这很混乱。作为@macmoonshine的 – clemens
已经说过你有几个变量叫'kgram'。这会抛出错误。它应该工作,如果你使用每个变量的唯一名称 – XtremeBaumer
你可以把行号码 –