要求程序可以作出基本的单词频率的统计,也能够统计出文本的全部单词数;程序功能强大,能分析较大的文本;操做简介易懂。一目了然。java
基本功能:能够进行基本的词频统计,实现中英文分别统计
扩展功能:对于具体的文本甚至网络文本进行分析git
public class WordCount { public static void main(String[] args) throws FileNotFoundException,IOException{ try{ //使用流的方法读取文件 BufferedReader br = new BufferedReader(new FileReader( "F:\\javademo\\softwar_pro\\MRDemo\\words.txt")); //使用TreeMap方法自动将结果按Integer列 TreeMap<String,Integer> treemap = new TreeMap<String,Integer>(); //用来存储读取的单词 String readLine = null; //记录单词的总数 int count = 0; while((readLine = br.readLine())!=null){ //将字母排序为小写 readLine = readLine.toLowerCase(); //将全部单词以大写输出 //readLine = readLine.toUpperCase(); //过滤出只含有字母的字段 String[] str = readLine.split("[\\s]"); //过滤掉多个空格,“+”表明多个空格的意思 for(int i = 0;i<str.length;i++){ count++; String word = str[i].trim();//trim()用来去掉字符串首尾的空格 if(treemap.containsKey(word)){//判断此映射是否包含指定键的映射关系 treemap.put(word, treemap.get(word)+1); }else{ treemap.put(word, 1); } } }
System.out.println("按字典的输出顺序为:"); System.out.println("单词:"+"\t"+"单词出现的频率:" ); /** * 使用迭代器遍历取值: * Iterator是迭代器 * treemap.entrySet()是把TreeMap类型的数据转换成集合类型 * treemap.entrySet().iterator()获取集合的迭代器 */ Iterator<Map.Entry<String,Integer>> it = treemap.entrySet().iterator(); //判断是否存在下一个单词 while(it.hasNext()){ Map.Entry<String, Integer> entry = it.next();//获取map中每个键值 //输出结果 System.out.println(entry.getKey()+" "+entry.getValue()); br.close();//关闭流 } System.out.println("单词总数为:"+count+"个"); }catch(FileNotFoundException e){//异常处理 e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } } }
经过本次实践,让我深入认识到软件开发的不易,单人操做很是困难,须要有几个甚至一个团队来合做完成;
软件开发过程当中须要严谨的设计与计划,不然将会浪费不少的时间,有可能致使推倒重来,浪费财力物力;
经过开发过程我也认识到基础知识的严重不足,将会在之后的学习中弥补不足。github
PSP2.1 | 任务内容 | 计划共完成须要的时间(min) | 实际完成须要的时间(min) |
---|---|---|---|
Planning | 计划 | 30 | 42 |
Estimate | 估计这个任务须要多少时间,并规划大体工做步骤 | 30 | 42 |
Development | 开发 | 700 | 1088 |
Analysis | 需求分析 (包括学习新技术) | 120 | 150 |
Design Spec | 生成设计文档 | 50 | 50 |
Design Review | 设计复审 (和同事审核设计文档) | 20 | 15 |
Coding Standard | 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
Design | 具体设计 | 120 | 60 |
Coding | 具体编码 | 240 | 600 |
Code Review | 代码复审 | 60 | 80 |
Test | 测试(自我测试,修改代码,提交修改) | 60 | 120 |
Reporting | 报告 | 55 | 100 |
Test Report | 测试报告 | 20 | 32 |
Size Measurement | 计算工做量 | 10 | 10 |
Postmortem & Process Improvement Plan | 过后总结 ,并提出过程改进计划 | 25 | 60 |