How to implement this distance code...?

tonycrew

Well-known member
Joined
Apr 20, 2009
Messages
55
Programming Experience
Beginner
Ok as you know i am trying to sort of fuzzy match, anyway i am new to vb and just can't get my head round this. I have searched the net and found what i think is the code i need.

1st code is
VB.NET:
   Public Class StringSift2
        Private maxOffset As Integer
        Public Sub New()
            Me.New(5)
        End Sub
        Public Sub New(ByVal maxOffset As Integer)
            Me.maxOffset = maxOffset
        End Sub
        Public Function Distance(ByVal s1 As String, ByVal s2 As String) As Single
            If [String].IsNullOrEmpty(s1) Then
                Return If([String].IsNullOrEmpty(s2), 0, s2.Length)
            End If
            If [String].IsNullOrEmpty(s2) Then
                Return s1.Length
            End If
            Dim c As Integer = 0
            Dim offset1 As Integer = 0
            Dim offset2 As Integer = 0
            Dim dist As Integer = 0
            While (c + offset1 < s1.Length) AndAlso (c + offset2 < s2.Length)
                If s1(c + offset1) <> s2(c + offset2) Then
                    offset1 = 0
                    offset2 = 0
                    For i As Integer = 0 To maxOffset - 1
                        If (c + i < s1.Length) AndAlso (s1(c + i) = s2(c)) Then
                            If i > 0 Then
                                dist += 1
                                offset1 = i
                            End If
                            GoTo ender
                        End If
                        If (c + i < s2.Length) AndAlso (s1(c) = s2(c + i)) Then
                            If i > 0 Then
                                dist += 1
                                offset2 = i
                            End If
                            GoTo ender
                        End If
                    Next
                    dist += 1
                End If
ender:
                c += 1
            End While
            Return dist + (s1.Length - offset1 + s2.Length - offset2) / 2 - c
        End Function
        Public Function Similarity(ByVal s1 As String, ByVal s2 As String) As Single
            Dim dis As Single = Distance(s1, s2)
            Dim maxLen As Integer = Math.Max(s1.Length, s2.Length)
            If maxLen = 0 Then
                Return 1
            Else
                Return 1 - dis / maxLen
            End If
        End Function
    End Class

the second code is...

VB.NET:
Public Function Levenshtein_distance(ByVal s As String, ByVal t As String) As Integer
        ListBox1.Items.ToString()
        ListBox2.Items.ToString()
        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
        Dim n As Integer = s.Length
        'length of s
        Dim m As Integer = t.Length
        'length of t
        If n = 0 Then
            Return m
        End If
        If m = 0 Then
            Return n
        End If
        Dim 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 = s.Substring(i - 1, 1)
            ' Step 4
            For j = 1 To m
                t_j = t.Substring(j - 1, 1)
                ' Step 5
                If s_i = t_j Then
                    cost = 0
                Else
                    cost = 1
                End If
                ' Step 6
                d(i, j) = System.Math.Min(System.Math.Min((d((i - 1), j) + 1), (d(i, (j - 1)) + 1)), (d((i - 1), (j - 1)) + cost))
            Next j
        Next i
        ' Step 7
        Return d(n, m)
    End Function

So what do i want to do, i have 2 listboxes, i want them to be compared then the results sent to a treeview.node.
Problem is i don't know how to do this...
Any help would be appreciated thanks
 
Back
Top