WordCount2.0

结对作业:

  成员:201631062111,20163106531

  代码地址:https://gitee.com/zhengzhibin/codes/jqmg8tr67zobvxa4ku3ic70

 

PSP表格:

 

PSP2.1

PSP阶段

预估耗时

(分钟)

实际耗时

(分钟)

Planning

计划

 80

 100

· Estimate

· 估计这个任务需要多少时间

 80

 100

Development

开发

 300

600

· Analysis

· 需求分析 (包括学习新技术)

 100

 200

· Design Spec

· 生成设计文档

 60

 80

· Design Review

· 设计复审 (和同事审核设计文档)

 80

 100

· Coding Standard

· 代码规范 (为目前的开发制定合适的规范)

 60

 60

· Design

· 具体设计

 50

 60

· Coding

· 具体编码

 600

 1000

· Code Review

· 代码复审

 100

 150

· Test

· 测试(自我测试,修改代码,提交修改)

 240

 260

Reporting

报告

 300

 530

· Test Report

· 测试报告

 80

 150

· Size Measurement

· 计算工作量

 100

 180

· Postmortem & Process Improvement Plan

· 事后总结, 并提出过程改进计划

 120

 200

 

合计

 1670

2400

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3.设计流程

 

 

5.代码片段

5.1可视化界面的设计

  1 package workCount;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.Container;
  5 import java.awt.GridLayout;
  6 import java.awt.event.ActionEvent;
  7 import java.awt.event.ActionListener;
  8 import java.io.IOException;
  9 
 10 import javax.swing.BorderFactory;
 11 import javax.swing.JButton;
 12 import javax.swing.JCheckBox;
 13 import javax.swing.JFileChooser;
 14 import javax.swing.JFrame;
 15 import javax.swing.JPanel;
 16 import javax.swing.JTextArea;
 17 import javax.swing.JTextField;
 18 
 19 public class wcUI{
 20     
 21     static String result="";
 22     public wcUI(){
 23         
 24         JFrame jUI=new JFrame("查询工具");
 25         JPanel jPanel =new JPanel();
 26         jPanel.setLayout(null);
 27         jPanel.setBorder(BorderFactory.createTitledBorder("用户专属"));
 28         
 29         final JTextField showpath=new JTextField();//用于展示所查文件的路径
 30         final JTextArea showresult=new JTextArea();//展示结果
 31         JButton selectfile=new JButton("选择文件");//出发文件选择器
 32         JButton select=new JButton("查询");//执行查询功能
 33         final JTextField showstop=new JTextField();//展示停词表文件路径
 34         JButton selectstop=new JButton("停词表");//选择停词表
 35          
 36         //各种功能的复选框
 37         final JCheckBox command1=new JCheckBox("查字符");
 38         final JCheckBox command2=new JCheckBox("查单词");
 39         final JCheckBox command3=new JCheckBox("查行数");
 40         final JCheckBox command4=new JCheckBox("查多行");
 41         final JCheckBox command5=new JCheckBox("使用停词表查单词数");
 42         
 43         //设置控件的位置
 44         selectfile.setBounds(200, 20,100, 40);
 45         select.setBounds(300, 20, 80, 40);
 46         showpath.setBounds(0,20,200,40);
 47         showstop.setBounds(160, 125, 120, 20);
 48         selectstop.setBounds(310, 125, 60, 20);
 49         showresult.setBounds(0,160,400, 150);
 50         command1.setBounds(0, 60, 80, 60);
 51         command2.setBounds(90, 60, 80, 60);
 52         command3.setBounds(180, 60, 80, 60);
 53         command4.setBounds(270, 60, 80, 60);
 54         command5.setBounds(0, 100, 150, 60);
 55 
 56         //添加控件
 57         jUI.add(jPanel);
 58         jPanel.add(showstop);
 59         jPanel.add(selectstop);
 60         jPanel.add(select);
 61         jPanel.add(showpath);
 62         jPanel.add(showresult);
 63         jPanel.add(selectfile);
 64         jPanel.add(command1);
 65         jPanel.add(command2);
 66         jPanel.add(command3);
 67         jPanel.add(command4);
 68         jPanel.add(command5);
 69         
 70         
 71         jUI.setVisible(true);
 72         jPanel.setSize(400, 350);
 73         jUI.setSize(400, 350);
 74         //按钮的监听事件
 75         selectfile.addActionListener(new ActionListener() {
 76             @Override
 77             public void actionPerformed(ActionEvent e) {
 78                 // TODO Auto-generated method stub
 79                 //展示文件的路径
 80                 JFileChooser jFileChooser=new JFileChooser("选择文件");
 81                 jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
 82                 jFileChooser.showOpenDialog(null);//显示文件选择器
 83                 if(jFileChooser.getSelectedFile().getPath()!=null)
 84                 {
 85                     showpath.setText(jFileChooser.getSelectedFile().getPath());
 86                 }
 87                 else{
 88                     System.out.println("没有选择文件");
 89                 }
 90             }
 91         });
 92         select.addActionListener(new ActionListener() {
 93             
 94             @Override
 95             public void actionPerformed(ActionEvent e) {
 96                 // TODO Auto-generated method stub
 97                 workCount wCount=new workCount();
 98                 //根据复选框,选择功能并执行,返回结果
 99                 try {
100                         if(command1.isSelected()){
101                             result+="\n"+wCount.count_char(showpath.getText());
102                         }
103                         if(command2.isSelected()){
104                             result+="\n"+wCount.count_word(showpath.getText());
105                         }
106                         if(command3.isSelected()){
107                             result+="\n"+wCount.count_line(showpath.getText());
108                         }
109                         if(command4.isSelected()){
110                             result+="\n"+wCount.count_moreLine(showpath.getText());
111                         }
112                         if(command5.isSelected()){
113                             result+="\n使用停词表后,单词数为:"+wCount.stopWordList(showpath.getText(), showstop.getText());
114                         }
115                         showresult.setText(result);
116                     } catch (IOException e1) {
117                         // TODO Auto-generated catch block
118                         e1.printStackTrace();
119                 }
120                 
121             }
122         });
123         selectstop.addActionListener(new ActionListener() {
124             //选择停词表,并展示到界面上
125             @Override
126             public void actionPerformed(ActionEvent e) {
127                 // TODO Auto-generated method stub
128                 JFileChooser jFileChooser1=new JFileChooser();
129                 jFileChooser1.showOpenDialog(null);//显示文件选择器
130                 if(jFileChooser1.getSelectedFile().getPath()!=null){
131                     showstop.setText(jFileChooser1.getSelectedFile().getPath());
132                 }else{
133                     showstop.setText(null);
134                 }
135             }
136         });
137     }
138 }

