[英]Word count function that counts words in a txt file
我对Java很陌生,所以请多多包涵。
我目前正在尝试创建执行以下操作的代码:
将代码添加到您的processFile
函数中,以计算每个单词在文件中出现的次数。
将代码添加到您的processFile
函数中, processFile
函数循环遍历HashMap
以查找最常用的单词。 循环后,为奖金要求#1添加的变量应包含最常用单词的值。
到目前为止,我已经提出了这个建议,并且想知道是否有人可以帮助我进一步发展。
Map<String, Integer> freq = new Hashmap<String, Integer>();
FileInputStream fi = new FileInputStream("readwords,txt");
Scanner input = new Scanner(fi);
while (input.hasNext()) {
String word = input.next().toLowerCase();
Integer f = freq.get(word);
if (f == null) {
freq.put(word,1);
}
else {
freq.put(word,f+1);
}
}
谢谢
您的语法接近,但是您混合使用了String
声明样式,您的泛型类型缺少一个>
并且您的变量名称不一致。 我想你想要类似的东西,
Map<String, Integer> map = new HashMap<>();
File file = new File("readwords.txt");
try (Scanner input = new Scanner(file)) {
while (input.hasNext()) {
String word = input.next().toLowerCase();
Integer f = map.get(word);
if (f == null) {
map.put(word, 1);
} else {
map.put(word, f + 1);
}
}
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
要计算单词数并获取最常用的单词,您可以尝试以下操作:
public void processFile() throws Exception {
Map<String, Integer> freq = new HashMap<>();
FileInputStream fi = new FileInputStream("readwords.txt");
String mostFreqWord = null;
Integer highestFreq = 0;
Scanner input = new Scanner(fi);
while (input.hasNext()) {
String word = input.next().toLowerCase();
Integer f = freq.get(word) == null ? 1 : freq.get(word) + 1;
freq.put(word, f);
if(f > highestFreq) {
mostFreqWord = word; // set most frequent word
highestFreq = f; // frequency of most frequent word
}
}
System.out.println("Word :" + mostFreqWord
+ " is the most frequent word with frequency:" + highestFreq);
}
因为我已经修改了您已经发布的代码,所以这里是我所做的修改的说明(我假设您已经知道您的原始代码在做什么)。
在循环内部,下面的行检查单词word
是否在循环中第一次遇到,如果是,则将其频率设置为1,否则将增加该单词的频率。
Integer f = freq.get(word) == null ? 1 : freq.get(word) + 1;
然后,它设置单词的最新频率: freq.put(word, f);
语句if(f > highestFreq)
检查最高频率是否仍然最高,如果不是,则更新highestFreq
和mostFreqWord
字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.