个人码云地址:https://gitee.com/NiBiWoWuLiao/PersonalProject-Java/tree/master/
PSP2.1 | 个人开发流程 | 预估耗费时间(分钟) | 实际耗费时间(分钟) |
---|---|---|---|
Planning | 计划 | 30 | 25 |
· Estimate | 明确需求和其他相关因素,估计每个阶段的时间成本 | 10 | 20 |
Development | 开发 | 200 | 270 |
· Analysis | 需求分析 (包括学习新技术) | 20 | 30 |
· Design Spec | 生成设计文档 | 10 | 15 |
· Design Review | 设计复审 | 10 | 45 |
· Coding Standard | 代码规范 | 30 | 40 |
· Design | 具体设计 | 30 | 50 |
· Coding | 具体编码 | 150 | 220 |
· Code Review | 代码复审 | 10 | 15 |
· Test | 测试(自我测试,修改代码,提交修改) | 30 | 60 |
Reporting | 报告 | 60 | 80 |
· | 测试报告 | 30 | 30 |
· | 计算工作量 | 30 | 25 |
· | 并提出过程改进计划 | 30 | 20 |
由于这道题是用来统计文本中字符,那我就先实现统计文本行数和统计字符数量和单词个数,行数就采用readline()函数判断进行加一操作。由于字符是不考虑汉字的,就需要判断该字符的ascii码是不是在[32-126]区间内,还需要考虑换行(ascii码为10进行),然后统计单词总数就按照分隔符空格以及非字母数字符号分割,还需要考虑开头是不是数字,就要用到正则表达式。对单词进行词频统计我选择用map来存放单词跟单词出现的次数。
相关函数操作:
sortWord()函数用于统计词频并排序
String s = lists.get(i).toLowerCase();//忽略大小写
if (!treeMap.containsKey(s)) {
//不存在
treeMap.put(s, 1);
} else {
//已经存在
int n = treeMap.get(s);
treeMap.put(s, n + 1);
}
charCount()函数用于统计字符数
while ((getchar = br.read()) != -1) {
//忽略汉字,换行也算字符(ascii码为10),数字字母符号的ascii码在32-126
if (getchar > 31 && getchar < 127 || getchar == 10) {
charNum++;
}
}
wordCount()函数用于统计单词数,当读取的一行不为空则为有效行
while (str != null) {//统计有效行的字符
String wordsArr[] = str.split("\\s*[^0-9a-zA-Z]+");//根据分隔符为数字字母以外的存放在数组
for (String word : wordsArr) {
//以4个英文字母开头,跟上字母数字符号
if (word.matches("[a-zA-Z]{4,}[a-zA-Z0-9]*")) {
lists.add(word);
}
}
str = br.readLine();
}
lineCount()函数用于统计文件有效行数
while (str != null) {//统计有效行的字符
lineNum++;
str = br.readLine();
}
writefile()函数将结果写入result.txt文件中
部分单元测试代码:(测试的函数为charCount()函数,lineCount()函数,wordCount()函数,测试文件内容为:我是张艺琳,由于中文不算单词所以可以设置word和character均为0,但是有效行数为1行)
还有一些测试的情况我用命令行测试看的比较明显:
是否按字典数输出
是否区分大小写
数字算不算单词
file123是一个单词,但1file不是一个单词
分析图,由Jprofiler生成:
原文:https://www.cnblogs.com/Zhangyilin/p/9641290.html