问题背景

批量下载文档的时候未能自动的命名,导致文件整理比较麻烦;除了文档外,还下载了文档索引文件,如下所示:

索引信息(TXT文件)

文档信息(PDF文件)

问题需求

通过利用索引信息,匹配索引文档中与文档库中题目最相似的字符串,并利用最相似的字符串对文档库中的文档进行重命名。

测试环境

|----PyCharm Python3.8
|----系统:Windows

匹配算法

为了快速计算两句话的相似度,且不考虑训练模型;通过资料查找,确定采用余弦匹配算法,代码如下:

文件命名为 cosin_computer.py

import math
import redef compute_cosine(text_a, text_b):# 找单词及词频words1 = text_a.split(' ')words2 = text_b.split(' ')# print(words1)words1_dict = {}words2_dict = {}for word in words1:# word = word.strip(",.?!;")word = re.sub('[^a-zA-Z]', '', word)word = word.lower()# print(word)if word != '' and word in words1_dict:num = words1_dict[word]words1_dict[word] = num + 1elif word != '':words1_dict[word] = 1else:continuefor word in words2:# word = word.strip(",.?!;")word = re.sub('[^a-zA-Z]', '', word)word = word.lower()if word != '' and  word in words2_dict:num = words2_dict[word]words2_dict[word] = num + 1elif word != '':words2_dict[word] = 1else:continue# print(words1_dict)# print(words2_dict)# return Truedic1 = sorted(words1_dict.items(), key=lambda asd: asd[1], reverse=True)dic2 = sorted(words2_dict.items(), key=lambda asd: asd[1], reverse=True)# print(dic1)# print(dic2)# 得到词向量words_key = []for i in range(len(dic1)):words_key.append(dic1[i][0])  # 向数组中添加元素for i in range(len(dic2)):if dic2[i][0] in words_key:# print 'has_key', dic2[i][0]passelse:  # 合并words_key.append(dic2[i][0])# print(words_key)vect1 = []vect2 = []for word in words_key:if word in words1_dict:vect1.append(words1_dict[word])else:vect1.append(0)if word in words2_dict:vect2.append(words2_dict[word])else:vect2.append(0)# print(vect1)# print(vect2)# 计算余弦相似度sum = 0sq1 = 0sq2 = 0for i in range(len(vect1)):sum += vect1[i] * vect2[i]sq1 += pow(vect1[i], 2)sq2 += pow(vect2[i], 2)try:result = round(float(sum) / (math.sqrt(sq1) * math.sqrt(sq2)), 2)except ZeroDivisionError:result = 0.0# print(result)return result

参考链接:link.

主函数 main.py

import os
from Py20201027_TextProcess.cosin_computer import compute_cosine #根据自己的情况做调整
# 加载文件
file = open('E:/F01_Researches/R01_ProductServiceSystem/PSS09-会议文献/CIRP Conference on Industrial Product-Service Systems/11th_ScienceDirect_citations.txt','r',encoding='UTF-8')
# 获取所有行
lines = file.readlines()
line_count = len(lines)
print(line_count)
'''
# 打印前10行
for i in range(1,line_count,11):print(i,'-->',lines[i])if i > 100:break
'''file_root = 'E:/F01_Researches/R01_ProductServiceSystem/PSS09-会议文献/CIRP Conference on Industrial Product-Service Systems'
# 加载文件列表
files = os.listdir(os.path.join(file_root,'11th'))
# 标题预处理
new_files = []
for i in range(len(files)):temp = files[i].split('-')temp = temp[0:-1]x = ' '.join(temp)new_files.append(x)# 行预处理
new_lines = []
for i in range(len(lines)):temp = lines[i].split('-')x = ' '.join(temp)new_lines.append(x)
#for i in range(10):
#    print("Before: ", files[i], '|-->After: ', new_files[i])count = 1
# 相似度计算
for i in range(1,line_count,11): #遍历text中的每个题目print('第',count,'-->',lines[i]) #打印当前行max_sim = 0.max_str_index = 0print('\t\tSim: ',end='\t')for j in range(len(new_files)):sim = compute_cosine(lines[i],new_files[j])print(sim, end='\t')if sim > max_sim:max_sim = simmax_str_index = j#print('\t',new_files[j],'\t','相似度:',sim)print('\n\t\t相似度最高的为:',files[max_str_index],'\t最大相似度为:',max_sim)count += 1

部分匹配结果

