繁体   English   中英

比较Python中的两个文本块

[英]Comparing two blocks of text in Python

我有一个可以从各种来源获得信息的系统。 我想确保我没有添加确切(或极其相似)的信息。 这是一个例子:

文字A:有一天,一个人走过山坡,看到了阳光

文字B:有一天,一个人走过山坡,看到了阳光

文字C:一个星期,一位女士在山上望着阳光

在这种情况下,我想获取一些信息块之间的差异的数值。 从那里,我可以应用以下逻辑:

  1. 将文本添加到数据库时,请检查数据库中的现有值
  2. 如果发现值非常相似,则不要添加
  3. 如果值看起来足够不同,则添加

因此,我们最终在数据库中获得了不同的信息,而不是重复信息,但是我们留有少量余地。

谁能告诉我如何在Python中尝试这种方法?

查看您的问题, difflib.SequenceMatcher.ratio()可能会派上用场。

这个漂亮的例程,使用两个字符串并计算在[0,1]范围内的相似性索引

快速演示

>>> for a,b in list(itertools.product(st, st)):
    print "Text 1 {}".format(a)
    print "Text 2 {}".format(b)
    print "Similarity Index {}".format(difflib.SequenceMatcher(None, a,b).ratio())
    print '-'*80


Text 1 One day a man walked over the hill and saw the sun
Text 2 One day a man walked over the hill and saw the sun
Similarity Index 1.0
--------------------------------------------------------------------------------
Text 1 One day a man walked over the hill and saw the sun
Text 2 One week a woman looked over a hill and saw the sun
Similarity Index 0.831683168317
--------------------------------------------------------------------------------
Text 1 One day a man walked over the hill and saw the sun
Text 2 One day a man walked over a hill and saw the sun
Similarity Index 0.959183673469
--------------------------------------------------------------------------------
Text 1 One week a woman looked over a hill and saw the sun
Text 2 One day a man walked over the hill and saw the sun
Similarity Index 0.831683168317
--------------------------------------------------------------------------------
Text 1 One week a woman looked over a hill and saw the sun
Text 2 One week a woman looked over a hill and saw the sun
Similarity Index 1.0
--------------------------------------------------------------------------------
Text 1 One week a woman looked over a hill and saw the sun
Text 2 One day a man walked over a hill and saw the sun
Similarity Index 0.868686868687
--------------------------------------------------------------------------------
Text 1 One day a man walked over a hill and saw the sun
Text 2 One day a man walked over the hill and saw the sun
Similarity Index 0.959183673469
--------------------------------------------------------------------------------
Text 1 One day a man walked over a hill and saw the sun
Text 2 One week a woman looked over a hill and saw the sun
Similarity Index 0.868686868687
--------------------------------------------------------------------------------
Text 1 One day a man walked over a hill and saw the sun
Text 2 One day a man walked over a hill and saw the sun
Similarity Index 1.0
--------------------------------------------------------------------------------

有几个python库可以帮助您。 看看这个问:

Levisthein距离是一种常用算法。 我发现nysiis算法非常有用。 尤其是要在数据库中保存字符串表示形式时。

链接将为您提供出色的概述:

一种原始的方式...但是您可以遍历字符串,比较另一个字符串中的等效顺序单词,然后得到匹配与失败的比率:

>>> aa = 'One day a man walked over the hill and saw the sun'
>>> bb = 'One day a man walked over a hill and saw the sun'
>>> matches = [a == b for a, b in zip(aa.split(' '), bb.split(' '))]
>>> matches
[True, True, True, True, True, True, False, True, True, True, True, True]
>>> sum(matches)
11
>>> len(matches)
12

因此,在此示例中,您可以看到匹配的11/12个单词。 然后,您可以设置通过/失败级别

在python或任何其他语言中,散列是删除重复项的最简单方法。

您可以维护一个已添加哈希表。 当您添加另一个时,只需检查是否存在哈希。

为此使用hashlib

添加hashlib用法示例

import hashlib
m1 = hashlib.md5()
m1.update(" the spammish repetition")
print m1.hexdigest()

m2 = hashlib.md5()
m2.update(" the spammish")
print m2.hexdigest()

m3 = hashlib.md5()
m3.update(" the spammish repetition")
print m3.hexdigest()

d21fe4d39740662f11ad2cf8035b471b
03498704df59a124ee6ac0681e64841b
d21fe4d39740662f11ad2cf8035b471b

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM