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