通过计算两个句子的相似度进行文件命批量修改相关推荐

  1. 两个list取交集_利用jieba计算两个句子的相似度

    1.余弦相似度,又称为余弦相似性,是通过计算两个向量的夹角余弦值来评估他们的相似度.余弦相似度将向量根据坐标值,绘制到向量空间中,如最常见的二维空间. # -*- coding: utf-8 -*- ...

  2. 编程之美3.3 计算两个字符串的相似度

          假如有两个字符串分别是:abcd,bbcd,那么,这两个字符串不相同的字符个数是1,即第一个字符时不相同的,定义字符串的相似度为 1 / (x + 1),其中,x 就是不相同的字符个数. ...

  3. C++/JAVA 计算两篇文章的相似度

    C++/JAVA 计算两篇文章的相似度 这位少侠,要不要进店瞧瞧? 实验介绍及思路 问题描述: 编写程序,计算任意两篇文章的相似度. 基本思路: 利用余弦相似度来计算其相似度. 完整代码 C++ 代码 ...

  4. 使用face_recognition:摄像头实时给人脸打马赛克、疲劳检测、活体检测(张嘴检测)、计算两张人脸之间的相似度、人脸校准

    日萌社 人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新) 使用face_recognition进行摄像头实时给人脸打马赛 ...

  5. 计算两组标签/关键词 相似度算法

    原文连接 http://www.zhaochao.net/index.php/2016/02/05/14/ 写作背景 标签在互联网行业有大量的应用,给博客打标签,给商品打标签,给新闻打标签.通常每篇文 ...

  6. 【python 图片搜索】python 快速计算两个图片的相似度

    一.图片相似度检测算法原理 我们日常中处理的数据大多数是文本和图片,既然文本有文本相似度,图片肯定也有图片相似度呀,是不是.下面介绍图片相似度检测的算法:检查两个图片的相似度,一个简单而快速的算法:感 ...

  7. java数组相似度_Java 计算两个字符串的相似度

    问题 许多程序会大量使用字符串.对于不同的字符串,我们希望能够有办法判断其相似程度.我们定义了一套操作方法来把两个不相同的字符串变得相同,具体的操作方法为: 1.修改一个字符(如把"a&qu ...

  8. python 两个word文档之间的相似度_如何用 word2vec 计算两个句子之间的相似度?

    现在是2018年7月,在这里总结这个问题下已有的答案,并补充一些2017年以来这方面研究的新进展. 从大类上分,计算句子相似度的方法可以分为两类: 1)无监督的方法,即不使用额外的标注数据,常用的方法 ...

  9. Word2vec 计算两个文本之间相似度

    安装gensim 并且有pyemd,详情见下 def wmdistance(self, document1, document2):"""Compute the Word ...

最新文章

  1. [zt] petshop4.0 详解之三
  2. Java微服务 vs Go微服务,究竟谁更强!?
  3. 《LINQ实战》译者感言
  4. ironic驱动-IMPITool
  5. 17秋福师《计算机应用基础》在线作业一,福师17春秋学期《计算机应用基础》在线作业一...
  6. 你以为ACI=SDN?大错特错!
  7. 文件操作工具类FileUtil
  8. linux 并行执行脚本,在bash / linux中并行运行shell脚本
  9. boost::process::args相关的测试程序
  10. nyoj13-Fibonacci数
  11. Weblogic的安装与卸载
  12. Sublime Text一个小插件——SideBarEnhancements
  13. three.js 来源目光(十三)Math/Ray.js
  14. Atitit 大数据索引技术attilax总结 目录 1. 面临的问题 2 1.1. 找到太多数据 2 1.2. 不支持多字段搜索 2 1.3. 不支持模糊搜索 2 1.4. 聚合搜索 2 1.5
  15. osm数据下载 python_用Python编写小工具下载OSM路网数据
  16. 华为企业网络常用图标大全(附PPT下载)
  17. Java 表格文字垂直居中_itextpdf中表格中单元格的文字水平垂直居中的设置
  18. Canvas基础教程
  19. 静态网网页设计成品下载
  20. 如何安装鸿蒙应用,华为鸿蒙OS系统手机怎么安装第三方的应用程序?

热门文章

  1. 电子计算机为什么123安不出来,hao123打不开,hao123怎么打不开 - hao123打不开 - 安全专题...
  2. 操作系统的备份与还原
  3. python中execute是啥_类Python中的Execute方法
  4. QQ空间伤感日志_其实放手不是不爱你
  5. 7、XPOSED三、在靠谱助手上使用
  6. python打印数字正方形_python打印空心正方形-女性时尚流行美容健康娱乐mv-ida网...
  7. 开学季,宿舍床垫怎么选?学会这5招,晚上睡的更香甜~
  8. gabor滤波器简介
  9. 设计大赛看到撒娇的萨科达斯科
  10. 2022换换二手苹果手机回收报价单