課程:軟件工程1916|W(福州大學)python
要求:連接git
目標:學習如何使用Github和爬蟲工具,體驗結對編程算法
代碼簽入記錄:
PairProject1編程
PairProject2
數組
PSP2.1 | Personal Software Process Stages | 預估耗時(分鐘) | 實際耗時(分鐘) |
---|---|---|---|
Planning | 計劃 | 30 | 42 |
• Estimate | • 估計這個任務須要多少時間 | 30 | 42 |
Development | 開發 | 700 | 1085 |
• Analysis | • 需求分析(包括學習新技術) | 120 | 150 |
• Design Spec | • 生成設計文檔 | 50 | 50 |
• Coding Review | • 設計複審 | 20 | 15 |
• Coding Standard | 代碼規範 (爲目前的開發制定合適的規範) | 30 | 30 |
• Design | • 具體設計 | 120 | 60 |
• Coding | • 具體編碼 | 240 | 600 |
• Code Review | • 代碼複審 | 60 | 60 |
• Test | • 測試(自我測試,修改代碼,提交修改) | 60 | 120 |
Reporting | 報告 | 55 | 100 |
• Test Report | • 測試報告 | 20 | 30 |
• Size Measurement | • 計算工做量 | 10 | 10 |
• Postmortem & Process Improvement Plan | • 過後總結, 並提出過程改進計劃 | 25 | 60 |
合計 | 785 | 1227 |
在剛開始拿到題目後,咱們小組決定使用C++實現。函數
代碼擁有countChar,countWord,countLine,countFre這四個函數,這四個函數之間是相互獨立的。單元測試則是將各個函數的功能分別進行白盒測試,每一個函數的功能通過10組數據的測試。
算法的關鍵在於對讀取的字符進行判斷。流程圖以下:工具
咱們小組在改進程序性能上所花費的時間大約爲60分鐘,所以咱們只是想出了改進的思路,而具體的實現可能還略有欠缺。性能
性能分析圖以下:單元測試
消耗最大的函數:countFre,即統計單詞詞頻的函數
選取countFre做爲關鍵代碼,如下爲代碼:
int countFre(FILE *file) { fseek(file, 0, SEEK_SET); char c; string temp="";//temp變量用於保存讀入的字符 int alacnt = 0;//alacnt變量用於統計當前temp字符串已有字符數 int w_cnt = 0;//w_cnt變量用於統計不重複的單詞的數目 while (fscanf_s(file, "%c", &c, sizeof(char)) != EOF) { if (c < 0 || c>255) continue;//非ascii碼跳過統計 if (isalpha(c) || isdigit(c) && alacnt >= 4) {//若是字符c爲字母或者字符c爲數字而且temp字符串已有4個或以上字母,則將字符c加入字符串 temp += c; alacnt++; } if (alacnt < 4 && !isalpha(c)) {//若是字符c不爲字母而且temp字符串已有字符數小於4,則清空字符串 temp = ""; alacnt = 0; } if (alacnt >= 4 && !(isalpha(c) || isdigit(c))) {//若是字符c不爲字母或者數字而且temp字符串已有字符數大於4,即爲分隔符的狀況下 bool found = false;//found用於表示該單詞是否曾出現過 for (int i = 0; i < (int)temp.length(); i++)//將該單詞轉換爲全小寫 temp[i] = tolower(temp[i]); for (int i = 0; i < w_cnt; i++) {//查找該單詞是否已存在 if (temp == word[i]) { times[i]++; found = true; break; } } if (!found) {//若是不存在就將該單詞添加到數組中 word[w_cnt] = temp; times[w_cnt]++; w_cnt++; } temp = ""; alacnt = 0; } } if (alacnt >= 4) {//判斷到文件結尾的最後一個字符串是否爲單詞 bool found = false; for (int i = 0; i < (int)temp.length(); i++) temp[i] = tolower(temp[i]); for (int i = 0; i < w_cnt; i++) { if (temp == word[i]) { times[i]++; found = true; break; } } if (!found) { word[w_cnt] = temp; times[w_cnt]++; w_cnt++; } } for (int i = 0; i < w_cnt - 1; i++) {//對詞頻數組進行排序 for (int j = 0; j < w_cnt - 1 - i; j++) { if (times[j] > times[j + 1] || times[j] == times[j + 1] && word[j] < word[j + 1]) { int temp; temp = times[j]; times[j] = times[j + 1]; times[j + 1] = temp; string s_temp; s_temp = word[j]; word[j] = word[j + 1]; word[j + 1] = s_temp; } } } return w_cnt; }
以爬蟲爬取到的論文做爲測試數據,每一個功能有10組測試數據。
void test() { for (char i = '0'; i <= '9'; i++) { char s[] = "testx.txt"; s[4] = i; FILE *in; errno_t err; if ((err = fopen_s(&in,s, "r")) != 0) { printf("Open file failed!"); exit(0); } printf("%s\n", s); printf("characters: %d\n", countChar(in)); printf("words: %d\n", countWord(in)); printf("lines: %d\n", countLine(in)); printf("\n"); fclose(in); } } int main() { test(); system("pause"); return 0; }
在此次做業過程當中,咱們小組也遇到了許多困難。起初,我和個人隊友都不會使用爬蟲工具爬取
論文信息,通過咱們的協商以後,個人隊友選擇去學習如何使用爬蟲工具,而我則先負責WordCount相關功能的編寫。再如咱們用爬蟲獲得的結果作測試數據時發現程序會崩潰,後來逐漸搜索數據發現爬取所獲得的論文結果中存在非ascii碼字符,因而咱們修改代碼使得程序可以正常運行。
我認爲個人隊友很是可靠,他對新知識的學習能力很是出色。此次做業過程就是由他來學習爬蟲的寫法,而且他也沒有辜負個人指望成功地完成了這項任務。可是咱們可能須要更多的溝通。
咱們小組在附加題方面想對數據的圖形可視化作出一些努力。咱們有如下兩種思路:
效果以下圖:
因爲時間緊迫,咱們並無具體實現。