Skip to main content
 首页 » 编程设计

python之使用重音编辑距离

2025年05月04日11mengfanrong

python 中是否有一些考虑重音的编辑距离。 例如,在哪里拥有以下属性(property)

d('ab', 'ac') > d('àb', 'ab') > 0 

请您参考如下方法:

随着Levenshtein module :

In [1]: import unicodedata, string 
 
In [2]: from Levenshtein import distance 
 
In [3]: def remove_accents(data): 
   ...:     return ''.join(x for x in unicodedata.normalize('NFKD', data) 
   ...:                             if x in string.ascii_letters).lower() 
 
In [4]: def norm_dist(s1, s2): 
   ...:     norm1, norm2 = remove_accents(s1), remove_accents(s2) 
   ...:     d1, d2 = distance(s1, s2), distance(norm1, norm2) 
   ...:     return (d1+d2)/2. 
 
In [5]: norm_dist(u'ab', u'ac') 
Out[5]: 1.0 
 
In [6]: norm_dist(u'àb', u'ab') 
Out[6]: 0.5