Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用LSH的近似字符串匹配_Python_String_Hash_Locality Sensitive Hash - Fatal编程技术网

Python 使用LSH的近似字符串匹配

Python 使用LSH的近似字符串匹配,python,string,hash,locality-sensitive-hash,Python,String,Hash,Locality Sensitive Hash,我想使用位置敏感哈希近似匹配字符串。我有许多大于10M的字符串可能包含拼写错误。对于每个字符串,我希望与所有其他字符串进行比较,并根据某个阈值选择具有编辑距离的字符串 也就是说,朴素的解决方案需要O(n^2)比较。为了避免这个问题,我考虑使用对位置敏感的哈希。然后相似的字符串将产生相同的bucket,我只需要在bucket内搜索。所以它是O(n*C),其中C是桶的大小 但是,我不理解如何表示字符串。如果是文本,我会用向量空间表示。我的主要问题是,如果使用LSH,然后使用字符串的适当向量表示,这是

我想使用位置敏感哈希近似匹配字符串。我有许多大于10M的字符串可能包含拼写错误。对于每个字符串,我希望与所有其他字符串进行比较,并根据某个阈值选择具有编辑距离的字符串

也就是说,朴素的解决方案需要O(n^2)比较。为了避免这个问题,我考虑使用对位置敏感的哈希。然后相似的字符串将产生相同的bucket,我只需要在bucket内搜索。所以它是O(n*C),其中C是桶的大小

但是,我不理解如何表示字符串。如果是文本,我会用向量空间表示。我的主要问题是,如果使用LSH,然后使用字符串的适当向量表示,这是否容易处理


我是否能够使用已实现的库执行此任务?或者这取决于我的问题,所以我必须自己实施?有任何python包可以做到这一点吗?

我在这方面找到的最好的学术资源是海量数据集的挖掘,它提供了对位置敏感的哈希和minhashing的极好概述

简单地说,这个想法是取几个字符串,对这些字符串进行矢量化,然后在得到的向量上传递一个滑动窗口。如果两个向量在同一窗口位置具有相同的值,则将它们标记为更细粒度相似性分析的候选向量

Python datasketch库中有一个很好的实现(
pip安装datasketch
)。下面的示例显示了您可以捕捉模糊字符串相似性:

from datasketch import MinHash, MinHashLSH
from nltk import ngrams

data = ['minhash is a probabilistic data structure for estimating the similarity between datasets',
  'finhash dis fa frobabilistic fata ftructure for festimating the fimilarity fetween fatasets',
  'weights controls the relative importance between minizing false positive',
  'wfights cfntrols the rflative ifportance befween minizing fflse posftive',
]

# Create an MinHashLSH index optimized for Jaccard threshold 0.5,
# that accepts MinHash objects with 128 permutations functions
lsh = MinHashLSH(threshold=0.4, num_perm=128)

# Create MinHash objects
minhashes = {}
for c, i in enumerate(data):
  minhash = MinHash(num_perm=128)
  for d in ngrams(i, 3):
    minhash.update("".join(d).encode('utf-8'))
  lsh.insert(c, minhash)
  minhashes[c] = minhash

for i in xrange(len(minhashes.keys())):
  result = lsh.query(minhashes[i])
  print "Candidates with Jaccard similarity > 0.4 for input", i, ":", result
这将返回:

Candidates with Jaccard similarity > 0.4 for input 0 : [0, 1]
Candidates with Jaccard similarity > 0.4 for input 1 : [0, 1]
Candidates with Jaccard similarity > 0.4 for input 2 : [2, 3]
Candidates with Jaccard similarity > 0.4 for input 3 : [2, 3]

我不明白这个问题。你说你知道“如果是文本”该怎么做,但你不知道如何处理字符串?你头脑中的“文本”和“字符串”之间的相关区别是什么使得这个问题无法解决?我可以用向量空间模型来表示文本。对于字符串,我没有外观向量,而是表示不同内容的ASCII码向量。我看到了问题所在。我熟悉的LSH版本(早期版本)采用欧几里得度量。对于字符串,使用字符串度量。。。这是行不通的。非欧几里德空间需要LSH。如果阈值为.5,则只返回输入。要获得正确的结果,需要.42或更低的阈值(Python3.5和当前版本的datasketch)@duhaime此代码在Python3上工作吗?当我使用它时,对于输入0:[0]和类似的
1:[1]
,我会得到Jaccard相似度>0.5的输出
候选者,依此类推on@iam.Carrot我刚刚在3.6.1(Anaconda distribution)上进行了测试,得到了上面的输出。也许尝试更新您的datasketch和nltk库
pip install datasketch-U&&pip install nltk-U
@duhaime我也试过了。我使用的是
IDLE
当我将阈值降低到
0.4
时,它仍然会显示相同的结果,并开始给出您所得到的输出shown@duhaime请您看看我的问题,并说明您的回答是否能解决我的问题?这会很有帮助的