軟工實踐第五次作業-爬蟲和自定義詞頻統計
阿新 • • 發佈:2018-10-11
system 屬性 project html標簽 ttr ont 標題 改進 提交
軟工實踐第五次作業-爬蟲和自定義詞頻統計
題目地址:https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1816W/homework/2160
Git地址
031602111傅海濤
031602115黃家雄
PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
---|---|---|---|
Planning | 計劃 | ||
? Estimate | ? 估計這個任務需要多少時間 | 600 | 750 |
Development | 開發 | ||
? Analysis | ? 需求分析 (包括學習新技術) | 40 | 50 |
? Design Spec | ? 生成設計文檔 | 10 | 20 |
? Design Review | ? 設計復審 | 10 | 20 |
? Coding Standard | ? 代碼規範 (為目前的開發制定合適的規範) | 10 | 20 |
? Design | ? 具體設計 | 60 | 100 |
? Coding | ? 具體編碼 | 400 | 550 |
? Code Review | ? 代碼復審 | 10 | 20 |
? Test | ? 測試(自我測試,修改代碼,提交修改) | 30 | 200 |
Reporting | 報告 | ||
? Test Repor | ? 測試報告 | 10 | 10 |
? Size Measurement | ? 計算工作量 | 20 | 20 |
? Postmortem & Process Improvement Plan | ? 事後總結,並提出過程改進計劃 | 30 | 40 |
合計 | 630 | 1050 |
分工
使用工具:Java
傅海濤:爬蟲,詞頻統計
黃家雄:詞頻統計
解題思路
爬蟲
網站源碼查看分析
爬蟲實現分析
- 獲取全部代碼
- 獲取Title和摘要url:篩選<dt> 標簽,得到的內容contents清洗掉所有html標簽,剩下的就是Title的內容,存到名為titles的List裏.在把之前得到的content中篩選 <a> 標簽的 href屬性,存到名為urls的List裏.
- 將得到的urls傳到封裝好的函數中進行爬蟲.操做和上面差不多,篩選<div id="abstract">的內容,在清洗掉html的標簽就可以獲得所有的摘要.把內容存到 名為abstracts的List裏.
- 將獲得的titles和abstracts進行拼接並生成到result.txt中.
獲取title和url
//獲取title和url
public static void getTitle(String content){
Pattern r = Pattern.compile("<dt [^>]*?>[\\w\\W]*?<\\/dt>");
Matcher m = r.matcher(content);
while ( true ) {
if (m.find()) {
Pattern r2 = Pattern.compile("<a [^>]*?>[\\w\\W]*?<\\/a>");
Matcher m2 = r2.matcher(m.group(0));
//輸出標題
if (m2.find()) {
String url = GetContent.match(m2.group(0), "a", "href");
// System.out.println(url);
urls.add(url);
//篩除html標簽
String title = outHtml(m2.group(0));
titles.add(title);
// System.out.println(title);
}
} else {
break;
}
}
}
獲取html標簽裏面的某個屬性值
//獲取html標簽裏面的某個屬性值
public static String match(String source, String element, String attr) {
String result = new String();
String reg = "<" + element + "[^<>]*?\\s" + attr + "=[‘\"]?(.*?)[‘\"].*?>";
Matcher m = Pattern.compile(reg).matcher(source);
while (m.find()) {
String r = m.group(1);
result = r;
}
return result;
}
清除html標簽
//篩除html標簽
public static String outHtml(String cont){
String con =cont.replaceAll("</?[^>]+>", "");
return con;
}
詞頻統計
分析
- 主要功能和之前的一次作業要求一樣,在此基礎上添加新的自定義命令,之前的統計只是把單詞的數量累加,就好比權重為1,只需要把按照命令把title的累加*10.至於輸出n個就只需在原來的基礎上面封裝個函數並把n傳進.至於詞組統計則需要將原來單詞統計上修改判斷單詞數量並拼接輸出.
- 關鍵代碼分析
軟工實踐第五次作業-爬蟲和自定義詞頻統計