使用 JAVA 计算单词出现的次数

Count how many times a word appeared using JAVA

分词然后统计单词出现的次数

示例:'the quick brown fox quick'

预期输出:

- 1 快 - 2 棕色 - 1 狐狸 - 1

public class Tokenizer
{
  public static void main(String[] args)
  {
    int index = 0; int tokenCount;
    String words[] = new String [50];
    String message="The Quick brown fox the";

    StringTokenizer string = new StringTokenizer(message);

    tokenCount = string.countTokens();
    System.out.println("Number of tokens = " + tokenCount);
    while (string.hasMoreTokens()) 
        { words[index] = string.nextToken(); index++; }
    for (index=0;index<tokenCount; index++)
        { System.out.println(words[index]); }
  }
}

这里需要用一个java.util.Map来维护单词和对应的计数:

import java.util.Map;
import java.util.HashMap;
public class Tokenizer
{
  public static void main(String[] args)
  {
    int index = 0; int tokenCount;
    Map<String,Integer> wordCount = new HashMap<String,Integer>();
    String message="The Quick brown fox the";

    StringTokenizer string = new StringTokenizer(message);

    tokenCount = string.countTokens();
    System.out.println("Number of tokens = " + tokenCount);
    while (string.hasMoreTokens()) { 
          String word = string.nextToken();
          Integer count = wordCount.get(word);
          if(count == null) { //this means the word was encountered the first time
          wordCount.put(word, 1);
        }
        else { //word was already encountered we need to increment the count
          wordCount.put(word, count + 1);
        }
     }
    for (String words : wordCount.keySet())
        { System.out.println("Word : " +  word + " has count :" +wordCount.get(word); 
    }
  }
}

您可以为此使用 Map

HashMap<String, Integer> wordMap = new HashMap<>(); //here String key will be the word and integer value will be the word count
 while (string.hasMoreTokens()) 
 {

    String word= string.nextToken(); 
    if(wordCount.get(word) == null){ //if string is not present in map, add this occurence with wordCount as 1
        wordMap.put(word,1);
    }
    else{   //if string is already present in map, increment the wordCount by 1
        int count = wordCount.get(word);
        wordwordCount.put(word, count+1);
    }
}
//now traverse the hashmap and print the word along with its count

希望对您有所帮助!

祝你好运!