根据python-Levenshtein.ratio
来源:
https://github.com/miohtama/python-Levenshtein/blob/master/Levenshtein.c#L722
它被计算为(lensum - ldist) / lensum
。这适用于
distance('ab', 'a') = 1
ratio('ab', 'a') = 0.666666
然而,它似乎与
distance('ab', 'ac') = 1
ratio('ab', 'ac') = 0.5
我觉得我一定错过了一件很简单的事。。但为什么不0.75
?
Levenshtein距离对于
'ab'
和'ac'
如下:所以排列是:
对齐长度=2
不匹配数=1
Levenshtein Distance
是1
,因为只需要一个替换就可以将ac
转移到ab
(或反向)距离比=(Levenshtein距离)/(对齐长度)=0.5
编辑
你在写
(lensum - ldist) / lensum
=(1 - ldist/lensum)
=1-0.5=0.5。但这是匹配(而不是距离)
REFFRENCE你可能会注意到
Matching %
其中
l
是levenshtein distance
,而m
是length of the longest of the two
单词:(注意:有些作者使用两者中最长的一个,我使用对齐长度)
为什么有些作者除以对齐长度,另一个除以两者的最大长度?。。因为Levenshtein不考虑gap。距离=编辑次数(插入+删除+替换),而Needleman–Wunsch algorithm这是标准的全局对齐方式,请考虑间隙。这是Neederman–Wunsch和Levenshtein之间的(间隙)差异,因此很多论文使用两个序列之间的最大距离(但这是我自己的理解,我不确定100%)
下面是关于PAITERN分析的IEEE事务:Computation of Normalized Edit Distance and Applications在本文中,规范化编辑距离如下:
虽然没有绝对的标准,但是规范化的levensinide距离是最常用的定义。这两个例子都是0.5。
由于
max
是Levenshtein距离的最低上限,因此它是有意义的:要从b
中获得a
,在len(a) > len(b)
中,您始终可以用a
中的相应元素替换len(b)
中的第一个b
元素,然后插入缺少的部分a[len(b):]
,总共进行len(a)
编辑操作。这个参数显然扩展到了
len(a) <= len(b)
的情况。要将规范化距离转换为相似性度量,请将其从1中减去:1 - ldist / max(len(a), len(b))
。通过更仔细地研究C代码,我发现这种明显的矛盾是由于
ratio
对待“替换”编辑操作与对待其他操作不同(即代价为2),而distance
对待它们都一样,代价为1。这可以在
ratio_py
函数中对内部levenshtein_common
函数的调用中看到:https://github.com/miohtama/python-Levenshtein/blob/master/Levenshtein.c#L727
通过
distance_py
函数:https://github.com/miohtama/python-Levenshtein/blob/master/Levenshtein.c#L715
最终导致不同的成本参数被发送到另一个内部函数
lev_edit_distance
,该函数包含以下文档片段:lev_edit_distance()的代码:
[回答]
所以在我的例子中
ratio('ab', 'ac')
意味着在字符串(4)的总长度上进行替换操作(成本为2),因此2/4 = 0.5
。这就解释了“如何”,我想剩下的唯一方面就是“为什么”,但目前我对这种理解感到满意。
相关问题 更多 >
编程相关推荐