Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created August 24, 2018 18:21
Show Gist options
  • Save kmaher9/2490ffa3050a70b5a6339e8a728c9876 to your computer and use it in GitHub Desktop.
Save kmaher9/2490ffa3050a70b5a6339e8a728c9876 to your computer and use it in GitHub Desktop.
this is the only file needed - it is a package implementation of the Levenshtein Distance algorithm, as used in string comparison.
package levenshtein
import (
"unicode/utf8"
)
func computeLevenshteinValue(a, b string) int {
f := make([]int, utf8.RuneCountInString(b)+1)
for j := range f {
f[j] = j
}
for _, ca := range a {
j := 1
fj1 := f[0]
f[0]++
for _, cb := range b {
mn := min(f[j]+1, f[j-1]+1)
if cb != ca {
mn = min(mn, fj1+1)
} else {
mn = min(mn, fj1)
}
fj1, f[j] = f[j], mn
j++
}
}
return f[len(f)-1]
}
func computeLevenshteinPercentage(a, b string) float64 {
distance := computeLevenshteinValue(a, b)
length := calculateLongestWord(a, b)
return (1.00 - float64(distance)/float64(length)) * 100.00
}
func calculateLongestWord(a, b string) int {
if len(a) >= len(b) {
return len(a)
}
return len(b)
}
func min(a, b int) int {
if a <= b {
return a
}
return b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment