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

LessThanDot

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.

Navigation

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)
       Next
       
       For i = 1 To n
           t(i) = Mid(word2, i, 1)
       Next
       
       For i = 0 To m
           d(i, 0) = i
       Next
       
       For j = 0 To n
           d(0, j) = j
       Next
           
        
       For i = 1 To m
           For j = 1 To n
                     
               If s(i) = t(j) Then
                   cost = 0
               Else
                   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)
               Next
               
               d(i, j) = r
           
           Next
       
       Next
        
       LevenshteinDistance = d(m, n)
   
   End Function
   

Further information

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

613 Rating: 2.4/5 (16 votes cast)