java - 计算两个文件中重复出现的单词数

标签 java word-count

我有一个代码,可以计算文件中单词的出现次数。我想将其与 2 个文件一起使用,并在单独的表中显示经常出现的(两个文件都包含)单词。你的想法是什么?如何将它与 2 个文件一起使用?

    while ((inputLine = bufferedReader.readLine()) != null) {
        String[] words = inputLine.split("[ \n\t\r.,;:!?(){}]");

        for (int counter = 0; counter < words.length; counter++) {
            String key = words[counter].toLowerCase();
            if (key.length() > 0) {
                if (crunchifyMap.get(key) == null) {
                    crunchifyMap.put(key, 1);
                } else {
                    int value = crunchifyMap.get(key).intValue();
                    value++;
                    crunchifyMap.put(key, value);
                }
            }
        }
    }
    Set<Map.Entry<String, Integer>> entrySet = crunchifyMap.entrySet();
    System.out.println("Words" + "\t\t" + "# of Occurances");
    for (Map.Entry<String, Integer> entry : entrySet) {
        System.out.println(entry.getKey() + "\t\t" + entry.getValue());
    }

最佳答案

您可能应该使用以下(非常粗略的)算法:

  1. 读取第一个文件并将所有单词存储在 Set words 中;
  2. 读取第二个文件并将所有单词存储在 Set words2 中;
  3. 通过保留words中的所有单词来计算相交集也包含在 words2 中: words.retainAll(words2)
  4. words包含您的最终列表。

请注意,如果将文件读取算法放入类似的方法中,则可以重用该算法

public Set<String> readWords(Reader reader) {
    ....
}

计算出现频率

如果您还想知道发生的频率,您应该将每个文件读入 Map<String, Integer>它将每个单词映射到它在该文件中出现的频率。

新的 Map.merge(...) 函数(自 Java 8 起)简化了计数:

Map<String, Integer> freq = new HashMap<>();
for(String word : words) {
    // insert 1 or increment already mapped value
    freq.merge(word, 1, Integer::sum);
}

然后应用以下稍作修改的算法:

  1. 读取第一个文件并将所有单词存储在 Map wordsFreq1 中;
  2. 读取第二个文件并将所有单词存储在 Map wordsFreq2 中;
  3. 从第一张 map 中提取单词:Set<String> words = wordsFreq1.keySet()
  4. 通过保留第二张 map 中的所有单词来计算交集:words.retainAll(wordsFreq2.keySet())
  5. 现在words包含所有共同的单词,并且 wordsFreq1wordsFreq2两个文件中所有单词的频率。

通过这三种数据结构,你可以轻松获得你想要的所有信息。示例:

    Map<String, Integer> wordsFreq1 = ... // read from file
    Map<String, Integer> wordsFreq2 = ... // read from file

    Set<String> commonWords = new HashSet<>(wordsFreq1.keySet());
    commonWords.retainAll(wordsFreq2.keySet());

    // Map that contains the summarized frequencies of all words
    Map<String, Integer> allWordsTotalFreq = new HashMap<>(wordsFreq1);
    wordsFreq2.forEach((word, freq) -> allWordsTotalFreq.merge(word, freq, Integer::sum));

    // Map that contains the summarized frequencies of words in common
    Map<String, Integer> commonWordsTotalFreq = new HashMap<>(allWordsTotalFreq);
    commonWordsTotalFreq.keySet().retainAll(commonWords);

    // List of common words sorted by frequency:
    List<String> list = new ArrayList<>(commonWords);
    Collections.sort(list, Comparator.comparingInt(commonWordsTotalFreq::get).reversed());

关于java - 计算两个文件中重复出现的单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087362/

相关文章:

java - spring boot swagger中手动指定POST请求体

java - 每当我运行 android 登录应用程序时,它都会在日志中崩溃一系列错误消息

java - MapBox Android SDK : How to download offline map during app installation?

r - 计算字符串中所有单词的数量

java - Hadoop Java 字数统计调整不起作用 - 尝试对所有内容进行求和

hadoop - 这个模型是否适合 hadoop?

java - 无法编译 WordCount.java

java - Mac OSX - IllegalStateException : The driver is not executable:

java - 如何将具有多种类型的中型数据集建模为一个类

hadoop - 在 cassandra 中运行 hadoop wordcount 示例时出现异常