统计词频java_[Java教程]词频统计

本文介绍了一个使用Java进行词频统计的示例,通过HashMap存储单词及其出现次数,并使用Comparator进行排序。代码包括对输入句子的处理、统计单词计数以及按词频降序输出。
摘要由CSDN通过智能技术生成

[Java教程]词频统计

0 2016-09-04 19:00:04 执行代码 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 map=new HashMap();//用于统计各个单词的个数,排序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 map){32     //声明集合folder,存放单词和单词个数33         List> folder = new ArrayList>(map.entrySet());34         Collections.sort(folder, new Comparator>() {35             public int compare(Map.Entry obj1, Map.Entry obj2) {36             return (obj2.getValue() - obj1.getValue());37            }38         });39     //输出40     for (int i = 0; i < folder.size(); i++) {41         Entry en = folder.get(i);42         System.out.println(en.getKey()+":"+en.getValue());43       }44   }45 }

本文网址:http://www.shaoqun.com/a/249684.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值