weighted_levenshtein.py 2.9 KB

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