1、要求地址
- 博客要求地址:https://www.cnblogs.com/happyzm/p/9626779.html
- Fork碼云項目地址:https://gitee.com/YeHei/PairProject-Java/tree/master
- 結對伙伴:余碩銘 博客地址:https://gitee.com/hellolv/PersonalProject-Java
2.PSP表格
PSP2.1 | 個人開發流程 | 預估耗費時間(分鐘) | 實際耗費時間(分鐘) |
---|---|---|---|
Planning | 計劃 | 30 | 30 |
· Estimate | 明確需求和其他相關因素,估計每個階段的時間成本 | 30 | 50 |
Development | 開發 | 550 | 650 |
· Analysis | 需求分析 (包括學習新技術) | 50 | 60 |
· Design Spec | 生成設計文檔 | 20 | 30 |
· Design Review | 設計復審 | 40 | 50 |
· Coding Standard | 代碼規范 | 10 | 15 |
· Design | 具體設計 | 45 | 85 |
· Coding | 具體編碼 | 100 | 110 |
· Code Review | 代碼復審 | 30 | 25 |
· Test | 測試(自我測試,修改代碼,提交修改) | 60 | 240 |
Reporting | 報告 | 30 | 40 |
· | 測試報告 | 15 | 15 |
· | 計算工作量 | 5 | 10 |
· | 并提出過程改進計劃 | 20 | 15 |
3.基本思路:
根據題目的要求,首先需要在對已經建好或導入的文檔進行讀取,這里就需要有文件讀取類,對文件中的所有進行讀取。
文件讀取之后,將相應的字符、單詞、行數等進行計數,并在判斷是否為單詞后,利用Map實現對單詞的詞頻統計,根據題目中要求進行計算。
對統計出來的數據輸出。
4.設計實現過程。
代碼組織:
1.WordCount類:對字符數、單詞數、行數等進行統計。
- countChar();統計字符數
- countWords();統計單詞數
- countLine();統計行數
- WordFre();實現單詞詞頻統計并出現次數從高到低排列
2.FileRead類:導入需要WordCount的文件,進行文件讀取等操作
- FileOutput();文件讀取
- FileInput();文件寫入
3.Main類:實現數據的輸出和文件路徑的輸入,并將WordCount類和FileInput類調用,實現功能。:
5、主要函數
- countChar() : 統計字符數量
public static int countChar(String str) { //統計字符數量 char s; int CharSum=0; for (int i = 0; i < str.length(); i++) { s=str.charAt(i); if (s>=32 && s<=126 || s=='\r' || s=='\n' || s=='\t') { CharSum++; } } return CharSum; }
- countWords():統計單詞數量
public int countWords(String[] str) { //統計單詞數量String str1=text;int WordsSum=0; String[] words=str1.split("\\s*[^0-9a-zA-Z]"); //調用正則表達式中spilt()方法來切分字段,將字符串中的單詞提取出來 for(String s:words) { if(s.matches("[a-zA-Z{4,}[z-zA-Z0-0]*")) { //調用matchs方法來判斷這個字符串是否在給定的正則表達式匹配 WordsSum++; } } return WordsSum; }
- countLine :統計行數
public static int countLine(String[] str) { //統計行數 int LineSum=0; for (int i = 0; i < str.length; i++) { while(str[i]!=null) { if(str[i].trim().length()==0 || str[i].trim().equals("")) { continue; //調用trim()方法將頭尾空格去掉,來判斷行數 } LineSum++; } } return LineSum; }
- WordFre :計算全文的單詞詞頻:
public List<Entry<String, Integer>> WordFre(){WordFre= new HashMap<String, Integer>();String t = text; String[] words = t.split("\\s"); // 將字符串進行分割 for (int i = 0; i < words.length; i++) { if (isWord(words[i])) { // 判斷是否為單詞,調用isWord函數 words[i] = words[i].trim().toLowerCase();//將大寫字母變成小寫字母 if (WordFre.get(words[i]) == null) { // 判斷之前Map中是否出現過該字符串 WordFre.put(words[i], 1);// 如果為新單詞,放入map中作為key值,value設為1 } else WordFre.put(words[i], WordFre.get(words[i]) + 1);//如果出現過的單詞則將value值+1 } }
- 將詞頻排序用list儲存:
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(WordFre.entrySet());//用list列表儲存鍵值 list.sort(new Comparator<Map.Entry<String, Integer>>() {//對list排序 @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { if (o1.getValue() == o2.getValue()) {//判斷詞頻是否相等 return o1.getKey().compareTo(o2.getKey());//字典序排列 } return o2.getValue() - o1.getValue();//降序排列 } }); return list;
6、結合在構建之法中學習到的相關內容與結對項目的實踐經歷,描述結對的感受,是否1+1>2?
- 實驗感受:在這次實驗中,感覺并有完全實現1+1大于2,由于時間間隔在國慶,所以沒有較長時間的在一起討論項目,對于gui界面沒有完善好