weighted_levenshtein.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 StringDistance
  21. def default_insertion_cost(char):
  22. return 1.0
  23. def default_deletion_cost(char):
  24. return 1.0
  25. def default_substitution_cost(char_a, char_b):
  26. return 1.0
  27. class WeightedLevenshtein(StringDistance):
  28. def __init__(self,
  29. substitution_cost_fn=default_substitution_cost,
  30. insertion_cost_fn=default_insertion_cost,
  31. deletion_cost_fn=default_deletion_cost,
  32. ):
  33. self.substitution_cost_fn = substitution_cost_fn
  34. self.insertion_cost_fn = insertion_cost_fn
  35. self.deletion_cost_fn = deletion_cost_fn
  36. def distance(self, s0, s1):
  37. if s0 is None:
  38. raise TypeError("Argument s0 is NoneType.")
  39. if s1 is None:
  40. raise TypeError("Argument s1 is NoneType.")
  41. if s0 == s1:
  42. return 0.0
  43. if len(s0) == 0:
  44. return len(s1)
  45. if len(s1) == 0:
  46. return len(s0)
  47. v0, v1 = [0.0] * (len(s1) + 1), [0.0] * (len(s1) + 1)
  48. v0[0] = 0
  49. for i in range(1, len(v0)):
  50. v0[i] = v0[i - 1] + self.insertion_cost_fn(s1[i - 1])
  51. for i in range(len(s0)):
  52. s0i = s0[i]
  53. deletion_cost = self.deletion_cost_fn(s0i)
  54. v1[0] = v0[0] + deletion_cost
  55. for j in range(len(s1)):
  56. s1j = s1[j]
  57. cost = 0
  58. if s0i != s1j:
  59. cost = self.substitution_cost_fn(s0i, s1j)
  60. insertion_cost = self.insertion_cost_fn(s1j)
  61. v1[j + 1] = min(v1[j] + insertion_cost, v0[j + 1] + deletion_cost, v0[j] + cost)
  62. v0, v1 = v1, v0
  63. return v0[len(s1)]