单词统计

题目:

第0步:输出某个英文文本文件中 26 字母出现的频率,由高到低排列,并显示字母出现的百分比,精确到小数点后面两位。

第1步:输出单个文件中的前 N 个最常出现的英语单词。作用:一个用于统计文本文件中的英语单词出现频率。

设计思想:首先是统计字母,我们应该先把要统计的文件读取,遍历统计字母出现的次数,将大写字母转换为小写字母;统计单词也需要将大写字母转换为小写,只要遇到空格则记为一个单词,遍历一遍统计单词个数。

遇到的问题:不知道该如何显示百分比,后来在网上查找发现可以用formattedDecimalToPercentage这个函数来实现

package test;import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;public class zimu {private static String formattedDecimalToPercentage(double decimal){//获取格式化对象NumberFormat nt = NumberFormat.getPercentInstance();//设置百分数精确度2即保留两位小数nt.setMinimumFractionDigits(2);return nt.format(decimal);}
public static void main(String []args) {String a1;char a='A';int a2[]=new int[27];char b1[]=new char[26];char b2[]=new char[26];double c1[]=new double[26];for(int i=0;i<26;i++){b1[i]=a;b2[i]=(char)(a+32);a++;}try {BufferedReader in = new BufferedReader(new FileReader("D:\\piao.txt"));String str;while ((str = in.readLine()) != null) {char[] d=str.toCharArray();for(int i=0;i<d.length-1;i++) {for(int j=0;j<26;j++) {if(b1[j]==d[i]||b2[j]==d[i]) {a2[j]++;}}}}a2[26]=0;for(int i=0;i<26;i++) {a2[26]=a2[i]+a2[26];}for(int i=0;i<26;i++) {c1[i]=(double)((double)a2[i]/(double)a2[26]);}for(int i=0;i<26;i++) {System.out.print(b1[i]);System.out.print("和");System.out.print(b2[i]);System.out.print("出现的次数为:");System.out.print(a2[i]);double d=(double)((double)a2[i]/(double)a2[26]);String result2=formattedDecimalToPercentage(d);System.out.println("   百分比为:"+result2);}System.out.println("       ");System.out.println("出现单词次数较多的前十个为:");BufferedReader reader = new BufferedReader(new FileReader("D:\\\\piao.txt"));StringBuffer buffer = new StringBuffer();String line = null;while ((line = reader.readLine()) != null) {buffer.append(line);}reader.close();Pattern expression = Pattern.compile("[a-zA-Z]+");String string = buffer.toString();Matcher matcher = expression.matcher(string);//Map<String, Integer> map = new TreeMap<String, Integer>();String word = "";int times = 0;while (matcher.find()) {word = matcher.group();if (map.containsKey(word)) {times = map.get(word);map.put(word, times + 1);} else {map.put(word, 1);}}List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {public int compare(Map.Entry<String, Integer> left,Map.Entry<String, Integer> right) {return (left.getValue()).compareTo(right.getValue());}};Collections.sort(list, comparator);// 排序int last = list.size() - 1;int asdad=0;for(int i=last;i>0;i--) {String key = list.get(i).getKey();Integer value = list.get(i).getValue();asdad=asdad+value;}for (int i = last; i > last - 10; i--) {String key = list.get(i).getKey();Integer value = list.get(i).getValue();System.out.print(key + " :" + value);double d=(double)((double)value/(double)asdad);String result2=formattedDecimalToPercentage(d);System.out.println("   百分比为:"+result2);}} catch (IOException e) {}
}
}

  结果截图:

         

总结:这次课上的作业让我对文件的读取有了进一步的掌握,文件的读取已经做过很多次了,但是自己还不是很了解,应该多加练习,formattedDecimalToPercentage这个函数可以用来小数格式化和百分比的显示

转载于:https://www.cnblogs.com/zhang12345/p/11062766.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/569469.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

html 经常会用到的英语名词

avatar 头像 portfolio 作品集 mask 面罩 shiny-wrapper 包裹器 shiny-inner 内部

轻松自动化---selenium-webdriver(python) (四)

https://www.cnblogs.com/fnng/p/3190966.html http://www.testclass.net/ 测试教程网&#xff0c;专业的selenium 学习网站。 本节要解决的问题&#xff1a; 如何定位一组元素&#xff1f; 场景 从上一节的例子中可以看出&#xff0c;webdriver可以很方便的使用findElement方…

如何用CSS画一个三角形

border-left-color: border-top-width:中心距顶部距离 .userCard .welcome .triangle{ border: 8px solid transparent;width: 0px;height: 0px;border-left-color: #E8676B;border-top-width: 0; } 结果 → 这篇博客很清楚&#xff0c;可借助参考&#xff1a;https://…

CSS hr修改样式

hr 默认样式 若加上这样CSS hr{height: 0;border: none;border-top: 1px solid red; } 会变为

CSS height高度 和 width宽度

1、不到情非得已不要用height&#xff0c;height是bug的来源 2、内联元素的高度由 line-height 决定 3、块级元素的高度由 其内部文档流元素的高度总和决定 文档流&#xff1a;文档内元素的流动方向 4、max-width 是最大宽度&#xff0c;可以自适应&#xff0c;若屏幕小…

scrapy框架的日志等级和请求传参

Scrapy的日志等级 - 在使用scrapy crawl spiderFileName运行程序时&#xff0c;在终端里打印输出的就是scrapy的日志信息。- 日志信息的种类&#xff1a;ERROR &#xff1a; 一般错误WARNING : 警告INFO : 一般的信息DEBUG &#xff1a; 调试信息- 设置日志信息指定输出&#x…

绝对定位absolute、相对定位relative(脱离文档流)

1、脱离文档流&#xff0c;&#xff1a;父元素高度不再包含文档流高度 position&#xff1a;absolute 子元素 position&#xff1a;relative 父元素&#xff0c; 子元素相对于祖先中的第一个relative定位&#xff0c;用到的 top:0; left:0; 不然出bug 2、相对于屏幕…

第32月第8天 打包 Framework 时使用 CocoaPods 引入第三方库的方法

1. 但是可以发现打包完后的 .framework 包里面是没有 CocoaPods 集成的第三方库的&#xff0c;所以在使用 .framework 包时&#xff0c;工程中必须也要集成 .framework 包中用到的第三方库&#xff0c;否则会找不到文件而报错。 https://www.cnblogs.com/nelsen-chen/p/9353520…

CSS 加过渡效果transition

transition: box-shadow 0.3s; 给按钮加效果 transition: all 0.7s; 给进度条

php后端语言的基本语法

<?php$num 1;//php中定义一个变量echo $num;//php中打印一个值&#xff08;与console。log类似&#xff09;$arr array(1,2,3,4,5,6,7,89);//在php中定义一个数组//echo $arr;php中使用echo不能输出集合&#xff08;数组与对象&#xff09;&#xff0c;//必须采用print_r…

CSS居中问题

1.左浮后&#xff0c;想居中&#xff1a; 给他“爸爸”居中&#xff0c;如果没有宽度&#xff0c;不能用 margin&#xff1a;0 auto;可以用text-align&#xff1a;center&#xff1b; 2.继1&#xff0c;如果“爸爸”也没有确定的宽&#xff0c;想变窄 “爸爸”只能也变成 di…

12-order by和group by 原理和优化 sort by 倒叙

原理和优化 groupBy分组&#xff1f;会造成一个数据倾斜 1.解决方案&#xff1a;将输入在map端进行部分的打乱重分 set hive.groupby.skewindatatrue; 2.在map到reduce端的时候设置combiner进行合并 set hive.map.aggrtrue; 3.在combiner进行合并的时候要知道数据量的大小&…

内联元素(display:inline-block)注意事项

1.内联元素margin的上下没有用&#xff0c;加入 display:inline-block 后&#xff0c;才有用&#xff1b; 不是block块级元素了&#xff0c;那么想居中&#xff0c;用 margin&#xff1a;0 auto; 没用了&#xff0c; 那想居中&#xff0c;在他“父亲”加上 text-align&#x…

多个excel文件内容合并到一个excel文件的多个sheet的小程序

# -*- coding:utf-8 -*-import xlrd, xlsxwriter# 待合并excelallxls ["D:\\excelcs\\***.xlsx"]allxls1 ["D:\\excelcs\\***.xlsx"]allxls2 ["D:\\excelcs\\***.xlsx"]#print(allxls[0:2])# 目标excelend_xls "D:\\excelcs\\***.xlsx&…

【编程训练-考研上机模拟】综合模拟2-2019浙大上机模拟(晴神)

A - next[i] Problem Description 在字符串匹配的KMP算法中有一个重要的概念是next数组&#xff0c;求解它的过程让不少同学伤透了心。next数组的直接语义其实是&#xff1a;使“长度为L的前缀”与“长度为L的后缀”相同的最大L&#xff0c;且满足条件的前后缀不能是原字符串本…

CSS常用工具

wall haven 墙纸 CSS tricks shape 用CSS作图 iconfont.cn 小图标工具网站&#xff0c;&#xff08;添加至项目→symbol→查链接→生成代码→<script>→使用帮助&#xff09; CSS3 linear gradient &#xff08;generator&#xff09; 线性渐变工具 CSS animation…

CSS li 一半左浮一半右浮

.skills >ol li{float: left; 左浮width: 48%;margin-bottom: 40px; } .skills >ol li:nth-child(even){ //even 偶数 odd 奇数float: right; 右浮 }另一种 .skills >ol li:nth-child(1){ //第一个margin-left: 0; }.skills >ol li:first-child{ //…

addEventListener事件委托

什么是事件委托&#xff1a;通俗的讲&#xff0c;事件就是onclick&#xff0c;onmouseover&#xff0c;onmouseout&#xff0c;等就是事件&#xff0c;委托呢&#xff0c;就是让别人来做&#xff0c;这个事件本来是加在某些元素上的&#xff0c;然而你却加到别人身上来做&#…

HTML常遇问题

1、span 里面套 div 会有问题&#xff0c;可以套 span&#xff0c;再加语句 display:block; 相当于套了div 2. 外部不能加padding&#xff0c;所以可以再里层加 div&#xff0c;在加padding可以了 3、display:block; display:inline-block; 让border将它们包住 4、*{ …

bzoj3771:Triple

传送门 生成函数 设生成函数\(f(x)\)&#xff0c;可以将系数定为选的方案数&#xff0c;指数定为代价 那么\[ f(x)\sum_{i1}^{n}x^{w_i} \] 然后答案就是\(f^3(x)f^2(x)f(x)\)然后去掉重复的情况 然后我们设\[ A(x)\sum_{i1}^{n}x^{2w_i}\\ B(x)\sum_{i1}^{n}x^{3w_i} \] 重复的…