翼度科技»论坛 编程开发 python 查看内容

软件工程第一次个人作业

6

主题

6

帖子

18

积分

新手上路

Rank: 1

积分
18
代码仓库第一次个人项目-论文查重系统


目录

PSP表格

PSP2.1Personal Software Process Stages预估耗时(分钟)实际耗时(分钟)Planning计划120100· Estimate· 估计这个任务需要多少时间480600Development开发180240· Analysis· 需求分析 (包括学习新技术)120240· Design Spec· 生成设计文档6050· Design Review· 设计复审3020· Coding Standard· 代码规范 (为目前的开发制定合适的规范)2010· Design· 具体设计90120· Coding· 具体编码4040· Code Review· 代码复审3040· Test· 测试(自我测试,修改代码,提交修改)120210Reporting报告3050· Test Repor· 测试报告2015· Size Measurement· 计算工作量2010· Postmortem & Process Improvement Plan· 事后总结, 并提出过程改进计划3035· 合计480600计算模块接口的设计与实现过程

本文使用SimHash和海明距离计算文章重复率,原理参考自:https://blog.csdn.net/wxgxgp/article/details/104106867
在分词与权重计算部分参考自jieba库的Github:https://github.com/fxsjy/jieba
用jieba库分词,并手动删除停用词
  1. stopWords = [' ', '!', ',', '.', '?', '!', '?', ',', '。', '\n', '\t', '\b', '"', '“', '”', ':', '《', '》', '<', '>']
  2. splitWords = jieba.lcut(source)
  3. splitWords = del_stopWords(splitWords, stopWords)
  4. #删除停用词
  5. def del_stopWords(split_sentence, stopWords):
  6.     i = 0
  7.     while (len(split_sentence) != 0):
  8.         
  9.         if (i >= len(split_sentence)):
  10.             break
  11.         
  12.         #匹配停用词
  13.         tmp = False
  14.         for n in stopWords:
  15.             if (split_sentence[i] == n):
  16.                 tmp = True
  17.                 break      
  18.         if (tmp):
  19.             split_sentence.pop(i)
  20.             continue
  21.         i += 1
  22.     return split_sentence
复制代码
用哈希函数转换分词为64位01字符串,并用jieba库的TF-IDF 算法对关键词进行抽取并计算权重(词频)
  1. # 哈希函数,输入单个分词
  2. def string_hash(source):
  3.         if source == "":
  4.             return 0
  5.         else:
  6.             x = ord(source[0]) << 7
  7.             m = 1000003
  8.             mask = 2**128 - 1
  9.             for c in source:
  10.                 x = ((x*m)^ord(c)) & mask
  11.             x ^= len(source)
  12.             if x == -1:
  13.                 x = -2
  14.             x = bin(x).replace('0b', '').zfill(64)[-64:]
  15.             
  16.             return str(x)
  17. #计算权重并加权,输入分词列表,可以去重并重新排序
  18. def count_weight(split_sentence, listSize):
  19.     keyWords = jieba.analyse.extract_tags("|".join(split_sentence), topK=listSize, withWeight=True)   
  20.     list_weightPluse = list()
  21.     a = list(map(int, string_hash(keyWords[0][0])))
  22.     for index in range(len(keyWords)):
  23.         tmp = list(map(int, string_hash(keyWords[index][0])))        
  24.         tmp = np.subtract(np.multiply(tmp, 2), 1)   #把0 1转换为-1 1            
  25.         list_weightPluse.append(np.multiply(tmp, keyWords[index][1]))
  26.    
  27.     return list_weightPluse
复制代码
用按位异或的方式计算海明距离,并计算相似度
  1. #合并加权后的哈希值,输入加权哈希值列表
  2. def mergeHash(list_weightPluse):
  3.     mergeHash_list = [0] * 64
  4.     for index in range(len(list_weightPluse)):
  5.         mergeHash_list = np.add(mergeHash_list, list_weightPluse[index])
  6.     return mergeHash_list
  7. #降维,大于0的输出1,小于等于0的输出0
  8. def reduction(mergeHash_list):
  9.     reduction_list = list()
  10.     for index in range(len(mergeHash_list)):
  11.         if (mergeHash_list[index] > 0):
  12.             reduction_list.append(1)
  13.         else:
  14.             reduction_list.append(0)
  15.    
  16.     return reduction_list
复制代码
文件的读取和结果文件的输出
  1. #海明距离
  2. def getDistance(list_1, list_2):
  3.     distance = 0
  4.     for index in range(len(list_1)):
  5.         if (list_1[index] ^ list_2[index] == 1):
  6.             distance += 1
  7.     return distance
  8.        
  9. #将汉明距离转换为相似度
  10. similarity = round((64 - hanmingDistance) / 64 * 100, 2)
复制代码
尝试输出结果

性能分析


单元测试

代码覆盖率



单元测试

因为分词的准确性直接关系到最后输出结果过的准确性,所以这次我主要测试分词功能是否完善


可见,当完整的词语被空格或者其它符号打断时,jieba库似乎不能很好的检测出来,如果是一些比较重要的词出现增删改的情况,很有可能会影响相似度的计算。
如果考虑在分词前将停用词去除,如果处理的文章里有英文,则会导致单词粘连的情况而无法正确识别英文单词。
也许有更好的方法解决这个问题,比如区分各种情况,再根据不同情况用不同的方法去除干扰词。

来源:https://www.cnblogs.com/Trigger-Kougamy/p/17216481.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具