python-Levenshtein ratio calculation

烈酒焚心 提交于 2019-12-08 20:02:58

From Lukas's comment, the reason for this is that the ratio() uses a cost of 2 for replace operations rather than the normal cost of 1 for the Levenshtein distance. Here's an example calculation:

a = 'bjork gudmundsdottir'
b = 'b. gudmundsson gunnar'

>>> Levenshtein.editops(a,b)
[('delete', 1, 1), ('delete', 2, 1), ('delete', 3, 1), ('replace', 4, 1), ('replace', 14, 11), ('insert', 16, 13), ('insert', 16, 14), ('insert', 16, 15), ('insert', 16, 16), ('replace', 16, 17), ('replace', 17, 18), ('replace', 18, 19)]

>>> ldist = sum([2 for item in Levenshtein.editops(a,b) if item[0] == 'replace']) 
          + sum([1 for item in Levenshtein.editops(a,b) if item[0] != 'replace']) # 17
ln = len(a) + len(b) # 41

>>> (41.0-17.0)/41.0
0.5853658536585366
>>> Levenshtein.ratio(a,b)
0.5853658536585366
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!