Conditions | 79 |
Total Lines | 275 |
Code Lines | 192 |
Lines | 33 |
Ratio | 12 % |
Tests | 132 |
CRAP Score | 79 |
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._synoname.Synoname.dist_abs() 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 -*- |
||
342 | 1 | def dist_abs( |
|
343 | self, |
||
344 | src, |
||
345 | tar, |
||
346 | word_approx_min=0.3, |
||
347 | char_approx_min=0.73, |
||
348 | tests=2 ** 12 - 1, |
||
349 | ret_name=False, |
||
350 | ): |
||
351 | """Return the Synoname similarity type of two words. |
||
352 | |||
353 | Args: |
||
354 | src (str): Source string for comparison |
||
355 | tar (str): Target string for comparison |
||
356 | word_approx_min (float): the minimum word approximation value to |
||
357 | signal a 'word_approx' match |
||
358 | char_approx_min (float): the minimum character approximation value |
||
359 | to signal a 'char_approx' match |
||
360 | tests (int or Iterable): either an integer indicating tests to |
||
361 | perform or a list of test names to perform (defaults to |
||
362 | performing all tests) |
||
363 | ret_name (bool): If True, returns the match name rather than its |
||
364 | integer equivalent |
||
365 | |||
366 | Returns: |
||
367 | int (or str if ret_name is True): Synoname value |
||
368 | |||
369 | Examples: |
||
370 | >>> synoname(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', '')) |
||
371 | 2 |
||
372 | >>> synoname(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', ''), |
||
373 | ... ret_name=True) |
||
374 | 'omission' |
||
375 | >>> synoname(('Dore', 'Gustave', ''), |
||
376 | ... ('Dore', 'Paul Gustave Louis Christophe', ''), |
||
377 | ... ret_name=True) |
||
378 | 'inclusion' |
||
379 | >>> synoname(('Pereira', 'I. R.', ''), ('Pereira', 'I. Smith', ''), |
||
380 | ... ret_name=True) |
||
381 | 'word_approx' |
||
382 | |||
383 | """ |
||
384 | 1 | if isinstance(tests, Iterable): |
|
385 | 1 | new_tests = 0 |
|
386 | 1 | for term in tests: |
|
387 | 1 | if term in self.test_dict: |
|
388 | 1 | new_tests += self.test_dict[term] |
|
389 | 1 | tests = new_tests |
|
390 | |||
391 | 1 | if isinstance(src, tuple): |
|
392 | 1 | src_ln, src_fn, src_qual = src |
|
393 | 1 | elif '#' in src: |
|
394 | 1 | src_ln, src_fn, src_qual = src.split('#')[-3:] |
|
395 | else: |
||
396 | 1 | src_ln, src_fn, src_qual = src, '', '' |
|
397 | |||
398 | 1 | if isinstance(tar, tuple): |
|
399 | 1 | tar_ln, tar_fn, tar_qual = tar |
|
400 | 1 | elif '#' in tar: |
|
401 | 1 | tar_ln, tar_fn, tar_qual = tar.split('#')[-3:] |
|
402 | else: |
||
403 | 1 | tar_ln, tar_fn, tar_qual = tar, '', '' |
|
404 | |||
405 | 1 | def _split_special(spec): |
|
406 | 1 | spec_list = [] |
|
407 | 1 | while spec: |
|
408 | 1 | spec_list.append((int(spec[:3]), spec[3:4])) |
|
409 | 1 | spec = spec[4:] |
|
410 | 1 | return spec_list |
|
411 | |||
412 | 1 | def _fmt_retval(val): |
|
413 | 1 | if ret_name: |
|
414 | 1 | return self.match_name[val] |
|
415 | 1 | return val |
|
416 | |||
417 | # 1. Preprocessing |
||
418 | |||
419 | # Lowercasing |
||
420 | 1 | src_fn = src_fn.strip().lower() |
|
421 | 1 | src_ln = src_ln.strip().lower() |
|
422 | 1 | src_qual = src_qual.strip().lower() |
|
423 | |||
424 | 1 | tar_fn = tar_fn.strip().lower() |
|
425 | 1 | tar_ln = tar_ln.strip().lower() |
|
426 | 1 | tar_qual = tar_qual.strip().lower() |
|
427 | |||
428 | # Create toolcodes |
||
429 | 1 | src_ln, src_fn, src_tc = self.stc.fingerprint(src_ln, src_fn, src_qual) |
|
430 | 1 | tar_ln, tar_fn, tar_tc = self.stc.fingerprint(tar_ln, tar_fn, tar_qual) |
|
431 | |||
432 | 1 | src_generation = int(src_tc[2]) |
|
433 | 1 | src_romancode = int(src_tc[3:6]) |
|
434 | 1 | src_len_fn = int(src_tc[6:8]) |
|
435 | 1 | src_tc = src_tc.split('$') |
|
436 | 1 | src_specials = _split_special(src_tc[1]) |
|
437 | |||
438 | 1 | tar_generation = int(tar_tc[2]) |
|
439 | 1 | tar_romancode = int(tar_tc[3:6]) |
|
440 | 1 | tar_len_fn = int(tar_tc[6:8]) |
|
441 | 1 | tar_tc = tar_tc.split('$') |
|
442 | 1 | tar_specials = _split_special(tar_tc[1]) |
|
443 | |||
444 | 1 | gen_conflict = (src_generation != tar_generation) and bool( |
|
445 | src_generation or tar_generation |
||
446 | ) |
||
447 | 1 | roman_conflict = (src_romancode != tar_romancode) and bool( |
|
448 | src_romancode or tar_romancode |
||
449 | ) |
||
450 | |||
451 | 1 | ln_equal = src_ln == tar_ln |
|
452 | 1 | fn_equal = src_fn == tar_fn |
|
453 | |||
454 | # approx_c |
||
455 | 1 | def _approx_c(): |
|
456 | 1 | if gen_conflict or roman_conflict: |
|
457 | 1 | return False, 0 |
|
458 | |||
459 | 1 | full_src = ' '.join((src_ln, src_fn)) |
|
460 | 1 | if full_src.startswith('master '): |
|
461 | 1 | full_src = full_src[len('master ') :] |
|
462 | 1 | for intro in [ |
|
463 | 'of the ', |
||
464 | 'of ', |
||
465 | 'known as the ', |
||
466 | 'with the ', |
||
467 | 'with ', |
||
468 | ]: |
||
469 | 1 | if full_src.startswith(intro): |
|
470 | 1 | full_src = full_src[len(intro) :] |
|
471 | |||
472 | 1 | full_tar = ' '.join((tar_ln, tar_fn)) |
|
473 | 1 | if full_tar.startswith('master '): |
|
474 | 1 | full_tar = full_tar[len('master ') :] |
|
475 | 1 | for intro in [ |
|
476 | 'of the ', |
||
477 | 'of ', |
||
478 | 'known as the ', |
||
479 | 'with the ', |
||
480 | 'with ', |
||
481 | ]: |
||
482 | 1 | if full_tar.startswith(intro): |
|
483 | 1 | full_tar = full_tar[len(intro) :] |
|
484 | |||
485 | 1 | loc_ratio = sim_ratcliff_obershelp(full_src, full_tar) |
|
486 | 1 | return loc_ratio >= char_approx_min, loc_ratio |
|
487 | |||
488 | 1 | approx_c_result, ca_ratio = _approx_c() |
|
489 | |||
490 | 1 | if tests & self.test_dict['exact'] and fn_equal and ln_equal: |
|
491 | 1 | return _fmt_retval(self.match_type_dict['exact']) |
|
492 | 1 | if tests & self.test_dict['omission']: |
|
493 | 1 | View Code Duplication | if ( |
494 | fn_equal |
||
495 | and levenshtein(src_ln, tar_ln, cost=(1, 1, 99, 99)) == 1 |
||
496 | ): |
||
497 | 1 | if not roman_conflict: |
|
498 | 1 | return _fmt_retval(self.match_type_dict['omission']) |
|
499 | 1 | elif ( |
|
500 | ln_equal |
||
501 | and levenshtein(src_fn, tar_fn, cost=(1, 1, 99, 99)) == 1 |
||
502 | ): |
||
503 | 1 | return _fmt_retval(self.match_type_dict['omission']) |
|
504 | 1 | View Code Duplication | if tests & self.test_dict['substitution']: |
505 | 1 | if ( |
|
506 | fn_equal |
||
507 | and levenshtein(src_ln, tar_ln, cost=(99, 99, 1, 99)) == 1 |
||
508 | ): |
||
509 | 1 | return _fmt_retval(self.match_type_dict['substitution']) |
|
510 | 1 | elif ( |
|
511 | ln_equal |
||
512 | and levenshtein(src_fn, tar_fn, cost=(99, 99, 1, 99)) == 1 |
||
513 | ): |
||
514 | 1 | return _fmt_retval(self.match_type_dict['substitution']) |
|
515 | 1 | View Code Duplication | if tests & self.test_dict['transposition']: |
516 | 1 | if fn_equal and ( |
|
517 | levenshtein(src_ln, tar_ln, mode='osa', cost=(99, 99, 99, 1)) |
||
518 | == 1 |
||
519 | ): |
||
520 | 1 | return _fmt_retval(self.match_type_dict['transposition']) |
|
521 | 1 | elif ln_equal and ( |
|
522 | levenshtein(src_fn, tar_fn, mode='osa', cost=(99, 99, 99, 1)) |
||
523 | == 1 |
||
524 | ): |
||
525 | 1 | return _fmt_retval(self.match_type_dict['transposition']) |
|
526 | 1 | if tests & self.test_dict['punctuation']: |
|
527 | 1 | np_src_fn = self._synoname_strip_punct(src_fn) |
|
528 | 1 | np_tar_fn = self._synoname_strip_punct(tar_fn) |
|
529 | 1 | np_src_ln = self._synoname_strip_punct(src_ln) |
|
530 | 1 | np_tar_ln = self._synoname_strip_punct(tar_ln) |
|
531 | |||
532 | 1 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
|
533 | 1 | return _fmt_retval(self.match_type_dict['punctuation']) |
|
534 | |||
535 | 1 | np_src_fn = self._synoname_strip_punct(src_fn.replace('-', ' ')) |
|
536 | 1 | np_tar_fn = self._synoname_strip_punct(tar_fn.replace('-', ' ')) |
|
537 | 1 | np_src_ln = self._synoname_strip_punct(src_ln.replace('-', ' ')) |
|
538 | 1 | np_tar_ln = self._synoname_strip_punct(tar_ln.replace('-', ' ')) |
|
539 | |||
540 | 1 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
|
541 | 1 | return _fmt_retval(self.match_type_dict['punctuation']) |
|
542 | |||
543 | 1 | if tests & self.test_dict['initials'] and ln_equal: |
|
544 | 1 | if src_fn and tar_fn: |
|
545 | 1 | src_initials = self._synoname_strip_punct(src_fn).split() |
|
546 | 1 | tar_initials = self._synoname_strip_punct(tar_fn).split() |
|
547 | 1 | initials = bool( |
|
548 | (len(src_initials) == len(''.join(src_initials))) |
||
549 | or (len(tar_initials) == len(''.join(tar_initials))) |
||
550 | ) |
||
551 | 1 | if initials: |
|
552 | 1 | src_initials = ''.join(_[0] for _ in src_initials) |
|
553 | 1 | tar_initials = ''.join(_[0] for _ in tar_initials) |
|
554 | 1 | if src_initials == tar_initials: |
|
555 | 1 | return _fmt_retval(self.match_type_dict['initials']) |
|
556 | 1 | initial_diff = abs(len(src_initials) - len(tar_initials)) |
|
557 | 1 | if initial_diff and ( |
|
558 | ( |
||
559 | initial_diff |
||
560 | == levenshtein( |
||
561 | src_initials, |
||
562 | tar_initials, |
||
563 | cost=(1, 99, 99, 99), |
||
564 | ) |
||
565 | ) |
||
566 | or ( |
||
567 | initial_diff |
||
568 | == levenshtein( |
||
569 | tar_initials, |
||
570 | src_initials, |
||
571 | cost=(1, 99, 99, 99), |
||
572 | ) |
||
573 | ) |
||
574 | ): |
||
575 | 1 | return _fmt_retval(self.match_type_dict['initials']) |
|
576 | 1 | if tests & self.test_dict['extension']: |
|
577 | 1 | if src_ln[1] == tar_ln[1] and ( |
|
578 | src_ln.startswith(tar_ln) or tar_ln.startswith(src_ln) |
||
579 | ): |
||
580 | 1 | if ( |
|
581 | (not src_len_fn and not tar_len_fn) |
||
582 | or (tar_fn and src_fn.startswith(tar_fn)) |
||
583 | or (src_fn and tar_fn.startswith(src_fn)) |
||
584 | ) and not roman_conflict: |
||
585 | 1 | return _fmt_retval(self.match_type_dict['extension']) |
|
586 | 1 | if tests & self.test_dict['inclusion'] and ln_equal: |
|
587 | 1 | if (src_fn and src_fn in tar_fn) or (tar_fn and tar_fn in src_ln): |
|
588 | 1 | return _fmt_retval(self.match_type_dict['inclusion']) |
|
589 | 1 | if tests & self.test_dict['no_first'] and ln_equal: |
|
590 | 1 | if src_fn == '' or tar_fn == '': |
|
591 | 1 | return _fmt_retval(self.match_type_dict['no_first']) |
|
592 | 1 | if tests & self.test_dict['word_approx']: |
|
593 | 1 | ratio = self._synoname_word_approximation( |
|
594 | src_ln, |
||
595 | tar_ln, |
||
596 | src_fn, |
||
597 | tar_fn, |
||
598 | { |
||
599 | 'gen_conflict': gen_conflict, |
||
600 | 'roman_conflict': roman_conflict, |
||
601 | 'src_specials': src_specials, |
||
602 | 'tar_specials': tar_specials, |
||
603 | }, |
||
604 | ) |
||
605 | 1 | if ratio == 1 and tests & self.test_dict['confusions']: |
|
606 | 1 | if ( |
|
607 | ' '.join((src_fn, src_ln)).strip() |
||
608 | == ' '.join((tar_fn, tar_ln)).strip() |
||
609 | ): |
||
610 | 1 | return _fmt_retval(self.match_type_dict['confusions']) |
|
611 | 1 | if ratio >= word_approx_min: |
|
612 | 1 | return _fmt_retval(self.match_type_dict['word_approx']) |
|
613 | 1 | if tests & self.test_dict['char_approx']: |
|
614 | 1 | if ca_ratio >= char_approx_min: |
|
615 | 1 | return _fmt_retval(self.match_type_dict['char_approx']) |
|
616 | 1 | return _fmt_retval(self.match_type_dict['no_match']) |
|
617 | |||
701 |