要求:

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 = newFileReader(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 =((Integer)hm.get(word)).intValue();12 value++;13 hm.put(word, newInteger(value));14 }15 else{16 hm.put(word, new Integer(1));17 }18 }

排序类:

1 importjava.util.Comparator;2 importjava.util.TreeMap;3

4 public class ByValueComparator implements Comparator{5 TreeMaptreemap;6 public ByValueComparator(TreeMaptm) {7 this.treemap =tm;8 }9

10 @Override11 public intcompare(String o1, String o2) {12 //TODO Auto-generated method stub

13 if(!treemap.containsKey(o1) || !treemap.containsKey(o2)) {14 return 0;15 }16 if(treemap.get(o1)

输出结果:

TreeMap tm = newTreeMap(hm);

ByValueComparator bvc= newByValueComparator(tm);

List ll = new ArrayList(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 people

who 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——32

to——19

who——9

those——9

the——8

have——7

and——7

of——6

make——6

that——6

want——6

your——4

with——4

when——4

one——4

life——4

a——4

in——4

enough——4

for——3

don’t——3

just——3

it——3

on——3

them——3

their——3

will——3

what——2

were——2

way——2

touched——2

this——2

things——2

so——2

smiling——2

smile——2

really——2

people——2

past——2

only——2

miss——2

message——2

let——2

is——2

hurts——2

go——2

everyone——2

do——2

crying——2

be——2

around——2

are——2

appreciate——2

The——2

another——1

always——1

along——1

all——1

When——1

There——1

Please——1

May——1

Love——1

Live——1

If——1

Happiness——1

Dream——1

And——1

Always——1

die——1

day——1

cry——1

comes——1

chance——1

can’t——1

can——1

brightest——1

brighter——1

brighten——1

born——1

best——1

begins——1

because——1

based——1

bad——1

happen——1

grows——1

go;be——1

future——1

from——1

friendship——1

forgotten——1

feel——1

failures——1

everything;they——1

everything——1

ends——1

dreams——1

dream;go——1

down——1

know——1

kiss——1

keep——1

importance——1

if——1

hurt——1

human——1

hug——1

hope——1

heartaches——1

happy——1

happiness——1

happiest——1

or——1

opportunity——1

nothing——1

need——1

necessarily——1

much——1

most——1

moments——1

mean——1

lives——1

lifeuntil——1

lies——1

side——1

send——1

see——1

searched——1

real——1

re——1

put——1

probably——1

pick——1

person——1

peoplewho——1

out——1

others’shoes——1

other——1

tried——1

trials——1

too——1

they——1

tear——1

sweet——1

strong——1

sorrow——1

something——1

someone’s——1

someone——1

yourself——1

worry——1

where——1

well——1

was——1

代码地址:https://coding.net/u/regretless/p/WordFrequencyCount/git

java 词频统计_Java实现的词频统计相关推荐

  1. java统计词频算法_Java实现的词频统计——功能改进

    本次改进是在原有功能需求及代码基础上额外做的修改,保证了原有的基础需求之外添加了新需求的功能. 功能: 1. 小文件输入--从控制台由用户输入到文件中,再对文件进行统计: 2.支持命令行输入英文作品的 ...

  2. java 文本词频统计_Java实现中文词频统计

    昨日有个中文词频统计的需求, 百度一番后, 发现一大堆标题党文章, 讲的与内容严重不符, 这里就简单记录下自己实现的流程吧! 与英文单词的词频统计不同, 中文的难点在于如何分词, 不过好在有许多优秀的 ...

  3. java实验6 词频统计_Java实现的词频统计——单元测试

    前言:本次测试过程中发现了几个未知字符,这里将其转化为十六进制码对其加以区分. 1)保存统计结果的Result文件中显示如图: 2)将其复制到eclipse环境下的切分方法StringTokenize ...

  4. java 文本词频统计_java实现文本词频统计

    File f=new File(path); Mapmap=new HashMap<>(); Version matchVersion = Version.LUCENE_31; Analy ...

  5. java web 在线人数_java网页中怎样统计在线人数

    展开全部 1.用session超时,session为null就表示下线了 2.也可以采用数据库中设置 临时表 来处理 一个用户登陆时向表中插进一62616964757a686964616fe4b893 ...

  6. java查询app下载量统计_java基础增强:统计网上app下载情况,并排序

    一入编程深似海,从此妹子是路人. 案例: 统计网站app下载的情况,后台数据如下: 日期,用户名,app名,下载渠道,所在城市,app版本 2017-08-15,xx老师,陌陌,app store,上 ...

  7. java 错误输出_Java中的字数统计错误输出

    我是Java新手. 我编写了一个代码来计算文件中特定单词的出现次数.要向用户询问要计算的单词.我写了两个代码.第一个代码运行正常,计数正确.而在第二个代码中,计数较少.我无法弄清楚原因.有人可以帮我找 ...

  8. python单词词频字典_Python字典使用--词频统计的GUI实现

    字典是针对非序列集合而提供的一种数据类型,字典中的数据是无序排列的. 字典的操作 为字典增加一项 dict[key] = value students = {"Z004":&quo ...

  9. 在“3_人民日报语料”中统计“日语借词”的词频;

    3. 在"3_人民日报语料"中统计"日语借词"的词频; pyhton方法 # -*- coding: utf-8 -*- import json japanes ...

  10. 统计csv词频_基于给定词语列表统计词频

    基于给定词语列表并统计词频,统计结果输出到csv中.有两种实现方法 方法一:常规的统计给定关键词的词频 思路: 第一步,整理好待查询query_words 第二步,新建一个csv文件,并且将query ...

最新文章

  1. 短视频个性化Push工程精进之路
  2. Linux系统下如何查看已经登录用户
  3. Python之sort()函数详解
  4. sublime使用技巧总结
  5. 【译】BINDER - ANALYSIS AND EXPLOITATION OF CVE-2020-0041
  6. Android 系统自带 Theme(主题)
  7. 多语言应用开发中本地化信息对照表
  8. 深度学习之 epoch batch iteration
  9. 别再写一堆的 for 循环了!Java 8 中的 Stream 轻松遍历树形结构,是真的牛逼!...
  10. (20)python_matplotlib解决中文乱码问题
  11. 汽车类自媒体怎么找素材?这几个办法很好用
  12. WCG2008科隆总决赛 公开票选你想要的游戏
  13. 【leetcode】995. Minimum Number of K Consecutive Bit Flips
  14. Unity——射线检测
  15. 如何将PS中的图片字体变为黑色
  16. 拼多多推广没用怎么办?
  17. Mathematica实例——利用Mathematica演示量子力学中的波包演化
  18. 技术分析:苹果之后 HTML5将改变移动互联网
  19. JVM之垃圾收集算法
  20. vc只能调用matlab子函数,VC调用matlab函数

热门文章

  1. 服务器cpu占用过高一般是什么原因,如何解决服务器cpu使用率过高的問題
  2. JAVA版12306抢票工具
  3. 今晚与小妹亚美合作了一把qq游戏,大获全胜!
  4. 网站建设:制作一个网站一般要多少钱?
  5. Python基础概要(一天快速入门)
  6. Elasticsearch7.13+kibana7.13安装
  7. python随机生成20个数字_你如何在Python中生成20个随机数字
  8. python输入两个数求和笔试题_Python练习题1.1从键盘输入两个数,求它们的和并输出...
  9. 计算机蓝牙快捷键,电脑蓝牙在哪里开?蓝牙快捷方式介绍
  10. 【演示文稿制作软件】Focusky教程 | 贯穿整个演示文稿背景音乐的添加与设置