java程序查找单词的分布数组

标签 java word-frequency

我想查找文件每一行中某个单词的出现频率。我想对文件中的每个单词执行此操作。我在 java 中使用 BufferedReader 和 FileReader。

最佳答案

我推荐两件事:

1)更好地分解你的问题。 您是否想找出每行中单词的频率?或者文件中单词的出现频率。例如,如果您的输入是:

test test dog cat dog dog cat
test cat dog dog cat test

你想要吗:

test: 2, 1
dog: 3, 2
cat: 2, 2

或者你想要

test: 3
dog: 5
cat: 4

2) 这是您需要的工具

Map<String,Integer> wordCounts; // store your word counts in your class

/**
 * countWord- a method to signify counting a word
 * @param word the word just counted
 */   
public void countWord(String word) {
    int counts = 0; // assume it does not exist yet
    if(wordCounts.containsKey(word)) { // but if it does, get that count
        counts = wordCounts.get(word);
    } /* end if(contains(word))*/
    wordCounts.put(word,counts + 1); // tell the map to put one more than there was
} /* end countWord */

关于java程序查找单词的分布数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5043508/

相关文章:

java - 使用jpa规范时如何在排序中应用合并表达式

java - 类泛型的方法泛型绑定(bind)限制

java - 可以数组。 sort 对已添加到数组中的枚举进行排序。?

objective-c - 支持1词频的标签云算法

java - 字符串频率搜索未找到所有单词

java - 有没有办法禁止将类用作 spring bean?

java - 如何使用 servlet 4.0 配置 taglib

java - vector 空间是多少?

algorithm - 在大量文本中查找最常见短语的高效算法

mysql - group_concat 以及该记录中每个单词的频率