weighted_levenshtein_test.py 978 B

12345678910111213141516171819202122232425262728293031
  1. import unittest
  2. from .weighted_levenshtein import WeightedLevenshtein, CharacterSubstitutionInterface
  3. class CharSub(CharacterSubstitutionInterface):
  4. def cost(self, c0, c1):
  5. return 1.0
  6. class TestWeightedLevenshtein(unittest.TestCase):
  7. def test_weighted_levenshtein(self):
  8. a = WeightedLevenshtein(character_substitution=CharSub())
  9. s0 = ""
  10. s1 = ""
  11. s2 = "上海"
  12. s3 = "上海市"
  13. distance_format = "distance: {:.4}\t between {} and {}"
  14. print(distance_format.format(str(a.distance(s0, s1)), s0, s1))
  15. print(distance_format.format(str(a.distance(s0, s2)), s0, s2))
  16. print(distance_format.format(str(a.distance(s0, s3)), s0, s3))
  17. print(distance_format.format(str(a.distance(s1, s2)), s1, s2))
  18. print(distance_format.format(str(a.distance(s1, s3)), s1, s3))
  19. print(distance_format.format(str(a.distance(s2, s3)), s2, s3))
  20. if __name__ == "__main__":
  21. unittest.main()