Date: 02-16-2022
Return to Index
created by gbSnippets
'Credit: Michael Gilleland http://www.merriampark.com/ld.htm
'useful link: http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Dynamic/Edit/
'useful link:
'What is Levenshtein Distance?
'Levenshtein distance (LD) is a measure of the similarity between two strings,
'which we will refer to as the source string (s) and the target string (t).
'The distance is the number of deletions, insertions, or substitutions required
'to transform s into t. For example,
If s is "test" and t is "test", then LD(s,t) = 0, because no transformations are needed. The strings are already identical.
If s is "test" and t is "tent", then LD(s,t) = 1, because one substitution (change "s" to "n") is sufficient to transform s into t.
'The greater the Levenshtein distance, the more different the strings are.
'Levenshtein distance is named after the Russian scientist Vladimir Levenshtein,
'who devised the algorithm in 1965. If you can't spell or pronounce Levenshtein,
'the metric is also sometimes called edit distance.
'The Levenshtein distance algorithm has been used in:
' Spell checking
' Speech recognition
' DNA analysis
' Plagiarism detection
'The Algorithm
'Step Description
'1 Set n to be the length of s.
' Set m to be the length of t.
' If n = 0, return m and exit.
' If m = 0, return n and exit.
' Construct a matrix containing 0..m rows and 0..n columns.
'2 Initialize the first row to 0..n.
' Initialize the first column to 0..m.
'3 Examine each character of s (i from 1 to n).
'4 Examine each character of t (j from 1 to m).
'5 If s[i] equals t[j], the cost is 0.
' If s[i] doesn't equal t[j], the cost is 1.
'6 Set cell d[i,j] of the matrix equal to the minimum of:
' a.The cell immediately above plus 1: d[i-1,j] + 1.
' b.The cell immediately to the left plus 1: d[i,j-1] + 1.
' c.The cell diagonally above and to the left plus the cost: d[i-1,j-1] + cost.
'7 After the iteration steps (3, 4, 5, 6) are complete, the distance is found in cell d[n,m].
'Visual Basic
'*******************************
'*** Get minimum of three values
'*******************************
Private Function Minimum(ByVal a As Integer, _
ByVal b As Integer, _
ByVal c As Integer) As Integer
Dim mi As Integer
mi = a
If b < mi Then
mi = b
End If
If c < mi Then
mi = c
End If
Minimum = mi
End Function
'********************************
'*** Compute Levenshtein Distance
'********************************
Public Function LD(ByVal s As String, ByVal t As String) As Integer
Dim d() As Integer ' matrix
Dim m As Integer ' length of t
Dim n As Integer ' length of s
Dim i As Integer ' iterates through s
Dim j As Integer ' iterates through t
Dim s_i As String ' ith character of s
Dim t_j As String ' jth character of t
Dim cost As Integer ' cost
' Step 1
n = Len(s)
m = Len(t)
If n = 0 Then
LD = m
Exit Function
End If
If m = 0 Then
LD = n
Exit Function
End If
ReDim d(0 To n, 0 To m) As Integer
' Step 2
For i = 0 To n
d(i, 0) = i
Next i
For j = 0 To m
d(0, j) = j
Next j
' Step 3
For i = 1 To n
s_i = Mid$(s, i, 1)
' Step 4
For j = 1 To m
t_j = Mid$(t, j, 1)
' Step 5
If s_i = t_j Then
cost = 0
Else
cost = 1
End If
' Step 6
d(i, j) = Minimum(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost)
Next j
Next i
' Step 7
LD = d(n, m)
Erase d
End Function
'gbs_01137
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm