Advertisement
Guest User

Untitled

a guest
May 29th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. Public Function strSimLookup(str1 As Variant, rRng As Range, Optional returnType) As Variant
  2. 'Return either the best match or the index of the best match
  3. 'depending on returnTYype parameter) between str1 and strings in rRng)
  4. ' returnType = 0 or omitted: returns the best matching string
  5. ' returnType = 1 : returns the index of the best matching string
  6. ' returnType = 2 : returns the similarity metric
  7.  
  8. Dim sPairs1 As Collection
  9. Dim sPairs2 As Collection
  10. Dim metric, bestMetric As Double
  11. Dim i, iBest As Long
  12. Const RETURN_STRING As Integer = 0
  13. Const RETURN_INDEX As Integer = 1
  14. Const RETURN_METRIC As Integer = 2
  15.  
  16. If IsMissing(returnType) Then returnType = RETURN_STRING
  17.  
  18. Set sPairs1 = New Collection
  19.  
  20. WordLetterPairs CStr(str1), sPairs1
  21.  
  22. bestMetric = -1
  23. iBest = -1
  24.  
  25. For i = 1 To rRng.Count
  26. 'Exit For '' delete this
  27. Set sPairs2 = New Collection
  28. WordLetterPairs CStr(rRng(i)), sPairs2
  29. metric = SimilarityMetric(sPairs1, sPairs2)
  30. If metric > bestMetric Then
  31. bestMetric = metric
  32. iBest = i
  33. End If
  34. Set sPairs2 = Nothing
  35. Next i
  36.  
  37. If iBest = -1 Then
  38. strSimLookup = CVErr(xlErrValue)
  39. Exit Function
  40. End If
  41.  
  42. Select Case returnType
  43. Case RETURN_STRING
  44. strSimLookup = CStr(rRng(iBest))
  45. Case RETURN_INDEX
  46. strSimLookup = iBest
  47. Case Else
  48. strSimLookup = bestMetric
  49. End Select
  50.  
  51. End Function
  52.  
  53. Public Function strSim(str1 As String, str2 As String) As Variant
  54. Dim ilen, iLen1, ilen2 As Integer
  55.  
  56. iLen1 = Len(str1)
  57. ilen2 = Len(str2)
  58.  
  59. If iLen1 >= ilen2 Then ilen = ilen2 Else ilen = iLen1
  60.  
  61. strSim = stringSimilarity(Left(str1, ilen), Left(str2, ilen))
  62.  
  63. End Function
  64.  
  65. Sub WordLetterPairs(str As String, pairColl As Collection)
  66. 'Tokenize str into words, then add all letter pairs to pairColl
  67.  
  68. Dim Words() As String
  69. Dim word, nPairs, pair As Integer
  70.  
  71. Words = Split(str)
  72.  
  73. If UBound(Words) < 0 Then
  74. Set pairColl = Nothing
  75. Exit Sub
  76. End If
  77.  
  78. For word = 0 To UBound(Words)
  79. 'Exit For '' delete this
  80. nPairs = Len(Words(word)) - 1
  81. If nPairs > 0 Then
  82. For pair = 1 To nPairs
  83. pairColl.Add Mid(Words(word), pair, 2)
  84. Next pair
  85. End If
  86. Next word
  87.  
  88. End Sub
  89. Private Function SimilarityMetric(sPairs1 As Collection, sPairs2 As Collection) As Variant
  90. 'Helper function to calculate similarity metric given two collections of letter pairs.
  91. 'This function is designed to allow the pair collections to be set up separately as needed.
  92. 'NOTE: sPairs2 collection will be altered as pairs are removed; copy the collection
  93. 'if this is not the desired behavior.
  94. 'Also assumes that collections will be deallocated somewhere else
  95.  
  96. Dim Intersect As Double
  97. Dim Union As Double
  98. Dim i, j As Long
  99.  
  100. If sPairs1.Count = 0 Or sPairs2.Count = 0 Then
  101. SimilarityMetric = CVErr(xlErrNA)
  102. Exit Function
  103. End If
  104.  
  105. Union = sPairs1.Count + sPairs2.Count
  106. Intersect = 0
  107.  
  108. For i = 1 To sPairs1.Count
  109. 'Exit For '' delete this
  110. For j = 1 To sPairs2.Count
  111. 'Exit For '' delete this
  112. If StrComp(sPairs1(i), sPairs2(j)) = 0 Then
  113. Intersect = Intersect + 1
  114. sPairs2.Remove j
  115. Exit For
  116. End If
  117. Next j
  118. Next i
  119.  
  120. SimilarityMetric = (2 * Intersect) / Union
  121.  
  122. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement