(lensum - ldist)/lensum
ldist不是距離,是成本

所述陣列的每個數字是不匹配來自以上,從左側或對角線
總和如果數字來自左側,他是一個插入,它來自上面它是一個刪除,它來自對角線它是一個替代品
插入和刪除成本爲1,替換成本爲2。 替換成本是2,因爲它是一個刪除和插入
AB AC成本是2,因爲它是一個替換
>>> import Levenshtein as lev
>>> lev.distance("ab","ac")
1
>>> lev.ratio("ab","ac")
0.5
>>> (4.0-1.0)/4.0 #Erro, the distance is 1 but the cost is 2 to be a replacement
0.75
>>> lev.ratio("ab","a")
0.6666666666666666
>>> lev.distance("ab","a")
1
>>> (3.0-1.0)/3.0 #Coincidence, the distance equal to the cost of insertion that is 1
0.6666666666666666
>>> x="ab"
>>> y="ac"
>>> lev.editops(x,y)
[('replace', 1, 1)]
>>> ldist = sum([2 for item in lev.editops(x,y) if item[0] == 'replace'])+ sum([1 for item in lev.editops(x,y) if item[0] != 'replace'])
>>> ldist
2
>>> ln=len(x)+len(y)
>>> ln
4
>>> (4.0-2.0)/4.0
0.5

更多信息:python-Levenshtein ratio calculation
又如:

成本是9(4替換=> 4×2 = 8和1刪除1 * 1 = 1,8 + 1 = 9)
str1=len("google") #6
str2=len("look-at") #7
str1 + str2 #13
距離= 5(根據矢量(7,6矩陣)= 5)
比爲(13-9)/ 13 = 0.3076923076923077
>>> c="look-at"
>>> d="google"
>>> lev.editops(c,d)
[('replace', 0, 0), ('delete', 3, 3), ('replace', 4, 3), ('replace', 5, 4), ('replace', 6, 5)]
>>> lev.ratio(c,d)
0.3076923076923077
>>> lev.distance(c,d)
5
我查庫(在鏈接給你),我也搞不清他爲什麼使用'sum'.Also '(1-1/3)= .666..'這是正確的根據代碼,但也('1 - 1/4)= 0.75'它如何.5?即使在文檔中也不清楚......但是計算Levenshtein距離的實際公式是在我的答案中。 –