Posted: Sat Dec 22, 2007 7:26 pm
Kevinowpb,
The script hasn't yet been modified to work with MM3, hence the [MM2] in the thread title.
The script hasn't yet been modified to work with MM3, hence the [MM2] in the thread title.
The Music Manager for Serious Collectors
http://www.mediamonkey.com/forum/
Code: Select all
' VB Script Document
option explicit
dim text1 : text1 = "abracadabra"
dim text2 : text2 = "abarcadabra"
dim text3 : text3 = "abarcababra"
dim text4 : text4 = "MozartSinatra"
wscript.echo damerau_levenshtein( text1, text2, 3 )
wscript.echo damerau_levenshtein( text2, text3, 3 )
wscript.echo damerau_levenshtein( text1, text3, 3 )
wscript.echo damerau_levenshtein( text1, text4, 20 )
Function damerau_levenshtein( s1, s2, limit )
ReDim result(Len(s1), Len(s2))
damerau_levenshtein = damerau_levenshtein_recurse( s1, s2, limit, result )
end function
Function damerau_levenshtein_recurse( s1, s2, limit, result )
'This function returns the Levenshtein distance capped by the limit parameter.
'Usage : e.g. damerau_levenshtein("Thibault","Gorisse") to get the exact distance
' or damerau_levenshtein("correctly written words","corectly writen words",4) to identify similar spellings
Dim diagonal
Dim horizontal
Dim vertical
Dim swap
Dim final
'Start of the strings analysis
If result(Len(s1), Len(s2)) < 1 Then
If Abs(Len(s1) - Len(s2)) >= limit Then
final = limit
Else
If Len(s1) = 0 Or Len(s2) = 0 Then
'End of recursivity
final = Len(s1) + Len(s2)
Else
'Core of levenshtein algorithm
If Mid(s1, 1, 1) = Mid(s2, 1, 1) Then
final = damerau_levenshtein_recurse(Mid(s1, 2), Mid(s2, 2), limit, result)
Else
If Mid(s1, 1, 1) = Mid(s2, 2, 1) And Mid(s1, 2, 1) = Mid(s2, 1, 1) Then
'Damerau extension counting swapped letters
swap = damerau_levenshtein_recurse(Mid(s1, 3), Mid(s2, 3), limit - 1, result)
final = 1 + swap
Else
'The function minimum is implemented via the limit parameter.
'The diagonal search usually reaches the limit the quickest.
diagonal = damerau_levenshtein_recurse(Mid(s1, 2), Mid(s2, 2), limit - 1, result)
horizontal = damerau_levenshtein_recurse(Mid(s1, 2), s2, diagonal, result)
vertical = damerau_levenshtein_recurse(s1, Mid(s2, 2), horizontal, result)
final = 1 + vertical
End If
End If
End If
End If
Else
'retrieve intermediate result
final = result(Len(s1), Len(s2)) - 1
End If
'returns the distance capped by the limit
If final < limit Then
damerau_levenshtein_recurse = final
'store intermediate result
result(Len(s1), Len(s2)) = final + 1
Else
damerau_levenshtein_recurse = limit
End If
End Function
Code: Select all
File does not exist. Make sure you specified correct file name.
File does not exist. Make sure you specified correct file name.