你的位置:首页 > Java教程

[Java教程]词频统计


执行代码

1 package zuoye1; 2 import java.util.ArrayList; 3 import java.util.Collections; 4 import java.util.Comparator; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 import java.util.StringTokenizer; 9 import java.util.Map.Entry;10 //词频统计(统计每个单词出现的次数,输出单词并排序)11 public class Word {12    public static void main(String[] args) {13       String sentence = " While sharing a nickel or a quarter may go a long way for them, it is hard to believe the people would simply lie over and die. But to some people in such an unfortunate situation, it is more than simple surrender but another aspect of personal proportions that have led them to lose hope and live the rest of their days in misery."; //将要输入的句子或段落。14       int wordCount=0; //每个单词出现的次数。15       HashMap<String,Integer> map=new HashMap<String,Integer>();//用于统计各个单词的个数,排序16       StringTokenizer token=new StringTokenizer(sentence);//这个类会将字符串分解成一个个的标记17       while(token.hasMoreTokens()){ //循环遍历18         wordCount++;19         String word=token.nextToken(", ?.!:\"\"''\n"); //括号里的字符的含义是说按照,空格 ? . : "" '' \n去分割20         if(map.containsKey(word)){ //HashMap不允许重复的key,所以利用这个特性,去统计单词的个数21             int count=map.get(word);22             map.put(word, count+1); //如果HashMap已有这个单词,则设置它的数量加123         }else25         map.put(word, 1); //如果没有这个单词,则新填入,数量为126         }27           System.out.println("总共单词数:"+wordCount);28           sort(map); //调用排序的方法,排序并输出!29        }30     //排序31     public static void sort(HashMap<String,Integer> map){32     //声明集合folder,存放单词和单词个数33         List<Map.Entry<String, Integer>> folder = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());34         Collections.sort(folder, new Comparator<Map.Entry<String, Integer>>() {35             public int compare(Map.Entry<String, Integer> obj1, Map.Entry<String, Integer> obj2) {36             return (obj2.getValue() - obj1.getValue());37            }38         });39     //输出40     for (int i = 0; i < folder.size(); i++) {41         Entry<String, Integer> en = folder.get(i);42         System.out.println(en.getKey()+":"+en.getValue());43       }44   }45 }