wagner-fisher & werner algorithms working
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package editdistance
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Werner(a, b string) int {
|
||||
|
||||
phrase_a := strings.Fields(a)
|
||||
phrase_b := strings.Fields(b)
|
||||
|
||||
m := len(phrase_a) + 1
|
||||
n := len(phrase_b) + 1
|
||||
distances := make([][]int, n)
|
||||
for i := range distances {
|
||||
distances[i] = make([]int, m)
|
||||
}
|
||||
// make the inital numbers on the top
|
||||
for i := 1; i < n; i++ {
|
||||
distances[i][0] = i
|
||||
}
|
||||
// make the inital numbers on the left side
|
||||
for i := 1; i < m; i++ {
|
||||
distances[0][i] = i
|
||||
}
|
||||
|
||||
for i := 1; i < n; i++ {
|
||||
for j := 1; j < m; j++ {
|
||||
subCost := 0
|
||||
if strings.EqualFold(phrase_a[j-1], phrase_b[i-1]) {
|
||||
subCost = 0
|
||||
} else {
|
||||
subCost = 1
|
||||
}
|
||||
value := min(
|
||||
distances[i-1][j]+1,
|
||||
distances[i][j-1]+1,
|
||||
distances[i-1][j-1]+subCost,
|
||||
)
|
||||
distances[i][j] = value
|
||||
}
|
||||
}
|
||||
|
||||
return distances[n-1][m-1]
|
||||
}
|
||||
Reference in New Issue
Block a user