软工16个人作业2

实现一个能够对文本文件中的单词的词频进行统计的控制台程序。
进行单元测试、回归测试、效能测试,在实现上述程序的过程中使用相关的工具。
进行个人软件过程(PSP)的实践,逐步记录自己在每个软件工程环节花费的时间。
使用源代码管理系统 (码云)。

1.项目地址

博客作业要求地址:https://www.cnblogs.com/happyzm/p/9559372.html
码云地址:https://gitee.com/wengmingqiang/PersonalProject-C

PSP表格

PSP2.1 ** 个人开发流程 ** ** 预估耗费时间(分钟)** 实际耗费时间(分钟)
Planning 计划 30 40
· Estimate 明确需求和其他相关因素,估计每个阶段的时间成本 20 30
Development 开发 700 500
· Analysis 需求分析 (包括学习新技术) 60 80
· Design Spec 生成设计文档 80 60
· Design Review 设计复审 30 15
· Coding Standard 代码规范 30 15
· Design 具体设计 100 80
· Coding 具体编码 100 180
· Code Review 代码复审 40 20
· Test 测试(自我测试,修改代码,提交修改) 60 180
Reporting 报告 50 90
· 测试报告 40 30
· 计算工作量 15 20
· 并提出过程改进计划 10 20

3.解题思路描述:

拿到这个项目,大致的思路就是,把文件中的字符都读到一个String字符串中,再对字符串进行操作
统计Ascii码:计算string的字符串的长度
统计行数:对文件每行每行的读取,有读取出数据则 行数line++ ,最后返回line
统计单词数:把String函数用split函数对字符串进行划分,存入到一个String数组中,再计算数组的长度
统计单词频度:用键值对(key-value)映射,单词作为key,单词数量作为value。

4设计实现过程

1.项目类的设计

  • WordDeal类,设计统计相关信息的方法。
  • ReadFile :类用来读取文件中的数据并且存到一个String数组中。
  • Main类,调用上面两个类的方法,实现具体功能。
    T- est类,用于进行单元测试。

2.代码分析

1.读取文件数据函数

public static String readFileContent(File file) throws Exception {

	FileInputStream fis = new FileInputStream(file);
	byte[] buf = new byte[1024];
	StringBuffer sb = new StringBuffer();
	while ((fis.read(buf)) != -1) {
		sb.append(new String(buf));
		buf = new byte[1024];// 重新生成,避免和上次读取的数据重复
	}
	return sb.toString();
	
	
	
}

2.获取文件字符 数函数

	public static int getCharCount(String text) // 统计文件字符数(ascll码(32~126),制表符,换行符,)
	 {
	     char c;
	     int charNum = 0;
	     for (int i = 0; i < text.length(); i++) {
	         c = text.charAt(i); //把字符串转化为字符数组
	         if (c >= 32 && c <= 126 || c == '\r' || c == '\n'|| c == '\t') {
	             charNum++;
	         }
	     }
	     return charNum;
	 }

3.计算行数

	 public static int getLineCount(String text)throws Exception  { // 统计有效行数

	
		 	int lineNum = 0;
		     String[] line = text.split("\r\n"); // 将每一行分开放入一个字符串数组
		     for (int i = 0; i < line.length; i++) { // 找出无效行,统计有效行

		         if (line[i].trim().length() == 0)
		             continue;
		         lineNum ++;
		     }
		     return lineNum;
		 }

4计算单词总数

public static int getWordsNum(String text) {
		 
		 String content = text.replace('\r', ' ');
		 content = text.replace('\b', ' ');
		 content = text.replace('\n', ' ');
		 
		 String [] words = content.split(" ");
		 int wordCount = 0;
		 
		 for(int i= 0; i<words.length;i++)
		 {
			 if (words[i].length()<4)
				 continue;
			 
			 int j = 0;
			 for(  j =0;j<4;j++)
			 { 
				 char c =words[i].charAt(j);
				 if(!((c>='A'&& c<='Z')||(c>='a'&& c<='z')))
					 break;
				
			 }
			
			 if(j==4)
				 wordCount++;

			
		 }
		  
		 return wordCount;
		 
	 }
	 

5统计单词频率

public static Map<String, Integer> getWordFreq(String text) // 统计单词词频(单词:以4个英文字母开头,跟上字母数字符号,单词以分隔符分割,不区分大小写。)
	 {
		 HashMap<String, Integer> wordFreq = new HashMap<String, Integer>();
		 
		 String content = text.replace('\r', ' ');
		 content = text.replace('\b', ' ');
		 content = text.replace('\n', ' ');
		 
		 String [] words = content.split(" ");
		 
		 
		 for(int i= 0; i<words.length;i++)
		 {
			 if (words[i].length()<4)
				 continue;
			 
			 int j = 0;
			 for(  j =0;j<4;j++)
			 { 
				 char c =words[i].charAt(j);
				 if(!((c>='A'&& c<='Z')||(c>='a'&& c<='z')))
					 break;
			 }
			
			 if(j==4)
			 {
				 words[i] = words[i].trim().toLowerCase();  // 将字符串转化为小写
                 if (wordFreq.get(words[i]) == null) 
	                 { // 判断之前Map中是否出现过该字符串
	                     wordFreq.put(words[i], 1);
	                 } 
                 else
                     wordFreq.put(words[i], wordFreq.get(words[i]) + 1); 
			 }
		 }
	     return wordFreq;
	 }
	 
	 
	 

测试

持续未完!!

posted on 2018-09-17 21:39  wengmq  阅读(158)  评论(4编辑  收藏  举报