Login or Sign Up to become a member!
LessThanDot Site Logo


Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.


Google Ads

Comparing Words: Levenshtein Distance

From Wiki

Jump to: navigation, search

Here is a method of comparing words, see also Soundex

   Sub Test()
       Debug.Print LevenshteinDistance("kitten", "sitting")
       Debug.Print LevenshteinDistance("saturday", "sunday")
       Debug.Print LevenshteinDistance("mite", "kite")
   End Sub
   Function LevenshteinDistance(word1, word2)
   Dim s As Variant
   Dim t As Variant
   Dim d As Variant
   Dim m, n
   Dim i, j, k
   Dim a(2), r
   Dim cost
       m = Len(word1)
       n = Len(word2)
       'This is the only way to use
       'variables to dimension an array
       ReDim s(m)
       ReDim t(n)
       ReDim d(m, n)
       For i = 1 To m
           s(i) = Mid(word1, i, 1)
       For i = 1 To n
           t(i) = Mid(word2, i, 1)
       For i = 0 To m
           d(i, 0) = i
       For j = 0 To n
           d(0, j) = j
       For i = 1 To m
           For j = 1 To n
               If s(i) = t(j) Then
                   cost = 0
                   cost = 1
               End If
               a(0) = d(i - 1, j) + 1             '  // deletion
               a(1) = d(i, j - 1) + 1             '  // insertion
               a(2) = d(i - 1, j - 1) + cost      '  // substitution
               r = a(0)
               For k = 1 To UBound(a)
                   If a(k) < r Then r = a(k)
               d(i, j) = r
       LevenshteinDistance = d(m, n)
   End Function

Further information

Levenshtein distance: http://en.wikipedia.org/wiki/Levenshtein_distance

613 Rating: 2.3/5 (48 votes cast)