[英]Word Frequency Counter - Java
import java.io.EOFException;
public interface ICharacterReader {
char GetNextChar() throws EOFException;
void Dispose();
}
import java.io.EOFException;
import java.util.Random;
public class SimpleCharacterReader implements ICharacterReader {
private int m_Pos = 0;
public static final char lf = '\n';
private String m_Content = "It was the best of times, it was the worst of times," +
lf +
"it was the age of wisdom, it was the age of foolishness," +
lf +
"it was the epoch of belief, it was the epoch of incredulity," +
lf +
"it was the season of Light, it was the season of Darkness," +
lf +
"it was the spring of hope, it was the winter of despair," +
lf +
"we had everything before us, we had nothing before us," +
lf +
"countries it was clearer than crystal to the lords of the State" +
lf +
"preserves of loaves and fishes, that things in general were" +
lf +
"settled for ever";
Random m_Rnd = new Random();
public char GetNextChar() throws EOFException {
if (m_Pos >= m_Content.length()) {
throw new EOFException();
}
return m_Content.charAt(m_Pos++);
}
public void Dispose() {
// do nothing
}
}
基本上,我创建了一个名为 ICharacterReader 的接口,它获取句子中的下一个字符,并在没有更多字符时抛出异常。 在它下面,我创建了一个名为 SimpleCharacterReader 的类,其中包含需要按词频计算的随机句子列表。 但是,现在我正在尝试创建一个单独的类,该类将 ICharacterReader 接口作为参数并简单地返回词频。 我是编程的初学者,所以不太确定在这里做什么,任何简单的建议将不胜感激。
您的任务可以分两部分完成:
char
数据并组合成String
只需使用StringBuilder
并附加char
直到出现异常。
ICharacterReader reader = ...
StringBuilder sb = new StringBuilder();
try{
while (true) {
sb.append(reader.GetNextChar());
}
} catch (EOFException ex) {
}
String stringData = sb.toString();
简单地使用正则表达式拆分单词,然后简单地计算每个单词出现的频率。 您可以使用Stream
API 轻松完成此操作:
Map<String, Long> frequencies = Arrays.stream(stringData.split(" +|\n"))
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.