一、PSP表格
PSP2.1 | Personal Software Process Stages | 預估耗時(分鍾) | 實際耗時(分鍾) |
---|---|---|---|
Planning | 計划 | 10 | 30 |
· Estimate | · 估計這個任務需要多少時間 | 10 | 30 |
Development | 開發 | 900 | 740 |
· Analysis | · 需求分析 (包括學習新技術) | 60 | 120 |
· Design Spec | · 生成設計文檔 | 60 | 30 |
· Design Review | · 設計復審 | 60 | 60 |
· Coding Standard | · 代碼規范 (為目前的開發制定合適的規范) | 30 | 10 |
· Design | · 具體設計 | 120 | 30 |
· Coding | · 具體編碼 | 360 | 360 |
· Code Review | · 代碼復審 | 120 | 40 |
· Test | · 測試(自我測試,修改代碼,提交修改) | 90 | 90 |
Reporting | 報告 | 120 | 140 |
· Test Repor | · 測試報告 | 60 | 90 |
· Size Measurement | · 計算工作量 | 20 | 10 |
· Postmortem & Process Improvement Plan | · 事后總結, 並提出過程改進計划 | 40 | 40 |
· 合計 | 1020 | 910 |
二、計算模塊接口
1) 計算模塊接口的設計與實現過程。設計包括代碼如何組織,比如會有幾個類,幾個函數,他們之間關系如何,關鍵函數是否需要畫出流程圖?說明你的算法的關鍵(不必列出源代碼),以及獨到之處。
算法關鍵是DFA算法
First.在程序里接受命令行參數
- 讀入敏感詞詞匯文件和待檢測文件,將其中的內容轉化為字符串,敏感詞生成的字符串用於傳到
public Map WordsHashMap(Set<String> keyWords)
函數中生成哈希樹。
public class InputFile {
public static String inputfile(String name) throws IOException {
/*Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();*/
File f = new File(name);
FileInputStream word=new FileInputStream(f);
InputStreamReader wordreader=new InputStreamReader(word,"UTF-8");
BufferedReader reader = new BufferedReader(wordreader);//將文件通過絕對路徑讀出文件
String br =null;
String bt ="";
while ((br = reader.readLine()) != null) {//按行讀取
bt+=br;//按行讀入
bt+='\n';
}//將文本轉換為字符串
return bt;
}
- 將答案放到讀出文件中
public class OutputFile {
public OutputFile() {
}
public static void rwFile(String file, String filepath) {
FileWriter fw = null;
try {
fw = new FileWriter(filepath, true);
fw.write(file);
fw.flush();
} catch (FileNotFoundException var14) {
var14.printStackTrace();
} catch (IOException var15) {
var15.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException var13) {
var13.printStackTrace();
}
}
}
}
}
Second.敏感詞檢測
- 構造敏感詞
public class AddSensitiveWords {
public Set<String> addSensitiveWords(String[] ss)throws PinyinException{
Set<String>set = new HashSet<>();
/*System.out.println(sword.toString());*/
int j= ss.length;
for(int i=0;i<j;i++){
set.add(ss[i]);
}
for(int i=0;i<j;i++){
String word=ss[i];
/* System.out.println(ss[i]);*/
char key=word.charAt(0);
if(ChineseHelper.isChinese(key)){
set.add(PinyinHelper.convertToPinyinString(ss[i],"", PinyinFormat.WITHOUT_TONE));//將中文轉化成拼音
/*System.out.println(PinyinHelper.convertToPinyinString(ss[i],"", PinyinFormat.WITHOUT_TONE));*/
set.add(PinyinHelper.getShortPinyin(ss[i]));//將中文轉換成拼音開頭縮寫
}
}//將轉化后的新增詞匯加入set當中
return set;
}
}
- 實現敏感詞查詢
- 最簡易實現,計算總個數,檢測出與給出的敏感詞文件完全相同的形式的詞匯
- 查找英文敏感詞的大小寫形式
public boolean isContaintSensitiveWord(String txt, int matchType)
public List<String> getSensitiveWord(String txt, int matchType)
public int CheckSensitiveWord(String txt, int beginIndex, int matchType)
- 輸出個數、行數、敏感詞原型
Final.打包——jar包
2) 計算模塊接口部分的性能改進。記錄在改進計算模塊性能上所花費的時間,描述你改進的思路,並展示一張性能分析圖(由VS 2019、JProfiler或者Jetbrains系列IDE自帶的Profiler的性能分析工具自動生成),並展示你程序中消耗最大的函數。
消耗較大的是一些樹的生成,可通過減少結點或者最小路徑算法來簡化算法。
3) 計算模塊部分單元測試展示。展示出項目部分單元測試代碼,並說明測試的函數,構造測試數據的思路。並將單元測試得到的測試覆蓋率截圖,發表在博客中。
對AddSensitiveWordsTest函數的單元測試
package com;
import com.github.stuxuhai.jpinyin.PinyinException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.HashSet;
import java.util.Set;
import static org.junit.Assert.*;
public class AddSensitiveWordsTest {
@Before
public void setUp() throws Exception {
System.out.println("測試開始!");
}
@After
public void tearDown() throws Exception {
System.out.println("測試結束!");
}
@Test
public void addSensitiveWords()throws PinyinException {
String[] ss={"笨蛋","sMart","小可愛"};
Set<String>aa =new HashSet<>();
aa.add("笨蛋");
aa.add("sMart");
aa.add("小可愛");
aa.add("bendan");
aa.add("xiaokeai");
aa.add("bd");
aa.add("xka");
AddSensitiveWords awords=new AddSensitiveWords();
assertEquals(aa,awords.addSensitiveWords(ss));
}
}
按照目前這個函數的功能是將一些中文的敏感詞進行擴充,比如將中文轉換成拼音和拼音縮寫,而英文則不做處理。
對InputFileTest函數的單元測試
package com;
import java.io.IOException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class InputFileTest {
InputFile input = new InputFile();
public InputFileTest() {
}
@Before
public void setUp() throws Exception {
System.out.println("測試開始");
}
@After
public void tearDown() throws Exception {
System.out.println("測試結束");
}
@Test
public void inputfile() throws IOException {
InputFile var10001 = this.input;
Assert.assertEquals("我盡力了哈哈哈害人不淺太過分了要少熬夜dsqbdquidq@#!#!&*#!hhhh1hw1uhw1\n", InputFile.inputfile("C:\\Users\\24427\\Desktop\\test.txt"));
}
}
對於讀到的文件的數據轉換成字符串形式並返回主函數,驗證是否有亂碼或者換行符錯誤。
4) 計算模塊部分異常處理說明。在博客中詳細介紹每種異常的設計目標。每種異常都要選擇一個單元測試樣例發布在博客中,並指明錯誤對應的場景。
對AddSensitiveWords函數輸出結果異常,因為在處理時每個字符串之后都加了一個換行符。但是對解題過程影響不大。
三、心得
- 不要想着一步登天,一個看似很困難的項目可以分為好幾步去做。要先去分析這個項目,先找到它的基本功能,在將基本功能拆解,之后再進行功能的拓展。比如這個敏感詞檢測的題目,可以先實現將沒有任何變形的敏感詞找到並統計個數,再實現更加進階的功能。二步驟可以拆解為:輸入文件存入一個設定的變量中->構建敏感詞的hashmap樹->在待檢測文件中檢測敏感詞,並輸出總數->輸出答案到指定文件的位置。而每個步驟可以分為一個類,這樣能使自己的思路更清楚。
- 無論用哪一門語言,基礎知識得先掌握。不然一下直奔算法,將會看不懂網上所提供的代碼的每一步含義。還需要了解語言的一些原有的函數,這會在打代碼的過程中減少很多困難並減少代碼量。