-
Notifications
You must be signed in to change notification settings - Fork 2
/
jaro.go
264 lines (213 loc) · 4.74 KB
/
jaro.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package muzzy
import "sync"
// WinklerScalingFactor how much the score is adjusted upwards for having common prefixes.
const WinklerScalingFactor = 0.1
// JaroWinklerSimilarity return how close s1 and s2 increase similarity of same prefixed.
func JaroWinklerSimilarity(s1, s2 string) float64 {
s := JaroSimilarity(s1, s2)
l, r1, r2 := 0, []rune(s1), []rune(s2)
n := len(r1)
if len(r2) < n {
n = len(r2)
}
for ; l < n && r1[l] == r2[l]; l++ {
}
return s + float64(l)*(1-s)*WinklerScalingFactor
}
// JaroSimilarity return how close s1 to s2
//
// To find the Jaro similarity we should find the number of matched characters $m$.
// If $m$ is equal zero then strings absolutely different and similarity is equal zero too.
// To simplify algorithm we assume that length of `s2` great or equal length of `s1`.
// If it is not, we swap strings.
//
// Characters of strings `s1` and `s2` are matched if they equal and their positions are closer
// than half of `s2` length. Of course, one character may be used only in one matched pair.
//
// If $m$ is not zero, we also should find the number of transpositions $t$,
// i.e. the number of matched pairs that switch their order.
//
// The Jaro similarity is
// \[
// \frac{1}{3}\left(\frac{m}{\|s_1\|} + \frac{m}{\|s_2\|} + \frac{m - t}{m}\right)
// \].
func JaroSimilarity(s1, s2 string) float64 {
return newJaroCalculator(s1, s2).Do()
}
type jaroCalculator struct {
s1, s2 []rune
l1, l2 []bool
}
func newJaroCalculator(s1, s2 string) *jaroCalculator {
jc := &jaroCalculator{s1: []rune(s1), s2: []rune(s2)}
if len(jc.s1) > len(jc.s2) {
jc.s1, jc.s2 = jc.s2, jc.s1
}
jc.l1 = make([]bool, len(jc.s1))
jc.l2 = make([]bool, len(jc.s2))
return jc
}
// To find matched characters we use a cartesian tree of characters of `s2`,
// where the priority is a character position and the key is a character code.
// We will control the epsilon neighborhood of the current character in `s1`
// by adding the next character of `s2` in the tree on every step,
// and poping root if its priority too far from the current character position.
func (jc *jaroCalculator) Do() float64 {
m := jc.FindMatchesCartesian()
if m == 0 {
return 0
}
t := jc.FindTranspositions()
n1, n2 := float64(len(jc.s1)), float64(len(jc.s2))
return (m/n1 + m/n2 + (m-t)/m) / 3
}
func (jc *jaroCalculator) FindMatchesCartesian() float64 {
eps := len(jc.s2) >> 1
tree := newCartesianTree()
m := 0.0
for i := 0; i < eps; i++ {
tree.Add(jc.s2[i])
}
for i := range jc.s1 {
if i+eps < len(jc.s2) {
tree.Add(jc.s2[i+eps])
}
if !tree.Empty() && tree.Peek() < i-eps {
tree.Pop()
}
j := tree.SearchAndDelete(jc.s1[i])
if j != -1 {
jc.l1[i] = true
jc.l2[j] = true
m++
}
}
return m
}
func (jc *jaroCalculator) FindTranspositions() float64 {
t := 0.0
i, j := 0, 0
for ; i < len(jc.s1); i++ {
if jc.l1[i] {
for ; j < len(jc.s2); j++ {
if jc.l2[j] {
j++
break
}
}
if jc.s1[i] != jc.s2[j-1] {
t++
}
}
}
return t
}
type node struct {
priority int
key rune
left *node
right *node
}
func (n *node) Add(m *node) {
for {
next := &n.right
if m.key < n.key {
next = &n.left
}
if *next == nil {
*next = m
return
}
n = *next
}
}
func (n *node) Search(key rune) (res, parent *node) {
res = n
for res != nil {
if key == res.key {
return
}
parent = res
if key < res.key {
res = res.left
} else {
res = res.right
}
}
return nil, nil
}
type cartesianTree struct {
root *node
pool *sync.Pool
next int
}
func newCartesianTree() *cartesianTree {
return &cartesianTree{
pool: &sync.Pool{
New: func() interface{} { return &node{} },
},
}
}
func (tree *cartesianTree) Add(key rune) {
n := tree.pool.Get().(*node)
n.priority = tree.next
n.key = key
n.left = nil
n.right = nil
if tree.root == nil {
tree.root = n
} else {
tree.root.Add(n)
}
tree.next++
}
func (tree *cartesianTree) SearchAndDelete(key rune) int {
n, p := tree.root.Search(key)
if n == nil {
return -1
}
m := tree.Merge(n.left, n.right)
switch {
case p == nil:
tree.root = m
case p.left == n:
p.left = m
default:
p.right = m
}
res := n.priority
tree.pool.Put(n)
return res
}
func (tree *cartesianTree) Empty() bool {
return tree.root == nil
}
func (tree *cartesianTree) Peek() int {
return tree.root.priority
}
func (tree *cartesianTree) Pop() {
m := tree.Merge(tree.root.left, tree.root.right)
if m == nil {
tree.root = nil
} else {
*tree.root = *m
tree.pool.Put(m)
}
}
func (tree *cartesianTree) Merge(n, m *node) *node {
if n == nil {
return m
}
if m == nil {
return n
}
if m.priority < n.priority {
n, m = m, n
}
if m.key < n.key {
n.left = tree.Merge(n.left, m)
} else {
n.right = tree.Merge(n.right, m)
}
return n
}