Conditions | 79 |
Total Lines | 297 |
Code Lines | 211 |
Lines | 0 |
Ratio | 0 % |
Tests | 135 |
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() 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 -*- |
||
257 | 1 | def synoname( |
|
258 | src, |
||
259 | tar, |
||
260 | word_approx_min=0.3, |
||
261 | char_approx_min=0.73, |
||
262 | tests=2 ** 12 - 1, |
||
263 | ret_name=False, |
||
264 | ): |
||
265 | """Return the Synoname similarity type of two words. |
||
266 | |||
267 | Cf. :cite:`Getty:1991,Gross:1991` |
||
268 | |||
269 | :param str src: source string for comparison |
||
270 | :param str tar: target string for comparison |
||
271 | :param bool ret_name: return the name of the match type rather than the |
||
272 | int value |
||
273 | :param float word_approx_min: the minimum word approximation value to |
||
274 | signal a 'word_approx' match |
||
275 | :param float char_approx_min: the minimum character approximation value to |
||
276 | signal a 'char_approx' match |
||
277 | :param int or Iterable tests: either an integer indicating tests to |
||
278 | perform or a list of test names to perform (defaults to performing all |
||
279 | tests) |
||
280 | :param bool ret_name: if True, returns the match name rather than its |
||
281 | integer equivalent |
||
282 | :returns: Synoname value |
||
283 | :rtype: int (or str if ret_name is True) |
||
284 | |||
285 | >>> synoname(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', '')) |
||
286 | 2 |
||
287 | >>> synoname(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', ''), |
||
288 | ... ret_name=True) |
||
289 | 'omission' |
||
290 | >>> synoname(('Dore', 'Gustave', ''), |
||
291 | ... ('Dore', 'Paul Gustave Louis Christophe', ''), |
||
292 | ... ret_name=True) |
||
293 | 'inclusion' |
||
294 | >>> synoname(('Pereira', 'I. R.', ''), ('Pereira', 'I. Smith', ''), |
||
295 | ... ret_name=True) |
||
296 | 'word_approx' |
||
297 | """ |
||
298 | 1 | test_dict = { |
|
299 | val: 2 ** n |
||
300 | for n, val in enumerate( |
||
301 | [ |
||
302 | 'exact', |
||
303 | 'omission', |
||
304 | 'substitution', |
||
305 | 'transposition', |
||
306 | 'punctuation', |
||
307 | 'initials', |
||
308 | 'extension', |
||
309 | 'inclusion', |
||
310 | 'no_first', |
||
311 | 'word_approx', |
||
312 | 'confusions', |
||
313 | 'char_approx', |
||
314 | ] |
||
315 | ) |
||
316 | } |
||
317 | 1 | match_name = [ |
|
318 | '', |
||
319 | 'exact', |
||
320 | 'omission', |
||
321 | 'substitution', |
||
322 | 'transposition', |
||
323 | 'punctuation', |
||
324 | 'initials', |
||
325 | 'extension', |
||
326 | 'inclusion', |
||
327 | 'no_first', |
||
328 | 'word_approx', |
||
329 | 'confusions', |
||
330 | 'char_approx', |
||
331 | 'no_match', |
||
332 | ] |
||
333 | 1 | match_type_dict = {val: n for n, val in enumerate(match_name)} |
|
334 | |||
335 | 1 | if isinstance(tests, Iterable): |
|
336 | 1 | new_tests = 0 |
|
337 | 1 | for term in tests: |
|
338 | 1 | if term in test_dict: |
|
339 | 1 | new_tests += test_dict[term] |
|
340 | 1 | tests = new_tests |
|
341 | |||
342 | 1 | if isinstance(src, tuple): |
|
343 | 1 | src_ln, src_fn, src_qual = src |
|
344 | 1 | elif '#' in src: |
|
345 | 1 | src_ln, src_fn, src_qual = src.split('#')[-3:] |
|
346 | else: |
||
347 | 1 | src_ln, src_fn, src_qual = src, '', '' |
|
348 | |||
349 | 1 | if isinstance(tar, tuple): |
|
350 | 1 | tar_ln, tar_fn, tar_qual = tar |
|
351 | 1 | elif '#' in tar: |
|
352 | 1 | tar_ln, tar_fn, tar_qual = tar.split('#')[-3:] |
|
353 | else: |
||
354 | 1 | tar_ln, tar_fn, tar_qual = tar, '', '' |
|
355 | |||
356 | 1 | def _split_special(spec): |
|
357 | 1 | spec_list = [] |
|
358 | 1 | while spec: |
|
359 | 1 | spec_list.append((int(spec[:3]), spec[3:4])) |
|
360 | 1 | spec = spec[4:] |
|
361 | 1 | return spec_list |
|
362 | |||
363 | 1 | def _fmt_retval(val): |
|
364 | 1 | if ret_name: |
|
365 | 1 | return match_name[val] |
|
366 | 1 | return val |
|
367 | |||
368 | # 1. Preprocessing |
||
369 | |||
370 | # Lowercasing |
||
371 | 1 | src_fn = src_fn.strip().lower() |
|
372 | 1 | src_ln = src_ln.strip().lower() |
|
373 | 1 | src_qual = src_qual.strip().lower() |
|
374 | |||
375 | 1 | tar_fn = tar_fn.strip().lower() |
|
376 | 1 | tar_ln = tar_ln.strip().lower() |
|
377 | 1 | tar_qual = tar_qual.strip().lower() |
|
378 | |||
379 | # Create toolcodes |
||
380 | 1 | src_ln, src_fn, src_tc = synoname_toolcode(src_ln, src_fn, src_qual) |
|
381 | 1 | tar_ln, tar_fn, tar_tc = synoname_toolcode(tar_ln, tar_fn, tar_qual) |
|
382 | |||
383 | 1 | src_generation = int(src_tc[2]) |
|
384 | 1 | src_romancode = int(src_tc[3:6]) |
|
385 | 1 | src_len_fn = int(src_tc[6:8]) |
|
386 | 1 | src_tc = src_tc.split('$') |
|
387 | 1 | src_specials = _split_special(src_tc[1]) |
|
388 | |||
389 | 1 | tar_generation = int(tar_tc[2]) |
|
390 | 1 | tar_romancode = int(tar_tc[3:6]) |
|
391 | 1 | tar_len_fn = int(tar_tc[6:8]) |
|
392 | 1 | tar_tc = tar_tc.split('$') |
|
393 | 1 | tar_specials = _split_special(tar_tc[1]) |
|
394 | |||
395 | 1 | gen_conflict = (src_generation != tar_generation) and bool( |
|
396 | src_generation or tar_generation |
||
397 | ) |
||
398 | 1 | roman_conflict = (src_romancode != tar_romancode) and bool( |
|
399 | src_romancode or tar_romancode |
||
400 | ) |
||
401 | |||
402 | 1 | ln_equal = src_ln == tar_ln |
|
403 | 1 | fn_equal = src_fn == tar_fn |
|
404 | |||
405 | # approx_c |
||
406 | 1 | def _approx_c(): |
|
407 | 1 | if gen_conflict or roman_conflict: |
|
408 | 1 | return False, 0 |
|
409 | |||
410 | 1 | full_src = ' '.join((src_ln, src_fn)) |
|
411 | 1 | if full_src.startswith('master '): |
|
412 | 1 | full_src = full_src[len('master ') :] |
|
413 | 1 | for intro in [ |
|
414 | 'of the ', |
||
415 | 'of ', |
||
416 | 'known as the ', |
||
417 | 'with the ', |
||
418 | 'with ', |
||
419 | ]: |
||
420 | 1 | if full_src.startswith(intro): |
|
421 | 1 | full_src = full_src[len(intro) :] |
|
422 | |||
423 | 1 | full_tar = ' '.join((tar_ln, tar_fn)) |
|
424 | 1 | if full_tar.startswith('master '): |
|
425 | 1 | full_tar = full_tar[len('master ') :] |
|
426 | 1 | for intro in [ |
|
427 | 'of the ', |
||
428 | 'of ', |
||
429 | 'known as the ', |
||
430 | 'with the ', |
||
431 | 'with ', |
||
432 | ]: |
||
433 | 1 | if full_tar.startswith(intro): |
|
434 | 1 | full_tar = full_tar[len(intro) :] |
|
435 | |||
436 | 1 | loc_ratio = sim_ratcliff_obershelp(full_src, full_tar) |
|
437 | 1 | return loc_ratio >= char_approx_min, loc_ratio |
|
438 | |||
439 | 1 | approx_c_result, ca_ratio = _approx_c() |
|
440 | |||
441 | 1 | if tests & test_dict['exact'] and fn_equal and ln_equal: |
|
442 | 1 | return _fmt_retval(match_type_dict['exact']) |
|
443 | 1 | if tests & test_dict['omission']: |
|
444 | 1 | if fn_equal and levenshtein(src_ln, tar_ln, cost=(1, 1, 99, 99)) == 1: |
|
445 | 1 | if not roman_conflict: |
|
446 | 1 | return _fmt_retval(match_type_dict['omission']) |
|
447 | 1 | elif ( |
|
448 | ln_equal and levenshtein(src_fn, tar_fn, cost=(1, 1, 99, 99)) == 1 |
||
449 | ): |
||
450 | 1 | return _fmt_retval(match_type_dict['omission']) |
|
451 | 1 | if tests & test_dict['substitution']: |
|
452 | 1 | if fn_equal and levenshtein(src_ln, tar_ln, cost=(99, 99, 1, 99)) == 1: |
|
453 | 1 | return _fmt_retval(match_type_dict['substitution']) |
|
454 | 1 | elif ( |
|
455 | ln_equal and levenshtein(src_fn, tar_fn, cost=(99, 99, 1, 99)) == 1 |
||
456 | ): |
||
457 | 1 | return _fmt_retval(match_type_dict['substitution']) |
|
458 | 1 | if tests & test_dict['transposition']: |
|
459 | 1 | if fn_equal and ( |
|
460 | levenshtein(src_ln, tar_ln, mode='osa', cost=(99, 99, 99, 1)) == 1 |
||
461 | ): |
||
462 | 1 | return _fmt_retval(match_type_dict['transposition']) |
|
463 | 1 | elif ln_equal and ( |
|
464 | levenshtein(src_fn, tar_fn, mode='osa', cost=(99, 99, 99, 1)) == 1 |
||
465 | ): |
||
466 | 1 | return _fmt_retval(match_type_dict['transposition']) |
|
467 | 1 | if tests & test_dict['punctuation']: |
|
468 | 1 | np_src_fn = _synoname_strip_punct(src_fn) |
|
469 | 1 | np_tar_fn = _synoname_strip_punct(tar_fn) |
|
470 | 1 | np_src_ln = _synoname_strip_punct(src_ln) |
|
471 | 1 | np_tar_ln = _synoname_strip_punct(tar_ln) |
|
472 | |||
473 | 1 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
|
474 | 1 | return _fmt_retval(match_type_dict['punctuation']) |
|
475 | |||
476 | 1 | np_src_fn = _synoname_strip_punct(src_fn.replace('-', ' ')) |
|
477 | 1 | np_tar_fn = _synoname_strip_punct(tar_fn.replace('-', ' ')) |
|
478 | 1 | np_src_ln = _synoname_strip_punct(src_ln.replace('-', ' ')) |
|
479 | 1 | np_tar_ln = _synoname_strip_punct(tar_ln.replace('-', ' ')) |
|
480 | |||
481 | 1 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
|
482 | 1 | return _fmt_retval(match_type_dict['punctuation']) |
|
483 | |||
484 | 1 | if tests & test_dict['initials'] and ln_equal: |
|
485 | 1 | if src_fn and tar_fn: |
|
486 | 1 | src_initials = _synoname_strip_punct(src_fn).split() |
|
487 | 1 | tar_initials = _synoname_strip_punct(tar_fn).split() |
|
488 | 1 | initials = bool( |
|
489 | (len(src_initials) == len(''.join(src_initials))) |
||
490 | or (len(tar_initials) == len(''.join(tar_initials))) |
||
491 | ) |
||
492 | 1 | if initials: |
|
493 | 1 | src_initials = ''.join(_[0] for _ in src_initials) |
|
494 | 1 | tar_initials = ''.join(_[0] for _ in tar_initials) |
|
495 | 1 | if src_initials == tar_initials: |
|
496 | 1 | return _fmt_retval(match_type_dict['initials']) |
|
497 | 1 | initial_diff = abs(len(src_initials) - len(tar_initials)) |
|
498 | 1 | if initial_diff and ( |
|
499 | ( |
||
500 | initial_diff |
||
501 | == levenshtein( |
||
502 | src_initials, tar_initials, cost=(1, 99, 99, 99) |
||
503 | ) |
||
504 | ) |
||
505 | or ( |
||
506 | initial_diff |
||
507 | == levenshtein( |
||
508 | tar_initials, src_initials, cost=(1, 99, 99, 99) |
||
509 | ) |
||
510 | ) |
||
511 | ): |
||
512 | 1 | return _fmt_retval(match_type_dict['initials']) |
|
513 | 1 | if tests & test_dict['extension']: |
|
514 | 1 | if src_ln[1] == tar_ln[1] and ( |
|
515 | src_ln.startswith(tar_ln) or tar_ln.startswith(src_ln) |
||
516 | ): |
||
517 | 1 | if ( |
|
518 | (not src_len_fn and not tar_len_fn) |
||
519 | or (tar_fn and src_fn.startswith(tar_fn)) |
||
520 | or (src_fn and tar_fn.startswith(src_fn)) |
||
521 | ) and not roman_conflict: |
||
522 | 1 | return _fmt_retval(match_type_dict['extension']) |
|
523 | 1 | if tests & test_dict['inclusion'] and ln_equal: |
|
524 | 1 | if (src_fn and src_fn in tar_fn) or (tar_fn and tar_fn in src_ln): |
|
525 | 1 | return _fmt_retval(match_type_dict['inclusion']) |
|
526 | 1 | if tests & test_dict['no_first'] and ln_equal: |
|
527 | 1 | if src_fn == '' or tar_fn == '': |
|
528 | 1 | return _fmt_retval(match_type_dict['no_first']) |
|
529 | 1 | if tests & test_dict['word_approx']: |
|
530 | 1 | ratio = _synoname_word_approximation( |
|
531 | src_ln, |
||
532 | tar_ln, |
||
533 | src_fn, |
||
534 | tar_fn, |
||
535 | { |
||
536 | 'gen_conflict': gen_conflict, |
||
537 | 'roman_conflict': roman_conflict, |
||
538 | 'src_specials': src_specials, |
||
539 | 'tar_specials': tar_specials, |
||
540 | }, |
||
541 | ) |
||
542 | 1 | if ratio == 1 and tests & test_dict['confusions']: |
|
543 | 1 | if ( |
|
544 | ' '.join((src_fn, src_ln)).strip() |
||
545 | == ' '.join((tar_fn, tar_ln)).strip() |
||
546 | ): |
||
547 | 1 | return _fmt_retval(match_type_dict['confusions']) |
|
548 | 1 | if ratio >= word_approx_min: |
|
549 | 1 | return _fmt_retval(match_type_dict['word_approx']) |
|
550 | 1 | if tests & test_dict['char_approx']: |
|
551 | 1 | if ca_ratio >= char_approx_min: |
|
552 | 1 | return _fmt_retval(match_type_dict['char_approx']) |
|
553 | 1 | return _fmt_retval(match_type_dict['no_match']) |
|
554 | |||
560 |