|
各位,端午节快乐! 我在处理和字符串相关的内容,,,需要使用模糊匹配相关的字符串,我用VBA写了一段代码,可以简单实现计算.但是,出现一点瑕疵,需要完善一下代码,请求各位赐教!
代码如下:
'最小编辑距离算法
Function Min_ed(str1, str2)
Dim str1_len
Dim str2_len
Dim arr()
Dim xx, yy
Dim temp1, temp2, temp3
str1_len = Len(str1)
str2_len = Len(str2)
ReDim arr(str1_len, str2_len)
For xx = 0 To str1_len
For yy = 0 To str2_len
If xx = 0 Then
arr(xx, yy) = yy
ElseIf yy = 0 Then
arr(xx, yy) = xx
Else
If UCase(Mid(str2, xx, 1)) = UCase(Mid(str1, yy, 1)) Then
temp1 = arr(xx - 1, yy - 1) '左上角的值
temp2 = arr(xx - 1, yy) + 1 '上边的值
temp3 = arr(xx, yy - 1) + 1 '左边的值
arr(xx, yy) = Application.WorksheetFunction.Min(temp1, temp2, temp3)
Else
temp1 = arr(xx - 1, yy - 1) + 1
temp2 = arr(xx - 1, yy) + 1
temp3 = arr(xx, yy - 1) + 1
arr(xx, yy) = Application.WorksheetFunction.Min(temp1, temp2, temp3)
End If
End If
Next
Next
Min_ed = arr(str1_len, str2_len) '取数组最后一行一列的数值
End Function
这是最小编辑距离算法的原理
https://blog.csdn.net/weixin_43080758/article/details/83272957
这是别人用python算法实现的
其实python有一个第三方库叫 FuzzyWuzzy 就很好的实现了这种算法
在计算的时候出现了一点问题:
比如 str1="我爱中国" str2="我爱中华人民共和国" str3="我爱天国"
计算如下:
Min_ed(str1, str2)=6
Min_ed(str1, str3)=1
按道理来说,str2 完全包含 str1 ,str2不完全包含str3,,,,str1和str2的最小编辑距离应该小于str1和str3的最小编辑距离,,,,
不是算法的问题,,,只是需要做一个分支语句,,,使计算这种包含关系的字符串的结果为0
请各位赐教!
|
|