jaro_winkler.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright (c) 2018 luozhouyang
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in all
  11. # copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from .string_distance import NormalizedStringDistance
  21. from .string_similarity import NormalizedStringSimilarity
  22. class JaroWinkler(NormalizedStringSimilarity, NormalizedStringDistance):
  23. def __init__(self, threshold=0.7):
  24. self.threshold = threshold
  25. self.three = 3
  26. self.jw_coef = 0.1
  27. def get_threshold(self):
  28. return self.threshold
  29. def similarity(self, s0, s1):
  30. if s0 is None:
  31. raise TypeError("Argument s0 is NoneType.")
  32. if s1 is None:
  33. raise TypeError("Argument s1 is NoneType.")
  34. if s0 == s1:
  35. return 1.0
  36. mtp = self.matches(s0, s1)
  37. m = mtp[0]
  38. if m == 0:
  39. return 0.0
  40. j = (m / len(s0) + m / len(s1) + (m - mtp[1]) / m) / self.three
  41. jw = j
  42. if j > self.get_threshold():
  43. jw = j + min(self.jw_coef, 1.0 / mtp[self.three]) * mtp[2] * (1 - j)
  44. return jw
  45. def distance(self, s0, s1):
  46. return 1.0 - self.similarity(s0, s1)
  47. @staticmethod
  48. def matches(s0, s1):
  49. if len(s0) > len(s1):
  50. max_str = s0
  51. min_str = s1
  52. else:
  53. max_str = s1
  54. min_str = s0
  55. ran = int(max(len(max_str) / 2 - 1, 0))
  56. match_indexes = [-1] * len(min_str)
  57. match_flags = [False] * len(max_str)
  58. matches = 0
  59. for mi in range(len(min_str)):
  60. c1 = min_str[mi]
  61. for xi in range(max(mi - ran, 0), min(mi + ran + 1, len(max_str))):
  62. if not match_flags[xi] and c1 == max_str[xi]:
  63. match_indexes[mi] = xi
  64. match_flags[xi] = True
  65. matches += 1
  66. break
  67. ms0, ms1 = [0] * matches, [0] * matches
  68. si = 0
  69. for i in range(len(min_str)):
  70. if match_indexes[i] != -1:
  71. ms0[si] = min_str[i]
  72. si += 1
  73. si = 0
  74. for j in range(len(max_str)):
  75. if match_flags[j]:
  76. ms1[si] = max_str[j]
  77. si += 1
  78. transpositions = 0
  79. for mi in range(len(ms0)):
  80. if ms0[mi] != ms1[mi]:
  81. transpositions += 1
  82. prefix = 0
  83. for mi in range(len(min_str)):
  84. if s0[mi] == s1[mi]:
  85. prefix += 1
  86. else:
  87. break
  88. return [matches, int(transpositions / 2), prefix, len(max_str)]