-
Notifications
You must be signed in to change notification settings - Fork 6
/
Similarity.scala
366 lines (282 loc) · 8.09 KB
/
Similarity.scala
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/** Simple Scala wrappers to turn an existing string similarity functions into
* UDFs Additionaly some useful utilities are included
*/
package uk.gov.moj.dash.linkage
import org.apache.spark.sql.types._
import org.apache.spark.sql.expressions.UserDefinedFunction
import org.apache.spark.sql.catalyst.expressions.Literal
import org.apache.spark.sql.functions.udf
import org.apache.spark.sql.api.java.UDF2
import org.apache.spark.sql.api.java.UDF1
import org.apache.spark.sql.Row
import org.apache.commons.text.similarity
import org.apache.commons.codec.language
import scala.collection.mutable
class sqlEscape extends UDF1[String, String] {
override def call(s: String): String = Literal(s).sql
}
object sqlEscape {
def apply(): sqlEscape = {
new sqlEscape()
}
}
class DoubleMetaphone extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
val m = new language.DoubleMetaphone()
m.doubleMetaphone(input)
}
}
object DoubleMetaphone {
def apply(): DoubleMetaphone = {
new DoubleMetaphone()
}
}
class DoubleMetaphoneAlt extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
val m = new language.DoubleMetaphone()
m.doubleMetaphone(input, true)
}
}
object DoubleMetaphoneAlt {
def apply(): DoubleMetaphoneAlt = {
new DoubleMetaphoneAlt()
}
}
class QgramTokeniser extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
input.sliding(2).toList.mkString(" ")
}
}
object QgramTokeniser {
def apply(): QgramTokeniser = {
new QgramTokeniser()
}
}
class Q2gramTokeniser extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
input.sliding(2).toList.mkString(" ")
}
}
object Q2gramTokeniser {
def apply(): Q2gramTokeniser = {
new Q2gramTokeniser()
}
}
class Q3gramTokeniser extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
input.sliding(3).toList.mkString(" ")
}
}
object Q3gramTokeniser {
def apply(): Q3gramTokeniser = {
new Q3gramTokeniser()
}
}
class Q4gramTokeniser extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
input.sliding(4).toList.mkString(" ")
}
}
object Q4gramTokeniser {
def apply(): Q4gramTokeniser = {
new Q4gramTokeniser()
}
}
class Q5gramTokeniser extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
input.sliding(5).toList.mkString(" ")
}
}
object Q5gramTokeniser {
def apply(): Q5gramTokeniser = {
new Q5gramTokeniser()
}
}
class Q6gramTokeniser extends UDF1[String, String] {
override def call(input: String): String = {
// This has to be instantiated here (i.e. on the worker node)
input.sliding(6).toList.mkString(" ")
}
}
object Q6gramTokeniser {
def apply(): Q6gramTokeniser = {
new Q6gramTokeniser()
}
}
class JaroWinklerSimilarity extends UDF2[String, String, Double] {
override def call(left: String, right: String): Double = {
// This has to be instantiated here (i.e. on the worker node)
if ((left != null) & (right != null)) {
val jwsim = new similarity.JaroWinklerSimilarity()
jwsim(left, right)
} else {
0.0
}
}
}
object JaroWinklerSimilarity {
def apply(): JaroWinklerSimilarity = {
new JaroWinklerSimilarity()
}
}
class JaccardSimilarity extends UDF2[String, String, Double] {
override def call(left: String, right: String): Double = {
// This has to be instantiated here (i.e. on the worker node)
if ((left != null) & (right != null)) {
val distance = new similarity.JaccardSimilarity()
distance(left, right)
} else {
0.0
}
}
}
object JaccardSimilarity {
def apply(): JaccardSimilarity = {
new JaccardSimilarity()
}
}
class CosineDistance extends UDF2[String, String, Double] {
override def call(left: String, right: String): Double = {
// This has to be instantiated here (i.e. on the worker node)
if ((left != null) & (right != null)) {
val distance = new similarity.CosineDistance()
distance(left, right)
} else {
1.0
}
}
}
object CosineDistance {
def apply(): CosineDistance = {
new CosineDistance()
}
}
class DualArrayExplode
extends UDF2[Seq[String], Seq[String], Seq[(String, String)]] {
override def call(x: Seq[String], y: Seq[String]): Seq[(String, String)] = {
// This has to be instantiated here (i.e. on the worker node)
val DualArrayExplode = (x: Seq[String], y: Seq[String]) => {
if ((x != null) & (y != null)) {
for (a <- x; b <- y) yield (a, b)
} else { List() }
}
DualArrayExplode(x, y)
}
}
object DualArrayExplode {
def apply(): DualArrayExplode = {
new DualArrayExplode()
}
}
class latlongexplode extends UDF2[Seq[Row], Seq[Row], Seq[(Row, Row)]] {
override def call(x: Seq[Row], y: Seq[Row]): Seq[(Row, Row)] = {
val latlongexplode = (x: Seq[Row], y: Seq[Row]) => {
if ((x != null) & (y != null)) {
for (a <- x; b <- y) yield (a, b)
} else { List() }
}
latlongexplode(x, y)
}
}
object latlongexplode {
def apply(): latlongexplode = {
new latlongexplode()
}
}
class LevDamerauDistance extends UDF2[String, String, Double] {
override def call(left: String, right: String): Double = {
if ((left != null) & (right != null)) {
val len1 = left.length
val len2 = right.length
val infinite = len1 + len2
// character array
val da = mutable.Map[Char, Int]().withDefaultValue(0)
// distance matrix
val score = Array.fill(len1 + 2, len2 + 2)(0)
score(0)(0) = infinite
for (i <- 0 to len1) {
score(i + 1)(0) = infinite
score(i + 1)(1) = i
}
for (i <- 0 to len2) {
score(0)(i + 1) = infinite
score(1)(i + 1) = i
}
for (i <- 1 to len1) {
var db = 0
for (j <- 1 to len2) {
val i1 = da(right(j - 1))
val j1 = db
var cost = 1
if (left(i - 1) == right(j - 1)) {
cost = 0
db = j
}
score(i + 1)(j + 1) = List(
score(i)(j) + cost,
score(i + 1)(j) + 1,
score(i)(j + 1) + 1,
score(i1)(j1) + (i - i1 - 1) + 1 + (j - j1 - 1)
).min
}
da(left(i - 1)) = i
}
score(len1 + 1)(len2 + 1)
} else {
100.0
}
}
}
object LevDamerauDistance {
def apply(): LevDamerauDistance = {
new LevDamerauDistance()
}
}
class JaroSimilarity extends UDF2[String, String, Double] {
override def call(left: String, right: String): Double = {
if ((left != null) & (right != null)) {
val s1_len = left.length
val s2_len = right.length
if (s1_len == 0 && s2_len == 0) return 1.0
val match_distance = Math.max(s1_len, s2_len) / 2 - 1
val s1_matches = Array.ofDim[Boolean](s1_len)
val s2_matches = Array.ofDim[Boolean](s2_len)
var matches = 0
for (i <- 0 until s1_len) {
val start = Math.max(0, i - match_distance)
val end = Math.min(i + match_distance + 1, s2_len)
start until end find { j => !s2_matches(j) && left(i) == right(j) } match {
case Some(j) =>
s1_matches(i) = true
s2_matches(j) = true
matches += 1
case None =>
}
}
if (matches == 0) return 0.0
var t = 0.0
var k = 0
0 until s1_len filter s1_matches foreach { i =>
while (!s2_matches(k)) k += 1
if (left(i) != right(k)) t += 0.5
k += 1
}
val m = matches.toDouble
(m / s1_len + m / s2_len + (m - t) / m) / 3.0
} else {
0.0
}
}
}
object JaroSimilarity {
def apply(): JaroSimilarity = {
new JaroSimilarity()
}
}