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