You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cointop/pkg/levenshtein/levenshtein.go

58 lines
1.2 KiB
Go

package levenshtein
// Distance Levenshtein distance
// The Levenshtein distance between two strings is defined as the minimum
// number of edits needed to transform one string into the other, with the
// allowable edit operations being insertion, deletion, or substitution of
// a single character
// http://en.wikipedia.org/wiki/Levenshtein_distance
//
// This implemention is optimized to use O(min(m,n)) space.
// It is based on the optimized C version found here:
// http://en.wikibooks.org/wiki/Algorithm_implementation/Strings/Levenshtein_distance#C
func Distance(str1, str2 string) int {
var cost, lastdiag, olddiag int
s1 := []rune(str1)
s2 := []rune(str2)
lenS1 := len(s1)
lenS2 := len(s2)
column := make([]int, lenS1+1)
for y := 1; y <= lenS1; y++ {
column[y] = y
}
for x := 1; x <= lenS2; x++ {
column[0] = x
lastdiag = x - 1
for y := 1; y <= lenS1; y++ {
olddiag = column[y]
cost = 0
if s1[y-1] != s2[x-1] {
cost = 1
}
column[y] = min(
column[y]+1,
column[y-1]+1,
lastdiag+cost)
lastdiag = olddiag
}
}
return column[lenS1]
}
func min(a, b, c int) int {
if a < b {
if a < c {
return a
}
} else {
if b < c {
return b
}
}
return c
}