软件工程实践之词频统计
生活随笔
收集整理的這篇文章主要介紹了
软件工程实践之词频统计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
GitHub倉庫地址:?https://github.com/softwarehsc/java1
PSP表格:
| 合計 | ? | 540 | ? |
| · Test Repor | · 測試報告 | 60 | 40 |
| · Test | · 測試(自我測試,修改代碼,提交修改) | 60 | 240 |
| · Size Measurement | · 計算工作量 | 10 | 10 |
| · Postmortem & Process Improvement Plan | · 事后總結, 并提出過程改進計劃 | 10 | 10 |
| · Estimate | · 估計這個任務需要多少時間 | 20 | 60 |
| · Design Spec | · 生成設計文檔 | 30 | 30 |
| · Design Review | · 設計復審 | 30 | 10 |
| · Design | · 具體設計 | 60 | 30 |
| · Coding Standard | · 代碼規范 (為目前的開發制定合適的規范) | 20 | 15 |
| · Coding | · 具體編碼 | 120 | 380 |
| · Code Review | · 代碼復審 | 60 | 30 |
| · Analysis | · 需求分析 (包括學習新技術) | 60 | 60 |
| Reporting | 報告 | 80 | 60 |
| Planning | 計劃 | 60 | 60 |
| Development | 開發 | 480 | 795 |
需求分析
統計文件的字符數:
- 只需要統計Ascii碼,漢字不需考慮
- 空格,水平制表符,換行符,均算字符
統計文件的單詞總數,單詞:至少以4個英文字母開頭,跟上字母數字符號,單詞以分隔符分割,不區分大小寫。
統計文件的有效行數:任何包含非空白字符的行,都需要統計。- 英文字母: A-Z,a-z
- 字母數字符號:A-Z, a-z,0-9
- 分割符:空格,非字母數字符號
- 例:file123是一個單詞, 123file不是一個單詞。file,File和FILE是同一個單詞
按照字典序輸出到文件result.txt:例如,windows95,windows98和windows2000同時出現時,則先輸出windows2000
- 輸出的單詞統一為小寫格式
輸出的格式為
實現過程
在eclipse建立文件main.java包含文件的讀入和主方法,調用lib.java中Print類的求characters、words、lines等的方法實現需求。
在WordCount類中首先通利用BufferedReader類通過從字符輸入流中讀取文本。將所有字母單詞轉換為小寫形式,再次讀取文檔,通過正則表達式
過濾掉標點符號分離出單詞存儲在lists列表中(List<String> lists = new ArrayList<String>();?)。最后通過wordsCount存儲單詞計數信息(Map<String, Integer> wordsCount = new TreeMap<String,Integer>();)
代碼說明
主方法類用于讀取文本文件統計字符個數 并調用Store類中的函數將words、lines等參數的值存入result.txt文件中: package main.java;import java.io.BufferedReader; import java.io.CharArrayWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.Reader; import java.io.Writer; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; import lib.java.Store; public class WordCount { public static void main(String[] args) throws Exception { try {//File file = new File(args[0]);//String pathname = file.getPath();Scanner scanner=new Scanner(System.in);String pathname=scanner.nextLine();Reader myReader = new FileReader(pathname);Reader myBufferedReader = new BufferedReader(myReader); //對文本處理CharArrayWriter tempStream = new CharArrayWriter();int i = -1;do {if(i!=-1)tempStream.write(i);i = myBufferedReader.read();if(i >= 65 && i <= 90){i += 32;}}while(i != -1);myBufferedReader.close();Writer myWriter = new FileWriter(pathname);tempStream.writeTo(myWriter);tempStream.flush();tempStream.close();myWriter.close();BufferedReader br = new BufferedReader(new FileReader(pathname)); int characterscount=0; int wordline = 0;int wordcount = 0;List<String> lists = new ArrayList<String>(); //存儲過濾后單詞的列表 String readLine = null;while((readLine = br.readLine()) != null){ wordline++;String[] wordsArr1 = readLine.split("[^a-zA-Z0-9]"); //過濾出只含有字母的單詞characterscount+=readLine.length();for (String word : wordsArr1) { if(word.length() != 0){ //去除長度為0的行while(!(word.charAt(0)>=97&&word.charAt(0)<=122)){word = word.substring(1, word.length());}if(word.length()>=4) wordcount++;lists.add(word); } } } br.close(); Map<String, Integer> wordsCount = new TreeMap<String,Integer>(); //單詞的詞頻統計 for (String li : lists) { if(wordsCount.get(li) != null){ wordsCount.put(li,wordsCount.get(li) + 1); }else{ wordsCount.put(li,1); } } Store st= new Store();st.store_result(wordsCount, wordline, wordcount, characterscount); }catch (Exception e) {e.printStackTrace();} } }
用Store類將輸出存入result.txt文件中:
1 package lib.java; 2 import java.io.BufferedWriter; 3 import java.io.File; 4 import java.io.FileWriter; 5 import java.util.ArrayList; 6 import java.util.Collections; 7 import java.util.Comparator; 8 import java.util.Map; 9 import java.util.Map.Entry; 10 11 public class Store { 12 13 public static void store_result(Map<String,Integer> oldmap,int wordline,int wordcount,int characterscount){ 14 ArrayList<Map.Entry<String,Integer>> list = new ArrayList<Map.Entry<String,Integer>>(oldmap.entrySet()); 15 Collections.sort(list,new Comparator<Map.Entry<String,Integer>>(){ 16 public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { 17 return o2.getValue() - o1.getValue(); //降序 18 } 19 }); 20 try { 21 //System.out.println(wordcount); 22 File file = new File("src//result.txt"); 23 BufferedWriter bi = new BufferedWriter(new FileWriter(file)); 24 bi.write("characters: "+(characterscount+wordline)+"\r\n"); 25 bi.write("words: "+wordcount+"\r\n"); 26 bi.write("lines: "+wordline+"\r\n"); 27 int k=0; 28 for(int i = 0; i<list.size(); i++){ 29 if(k>=10)break; 30 if(list.get(i).getKey().length()>3) 31 { 32 bi.write("<"+list.get(i).getKey()+">"+ ": " +list.get(i).getValue()+"\r\n"); 33 k++; 34 } 35 36 } 37 bi.close(); 38 }catch (Exception e) { 39 e.printStackTrace(); 40 } 41 42 } 43 }?
用EclEmma進行覆蓋率測試:
?
?
?命令行測試樣例:
?
?
?
??總結:
一開始沒有認真地理解題目意思,更沒有系統地分析解題過程。在沒有較為完善的計劃之前就直接寫程序導致走了很多的彎路。以后再做一個項目之前,哪怕這個項目結構不是很復雜,也應該寫好項目文檔,這樣在開發的過程中便能清楚地知道將要完成的模塊和更好的完善代碼。在測試過程中也能根據文檔快速地進行代碼優化。
?
?
?
?
?
?
?
?
?
?
?
?
轉載于:https://www.cnblogs.com/heshoucheng/p/9610814.html
總結
以上是生活随笔為你收集整理的软件工程实践之词频统计的全部內容,希望文章能夠幫你解決所遇到的問題。