2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-18 15:25:31 +00:00
cointop/pkg/levenshtein/levenshtein.go
Miguel Mota 01b248ee82 rm panicparse dep
Former-commit-id: 03f60c917e961b9a602201f44db08d96f9633063 [formerly 03f60c917e961b9a602201f44db08d96f9633063 [formerly ebc717d2c1907fc7a7840d674e260a9a033b4554 [formerly 664ed54783c9fdfd300eb39fd4b4a648a410659f]]]
Former-commit-id: aac9a01875dc0f2bb9cafdf6a70743e0268c0475
Former-commit-id: 587aa697ce2b26cd6a11406742349b60abad2c11 [formerly 61857f48e24c87948a072aa86c331f0a644bc26e]
Former-commit-id: 0690a6a5fce4865803e25015fb141e316279bf8f
2018-04-17 21:19:48 -07:00

56 lines
1.3 KiB
Go

package levenshtein
import (
"math"
"strings"
)
// DamerauLevenshteinDistance calculates the damerau-levenshtein distance between s1 and s2.
// Reference: [Damerau-Levenshtein Distance](http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance)
// Note that this calculation's result isn't normalized. (not between 0 and 1.)
// and if s1 and s2 are exactly the same, the result is 0.
func DamerauLevenshteinDistance(s1, s2 string) int {
if s1 == s2 {
return 0
}
s1Array := strings.Split(s1, "")
s2Array := strings.Split(s2, "")
lenS1Array := len(s1Array)
lenS2Array := len(s2Array)
m := make([][]int, lenS1Array+1)
var cost int
for i := range m {
m[i] = make([]int, lenS2Array+1)
}
for i := 0; i < lenS1Array+1; i++ {
for j := 0; j < lenS2Array+1; j++ {
if i == 0 {
m[i][j] = j
} else if j == 0 {
m[i][j] = i
} else {
cost = 0
if s1Array[i-1] != s2Array[j-1] {
cost = 1
}
m[i][j] = min(m[i-1][j]+1, m[i][j-1]+1, m[i-1][j-1]+cost)
if i > 1 && j > 1 && s1Array[i-1] == s2Array[j-2] && s1Array[i-2] == s2Array[j-1] {
m[i][j] = min(m[i][j], m[i-2][j-2]+cost)
}
}
}
}
return m[lenS1Array][lenS2Array]
}
// min returns the minimum number of passed int slices.
func min(is ...int) int {
min := int(math.MaxInt32)
for _, v := range is {
if min > v {
min = v
}
}
return min
}