Question : Determine the closest word in the list.

For example I have a list of words

Apple
Apply
Absurb
Absurd
Tree
Three

Then I have the word "Trhee", how will I determine that the closest word in the list is "THREE"?

If you had further question, please let me know.

Joseph

Answer : Determine the closest word in the list.

Similarity between words is often calculated using the Levenshtein Distance algorithm, that calculates the steps needed to transform a word into another. So you can call the Levenshtein function comparing 2 words and, the lower result indicates the closest word.

Hope that helps.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
    Public Function LevenshteinDistance(ByVal s As String, ByVal t As String) As Integer
        Dim n As Integer = s.Length
        'length of s
        Dim m As Integer = t.Length
        'length of t
        Dim d As Integer(,) = New Integer(n, m) {}
        ' matrix
        Dim cost As Integer
        ' cost
        ' Step 1

        If n = 0 Then
            Return m
        End If

        If m = 0 Then
            Return n
        End If

        ' Step 2
        Dim i As Integer = 0
        While i <= n
            d(i, 0) = System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While

        Dim j As Integer = 0
        While j <= m
            d(0, j) = System.Math.Max(System.Threading.Interlocked.Increment(j), j - 1)
        End While

        ' Step 3
        For o As Integer = 1 To n
            'Step 4
            For p As Integer = 1 To m
                ' Step 5
                cost = (If(t.Substring(p - 1, 1) = s.Substring(o - 1, 1), 0, 1))
                ' Step 6
                d(o, p) = System.Math.Min(System.Math.Min(d(o - 1, p) + 1, d(o, p - 1) + 1), d(o - 1, p - 1) + cost)
            Next
        Next

        ' Step 7
        Return d(n, m)
    End Function
Random Solutions  
 
programming4us programming4us