Results 1 to 25 of 25

Thread: How to compare the similarity of two words in VB?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    10

    How to compare the similarity of two words in VB?

    Hi i am kinda new for VB6, i am looking for some code that can compare the similarity of two words in VB, like "weather" and "wther"?

    anyone got an idea?

  2. #2
    Member
    Join Date
    Aug 2006
    Posts
    46

    Re: How to compare the similarity of two words in VB?

    Do you mean u want it to check if the words are the same if so try this code

    If Word1.text = Word2.text then
    Msgbox " The words are the same"
    Else if
    Msgbox "The Words are not the same"
    End if



    If this isnt what you mean can you explain a little more




    Dom
    Visual Basic 6 Novice

    Learning Direct X 8 for VB....



    http://externalweb.exhedra.com/DirectX4VB/ <--- Great Direct X Tutorials

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    10

    Re: How to compare the similarity of two words in VB?

    no i mean how to check the similarity? like 2 words or sentences have 90% are the same, can vb doing that?

  4. #4

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    10

    Re: How to compare the similarity of two words in VB?

    For example "weather" and "wther" are 70% similar

    "Unable to select an address from the list" and "Unable to select an address " has 23 characters out of 34 characters are the same so 67.6% are similar

  6. #6
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: How to compare the similarity of two words in VB?

    I imagine it's quite hard to write something like that... i'm interested if somebody has allready did it or is about to...

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to compare the similarity of two words in VB?

    Luckily I remembered that I wrote this years ago, and somehow managed to find the code.. it's certainly not pretty, but it does the job:
    VB Code:
    1. Public Function Similarity(ByVal String1 As String, _
    2.                            ByVal String2 As String, _
    3.                            Optional ByRef RetMatch As String, _
    4.                            Optional min_match = 1) As Single
    5. 'Returns percentile of similarity between 2 strings (ignores case)
    6.  
    7. '"RetMatch"  returns the characters that match(in order)
    8. '"min_match" specifies minimum number af char's in a row to match
    9.  
    10. Dim b1() As Byte, b2() As Byte
    11. Dim lngLen1 As Long, lngLen2 As Long
    12. Dim lngResult As Long
    13.  
    14.   If UCase(String1) = UCase(String2) Then       '..Exactly the same
    15.     Similarity = 1
    16.   Else                                          '..one string is empty
    17.     lngLen1 = Len(String1)
    18.     lngLen2 = Len(String2)
    19.     If (lngLen1 = 0) Or (lngLen2 = 0) Then
    20.       Similarity = 0
    21.     Else                                        '..otherwise find similarity
    22.       b1() = StrConv(UCase(String1), vbFromUnicode)
    23.       b2() = StrConv(UCase(String2), vbFromUnicode)
    24.       lngResult = Similarity_sub(0, lngLen1 - 1, _
    25.                                  0, lngLen2 - 1, _
    26.                                  b1, b2, _
    27.                                  String1, _
    28.                                  RetMatch, _
    29.                                  min_match)
    30.       Erase b1
    31.       Erase b2
    32.       If lngLen1 >= lngLen2 Then
    33.         Similarity = lngResult / lngLen1
    34.       Else
    35.         Similarity = lngResult / lngLen2
    36.       End If
    37.     End If
    38.   End If
    39.  
    40. End Function
    41.  
    42. Private Function Similarity_sub(ByVal start1 As Long, ByVal end1 As Long, _
    43.                                 ByVal start2 As Long, ByVal end2 As Long, _
    44.                                 ByRef b1() As Byte, ByRef b2() As Byte, _
    45.                                 ByVal FirstString As String, _
    46.                                 ByRef RetMatch As String, _
    47.                                 ByVal min_match As Long, _
    48.                                 Optional recur_level As Integer = 0) As Long
    49. '* CALLED BY: Similarity *  (RECURSIVE)
    50.  
    51. Dim lngCurr1 As Long, lngCurr2 As Long
    52. Dim lngMatchAt1 As Long, lngMatchAt2 As Long
    53. Dim I As Long
    54. Dim lngLongestMatch As Long, lngLocalLongestMatch As Long
    55. Dim strRetMatch1 As String, strRetMatch2 As String
    56.  
    57.   If (start1 > end1) Or (start1 < 0) Or (end1 - start1 + 1 < min_match) _
    58.   Or (start2 > end2) Or (start2 < 0) Or (end2 - start2 + 1 < min_match) Then
    59.     Exit Function     '(exit if start/end is out of string, or length is too short)
    60.   End If
    61.  
    62.   For lngCurr1 = start1 To end1        '(for each char of first string)
    63.     For lngCurr2 = start2 To end2        '(for each char of second string)
    64.       I = 0
    65.       Do Until b1(lngCurr1 + I) <> b2(lngCurr2 + I)   'as long as chars DO match..
    66.         I = I + 1
    67.         If I > lngLongestMatch Then     '..if longer than previous best, store starts & length
    68.           lngMatchAt1 = lngCurr1
    69.           lngMatchAt2 = lngCurr2
    70.           lngLongestMatch = I
    71.         End If
    72.         If (lngCurr1 + I) > end1 Or (lngCurr2 + I) > end2 Then Exit Do
    73.       Loop
    74.     Next lngCurr2
    75.   Next lngCurr1
    76.  
    77.   If lngLongestMatch < min_match Then Exit Function 'no matches at all, so no point checking for sub-matches!
    78.  
    79.   lngLocalLongestMatch = lngLongestMatch                   'call again for BEFORE + AFTER
    80.   RetMatch = ""
    81.                               'Find longest match BEFORE the current position
    82.   lngLongestMatch = lngLongestMatch _
    83.                   + Similarity_sub(start1, lngMatchAt1 - 1, _
    84.                                    start2, lngMatchAt2 - 1, _
    85.                                    b1, b2, _
    86.                                    FirstString, _
    87.                                    strRetMatch1, _
    88.                                    min_match, _
    89.                                    recur_level + 1)
    90.   If strRetMatch1 <> "" Then
    91.     RetMatch = RetMatch & strRetMatch1 & "*"
    92.   Else
    93.     RetMatch = RetMatch & IIf(recur_level = 0 _
    94.                               And lngLocalLongestMatch > 0 _
    95.                               And (lngMatchAt1 > 1 Or lngMatchAt2 > 1) _
    96.                               , "*", "")
    97.   End If
    98.  
    99.                               'add local longest
    100.   RetMatch = RetMatch & Mid$(FirstString, lngMatchAt1 + 1, lngLocalLongestMatch)
    101.                              
    102.                               'Find longest match AFTER the current position
    103.   lngLongestMatch = lngLongestMatch _
    104.                   + Similarity_sub(lngMatchAt1 + lngLocalLongestMatch, end1, _
    105.                                    lngMatchAt2 + lngLocalLongestMatch, end2, _
    106.                                    b1, b2, _
    107.                                    FirstString, _
    108.                                    strRetMatch2, _
    109.                                    min_match, _
    110.                                    recur_level + 1)
    111.  
    112.   If strRetMatch2 <> "" Then
    113.     RetMatch = RetMatch & "*" & strRetMatch2
    114.   Else
    115.     RetMatch = RetMatch & IIf(recur_level = 0 _
    116.                               And lngLocalLongestMatch > 0 _
    117.                               And ((lngMatchAt1 + lngLocalLongestMatch < end1) _
    118.                                    Or (lngMatchAt2 + lngLocalLongestMatch < end2)) _
    119.                               , "*", "")
    120.   End If
    121.                              'Return result
    122.   Similarity_sub = lngLongestMatch
    123.  
    124. End Function

    Just call Similarity, which you can do like this:
    VB Code:
    1. Dim sngPercentage as Single
    2. Dim strMatch as String
    3.  
    4.   sngPercentage = Similarity("Unable to select an address from the list", "Unable to select an address ", strMatch) * 100
    5. '(need to multiply by 100, as the code returns a percentile rather than percentage)
    6.  
    7.   MsgBox "Match of " & sngPercentage & "% " & vbNewLine & "Matching text: " & strMatch
    I don't know if this is what you want, but it finds multiple similarities if possible. For example, if the second string was changed to "Unable select an address list", it would match all of the characters to the first string, in 3 parts ("Unable" + " select an address " + "list").

    ..if you don't want that, just something like the For loops in _sub should be enough!
    Last edited by si_the_geek; Nov 24th, 2006 at 06:24 PM.

  8. #8
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: How to compare the similarity of two words in VB?

    I've never tried anything like this before, so maybe I'm missing something (someone tell me), but it seems pretty easy. This is what I came up with.

    VB Code:
    1. Option Explicit
    2.  
    3. 'Actual function that compares two strings.
    4. Private Function PercentTheSame(ByVal Text As String, ByVal CompareWith As String, Optional ByVal CaseSensitive As Boolean = False) As Single
    5.     Dim lonLenText As Long, lonLenCompare As Long
    6.     Dim lonLoop As Long, lonDiff As Long
    7.     Dim strCur As String, strC As String
    8.    
    9.     lonLenText = Len(Text)
    10.     lonLenCompare = Len(CompareWith)
    11.    
    12.     For lonLoop = 1 To lonLenText
    13.        
    14.         If lonLoop > lonLenCompare Then
    15.             lonDiff = lonDiff + 1
    16.         Else
    17.            
    18.             If CaseSensitive = False Then
    19.                 strCur = LCase$(Mid$(Text, lonLoop, 1))
    20.                 strC = LCase$(Mid$(CompareWith, lonLoop, 1))
    21.             Else
    22.                 strCur = Mid$(Text, lonLoop, 1)
    23.                 strC = Mid$(CompareWith, lonLoop, 1)
    24.             End If
    25.            
    26.             If Not strCur = strC Then
    27.                 lonDiff = lonDiff + 1
    28.             End If
    29.        
    30.         End If
    31.    
    32.     Next lonLoop
    33.    
    34.     PercentTheSame = CSng(((lonLenText - lonDiff) / lonLenText) * 100)
    35. End Function
    36.  
    37. 'Finds which string is longer to use in the comparison function.
    38. Private Function StartCompare(ByVal Text As String, ByVal CompareWith As String, Optional ByVal CaseSensitive As Boolean = False) As Single
    39.    
    40.     'Quick test so we don't waste time.
    41.     If Text = CompareWith Then
    42.         StartCompare = 100
    43.         Exit Function
    44.     End If
    45.    
    46.     If Len(Text) > Len(CompareWith) Then
    47.         StartCompare = PercentTheSame(Text, CompareWith, CaseSensitive)
    48.     Else
    49.         StartCompare = PercentTheSame(CompareWith, Text, CaseSensitive)
    50.     End If
    51.    
    52. End Function
    53.  
    54. Private Sub Form_Load()
    55.     Dim str1 As String, str2 As String
    56.    
    57.     str1 = "this is a test string"
    58.     str2 = "this is a sentence not too much like the first"
    59.    
    60.     MsgBox StartCompare(str1, str2)
    61.     'MsgBox StartCompare(str1, str2, True) 'TRUE for case-sensitive compare.
    62.    
    63. End Sub

  9. #9
    Lively Member twistedthoughts's Avatar
    Join Date
    Oct 2002
    Location
    dxb
    Posts
    114

    Re: How to compare the similarity of two words in VB?

    Hi
    Try the following & let me know if it helped...

    VB Code:
    1. Public Function SoundEx(cString As String) As String
    2.     Dim iLen As Integer
    3.     Dim cChar As String * 1
    4.     Dim jTemp As String * 1
    5.     Dim jTemp1 As String
    6.    
    7.     iLen = Len(Trim(cString))
    8.     For i = 2 To iLen
    9.         cChar = LCase(Mid(cString, i, 1))
    10.         Select Case cChar
    11.             Case "a", "e", "h", "i", "o", "u", "w", "y"
    12.                 jTemp = 0
    13.             Case "b", "f", "p", "v"
    14.                 jTemp = "1"
    15.             Case "c", "g", "j", "k", "q", "s", "x", "z"
    16.                 jTemp = "2"
    17.             Case "d", "t"
    18.                 jTemp = "3"
    19.             Case "l"
    20.                 jTemp = "4"
    21.             Case "m", "n"
    22.                 jTemp = "5"
    23.             Case "r"
    24.                 jTemp = "6"
    25.         End Select
    26.         If jTemp <> 0 Then
    27.             If Right(jTemp1, 1) <> jTemp Then
    28.                 jTemp1 = jTemp1 & jTemp
    29.             End If
    30.         End If
    31.         jTemp = 0
    32.     Next i
    33.     SoundEx = UCase(Mid(cString, 1, 1)) & jTemp1
    34.     SoundEx = SoundEx & String(4 - Len(SoundEx), "0")
    35. End Function

    Now for comparison, try

    VB Code:
    1. If SoundEx(Text1.Text) = SoundEx(text2.Text) Then
    2.         MsgBox "Yeah!!! They are similar"
    3.     Else
    4.         MsgBox "Oops!!! Doesn't match"
    5. End If
    Opinions Are Like Belly Buttons, Everyone Has One!

  10. #10
    New Member
    Join Date
    Aug 2018
    Posts
    1

    Re: How to compare the similarity of two words in VB?

    Hi there, long shot replying 12 years later but is this code based on a mathematical/statistical method or just your own intuition? Loving it regardless!

  11. #11

  12. #12
    New Member
    Join Date
    Mar 2024
    Posts
    2

    Re: How to compare the similarity of two words in VB?

    Can someone please explain how to utilize this vba? Everytime I put it in module I get sub error.

    Thank you!

  13. #13

  14. #14
    New Member
    Join Date
    Mar 2024
    Posts
    2

    Re: How to compare the similarity of two words in VB?

    Looks like it’s written for SQL not to be used in EXCEL is that right?

  15. #15
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: How to compare the similarity of two words in VB?

    It's VBA code with two versions. Put either one in a code module and see what happens. You can also search VBForums for 'soundx' and probably find an answer. BTW, what exactly happened when you tried the solutions from Digirev and twisted thoughts.

  16. #16
    Hyperactive Member
    Join Date
    Jul 2021
    Posts
    267

    Re: How to compare the similarity of two words in VB?

    How to compare the similarity of two words in VB?

    For example "weather" and "wther" are 70% similar

    "Unable to select an address from the list" and "Unable to select an address " has 23 characters out of 34 characters are the same so 67.6% are similar
    This got me interested in the algorithmic point of view. But also makes me wonder, what use there is to know the similarity percentage between two strings?

  17. #17
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,263

    Re: How to compare the similarity of two words in VB?

    Quote Originally Posted by Dry Bone View Post
    This got me interested in the algorithmic point of view. But also makes me wonder, what use there is to know the similarity percentage between two strings?
    Think google-search and/or Regex
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  18. #18
    Hyperactive Member
    Join Date
    Jul 2021
    Posts
    267

    Re: How to compare the similarity of two words in VB?

    Think google-search and/or Regex
    I don't get it. (And Regex makes me blackout :-))
    But I thought it could be useful to find a correction suggestions to a misspelled word.

  19. #19
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    6,179

    Re: How to compare the similarity of two words in VB?

    Quote Originally Posted by Dry Bone View Post
    This got me interested in the algorithmic point of view. But also makes me wonder, what use there is to know the similarity percentage between two strings?
    https://github.com/StefH/SimMetrics.Net -- some algorithms besides basic Levenstein.

    I've used this (similar library) to implement auto-correct on free-text search on a products catalog site.

    When the user is searching for a term which is not found in terms table (i.e. unique list of stemmed roots of all words in product descriptions) then a suggestion was made and the query was executed for the most similar search term instead (with an option to rollback to original).

    cheers,
    </wqw>

  20. #20
    Fanatic Member
    Join Date
    Apr 2021
    Posts
    616

    Re: How to compare the similarity of two words in VB?

    Quote Originally Posted by Dry Bone View Post
    what use there is to know the similarity percentage between two strings?
    Our task is not to ask why, unless asking gives an idea for implementation (which it often does, and the question should be worded that way). If someone asks for something and we have an idea of a way to do it, we should make suggestions.

    The original request also is very vague...phonetic similarity, characters, similes between words? We can assume character-based, in which case a custom algorithm which takes into account the length of the word and the number of characters that appear in both in the same place or nearby should do the job...it's something even the amateur coders can do, and is often something given as a programming task in classes, though I highly doubt people are still being taught VB6 these days :-)

  21. #21
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,454

    Re: How to compare the similarity of two words in VB?

    Quote Originally Posted by wqweto View Post
    https://github.com/StefH/SimMetrics.Net -- some algorithms besides basic Levenstein.
    Another one which plays in the same ballpark is Ratcliff/Obershelp:
    https://en.wikipedia.org/wiki/Gestalt_pattern_matching

    These algos do not work like "Google" (which is a FullText-search, based on AND/OR combined whole "stemmed words").
    It also has nothing to do with "RegExp"...

    Ratcliff is built into the RC5/6 SQLite-wrapper (as a UserDefined-Function):
    Code:
    Option Explicit
    
    Private Sub Form_Load()
      Dim MemDB As cMemDB, Rs As cRecordset
      Set MemDB = New_c.MemDB
          MemDB.Exec "Create Table T(ID Integer Primary Key, Name Text)"
        
        'Demo-inserts
        Const insName = "Insert Into T(Name) Values(?)"
              MemDB.ExecCmd insName, "Beth Mercks"
              MemDB.ExecCmd insName, "Brett Perks"
              MemDB.ExecCmd insName, "Fred Merx"
              MemDB.ExecCmd insName, "Mad Max"
              MemDB.ExecCmd insName, "Matt Murks"
              MemDB.ExecCmd insName, "Mett Wurst"
        
        'Ratcliff-query (unsharp search)
        Const qryUnsharp = "Select Top 5 Ratcliff(Name, ?) RcPerc, * From T " & _
                           " Order By RcPerc Desc"
              Set Rs = MemDB.GetRs(qryUnsharp, "Brad Merks") '<- SearchParameter-Passing
              Do Until Rs.EOF
                 If Rs.AbsolutePosition = 1 Then Debug.Print "RcPerc      Name"
                 Debug.Print Rs!RcPerc, Rs!Name
                 Rs.MoveNext
              Loop
    End Sub
    Olaf

  22. #22
    Hyperactive Member
    Join Date
    Jul 2021
    Posts
    267

    Re: How to compare the similarity of two words in VB?

    Wow! So much to learn!
    I didn't know so many algorithms exist for this.
    I only found about Myers algorithm, and, kind of, implemented some variant of it, since I didn't understand the original one.
    But I think mine is doing fine though :-)
    Maybe I will take the time to know more algorithms...

  23. #23
    Fanatic Member
    Join Date
    Jul 2017
    Posts
    761

    Re: How to compare the similarity of two words in VB?

    Would you share your code? I am also trying to compare texts, and Myers-Diff was the next thing I wanted to try.

  24. #24
    Hyperactive Member
    Join Date
    Jul 2021
    Posts
    267

    Re: How to compare the similarity of two words in VB?

    After adding this module, use TextDiff(s1, s2) which returns a value between 0-1. (1 means identitcal)
    Attached Files Attached Files

  25. #25
    Addicted Member
    Join Date
    Aug 2024
    Posts
    129

    Re: How to compare the similarity of two words in VB?

    I think the Levenshtein Distance method is the "Industry Standard"

    Code:
    Public Function LevenDis(sTxt1 As String, sTxt2 As String, Optional blnAsPercent = True) As Double
    Dim i As Long, j As Long
    Dim Len1, Len2 As Long
    Dim min1 As Long, min2 As Long
    Dim lDis() As Long
    
    Len1 = Len(sTxt1)
    Len2 = Len(sTxt2)
    ReDim lDis(Len1, Len2)
    
    For i = 0 To Len1: lDis(i, 0) = i: Next i
    For j = 0 To Len2:    lDis(0, j) = j: Next j
    
    For i = 1 To Len1
        For j = 1 To Len2
            If Mid(sTxt1, i, 1) = Mid(sTxt2, j, 1) Then
                lDis(i, j) = lDis(i - 1, j - 1)
            Else
                min1 = lDis(i - 1, j) + 1
                min2 = lDis(i, j - 1) + 1
                If min2 < min1 Then
                    min1 = min2
                End If
                min2 = lDis(i - 1, j - 1) + 1
                If min2 < min1 Then
                    min1 = min2
                End If
                lDis(i, j) = min1
            End If
        Next j
    Next i
    
    LevenDis = lDis(Len1, Len2)
    If blnAsPercent Then LevenDis = Abs(1 - (LevenDis / IIf(Len(sTxt1) > Len(sTxt2), Len(sTxt1), Len(sTxt2)))) * 100
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width