我的目标是创建一个程序,该程序可以接收文件,读取文件,将其解析为令牌,并以相对和绝对频率吐出索引。我了解如何标记常规字符串,但是不了解如何使用文件。此外,我不知道我的ngram类可以在所有这些地方使用。此外,还有更简单的方法来计算单词吗?我知道我听起来很傻,很抱歉,这是一个愚蠢的问题,但我确实需要帮助。我很迷茫,此代码不再有意义。
这是我所拥有的:
public static void main(String[] args) {
try {
File file = new File("jarg2912.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
stringBuffer.append("\n");
}
fileReader.close();
System.out.println("Index:");
//System.out.println(stringBuffer.toString());
//NgramIterator ngram = new NgramIterator(0, "jarg2912.txt");
StringTokenizer st = new StringTokenizer(fileReader.getEncoding());
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
} catch (IOException e) {
e.printStackTrace();
}
public class NgramIterator implements Iterator<String> {
String[] line;
int position = 0, number;
public NgramIterator(int n, String str) {
this.number = n;
this.line = str.split(" ");
}
@Override
public boolean hasNext() {
return position < line.length - number + 1;
}
@Override
public String next() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = position; i < position + number; i++) {
stringBuilder.append((i > position ? " " : "") + line[i]);
position++;
}
return stringBuilder.toString();
}