gstr_levenshtein.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gstr
  7. // Levenshtein calculates Levenshtein distance between two strings.
  8. // costIns: Defines the cost of insertion.
  9. // costRep: Defines the cost of replacement.
  10. // costDel: Defines the cost of deletion.
  11. // See http://php.net/manual/en/function.levenshtein.php.
  12. func Levenshtein(str1, str2 string, costIns, costRep, costDel int) int {
  13. var maxLen = 255
  14. l1 := len(str1)
  15. l2 := len(str2)
  16. if l1 == 0 {
  17. return l2 * costIns
  18. }
  19. if l2 == 0 {
  20. return l1 * costDel
  21. }
  22. if l1 > maxLen || l2 > maxLen {
  23. return -1
  24. }
  25. tmp := make([]int, l2+1)
  26. p1 := make([]int, l2+1)
  27. p2 := make([]int, l2+1)
  28. var c0, c1, c2 int
  29. var i1, i2 int
  30. for i2 := 0; i2 <= l2; i2++ {
  31. p1[i2] = i2 * costIns
  32. }
  33. for i1 = 0; i1 < l1; i1++ {
  34. p2[0] = p1[0] + costDel
  35. for i2 = 0; i2 < l2; i2++ {
  36. if str1[i1] == str2[i2] {
  37. c0 = p1[i2]
  38. } else {
  39. c0 = p1[i2] + costRep
  40. }
  41. c1 = p1[i2+1] + costDel
  42. if c1 < c0 {
  43. c0 = c1
  44. }
  45. c2 = p2[i2] + costIns
  46. if c2 < c0 {
  47. c0 = c2
  48. }
  49. p2[i2+1] = c0
  50. }
  51. tmp = p1
  52. p1 = p2
  53. p2 = tmp
  54. }
  55. c0 = p1[l2]
  56. return c0
  57. }