Comparing 2 strings at character level

I am looking for a way to compare 2 strings and calculate the number of differences between them using visual basic

Example compare "THE NETHERLANDS" WITH "THA NOTTHERLENDS"

should give back an value of 4 based on

1) "E" in first string position 3 does not match with "A" in second string position 3

2) "E" in first string position 6 does not match with "O" in second string position 6

3) Extra character "T" found on position 8 of string 2

4) "A" in first string position 12 does not match with "E" in second string position 13

Can anyone help me




Answer this question

Comparing 2 strings at character level

  • SDPointRdr

    Your code has a couple of minor typos; you probably don't want to take length of LenStr1 and LenStr2 when you decide how large the d matrix should be. The Minimum method also looks questionable to me.

    A quick translation - but please note that I haven't really verified that this actually works (I basically just translated the pseudocode into VB and ran it two times :))

    Public Function MyDamerauLevenshteinDistance(ByVal str1 As String, ByVal str2 As String) As Integer
    Dim lenStr1 As Integer
    = str1.Length
    Dim lenStr2 As Integer
    = str2.Length
    Dim d(lenStr1, lenStr2) As
    Integer
    Dim i, j, cost As
    Integer
    For i = 0 To
    lenStr1
    d(i, 0) = i
    Next
    For j = 0 To
    lenStr2
    d(0, j) = j
    Next
    For i = 1 To
    lenStr1
    For j = 1 To
    lenStr2
    If str1(i - 1) = str2(j - 1)
    Then
    cost = 0
    Else
    cost = 1
    End
    If
    d(i, j) = Math.Min( _
    Math.Min( _
    d(i - 1, j) + 1, _
    d(i, j - 1) + 1), _
    d(i - 1, j - 1) + cost _
    )
    If i > 1 AndAlso j > 1 AndAlso str1(i - 1) = str2(j - 2) AndAlso str1(i - 2) = str2(j - 1)
    Then
    d(i, j) = Math.Min( _
    d(i, j), _
    d(i - 2, j - 2) + cost)
    ' transposition
    End
    If
    Next
    Next
    Return
    d(lenStr1, lenStr2)
    End Function

    Best regards,
    Johan Stenberg



  • MgManoj

    Hi Johan,

    Do you have this code converted to VB I tried to programm it but did not succeed in doing so

    KInd regards,

    Rene

    The code I tried looked like this

    Dim String1 As String
    Dim String2 As String
    Dim Lengte1 As Integer
    Dim Lengte2 As Integer
    Dim d(0 To 7, 0 To 7) As Integer


    Private Function DamerauLevenshteinDistance(Str1 As String, Str2 As String, LenStr1 As Integer, LenStr2 As Integer) As Integer
    Dim i, j, cost As Integer
    For i = 0 To Len(LenStr1)
    d(i, 0) = i
    Next i

    For j = 0 To Len(LenStr2)
    d(0, j) = j
    Next j

    For i = 1 To LenStr1
    For j = 1 To LenStr2
    If Mid(Str1, i, 1) = Mid(Str2, j, 1) Then cost = 0 Else cost = 1
    d(i, j) = Minimum(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost)
    If (i > 1 And j > 1) Then
    If Mid(Str1, i, 1) = Mid(Str2, j - 1, 1) And Mid(Str1, i - 1, 1) = Mid(Str2, j, 1) Then
    d(i, j) = Minimum(d(i, j), d(i - 2, j - 2) + cost, 0)
    End If
    End If
    Next j
    Next i
    DamerauLevenshteinDistance = d(LenStr1, LenStr2)
    End Function
    Private Function Minimum(v1 As Integer, v2 As Integer, v3 As Integer) As Integer
    Dim i As Integer
    i = v1
    If i > v2 Then i = v2
    If v3 <> 0 Then If i > v3 Then i = v3
    Minimum = i
    End Function
    Private Sub Command1_Click()
    String1 = "MELTZER"
    String2 = "MELTZER"
    Lengte1 = Len(String1)
    Lengte2 = Len(String2)
    Debug.Print DamerauLevenshteinDistance(String1, String2, Lengte1, Lengte2)
    End Sub



  • CurtVas

    Hi Johan,

    It look indead like what I want to create in VB

    Kind regards,

    Rene



  • Beat

    Hi Johan,

    I made it working with some minor changes inVB

    Thanks for your help

    Rene



  • W.Yuan

    Are you trying to do something similar to http://en.wikipedia.org/wiki/Damerau-Levenshtein_distance

    Best regards,
    Johan Stenberg



  • Comparing 2 strings at character level