最开始使用的是SequenceMatcher来查找,之后改用MinHashLSH,速度有显著提升。
注意:
1.相似度阈值适当
2.最后输出的模板还需要人工修改,把其中可变部分去掉变为可以匹配的特殊字符,我使用的是.*,可以使用contains()
3.文本最好统一大小写
4.如果内容长短不一,可以考虑计算字符长度,然后对于每个长度区间设置不同的相似度阈值
import csv
#导入datasketch库的MinHash和MinHashLSH类,用于计算相似度和创建LSH索引
from datasketch import MinHash, MinHashLSH
import pandas as pd
def preprocess_text(text):
return text.split()
def find_templates(data, similarity_threshold):
# 创建LSH
lsh = MinHashLSH(threshold=similarity_threshold, num_perm=128)
templates = {}
minhashes = {}
for i, content in enumerate(data):
preprocessed_content = preprocess_text(content)
# 创建MinHash对象
m = MinHash(num_perm=128)
for word in preprocessed_content:
m.update(word.encode('utf8'))
# 检查是否已有相似的模板
result = lsh.query(m)
if result: # 如果LSH中已经存在相似的,就增加计数
templates[result[0]] += 1 # 只增加最相似的一个的计数
else: # 否则,添加为新的模板
lsh.insert(content, m)
templates[content] = 1
minhashes[content] = m
return templates
#打开文件并读取数据
with open('D:/file_p/data_ms_daxie.csv', 'r', encoding='utf-8') as file:
reader = csv.DictReader(file)
content_list = [row['content'].upper() for row in reader]
#调用`find_templates`函数,传入`content_list`和`similarity_threshold`作为参数,获得模板字典`templates`。
similarity_threshold = 0.65
templates = find_templates(content_list, similarity_threshold)
sorted_templates = sorted(templates.items(), key=lambda x: x[1], reverse=True)
df = pd.DataFrame(sorted_templates, columns=['template', 'count'])