-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevenstein.go
74 lines (73 loc) · 1.38 KB
/
levenstein.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// Information about the algorithm is available on Wikipedia
//
// https://en.wikipedia.org/wiki/Levenshtein_distance
//
package levenstein
// Distance returns levenstein distance between two strings
func Distance(left string, right string) int {
if len(left) == 0 {
return len(right)
}
if len(right) == 0 {
return len(left)
}
var (
a []byte
b []byte
)
if len(left) > len(right) {
a = []byte(left)
b = []byte(right)
} else {
a = []byte(right)
b = []byte(left)
}
var (
prev int
i1 int
l1 int = len(b) + 1
i2 int
l2 int = len(a)
i3 int
l3 int = len(b)
f []int = make([]int, len(b)+1)
)
pre:
f[i1] = i1
i1++
if i1 < l1 {
goto pre
}
outer:
prev = f[0]
f[0]++
inner:
if f[i3+1] <= f[i3] && b[i3] != a[i2] && f[i3+1] > prev {
prev, f[i3+1] = f[i3+1], prev+1
} else if f[i3+1] <= f[i3] && b[i3] != a[i2] {
prev, f[i3+1] = f[i3+1], f[i3+1]+1
} else if f[i3+1] <= f[i3] && f[i3+1]+1 > prev {
prev, f[i3+1] = f[i3+1], prev
} else if f[i3+1] <= f[i3] {
prev, f[i3+1] = f[i3+1], f[i3+1]+1
} else if b[i3] != a[i2] && f[i3] > prev {
prev, f[i3+1] = f[i3+1], prev+1
} else if b[i3] != a[i2] {
prev, f[i3+1] = f[i3+1], f[i3]+1
} else if f[i3]+1 > prev {
prev, f[i3+1] = f[i3+1], prev
} else {
prev, f[i3+1] = f[i3+1], f[i3]+1
}
i3++
if i3 != l3 {
goto inner
}
i3 = 0
i2++
if i2 != l2 {
goto outer
}
return f[len(f)-1]
}