-
Notifications
You must be signed in to change notification settings - Fork 0
/
levenshtein.go
167 lines (152 loc) · 3.52 KB
/
levenshtein.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package levenshtein
import "fmt"
/*
Weights are the costs of making a particular edit.
This struct functions as a configuration parameter
for the EditDistance functions
*/
type Weights struct {
Insert uint
Delete uint
Substitute uint
}
/*
NewDMatrix returns a new distance matrix, given
the dimensions as paramter. All the memory is
preallocated.
*/
func NewDMatrix(x, y uint) [][]uint {
d := make([][]uint, x+1, x+1)
for i := range d {
d[i] = make([]uint, y+1, y+1)
}
return d
}
/*
PrintMatrix prints the distance matrix
with all the rows lined up with each other
*/
func PrintMatrix(d [][]uint) {
for _, row := range d {
fmt.Println(row)
}
}
func min(x, y, z uint) uint {
min := x
if y < min {
min = y
} else if z < min {
min = z
}
return min
}
/*
EditDistance is the main function provided by this
package. It calculates the Levenshtein edit distance
between two strings provided as argument.
*/
func EditDistance(x, y []rune, w Weights) uint {
d := NewDMatrix(uint(len(x)), uint(len(y)))
for i := 0; i <= len(x); i++ {
d[i][0] = uint(i)
}
for j := 0; j <= len(y); j++ {
d[0][j] = uint(j)
}
for i := 1; i <= len(x); i++ {
for j := 1; j <= len(y); j++ {
if x[i-1] == y[j-1] {
d[i][j] = d[i-1][j-1]
} else {
d[i][j] = min(d[i-1][j]+w.Delete,
d[i][j-1]+w.Insert,
d[i-1][j-1]+w.Substitute,
)
}
}
}
// PrintMatrix(d)
return d[len(x)][len(y)]
}
/*
CompactEditDistance calculates distance with O(m)
memory rather than O(mn).
*/
func CompactEditDistance(x, y []rune, w Weights) uint {
prevRow := make([]uint, len(y)+1, len(y)+1)
currRow := make([]uint, len(y)+1, len(y)+1)
for i := 0; i <= len(y); i++ {
prevRow[i] = uint(i)
}
for i := 1; i <= len(x); i++ {
currRow[0] = uint(i)
for j := 1; j <= len(y); j++ {
if x[i-1] == y[j-1] {
currRow[j] = prevRow[j-1]
} else {
currRow[j] = min(prevRow[j]+w.Delete,
currRow[j-1]+w.Insert,
prevRow[j-1]+w.Substitute,
)
}
}
copy(prevRow, currRow)
}
return currRow[len(y)]
}
/*
BufferedCompactDist computes the distance using only
two rows, rather than a whole distance matrix. These two
rows are passed in as parameter, so the function itself does
not allocate.
*/
func BufferedCompactDist(x, y []rune, w Weights, prevRow []uint, currRow []uint) uint {
for i := 0; i <= len(y); i++ {
prevRow[i] = uint(i)
}
for i := 1; i <= len(x); i++ {
currRow[0] = uint(i)
for j := 1; j <= len(y); j++ {
if x[i-1] == y[j-1] {
currRow[j] = prevRow[j-1]
} else {
currRow[j] = min(prevRow[j]+w.Delete,
currRow[j-1]+w.Insert,
prevRow[j-1]+w.Substitute,
)
}
}
copy(prevRow, currRow)
}
return currRow[len(y)]
}
/*
BufferedEditDistance functions the same as EditDistance,
except that it takes the matrix as parameter, acting as a
preallocated buffer. This is useful for hot loops where you're
calculating edit distance of many string pairs. You can reuse
the buffer instead of allocating/deallocating on every function call.
Be sure to preallocate a buffer large enough to accomodate the
size range of all the pairs whose edit distances you'll be computing
*/
func BufferedEditDistance(x, y []rune, w Weights, d [][]uint) uint {
for i := 0; i <= len(x); i++ {
d[i][0] = uint(i)
}
for j := 0; j <= len(y); j++ {
d[0][j] = uint(j)
}
for i := 1; i <= len(x); i++ {
for j := 1; j <= len(y); j++ {
if x[i-1] == y[j-1] {
d[i][j] = d[i-1][j-1]
} else {
d[i][j] = min(d[i-1][j]+w.Delete,
d[i][j-1]+w.Insert,
d[i-1][j-1]+w.Substitute,
)
}
}
}
return d[len(x)][len(y)]
}