Conditions | 79 |
Total Lines | 287 |
Code Lines | 194 |
Lines | 34 |
Ratio | 11.85 % |
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 -*- |
||
425 | 1 | def dist_abs( |
|
426 | self, |
||
427 | src, |
||
428 | tar, |
||
429 | word_approx_min=0.3, |
||
430 | char_approx_min=0.73, |
||
431 | tests=2 ** 12 - 1, |
||
432 | ret_name=False, |
||
433 | ): |
||
434 | """Return the Synoname similarity type of two words. |
||
435 | |||
436 | Parameters |
||
437 | ---------- |
||
438 | src : str |
||
439 | Source string for comparison |
||
440 | tar : str |
||
441 | Target string for comparison |
||
442 | word_approx_min : float |
||
443 | The minimum word approximation value to signal a 'word_approx' |
||
444 | match |
||
445 | char_approx_min : float |
||
446 | The minimum character approximation value to signal a 'char_approx' |
||
447 | match |
||
448 | tests : int or Iterable |
||
449 | Either an integer indicating tests to perform or a list of test |
||
450 | names to perform (defaults to performing all tests) |
||
451 | ret_name : bool |
||
452 | If True, returns the match name rather than its integer equivalent |
||
453 | |||
454 | Returns |
||
455 | ------- |
||
456 | int (or str if ret_name is True) |
||
457 | Synoname value |
||
458 | |||
459 | Examples |
||
460 | -------- |
||
461 | >>> cmp = Synoname() |
||
462 | >>> cmp.dist_abs(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', '')) |
||
463 | 2 |
||
464 | >>> cmp.dist_abs(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', ''), |
||
465 | ... ret_name=True) |
||
466 | 'omission' |
||
467 | >>> cmp.dist_abs(('Dore', 'Gustave', ''), |
||
468 | ... ('Dore', 'Paul Gustave Louis Christophe', ''), ret_name=True) |
||
469 | 'inclusion' |
||
470 | >>> cmp.dist_abs(('Pereira', 'I. R.', ''), ('Pereira', 'I. Smith', ''), |
||
471 | ... ret_name=True) |
||
472 | 'word_approx' |
||
473 | |||
474 | """ |
||
475 | 1 | if isinstance(tests, Iterable): |
|
476 | 1 | new_tests = 0 |
|
477 | 1 | for term in tests: |
|
478 | 1 | if term in self._test_dict: |
|
479 | 1 | new_tests += self._test_dict[term] |
|
480 | 1 | tests = new_tests |
|
481 | |||
482 | 1 | if isinstance(src, tuple): |
|
483 | 1 | src_ln, src_fn, src_qual = src |
|
484 | 1 | elif '#' in src: |
|
485 | 1 | src_ln, src_fn, src_qual = src.split('#')[-3:] |
|
486 | else: |
||
487 | 1 | src_ln, src_fn, src_qual = src, '', '' |
|
488 | |||
489 | 1 | if isinstance(tar, tuple): |
|
490 | 1 | tar_ln, tar_fn, tar_qual = tar |
|
491 | 1 | elif '#' in tar: |
|
492 | 1 | tar_ln, tar_fn, tar_qual = tar.split('#')[-3:] |
|
493 | else: |
||
494 | 1 | tar_ln, tar_fn, tar_qual = tar, '', '' |
|
495 | |||
496 | 1 | def _split_special(spec): |
|
497 | 1 | spec_list = [] |
|
498 | 1 | while spec: |
|
499 | 1 | spec_list.append((int(spec[:3]), spec[3:4])) |
|
500 | 1 | spec = spec[4:] |
|
501 | 1 | return spec_list |
|
502 | |||
503 | 1 | def _fmt_retval(val): |
|
504 | 1 | if ret_name: |
|
505 | 1 | return self._match_name[val] |
|
506 | 1 | return val |
|
507 | |||
508 | # 1. Preprocessing |
||
509 | |||
510 | # Lowercasing |
||
511 | 1 | src_fn = src_fn.strip().lower() |
|
512 | 1 | src_ln = src_ln.strip().lower() |
|
513 | 1 | src_qual = src_qual.strip().lower() |
|
514 | |||
515 | 1 | tar_fn = tar_fn.strip().lower() |
|
516 | 1 | tar_ln = tar_ln.strip().lower() |
|
517 | 1 | tar_qual = tar_qual.strip().lower() |
|
518 | |||
519 | # Create toolcodes |
||
520 | 1 | src_ln, src_fn, src_tc = self._stc.fingerprint( |
|
521 | src_ln, src_fn, src_qual |
||
522 | ) |
||
523 | 1 | tar_ln, tar_fn, tar_tc = self._stc.fingerprint( |
|
524 | tar_ln, tar_fn, tar_qual |
||
525 | ) |
||
526 | |||
527 | 1 | src_generation = int(src_tc[2]) |
|
528 | 1 | src_romancode = int(src_tc[3:6]) |
|
529 | 1 | src_len_fn = int(src_tc[6:8]) |
|
530 | 1 | src_tc = src_tc.split('$') |
|
531 | 1 | src_specials = _split_special(src_tc[1]) |
|
532 | |||
533 | 1 | tar_generation = int(tar_tc[2]) |
|
534 | 1 | tar_romancode = int(tar_tc[3:6]) |
|
535 | 1 | tar_len_fn = int(tar_tc[6:8]) |
|
536 | 1 | tar_tc = tar_tc.split('$') |
|
537 | 1 | tar_specials = _split_special(tar_tc[1]) |
|
538 | |||
539 | 1 | gen_conflict = (src_generation != tar_generation) and bool( |
|
540 | src_generation or tar_generation |
||
541 | ) |
||
542 | 1 | roman_conflict = (src_romancode != tar_romancode) and bool( |
|
543 | src_romancode or tar_romancode |
||
544 | ) |
||
545 | |||
546 | 1 | ln_equal = src_ln == tar_ln |
|
547 | 1 | fn_equal = src_fn == tar_fn |
|
548 | |||
549 | # approx_c |
||
550 | 1 | def _approx_c(): |
|
551 | 1 | if gen_conflict or roman_conflict: |
|
552 | 1 | return False, 0 |
|
553 | |||
554 | 1 | full_src = ' '.join((src_ln, src_fn)) |
|
555 | 1 | if full_src.startswith('master '): |
|
556 | 1 | full_src = full_src[len('master ') :] |
|
557 | 1 | for intro in [ |
|
558 | 'of the ', |
||
559 | 'of ', |
||
560 | 'known as the ', |
||
561 | 'with the ', |
||
562 | 'with ', |
||
563 | ]: |
||
564 | 1 | if full_src.startswith(intro): |
|
565 | 1 | full_src = full_src[len(intro) :] |
|
566 | |||
567 | 1 | full_tar = ' '.join((tar_ln, tar_fn)) |
|
568 | 1 | if full_tar.startswith('master '): |
|
569 | 1 | full_tar = full_tar[len('master ') :] |
|
570 | 1 | for intro in [ |
|
571 | 'of the ', |
||
572 | 'of ', |
||
573 | 'known as the ', |
||
574 | 'with the ', |
||
575 | 'with ', |
||
576 | ]: |
||
577 | 1 | if full_tar.startswith(intro): |
|
578 | 1 | full_tar = full_tar[len(intro) :] |
|
579 | |||
580 | 1 | loc_ratio = sim_ratcliff_obershelp(full_src, full_tar) |
|
581 | 1 | return loc_ratio >= char_approx_min, loc_ratio |
|
582 | |||
583 | 1 | approx_c_result, ca_ratio = _approx_c() |
|
584 | |||
585 | 1 | if tests & self._test_dict['exact'] and fn_equal and ln_equal: |
|
586 | 1 | return _fmt_retval(self._match_type_dict['exact']) |
|
587 | 1 | View Code Duplication | if tests & self._test_dict['omission']: |
588 | 1 | if ( |
|
589 | fn_equal |
||
590 | and levenshtein(src_ln, tar_ln, cost=(1, 1, 99, 99)) == 1 |
||
591 | ): |
||
592 | 1 | if not roman_conflict: |
|
593 | 1 | return _fmt_retval(self._match_type_dict['omission']) |
|
594 | 1 | elif ( |
|
595 | ln_equal |
||
596 | and levenshtein(src_fn, tar_fn, cost=(1, 1, 99, 99)) == 1 |
||
597 | ): |
||
598 | 1 | return _fmt_retval(self._match_type_dict['omission']) |
|
599 | 1 | View Code Duplication | if tests & self._test_dict['substitution']: |
600 | 1 | if ( |
|
601 | fn_equal |
||
602 | and levenshtein(src_ln, tar_ln, cost=(99, 99, 1, 99)) == 1 |
||
603 | ): |
||
604 | 1 | return _fmt_retval(self._match_type_dict['substitution']) |
|
605 | 1 | elif ( |
|
606 | ln_equal |
||
607 | and levenshtein(src_fn, tar_fn, cost=(99, 99, 1, 99)) == 1 |
||
608 | ): |
||
609 | 1 | return _fmt_retval(self._match_type_dict['substitution']) |
|
610 | 1 | View Code Duplication | if tests & self._test_dict['transposition']: |
611 | 1 | if fn_equal and ( |
|
612 | levenshtein(src_ln, tar_ln, mode='osa', cost=(99, 99, 99, 1)) |
||
613 | == 1 |
||
614 | ): |
||
615 | 1 | return _fmt_retval(self._match_type_dict['transposition']) |
|
616 | 1 | elif ln_equal and ( |
|
617 | levenshtein(src_fn, tar_fn, mode='osa', cost=(99, 99, 99, 1)) |
||
618 | == 1 |
||
619 | ): |
||
620 | 1 | return _fmt_retval(self._match_type_dict['transposition']) |
|
621 | 1 | if tests & self._test_dict['punctuation']: |
|
622 | 1 | np_src_fn = self._synoname_strip_punct(src_fn) |
|
623 | 1 | np_tar_fn = self._synoname_strip_punct(tar_fn) |
|
624 | 1 | np_src_ln = self._synoname_strip_punct(src_ln) |
|
625 | 1 | np_tar_ln = self._synoname_strip_punct(tar_ln) |
|
626 | |||
627 | 1 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
|
628 | 1 | return _fmt_retval(self._match_type_dict['punctuation']) |
|
629 | |||
630 | 1 | np_src_fn = self._synoname_strip_punct(src_fn.replace('-', ' ')) |
|
631 | 1 | np_tar_fn = self._synoname_strip_punct(tar_fn.replace('-', ' ')) |
|
632 | 1 | np_src_ln = self._synoname_strip_punct(src_ln.replace('-', ' ')) |
|
633 | 1 | np_tar_ln = self._synoname_strip_punct(tar_ln.replace('-', ' ')) |
|
634 | |||
635 | 1 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
|
636 | 1 | return _fmt_retval(self._match_type_dict['punctuation']) |
|
637 | |||
638 | 1 | if tests & self._test_dict['initials'] and ln_equal: |
|
639 | 1 | if src_fn and tar_fn: |
|
640 | 1 | src_initials = self._synoname_strip_punct(src_fn).split() |
|
641 | 1 | tar_initials = self._synoname_strip_punct(tar_fn).split() |
|
642 | 1 | initials = bool( |
|
643 | (len(src_initials) == len(''.join(src_initials))) |
||
644 | or (len(tar_initials) == len(''.join(tar_initials))) |
||
645 | ) |
||
646 | 1 | if initials: |
|
647 | 1 | src_initials = ''.join(_[0] for _ in src_initials) |
|
648 | 1 | tar_initials = ''.join(_[0] for _ in tar_initials) |
|
649 | 1 | if src_initials == tar_initials: |
|
650 | 1 | return _fmt_retval(self._match_type_dict['initials']) |
|
651 | 1 | initial_diff = abs(len(src_initials) - len(tar_initials)) |
|
652 | 1 | if initial_diff and ( |
|
653 | ( |
||
654 | initial_diff |
||
655 | == levenshtein( |
||
656 | src_initials, |
||
657 | tar_initials, |
||
658 | cost=(1, 99, 99, 99), |
||
659 | ) |
||
660 | ) |
||
661 | or ( |
||
662 | initial_diff |
||
663 | == levenshtein( |
||
664 | tar_initials, |
||
665 | src_initials, |
||
666 | cost=(1, 99, 99, 99), |
||
667 | ) |
||
668 | ) |
||
669 | ): |
||
670 | 1 | return _fmt_retval(self._match_type_dict['initials']) |
|
671 | 1 | if tests & self._test_dict['extension']: |
|
672 | 1 | if src_ln[1] == tar_ln[1] and ( |
|
673 | src_ln.startswith(tar_ln) or tar_ln.startswith(src_ln) |
||
674 | ): |
||
675 | 1 | if ( |
|
676 | (not src_len_fn and not tar_len_fn) |
||
677 | or (tar_fn and src_fn.startswith(tar_fn)) |
||
678 | or (src_fn and tar_fn.startswith(src_fn)) |
||
679 | ) and not roman_conflict: |
||
680 | 1 | return _fmt_retval(self._match_type_dict['extension']) |
|
681 | 1 | if tests & self._test_dict['inclusion'] and ln_equal: |
|
682 | 1 | if (src_fn and src_fn in tar_fn) or (tar_fn and tar_fn in src_ln): |
|
683 | 1 | return _fmt_retval(self._match_type_dict['inclusion']) |
|
684 | 1 | if tests & self._test_dict['no_first'] and ln_equal: |
|
685 | 1 | if src_fn == '' or tar_fn == '': |
|
686 | 1 | return _fmt_retval(self._match_type_dict['no_first']) |
|
687 | 1 | if tests & self._test_dict['word_approx']: |
|
688 | 1 | ratio = self._synoname_word_approximation( |
|
689 | src_ln, |
||
690 | tar_ln, |
||
691 | src_fn, |
||
692 | tar_fn, |
||
693 | { |
||
694 | 'gen_conflict': gen_conflict, |
||
695 | 'roman_conflict': roman_conflict, |
||
696 | 'src_specials': src_specials, |
||
697 | 'tar_specials': tar_specials, |
||
698 | }, |
||
699 | ) |
||
700 | 1 | if ratio == 1 and tests & self._test_dict['confusions']: |
|
701 | 1 | if ( |
|
702 | ' '.join((src_fn, src_ln)).strip() |
||
703 | == ' '.join((tar_fn, tar_ln)).strip() |
||
704 | ): |
||
705 | 1 | return _fmt_retval(self._match_type_dict['confusions']) |
|
706 | 1 | if ratio >= word_approx_min: |
|
707 | 1 | return _fmt_retval(self._match_type_dict['word_approx']) |
|
708 | 1 | if tests & self._test_dict['char_approx']: |
|
709 | 1 | if ca_ratio >= char_approx_min: |
|
710 | 1 | return _fmt_retval(self._match_type_dict['char_approx']) |
|
711 | 1 | return _fmt_retval(self._match_type_dict['no_match']) |
|
712 | |||
809 |