在java中获取句子中最常用的两个单词

时间:2015-11-17 06:27:54

标签: java dictionary count

如果在计算所有单词的出现总数之后,如何在句子中获取两个最常用的单词,它还应显示两个最常用的单词

import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.util.Map;
import java.util.HashMap;

public class Tokenizer

{
  public static void main(String[] args)
  {
    int index = 0; int tokenCount; int i =0;
    Map<String,Integer> wordCount = new HashMap<String,Integer>();
    Map<Integer,Integer> letterCount = new HashMap<Integer,Integer>();
    String message="The Quick brown fox jumps over the lazy brown dog";

    StringTokenizer string = new StringTokenizer(message);


    tokenCount = string.countTokens();
    System.out.println("Number of tokens = " + tokenCount);
    while (string.hasMoreTokens()) { 
          String word = string.nextToken().toLowerCase();
          Integer count = wordCount.get(word);
          Integer lettercount = letterCount.get(word);

          if(count == null) { 
          wordCount.put(word, 1);
        }
        else { 
          wordCount.put(word, count + 1);
        }
     }
    for (String words : wordCount.keySet())
        {System.out.println("Word : " +  words + " has count :" +wordCount.get(words));

    }
 }

2 个答案:

答案 0 :(得分:1)

彻底浏览HashMap,然后跟踪最高计数。

int first, second;

first = second = Integer.MIN_VALUE;

String firstWord, secondWord;

for (Map.Entry<String, Integer> entry : map.entrySet())
{

   int count = entry.getValue();
   String word = entry.getKey();

    if (count > first)
    {
       second = first;
       secondWord = firstWord;
       first = count;
       firstWord = word;
    }
    else if (count > second && count != first)
    {
      second = count;
      secondWord = word;
    }
}

System.out.println(firstWord + " " + first);
System.out.println(secondWord + " " + second);

答案 1 :(得分:0)

您需要遍历地图的条目集。

这将返回包含键值和最大值的输入对象。

Map.Entry<String, Integer> max = null;

for (Map.Entry<String, Integer> entry : map.entrySet())
{
    if (max == null || entry.getValue().compareTo(max .getValue()) > 0)
    {
        max = entry;
    }
}

对于第二个最常用的单词,我会说你可以删除最大单词,然后再用这种方式,你可以检索第二个单词。