| Conditions | 31 |
| Total Lines | 160 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 57 |
| CRAP Score | 31 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like abydos.distance._jaro.sim_jaro_winkler() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | # -*- coding: utf-8 -*- |
||
| 261 | 1 | def sim_jaro_winkler( |
|
| 262 | src, |
||
| 263 | tar, |
||
| 264 | qval=1, |
||
| 265 | mode='winkler', |
||
| 266 | long_strings=False, |
||
| 267 | boost_threshold=0.7, |
||
| 268 | scaling_factor=0.1, |
||
| 269 | ): |
||
| 270 | """Return the Jaro or Jaro-Winkler similarity of two strings. |
||
| 271 | |||
| 272 | Jaro(-Winkler) distance is a string edit distance initially proposed by |
||
| 273 | Jaro and extended by Winkler :cite:`Jaro:1989,Winkler:1990`. |
||
| 274 | |||
| 275 | This is Python based on the C code for strcmp95: |
||
| 276 | http://web.archive.org/web/20110629121242/http://www.census.gov/geo/msb/stand/strcmp.c |
||
| 277 | :cite:`Winkler:1994`. The above file is a US Government publication and, |
||
| 278 | accordingly, in the public domain. |
||
| 279 | |||
| 280 | :param str src: source string for comparison |
||
| 281 | :param str tar: target string for comparison |
||
| 282 | :param int qval: the length of each q-gram (defaults to 1: character-wise |
||
| 283 | matching) |
||
| 284 | :param str mode: indicates which variant of this distance metric to |
||
| 285 | compute: |
||
| 286 | |||
| 287 | - 'winkler' -- computes the Jaro-Winkler distance (default) which |
||
| 288 | increases the score for matches near the start of the word |
||
| 289 | - 'jaro' -- computes the Jaro distance |
||
| 290 | |||
| 291 | The following arguments apply only when mode is 'winkler': |
||
| 292 | |||
| 293 | :param bool long_strings: set to True to "Increase the probability of a |
||
| 294 | match when the number of matched characters is large. This option |
||
| 295 | allows for a little more tolerance when the strings are large. It is |
||
| 296 | not an appropriate test when comparing fixed length fields such as |
||
| 297 | phone and social security numbers." |
||
| 298 | :param float boost_threshold: a value between 0 and 1, below which the |
||
| 299 | Winkler boost is not applied (defaults to 0.7) |
||
| 300 | :param float scaling_factor: a value between 0 and 0.25, indicating by how |
||
| 301 | much to boost scores for matching prefixes (defaults to 0.1) |
||
| 302 | |||
| 303 | :returns: Jaro or Jaro-Winkler similarity |
||
| 304 | :rtype: float |
||
| 305 | |||
| 306 | >>> round(sim_jaro_winkler('cat', 'hat'), 12) |
||
| 307 | 0.777777777778 |
||
| 308 | >>> round(sim_jaro_winkler('Niall', 'Neil'), 12) |
||
| 309 | 0.805 |
||
| 310 | >>> round(sim_jaro_winkler('aluminum', 'Catalan'), 12) |
||
| 311 | 0.60119047619 |
||
| 312 | >>> round(sim_jaro_winkler('ATCG', 'TAGC'), 12) |
||
| 313 | 0.833333333333 |
||
| 314 | |||
| 315 | >>> round(sim_jaro_winkler('cat', 'hat', mode='jaro'), 12) |
||
| 316 | 0.777777777778 |
||
| 317 | >>> round(sim_jaro_winkler('Niall', 'Neil', mode='jaro'), 12) |
||
| 318 | 0.783333333333 |
||
| 319 | >>> round(sim_jaro_winkler('aluminum', 'Catalan', mode='jaro'), 12) |
||
| 320 | 0.60119047619 |
||
| 321 | >>> round(sim_jaro_winkler('ATCG', 'TAGC', mode='jaro'), 12) |
||
| 322 | 0.833333333333 |
||
| 323 | """ |
||
| 324 | 1 | if mode == 'winkler': |
|
| 325 | 1 | if boost_threshold > 1 or boost_threshold < 0: |
|
| 326 | 1 | raise ValueError( |
|
| 327 | 'Unsupported boost_threshold assignment; ' |
||
| 328 | + 'boost_threshold must be between 0 and 1.' |
||
| 329 | ) |
||
| 330 | 1 | if scaling_factor > 0.25 or scaling_factor < 0: |
|
| 331 | 1 | raise ValueError( |
|
| 332 | 'Unsupported scaling_factor assignment; ' |
||
| 333 | + 'scaling_factor must be between 0 and 0.25.' |
||
| 334 | ) |
||
| 335 | |||
| 336 | 1 | if src == tar: |
|
| 337 | 1 | return 1.0 |
|
| 338 | |||
| 339 | 1 | src = QGrams(src.strip(), qval).ordered_list |
|
| 340 | 1 | tar = QGrams(tar.strip(), qval).ordered_list |
|
| 341 | |||
| 342 | 1 | lens = len(src) |
|
| 343 | 1 | lent = len(tar) |
|
| 344 | |||
| 345 | # If either string is blank - return - added in Version 2 |
||
| 346 | 1 | if lens == 0 or lent == 0: |
|
| 347 | 1 | return 0.0 |
|
| 348 | |||
| 349 | 1 | if lens > lent: |
|
| 350 | 1 | search_range = lens |
|
| 351 | 1 | minv = lent |
|
| 352 | else: |
||
| 353 | 1 | search_range = lent |
|
| 354 | 1 | minv = lens |
|
| 355 | |||
| 356 | # Zero out the flags |
||
| 357 | 1 | src_flag = [0] * search_range |
|
| 358 | 1 | tar_flag = [0] * search_range |
|
| 359 | 1 | search_range = max(0, search_range // 2 - 1) |
|
| 360 | |||
| 361 | # Looking only within the search range, count and flag the matched pairs. |
||
| 362 | 1 | num_com = 0 |
|
| 363 | 1 | yl1 = lent - 1 |
|
| 364 | 1 | for i in range(lens): |
|
| 365 | 1 | low_lim = (i - search_range) if (i >= search_range) else 0 |
|
| 366 | 1 | hi_lim = (i + search_range) if ((i + search_range) <= yl1) else yl1 |
|
| 367 | 1 | for j in range(low_lim, hi_lim + 1): |
|
| 368 | 1 | if (tar_flag[j] == 0) and (tar[j] == src[i]): |
|
| 369 | 1 | tar_flag[j] = 1 |
|
| 370 | 1 | src_flag[i] = 1 |
|
| 371 | 1 | num_com += 1 |
|
| 372 | 1 | break |
|
| 373 | |||
| 374 | # If no characters in common - return |
||
| 375 | 1 | if num_com == 0: |
|
| 376 | 1 | return 0.0 |
|
| 377 | |||
| 378 | # Count the number of transpositions |
||
| 379 | 1 | k = n_trans = 0 |
|
| 380 | 1 | for i in range(lens): |
|
| 381 | 1 | if src_flag[i] != 0: |
|
| 382 | 1 | j = 0 |
|
| 383 | 1 | for j in range(k, lent): # pragma: no branch |
|
| 384 | 1 | if tar_flag[j] != 0: |
|
| 385 | 1 | k = j + 1 |
|
| 386 | 1 | break |
|
| 387 | 1 | if src[i] != tar[j]: |
|
| 388 | 1 | n_trans += 1 |
|
| 389 | 1 | n_trans //= 2 |
|
| 390 | |||
| 391 | # Main weight computation for Jaro distance |
||
| 392 | 1 | weight = num_com / lens + num_com / lent + (num_com - n_trans) / num_com |
|
| 393 | 1 | weight /= 3.0 |
|
| 394 | |||
| 395 | # Continue to boost the weight if the strings are similar |
||
| 396 | # This is the Winkler portion of Jaro-Winkler distance |
||
| 397 | 1 | if mode == 'winkler' and weight > boost_threshold: |
|
| 398 | |||
| 399 | # Adjust for having up to the first 4 characters in common |
||
| 400 | 1 | j = 4 if (minv >= 4) else minv |
|
| 401 | 1 | i = 0 |
|
| 402 | 1 | while (i < j) and (src[i] == tar[i]): |
|
| 403 | 1 | i += 1 |
|
| 404 | 1 | weight += i * scaling_factor * (1.0 - weight) |
|
| 405 | |||
| 406 | # Optionally adjust for long strings. |
||
| 407 | |||
| 408 | # After agreeing beginning chars, at least two more must agree and |
||
| 409 | # the agreeing characters must be > .5 of remaining characters. |
||
| 410 | 1 | if ( |
|
| 411 | long_strings |
||
| 412 | and (minv > 4) |
||
| 413 | and (num_com > i + 1) |
||
| 414 | and (2 * num_com >= minv + i) |
||
| 415 | ): |
||
| 416 | 1 | weight += (1.0 - weight) * ( |
|
| 417 | (num_com - i - 1) / (lens + lent - i * 2 + 2) |
||
| 418 | ) |
||
| 419 | |||
| 420 | 1 | return weight |
|
| 421 | |||
| 490 |