结对第二次—文献摘要热词统计及进阶需求

本博客详细记录了一次软件工程课程的作业过程,包括需求分析、设计、编码和测试阶段。作业目标是创建一个控制台程序,用于统计文本文件中单词的词频,并实现了基本的单词统计功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作业格式

1611659-20190315163006555-1981420361.png

1611659-20190315173157677-328350478.png

作业正文

PSP表格

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)
Planning计划
• Estimate• 估计这个任务需要多少时间
Development开发
• Analysis• 需求分析 (包括学习新技术)60100
• Design Spec• 生成设计文档5050
• Design Review• 设计复审4045
• Coding Standard• 代码规范 (为目前的开发制定合适的规范)
• Design• 具体设计6060
• Coding• 具体编码600650
• Code Review• 代码复审5060
• Test• 测试(自我测试,修改代码,提交修改)50200
Reporting报告
• Test Report• 测试报告50100
• Size Measurement• 计算工作量3030
• Postmortem & Process Improvement Plan• 事后总结, 并提出过程改进计划2020
合计10101345

解题思路

在拿到题目之后,首先考虑的是语言的选择,考虑到由于221600118比较擅长使用Java以及jJava的类库比较强大所以选择使用Java实现;之后就是考虑如何完成需求,由于编程能力不足,最终只能选择完成基础需求。然后就是类的方面,考虑到需求大体可以分成三个功能需求,所以封装了3个类,分别对应字符数统计,行数统计以及单词数统计,最后再将3个类整合起来。在查找资料方面,主要使用百度和Google查找了Java的api以及去图书馆查阅了Java编程的相关书籍。

实现过程

用3个类分别实现字符数统计,行数统计以及单词数统计的功能,最后再由Main调用,字符的总数就是读入文件的总字符数;

1611659-20190315181418724-1311054715.png
行数由读入的换行符确定,再减去行中没有有效字符的行数;
1611659-20190315181429759-1683874292.png
比较难实现的是单词数的统计,按照需求,单词要求满足开头连续4个字符都是字母,碰到分隔符就截断,所以在处理时先对前四个字符特判,如果是则继续将字母或数字字符添加到当前单词上直到遇到分隔符,遇到分隔符后就将这个单词存入HashMap,HashMap的键值对为单词-频率,如果该单词已存在HashMap中则频率加一,再对剩下的字符串进行同样的操作。

1611659-20190315181443147-704830881.png

性能

由于编程能力不足,无法对程序在进行性能改进;程序中消耗最大的是判断是否是单词的函数。

1611659-20190315185814993-1735861758.png

关键代码

public class CountChar {
    
    public static int getNumber(String path){//字符数统计
        int num=0;
        try{    
            File file = new File(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            int cc;
            char ccc;
                    
            while((cc=br.read())!=-1) {
                ccc=(char)cc;
                if(ccc=='\n') {
                    num--;
                }
                if(cc>=0&&cc<=127) {
                    num++;
                }
            }
            br.close();
            return num;
        }catch(Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}

public class CountLine {//行数统计
        
    public static int getLine(String path) {
        
        int lines=0;

        try{    
            
            File file = new File(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String line;

            boolean bline=false;//行是否含有有效字符
          
            char c[];
            while ((line = br.readLine()) != null) {
                if(line.length()==0) { 
                    continue;
                }
          
                bline=false;
                c=line.toCharArray();
                for(int i=0;i<c.length;i++) {
                    int ch=(int)c[i];
                    if(ch>=33&&ch<127) {
                        bline=true;
                    }
                }
              if(bline) lines++;
            }
            br.close();
            return lines;
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        
        return 0;
    }
}

        public class CountWords {//词数统计
    public static HashMap<String, Integer> hash = new HashMap<String, Integer>();
    
    public static HashMap<String, Integer>  getWords(String path) {
        
        try{    
            
            File file = new File(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

            String line;
            
            while ((line = br.readLine()) != null) getWord(line);
            
            br.close();
            return hash;
           
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new HashMap<String, Integer>();
    }

    
    public static void getWord(String line) {//获得词语
        if(line.length()<4) return;
        
        String theline=line.toLowerCase();
        
        char c[]=theline.toCharArray();

        for(int i=0;i<c.length;i++) {
            int ch=(int)c[i];
            if(isAZ(ch)) {
                if(the4(c,i)) {
                    int last=0;
                    for(int j=i+4;j<c.length;j++) {
                        last=j;
                        if(!isAZ((int)c[last])&&!isNUM((int)c[last])) {
                            getWord(theline.substring(last,theline.length()));
                            break;
                        }
                    }
                    if(last==c.length-1) last++;
                    String word=theline.substring(i,last);
                    
                    if(hash.containsKey(word)) {
                        int nn=hash.get(word);
                        hash.put(word, nn+1);
                    }else {
                        hash.put(word, 1);
                    }
                    break;
                }
            }
        }
    }

        public static boolean the4(char[] line,int index) {//判断是否是单词
        int n=0;
        for(int i=index+1;i<line.length;i++) {
            int ch=(int)line[i];
            if(isAZ(ch)) {
                if(++n==3) return true;
            }else {
                return false;
            }
            
        }
        return false;
    }

单元测试

单元测试采用的数据包括将已有的样例混合和采用随机数随机生成的文件。

单元测试代码

1611659-20190315164853027-362029988.png

1611659-20190315164858294-1698480101.png

1611659-20190315164905627-64098426.png

部分文件测试结果

1611659-20190315181011163-1524992783.png

遇到的困难

在一开始的时候采用readline读入一整行的字符串,结果由于readline会消除换行符导致字符数统计错误,后来改用read直接读入整个文件中的字符再进行处理。

对队友的评价:

221600118:对时间的把握比我好,在我懒时,会督促我把进度提上来,起到了监督的作用。

221600120:做事认真,比较负责,遇到不懂上网去查询相关的资料让我们一起去学习,去弄懂,一些代码的编写比较熟练。

转载于:https://www.cnblogs.com/Azuration/p/10536508.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值