levenshtein.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package search
  15. import (
  16. "math"
  17. )
  18. func LevenshteinDistance(a, b string) int {
  19. la := len(a)
  20. lb := len(b)
  21. d := make([]int, la+1)
  22. var lastdiag, olddiag, temp int
  23. for i := 1; i <= la; i++ {
  24. d[i] = i
  25. }
  26. for i := 1; i <= lb; i++ {
  27. d[0] = i
  28. lastdiag = i - 1
  29. for j := 1; j <= la; j++ {
  30. olddiag = d[j]
  31. min := d[j] + 1
  32. if (d[j-1] + 1) < min {
  33. min = d[j-1] + 1
  34. }
  35. if a[j-1] == b[i-1] {
  36. temp = 0
  37. } else {
  38. temp = 1
  39. }
  40. if (lastdiag + temp) < min {
  41. min = lastdiag + temp
  42. }
  43. d[j] = min
  44. lastdiag = olddiag
  45. }
  46. }
  47. return d[la]
  48. }
  49. // LevenshteinDistanceMax same as LevenshteinDistance but
  50. // attempts to bail early once we know the distance
  51. // will be greater than max
  52. // in which case the first return val will be the max
  53. // and the second will be true, indicating max was exceeded
  54. func LevenshteinDistanceMax(a, b string, max int) (int, bool) {
  55. v, wasMax, _ := LevenshteinDistanceMaxReuseSlice(a, b, max, nil)
  56. return v, wasMax
  57. }
  58. func LevenshteinDistanceMaxReuseSlice(a, b string, max int, d []int) (int, bool, []int) {
  59. la := len(a)
  60. lb := len(b)
  61. ld := int(math.Abs(float64(la - lb)))
  62. if ld > max {
  63. return max, true, d
  64. }
  65. if cap(d) < la+1 {
  66. d = make([]int, la+1)
  67. }
  68. d = d[:la+1]
  69. var lastdiag, olddiag, temp int
  70. for i := 1; i <= la; i++ {
  71. d[i] = i
  72. }
  73. for i := 1; i <= lb; i++ {
  74. d[0] = i
  75. lastdiag = i - 1
  76. rowmin := max + 1
  77. for j := 1; j <= la; j++ {
  78. olddiag = d[j]
  79. min := d[j] + 1
  80. if (d[j-1] + 1) < min {
  81. min = d[j-1] + 1
  82. }
  83. if a[j-1] == b[i-1] {
  84. temp = 0
  85. } else {
  86. temp = 1
  87. }
  88. if (lastdiag + temp) < min {
  89. min = lastdiag + temp
  90. }
  91. if min < rowmin {
  92. rowmin = min
  93. }
  94. d[j] = min
  95. lastdiag = olddiag
  96. }
  97. // after each row if rowmin isn't less than max stop
  98. if rowmin > max {
  99. return max, true, d
  100. }
  101. }
  102. return d[la], false, d
  103. }