5.2停词表的设计

 1 public int stopWordList(String pathname,String stopList_filepath) throws IOException
 2         {  
 3                 count_word(pathname);
 4                 // 设置两个临时变量,strs和strs2用于对停词表里面的数据进行判断
 5                 String strs ="";
 6                 String strs2="";
 7                 // 设置num用于统计文本文件和停词表中单词的数量
 8                 int num=0;
 9                 // contentTxt(pathname," ")返回的是对象结果集的变量,对两个结果集进行遍历,返回及结果
10                 for(Object ob2:contentTxt(pathname," ")) 
11                 {   
12                     //文本文件中的单词数据,放到strs中
13                     strs = (String)ob2; 
14                     for(Object ob: contentTxt(stopList_filepath, " "))
15                     {
16                     // 停词表文件中的单词数据,放到strs2中
17                     strs2 = (String)ob;
18                     // 判断两个strs是否相等,相等就统计次数
19                     if(strs.equals(strs2))
20                             num++;
21                     }        
22                 }
23                 wordnum=wordnum-num;
24                 return wordnum;
25             }
26 
27          public static String Check(String str) {//消除对,。—/特殊字符的统计
28             if(str.startsWith(",") || str.startsWith(".")||str.startsWith("_")||str.startsWith("/") ) {
29                 str = str.substring(1, str.length());
30                 Check(str);
31             }else if(str.endsWith("()"))
32                 {str = str.substring(0, str.length()-2);}
33             else if (str.endsWith(",") || str.endsWith(".")||str.endsWith("_")||str.endsWith("/")||str.endsWith("(")||str.endsWith(")")) {
34                 str = str.substring(0, str.length()-1);
35                 Check(str);
36             }
37             return str;
38         }

 

 1 //传入两个参数,第一个参数为输入文本文件的位置数据,第二个为分割字符的参数,最后返回的是单词的结果集
 2         public ArrayList<String> contentTxt(String pathname,String split) throws IOException 
 3         {
 4             String string="";
 5             String stop_resultString="";//string作为拼接变量,将拼接后的文件存到stop_resultString
 6      
 7             File stopList_file=new File(pathname);
 8             if(!stopList_file.exists()) {//如果不存在,就创建新文件
 9                 stopList_file.createNewFile();
10             }
11             //构建文件输入流,将停词表里面的数据输入到计算机内存中
12             FileInputStream stopListInputStream=new FileInputStream(stopList_file);
13             
14             BufferedReader stopListBufferdReader=new BufferedReader(new InputStreamReader(stopListInputStream));
15             //遍历停词表的内容进行拼接
16             while((string=stopListBufferdReader.readLine())!=null)//遍历文件,获取文件中的数据
17                 {
18                    
19                     stop_resultString+=string;//将文件中文字存到stop_resultString
20                     
21                 }
22             // 将结果进行对应条件的分割
23             String[] arr = stop_resultString.split("\\.| |,");
24             ArrayList<String> set = new ArrayList<String>();//通过哈希表存放
25             for(int i=0;i<arr.length;i++) {
26                 set.add(arr[i]);
27             }
28             // 返回ArrayList对象结果集
29             return set; 
30         }

 

 4.测试用列

 4.1首先用-x进入可视化界面

 4.2进入图形界面

 

 此界面有查字符等功能

4.3点击选择文件,进入文件选择界面,选择需要的文件

 

 4.4在空白栏输出结果

 

4.5用停词表文件查询

 

6 个人总结

 此次项目对上次来说难度不大,但是当我们进行合作开发的时候,就发现了很多的问题,代码对原来的依赖度较高,

造成了后期动一发而牵全身的尴尬禁地。后来我们就认真的看了a阿里的编码规范,发现以前我们编码都很难懂对方的,

现在都好懂了很多。在设计停词表的时候,我想通过遍历队友给我的文件中的内容来消掉停词表对影响,但是后来才发现,

同伴没有返回数据列表,所以又自己写,还有我们的代码确实有很多重复的,造成了代码看起来很臃肿,这次实验让我们都

发现自己身上好多不足,在以后的编码中,一定要规范编码,避免后期维护或者开发造成很大的资源浪费。

 

posted on 2018-10-17 17:25  我在这里176  阅读(262)  评论(0编辑  收藏  举报

导航