| 1 |
import unicodedata |
| 2 |
from collections import defaultdict |
| 3 |
from itertools import zip_longest |
| 4 |
import warnings |
| 5 |
|
| 6 |
|
| 7 |
def _normalize(s): |
| 8 |
return unicodedata.normalize("NFKD", s) |
| 9 |
|
| 10 |
|
| 11 |
def _check_type(s): |
| 12 |
|
| 13 |
warnings.warn( |
| 14 |
"The jellyfish._jellyfish module is deprecated and will be removed in jellyfish 1.0.", |
| 15 |
DeprecationWarning, |
| 16 |
) |
| 17 |
if not isinstance(s, str): |
| 18 |
raise TypeError("expected str or unicode, got %s" % type(s).__name__) |
| 19 |
|
| 20 |
|
| 21 |
def levenshtein_distance(s1, s2): |
| 22 |
_check_type(s1) |
| 23 |
_check_type(s2) |
| 24 |
|
| 25 |
if s1 == s2: |
| 26 |
return 0 |
| 27 |
rows = len(s1) + 1 |
| 28 |
cols = len(s2) + 1 |
| 29 |
|
| 30 |
if not s1: |
| 31 |
return cols - 1 |
| 32 |
if not s2: |
| 33 |
return rows - 1 |
| 34 |
|
| 35 |
prev = None |
| 36 |
cur = range(cols) |
| 37 |
for r in range(1, rows): |
| 38 |
prev, cur = cur, [r] + [0] * (cols - 1) |
| 39 |
for c in range(1, cols): |
| 40 |
deletion = prev[c] + 1 |
| 41 |
insertion = cur[c - 1] + 1 |
| 42 |
edit = prev[c - 1] + (0 if s1[r - 1] == s2[c - 1] else 1) |
| 43 |
cur[c] = min(edit, deletion, insertion) |
| 44 |
|
| 45 |
return cur[-1] |
| 46 |
|
| 47 |
|
| 48 |
def _jaro_winkler(s1, s2, long_tolerance, winklerize): |
| 49 |
_check_type(s1) |
| 50 |
_check_type(s2) |
| 51 |
|
| 52 |
s1_len = len(s1) |
| 53 |
s2_len = len(s2) |
| 54 |
|
| 55 |
if not s1_len or not s2_len: |
| 56 |
return 0.0 |
| 57 |
|
| 58 |
min_len = min(s1_len, s2_len) |
| 59 |
search_range = max(s1_len, s2_len) |
| 60 |
search_range = (search_range // 2) - 1 |
| 61 |
if search_range < 0: |
| 62 |
search_range = 0 |
| 63 |
|
| 64 |
s1_flags = [False] * s1_len |
| 65 |
s2_flags = [False] * s2_len |
| 66 |
|
| 67 |
|
| 68 |
common_chars = 0 |
| 69 |
for i, s1_ch in enumerate(s1): |
| 70 |
low = max(0, i - search_range) |
| 71 |
hi = min(i + search_range, s2_len - 1) |
| 72 |
for j in range(low, hi + 1): |
| 73 |
if not s2_flags[j] and s2[j] == s1_ch: |
| 74 |
s1_flags[i] = s2_flags[j] = True |
| 75 |
common_chars += 1 |
| 76 |
break |
| 77 |
|
| 78 |
|
| 79 |
if not common_chars: |
| 80 |
return 0.0 |
| 81 |
|
| 82 |
|
| 83 |
k = trans_count = 0 |
| 84 |
for i, s1_f in enumerate(s1_flags): |
| 85 |
if s1_f: |
| 86 |
for j in range(k, s2_len): |
| 87 |
if s2_flags[j]: |
| 88 |
k = j + 1 |
| 89 |
break |
| 90 |
if s1[i] != s2[j]: |
| 91 |
trans_count += 1 |
| 92 |
trans_count //= 2 |
| 93 |
|
| 94 |
|
| 95 |
common_chars = float(common_chars) |
| 96 |
weight = ( |
| 97 |
( |
| 98 |
common_chars / s1_len |
| 99 |
+ common_chars / s2_len |
| 100 |
+ (common_chars - trans_count) / common_chars |
| 101 |
) |
| 102 |
) / 3 |
| 103 |
|
| 104 |
|
| 105 |
if winklerize and weight > 0.7: |
| 106 |
|
| 107 |
j = min(min_len, 4) |
| 108 |
i = 0 |
| 109 |
while i < j and s1[i] == s2[i]: |
| 110 |
i += 1 |
| 111 |
if i: |
| 112 |
weight += i * 0.1 * (1.0 - weight) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if ( |
| 118 |
long_tolerance |
| 119 |
and min_len > 4 |
| 120 |
and common_chars > i + 1 |
| 121 |
and 2 * common_chars >= min_len + i |
| 122 |
): |
| 123 |
weight += (1.0 - weight) * ( |
| 124 |
float(common_chars - i - 1) / float(s1_len + s2_len - i * 2 + 2) |
| 125 |
) |
| 126 |
|
| 127 |
return weight |
| 128 |
|
| 129 |
|
| 130 |
def jaro_similarity(s1, s2): |
| 131 |
return _jaro_winkler(s1, s2, False, False) |
| 132 |
|
| 133 |
|
| 134 |
def jaro_winkler_similarity(s1, s2, long_tolerance=False): |
| 135 |
return _jaro_winkler(s1, s2, long_tolerance, True) |
| 136 |
|
| 137 |
|
| 138 |
def damerau_levenshtein_distance(s1, s2): |
| 139 |
_check_type(s1) |
| 140 |
_check_type(s2) |
| 141 |
|
| 142 |
len1 = len(s1) |
| 143 |
len2 = len(s2) |
| 144 |
infinite = len1 + len2 |
| 145 |
|
| 146 |
|
| 147 |
da = defaultdict(int) |
| 148 |
|
| 149 |
|
| 150 |
score = [[0] * (len2 + 2) for x in range(len1 + 2)] |
| 151 |
|
| 152 |
score[0][0] = infinite |
| 153 |
for i in range(0, len1 + 1): |
| 154 |
score[i + 1][0] = infinite |
| 155 |
score[i + 1][1] = i |
| 156 |
for i in range(0, len2 + 1): |
| 157 |
score[0][i + 1] = infinite |
| 158 |
score[1][i + 1] = i |
| 159 |
|
| 160 |
for i in range(1, len1 + 1): |
| 161 |
db = 0 |
| 162 |
for j in range(1, len2 + 1): |
| 163 |
i1 = da[s2[j - 1]] |
| 164 |
j1 = db |
| 165 |
cost = 1 |
| 166 |
if s1[i - 1] == s2[j - 1]: |
| 167 |
cost = 0 |
| 168 |
db = j |
| 169 |
|
| 170 |
score[i + 1][j + 1] = min( |
| 171 |
score[i][j] + cost, |
| 172 |
score[i + 1][j] + 1, |
| 173 |
score[i][j + 1] + 1, |
| 174 |
score[i1][j1] + (i - i1 - 1) + 1 + (j - j1 - 1), |
| 175 |
) |
| 176 |
da[s1[i - 1]] = i |
| 177 |
|
| 178 |
return score[len1 + 1][len2 + 1] |
| 179 |
|
| 180 |
|
| 181 |
def soundex(s): |
| 182 |
_check_type(s) |
| 183 |
|
| 184 |
if not s: |
| 185 |
return "" |
| 186 |
|
| 187 |
s = _normalize(s) |
| 188 |
s = s.upper() |
| 189 |
|
| 190 |
replacements = ( |
| 191 |
("BFPV", "1"), |
| 192 |
("CGJKQSXZ", "2"), |
| 193 |
("DT", "3"), |
| 194 |
("L", "4"), |
| 195 |
("MN", "5"), |
| 196 |
("R", "6"), |
| 197 |
) |
| 198 |
result = [s[0]] |
| 199 |
count = 1 |
| 200 |
|
| 201 |
|
| 202 |
for lset, sub in replacements: |
| 203 |
if s[0] in lset: |
| 204 |
last = sub |
| 205 |
break |
| 206 |
else: |
| 207 |
last = None |
| 208 |
|
| 209 |
for letter in s[1:]: |
| 210 |
for lset, sub in replacements: |
| 211 |
if letter in lset: |
| 212 |
if sub != last: |
| 213 |
result.append(sub) |
| 214 |
count += 1 |
| 215 |
last = sub |
| 216 |
break |
| 217 |
else: |
| 218 |
if letter != "H" and letter != "W": |
| 219 |
|
| 220 |
last = None |
| 221 |
if count == 4: |
| 222 |
break |
| 223 |
|
| 224 |
result += "0" * (4 - count) |
| 225 |
return "".join(result) |
| 226 |
|
| 227 |
|
| 228 |
def hamming_distance(s1, s2): |
| 229 |
_check_type(s1) |
| 230 |
_check_type(s2) |
| 231 |
|
| 232 |
|
| 233 |
if len(s2) > len(s1): |
| 234 |
s1, s2 = s2, s1 |
| 235 |
|
| 236 |
|
| 237 |
distance = len(s1) - len(s2) |
| 238 |
for i, c in enumerate(s2): |
| 239 |
if c != s1[i]: |
| 240 |
distance += 1 |
| 241 |
|
| 242 |
return distance |
| 243 |
|
| 244 |
|
| 245 |
def nysiis(s): |
| 246 |
_check_type(s) |
| 247 |
|
| 248 |
if not s: |
| 249 |
return "" |
| 250 |
|
| 251 |
s = s.upper() |
| 252 |
key = [] |
| 253 |
|
| 254 |
|
| 255 |
if s.startswith("MAC"): |
| 256 |
s = "MCC" + s[3:] |
| 257 |
elif s.startswith("KN"): |
| 258 |
s = s[1:] |
| 259 |
elif s.startswith("K"): |
| 260 |
s = "C" + s[1:] |
| 261 |
elif s.startswith(("PH", "PF")): |
| 262 |
s = "FF" + s[2:] |
| 263 |
elif s.startswith("SCH"): |
| 264 |
s = "SSS" + s[3:] |
| 265 |
|
| 266 |
|
| 267 |
if s.endswith(("IE", "EE")): |
| 268 |
s = s[:-2] + "Y" |
| 269 |
elif s.endswith(("DT", "RT", "RD", "NT", "ND")): |
| 270 |
s = s[:-2] + "D" |
| 271 |
|
| 272 |
|
| 273 |
key.append(s[0]) |
| 274 |
|
| 275 |
|
| 276 |
i = 1 |
| 277 |
len_s = len(s) |
| 278 |
while i < len_s: |
| 279 |
ch = s[i] |
| 280 |
if ch == "E" and i + 1 < len_s and s[i + 1] == "V": |
| 281 |
ch = "AF" |
| 282 |
i += 1 |
| 283 |
elif ch in "AEIOU": |
| 284 |
ch = "A" |
| 285 |
elif ch == "Q": |
| 286 |
ch = "G" |
| 287 |
elif ch == "Z": |
| 288 |
ch = "S" |
| 289 |
elif ch == "M": |
| 290 |
ch = "N" |
| 291 |
elif ch == "K": |
| 292 |
if i + 1 < len(s) and s[i + 1] == "N": |
| 293 |
ch = "N" |
| 294 |
else: |
| 295 |
ch = "C" |
| 296 |
elif ch == "S" and s[i + 1 : i + 3] == "CH": |
| 297 |
ch = "SS" |
| 298 |
i += 2 |
| 299 |
elif ch == "P" and i + 1 < len(s) and s[i + 1] == "H": |
| 300 |
ch = "F" |
| 301 |
i += 1 |
| 302 |
elif ch == "H" and ( |
| 303 |
s[i - 1] not in "AEIOU" |
| 304 |
or (i + 1 < len(s) and s[i + 1] not in "AEIOU") |
| 305 |
or (i + 1 == len(s)) |
| 306 |
): |
| 307 |
if s[i - 1] in "AEIOU": |
| 308 |
ch = "A" |
| 309 |
else: |
| 310 |
ch = s[i - 1] |
| 311 |
elif ch == "W" and s[i - 1] in "AEIOU": |
| 312 |
ch = s[i - 1] |
| 313 |
|
| 314 |
if ch[-1] != key[-1][-1]: |
| 315 |
key.append(ch) |
| 316 |
|
| 317 |
i += 1 |
| 318 |
|
| 319 |
key = "".join(key) |
| 320 |
|
| 321 |
|
| 322 |
if key.endswith("S") and key != "S": |
| 323 |
key = key[:-1] |
| 324 |
|
| 325 |
|
| 326 |
if key.endswith("AY"): |
| 327 |
key = key[:-2] + "Y" |
| 328 |
|
| 329 |
|
| 330 |
if key.endswith("A") and key != "A": |
| 331 |
key = key[:-1] |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
return key |
| 336 |
|
| 337 |
|
| 338 |
def match_rating_codex(s): |
| 339 |
_check_type(s) |
| 340 |
|
| 341 |
|
| 342 |
s = s.upper().replace(" ", "") |
| 343 |
|
| 344 |
if not s.isalpha(): |
| 345 |
raise ValueError("string must be alphabetic") |
| 346 |
|
| 347 |
codex = [] |
| 348 |
|
| 349 |
prev = None |
| 350 |
first = True |
| 351 |
for c in s: |
| 352 |
|
| 353 |
|
| 354 |
if first or (c not in "AEIOU" and c != prev): |
| 355 |
codex.append(c) |
| 356 |
|
| 357 |
prev = c |
| 358 |
first = False |
| 359 |
|
| 360 |
|
| 361 |
if len(codex) > 6: |
| 362 |
return "".join(codex[:3] + codex[-3:]) |
| 363 |
else: |
| 364 |
return "".join(codex) |
| 365 |
|
| 366 |
|
| 367 |
def match_rating_comparison(s1, s2): |
| 368 |
codex1 = match_rating_codex(s1) |
| 369 |
codex2 = match_rating_codex(s2) |
| 370 |
len1 = len(codex1) |
| 371 |
len2 = len(codex2) |
| 372 |
res1 = [] |
| 373 |
res2 = [] |
| 374 |
|
| 375 |
|
| 376 |
if abs(len1 - len2) >= 3: |
| 377 |
return None |
| 378 |
|
| 379 |
|
| 380 |
lensum = len1 + len2 |
| 381 |
if lensum <= 4: |
| 382 |
min_rating = 5 |
| 383 |
elif lensum <= 7: |
| 384 |
min_rating = 4 |
| 385 |
elif lensum <= 11: |
| 386 |
min_rating = 3 |
| 387 |
else: |
| 388 |
min_rating = 2 |
| 389 |
|
| 390 |
|
| 391 |
for c1, c2 in zip_longest(codex1, codex2): |
| 392 |
if c1 != c2: |
| 393 |
if c1: |
| 394 |
res1.append(c1) |
| 395 |
if c2: |
| 396 |
res2.append(c2) |
| 397 |
|
| 398 |
unmatched_count1 = unmatched_count2 = 0 |
| 399 |
for c1, c2 in zip_longest(reversed(res1), reversed(res2)): |
| 400 |
if c1 != c2: |
| 401 |
if c1: |
| 402 |
unmatched_count1 += 1 |
| 403 |
if c2: |
| 404 |
unmatched_count2 += 1 |
| 405 |
|
| 406 |
return (6 - max(unmatched_count1, unmatched_count2)) >= min_rating |
| 407 |
|
| 408 |
|
| 409 |
def metaphone(s): |
| 410 |
_check_type(s) |
| 411 |
|
| 412 |
result = [] |
| 413 |
|
| 414 |
s = _normalize(s.lower()) |
| 415 |
|
| 416 |
|
| 417 |
if s.startswith(("kn", "gn", "pn", "wr", "ae")): |
| 418 |
s = s[1:] |
| 419 |
|
| 420 |
i = 0 |
| 421 |
|
| 422 |
while i < len(s): |
| 423 |
c = s[i] |
| 424 |
next = s[i + 1] if i < len(s) - 1 else "*****" |
| 425 |
nextnext = s[i + 2] if i < len(s) - 2 else "*****" |
| 426 |
|
| 427 |
|
| 428 |
if c == next and c != "c": |
| 429 |
i += 1 |
| 430 |
continue |
| 431 |
|
| 432 |
if c in "aeiou": |
| 433 |
if i == 0 or s[i - 1] == " ": |
| 434 |
result.append(c) |
| 435 |
elif c == "b": |
| 436 |
if (not (i != 0 and s[i - 1] == "m")) or next: |
| 437 |
result.append("b") |
| 438 |
elif c == "c": |
| 439 |
if next == "i" and nextnext == "a" or next == "h": |
| 440 |
result.append("x") |
| 441 |
i += 1 |
| 442 |
elif next in "iey": |
| 443 |
result.append("s") |
| 444 |
i += 1 |
| 445 |
else: |
| 446 |
result.append("k") |
| 447 |
elif c == "d": |
| 448 |
if next == "g" and nextnext in "iey": |
| 449 |
result.append("j") |
| 450 |
i += 2 |
| 451 |
else: |
| 452 |
result.append("t") |
| 453 |
elif c in "fjlmnr": |
| 454 |
result.append(c) |
| 455 |
elif c == "g": |
| 456 |
if next in "iey": |
| 457 |
result.append("j") |
| 458 |
elif next == "h" and nextnext and nextnext not in "aeiou": |
| 459 |
i += 1 |
| 460 |
elif next == "n" and not nextnext: |
| 461 |
i += 1 |
| 462 |
else: |
| 463 |
result.append("k") |
| 464 |
elif c == "h": |
| 465 |
if i == 0 or next in "aeiou" or s[i - 1] not in "aeiou": |
| 466 |
result.append("h") |
| 467 |
elif c == "k": |
| 468 |
if i == 0 or s[i - 1] != "c": |
| 469 |
result.append("k") |
| 470 |
elif c == "p": |
| 471 |
if next == "h": |
| 472 |
result.append("f") |
| 473 |
i += 1 |
| 474 |
else: |
| 475 |
result.append("p") |
| 476 |
elif c == "q": |
| 477 |
result.append("k") |
| 478 |
elif c == "s": |
| 479 |
if next == "h": |
| 480 |
result.append("x") |
| 481 |
i += 1 |
| 482 |
elif next == "i" and nextnext in "oa": |
| 483 |
result.append("x") |
| 484 |
i += 2 |
| 485 |
else: |
| 486 |
result.append("s") |
| 487 |
elif c == "t": |
| 488 |
if next == "i" and nextnext in "oa": |
| 489 |
result.append("x") |
| 490 |
elif next == "h": |
| 491 |
result.append("0") |
| 492 |
i += 1 |
| 493 |
elif next != "c" or nextnext != "h": |
| 494 |
result.append("t") |
| 495 |
elif c == "v": |
| 496 |
result.append("f") |
| 497 |
elif c == "w": |
| 498 |
if i == 0 and next == "h": |
| 499 |
i += 1 |
| 500 |
result.append("w") |
| 501 |
elif next in "aeiou": |
| 502 |
result.append("w") |
| 503 |
elif c == "x": |
| 504 |
if i == 0: |
| 505 |
if next == "h" or (next == "i" and nextnext in "oa"): |
| 506 |
result.append("x") |
| 507 |
else: |
| 508 |
result.append("s") |
| 509 |
else: |
| 510 |
result.append("k") |
| 511 |
result.append("s") |
| 512 |
elif c == "y": |
| 513 |
if next in "aeiou": |
| 514 |
result.append("y") |
| 515 |
elif c == "z": |
| 516 |
result.append("s") |
| 517 |
elif c == " ": |
| 518 |
if len(result) > 0 and result[-1] != " ": |
| 519 |
result.append(" ") |
| 520 |
|
| 521 |
i += 1 |
| 522 |
|
| 523 |
return "".join(result).upper() |