| 這個作業屬於哪個課程 | 軟件工程 |
|---|---|
| 這個作業要求在哪里 | 作業要求 |
| 這個作業的目標 | 論文查重 |
PSP表格
| PSP2.1 | Personal Software Process Stages | 預估耗時(分鍾) | 實際耗時(分鍾) |
|---|---|---|---|
| Planning | 計划 | 40 | 30 |
| Estimate | 估計這個任務需要多少時間 | 10 | 10 |
| Development | 開發 | 300 | 300 |
| Analysis | 需求分析 (包括學習新技術) | 120 | 180 |
| Design Spec | 生成設計文檔 | 20 | 20 |
| Design Review | 設計復審 | 20 | 30 |
| Coding Standard | 代碼規范 (為目前的開發制定合適的規范) | 10 | 10 |
| Design | 具體設計 | 40 | 3 |
| Coding | 具體編碼 | 240 | 240 |
| Code Review | 代碼復審 | 60 | 30 |
| Test | 測試(自我測試,修改代碼,提交修改) | 60 | 50 |
| Reporting | 報告 | 90 | 120 |
| Test Repor | 測試報告 | 20 | 20 |
| Size Measurement | 計算工作量 | 20 | 10 |
| Postmortem & Process Improvement Plan | 事后總結, 並提出過程改進計划 | 60 | 70 |
| 合計 | 1110 | 1150 |
前言
一開始我以為查重需要匹配基於web的數據庫,或是自行建庫,沒想到本次作業直接降低一個維度,只需要在本地文件中提取txt文檔,然后用一個簡單的算法查重,作業重點在於編程環境的運行,所以難度不大
編程接口
本人用的是pycharm,但是文本查重的重點在於匹配
jieba
這就涉及到分詞的算法,這里我推薦結巴分詞,具體是什么,我想dddd,這里也就不多贅述
如果環境和我一樣,那這里推薦pip安裝
1. pip install jieba
gensim
還有gensim需要安裝,這是一個多功能的包,包括dictionary.doc2bow和similarities.Similarity,可以有效解決論文查重的大部分問題
簡介在此
1. pip install gensim
代碼解析
similarities
通過余弦來計算其相似度

1. def calc_similarity(text1,text2):
2. corpus=convert_corpus(text1,text2)
3. similarity = gensim.similarities.Similarity('-Similarity-index', corpus, num_features=len(dictionary))
4. test_corpus_1 = dictionary.doc2bow(text1)
5. cosine_sim = similarity[test_corpus_1][1]
6. return cosine_sim
re.match
由於對比對象為中文或英文單詞,因此應該對讀取到的文件數據中存在的換行符\n、標點符號過濾掉,這里選擇用正則表達式來匹配符合的數據
1. def filter(str):
2. str = jieba.lcut(str)
3. result = []
4. for tags in str:
5. if (re.match(u"[a-zA-Z0-9\u4e00-\u9fa5]", tags)):
6. result.append(tags)
7. else:
8. pass
9. return result
dictionary
Doc2Bow是gensim中封裝的一個方法,主要用於實現Bow模型。
1. text1='John likes to watch movies. Mary likes too.'
2. text2='John also likes to watch football games.'
測試結果
在txt_code中輸入
1. path1 = "D:/Personal project/txt/orig.txt" #論文原文的文件的絕對路徑(作業要求)
2. path2 = "D:/Personal project/txt/orig_0.8_add.txt" #抄襲版論文的文件的絕對路徑
然后得出相似度結果

接口的性能改進
通過Run --> Profile ‘xxxx’:來進行時間消耗的分析


關注到filter函數:由於cut和lcut暫時找不到可提到的其他方法(jieba庫已經算很強大了),暫時沒辦法進行改進,因此考慮對正則表達式匹配改進
這里是先用lcut處理后再進行匹配過濾,這樣做顯得過於臃腫,可以考慮先匹配過濾之后再用lcut來處理
1. def filter(string):
2. pattern = re.compile(u"[^a-zA-Z0-9\u4e00-\u9fa5]")
3. string = pattern.sub("", string)
4. result = jieba.lcut(string)
5. return result
進行對比后,明顯發現速度提升了許多

單元測試
重新設計源代碼,新建txt_new_check
然后新建單元測試文件unit_test.py

可以發現預測值為0.99正確:

代碼覆蓋率

運行過程中異常處理
其實大部分unresolved reference之類的問題可以通過pip的使用解決
附上教程
在讀取指定文件路徑時,如果文件路徑不存在,程序將會出現異常,因此可以在讀取指定文件內容之前先判斷文件是否存在,若不存在則做出響應並且結束程序。
這里引入os.path.exists()方法用於檢驗文件是否存在:
1. def main_test():
2. path1 = input("輸入論文原文的文件的絕對路徑:")
3. path2 = input("輸入抄襲版論文的文件的絕對路徑:")
4. if not os.path.exists(path1) :
5. print("論文原文文件不存在!")
6. exit()
7. if not os.path.exists(path2):
8. print("抄襲版論文文件不存在!")
9. exit()
