首页 > 代码库 > Java实现的词频统计
Java实现的词频统计
要求:
1.读取文件;
2.记录出现的词汇及出现频率;
3.按照频率降序排列;
4.输出结果。
概要:
1.读取的文件路径是默认的,为了方便调试,将要统计的文章、段落复制到文本中即可;
2.只支持英文;
3.会按照词汇出现的频率降序排列。
实现:
1.使用FileReader、BufferedReader读取文件;
2.采用StringTokenizer进行字符分割;
3.用hashmap保存统计数据;
4.自定义一个类用来实现按value排序;
5.输出结果。
默认路径文件:
1 String filename = "E:/Test.txt";2 3 FileReader fk = new FileReader(filename);4 BufferedReader br = new BufferedReader(fk);
统计词频:
1 String s; 2 while((s = br.readLine()) != null) { 3 file += s; //读出整篇文章,存入String类的file中。 4 } 5 6 StringTokenizer st = new StringTokenizer(file," ,.!?\"‘"); //用于切分字符串 7 8 while(st.hasMoreTokens()) { 9 String word = st.nextToken();10 if(hm.get(word) != null) {11 int value =http://www.mamicode.com/ ((Integer)hm.get(word)).intValue();12 value++;13 hm.put(word, new Integer(value));14 } 15 else {16 hm.put(word, new Integer(1));17 }18 }
排序类:
1 import java.util.Comparator; 2 import java.util.TreeMap; 3 4 public class ByValueComparator implements Comparator<String> { 5 TreeMap<String, Integer> treemap; 6 public ByValueComparator(TreeMap<String, Integer> tm) { 7 this.treemap = tm; 8 } 9 10 @Override11 public int compare(String o1, String o2) {12 // TODO Auto-generated method stub13 if(!treemap.containsKey(o1) || !treemap.containsKey(o2)) {14 return 0;15 }16 if(treemap.get(o1) < treemap.get(o2)) {17 return 1;18 } else if(treemap.get(o1) == treemap.get(o2)) {19 return 0;20 } else {21 return -1;22 }23 }24 }
输出结果:
TreeMap tm = new TreeMap(hm); ByValueComparator bvc = new ByValueComparator(tm); List<String> ll = new ArrayList<String>(tm.keySet()); Collections.sort(ll, bvc); for(String str:ll){ System.out.println(str+"——"+tm.get(str)); }
实例验证:
There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do.May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too.The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of peoplewho have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in lifeuntil you let go of your past failures and heartaches.When you were born,you were crying and everyone around you was smiling.Live your life so that when you die,you‘re the one who is smiling and everyone around you is crying.Please send this message to those people who mean something to you,to those who have touched your life in one way or another,to those who make you smile when you really need it,to those that make you see the brighter side of things when you are really down,to those who you want to let them know that you appreciate their friendship.And if you don’t, don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.
结果:
you——32to——19who——9those——9the——8have——7and——7of——6make——6that——6want——6your——4with——4when——4one——4life——4a——4in——4enough——4for——3don’t——3just——3it——3on——3them——3their——3will——3what——2were——2way——2touched——2this——2things——2so——2smiling——2smile——2really——2people——2past——2only——2miss——2message——2let——2is——2hurts——2go——2everyone——2do——2crying——2be——2around——2are——2appreciate——2The——2another——1always——1along——1all——1When——1There——1Please——1May——1Love——1Live——1If——1Happiness——1Dream——1And——1Always——1die——1day——1cry——1comes——1chance——1can’t——1can——1brightest——1brighter——1brighten——1born——1best——1begins——1because——1based——1bad——1happen——1grows——1go;be——1future——1from——1friendship——1forgotten——1feel——1failures——1everything;they——1everything——1ends——1dreams——1dream;go——1down——1know——1kiss——1keep——1importance——1if——1hurt——1human——1hug——1hope——1heartaches——1happy——1happiness——1happiest——1or——1opportunity——1nothing——1need——1necessarily——1much——1most——1moments——1mean——1lives——1lifeuntil——1lies——1side——1send——1see——1searched——1real——1re——1put——1probably——1pick——1person——1peoplewho——1out——1others’shoes——1other——1tried——1trials——1too——1they——1tear——1sweet——1strong——1sorrow——1something——1someone’s——1someone——1yourself——1worry——1where——1well——1was——1
代码地址:https://coding.net/u/regretless/p/WordFrequencyCount/git
Java实现的词频统计
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。