Conditions | 79 |
Total Lines | 233 |
Code Lines | 170 |
Lines | 0 |
Ratio | 0 % |
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 -*- |
||
224 | def synoname(src, tar, word_approx_min=0.3, char_approx_min=0.73, |
||
225 | tests=2**12-1, ret_name=False): |
||
226 | """Return the Synoname similarity type of two words. |
||
227 | |||
228 | Cf. :cite:`Getty:1991,Gross:1991` |
||
229 | |||
230 | :param str src: source string for comparison |
||
231 | :param str tar: target string for comparison |
||
232 | :param bool ret_name: return the name of the match type rather than the |
||
233 | int value |
||
234 | :param float word_approx_min: the minimum word approximation value to |
||
235 | signal a 'word_approx' match |
||
236 | :param float char_approx_min: the minimum character approximation value to |
||
237 | signal a 'char_approx' match |
||
238 | :param int or Iterable tests: either an integer indicating tests to |
||
239 | perform or a list of test names to perform (defaults to performing all |
||
240 | tests) |
||
241 | :param bool ret_name: if True, returns the match name rather than its |
||
242 | integer equivalent |
||
243 | :returns: Synoname value |
||
244 | :rtype: int (or str if ret_name is True) |
||
245 | |||
246 | >>> synoname(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', '')) |
||
247 | 2 |
||
248 | >>> synoname(('Breghel', 'Pieter', ''), ('Brueghel', 'Pieter', ''), |
||
249 | ... ret_name=True) |
||
250 | 'omission' |
||
251 | >>> synoname(('Dore', 'Gustave', ''), |
||
252 | ... ('Dore', 'Paul Gustave Louis Christophe', ''), |
||
253 | ... ret_name=True) |
||
254 | 'inclusion' |
||
255 | >>> synoname(('Pereira', 'I. R.', ''), ('Pereira', 'I. Smith', ''), |
||
256 | ... ret_name=True) |
||
257 | 'word_approx' |
||
258 | """ |
||
259 | test_dict = {val: 2**n for n, val in enumerate([ |
||
260 | 'exact', 'omission', 'substitution', 'transposition', 'punctuation', |
||
261 | 'initials', 'extension', 'inclusion', 'no_first', 'word_approx', |
||
262 | 'confusions', 'char_approx'])} |
||
263 | match_name = ['', 'exact', 'omission', 'substitution', 'transposition', |
||
264 | 'punctuation', 'initials', 'extension', 'inclusion', |
||
265 | 'no_first', 'word_approx', 'confusions', 'char_approx', |
||
266 | 'no_match'] |
||
267 | match_type_dict = {val: n for n, val in enumerate(match_name)} |
||
268 | |||
269 | if isinstance(tests, Iterable): |
||
270 | new_tests = 0 |
||
271 | for term in tests: |
||
272 | if term in test_dict: |
||
273 | new_tests += test_dict[term] |
||
274 | tests = new_tests |
||
275 | |||
276 | if isinstance(src, tuple): |
||
277 | src_ln, src_fn, src_qual = src |
||
278 | elif '#' in src: |
||
279 | src_ln, src_fn, src_qual = src.split('#')[-3:] |
||
280 | else: |
||
281 | src_ln, src_fn, src_qual = src, '', '' |
||
282 | |||
283 | if isinstance(tar, tuple): |
||
284 | tar_ln, tar_fn, tar_qual = tar |
||
285 | elif '#' in tar: |
||
286 | tar_ln, tar_fn, tar_qual = tar.split('#')[-3:] |
||
287 | else: |
||
288 | tar_ln, tar_fn, tar_qual = tar, '', '' |
||
289 | |||
290 | def _split_special(spec): |
||
291 | spec_list = [] |
||
292 | while spec: |
||
293 | spec_list.append((int(spec[:3]), spec[3:4])) |
||
294 | spec = spec[4:] |
||
295 | return spec_list |
||
296 | |||
297 | def _fmt_retval(val): |
||
298 | if ret_name: |
||
299 | return match_name[val] |
||
300 | return val |
||
301 | |||
302 | # 1. Preprocessing |
||
303 | |||
304 | # Lowercasing |
||
305 | src_fn = src_fn.strip().lower() |
||
306 | src_ln = src_ln.strip().lower() |
||
307 | src_qual = src_qual.strip().lower() |
||
308 | |||
309 | tar_fn = tar_fn.strip().lower() |
||
310 | tar_ln = tar_ln.strip().lower() |
||
311 | tar_qual = tar_qual.strip().lower() |
||
312 | |||
313 | # Create toolcodes |
||
314 | src_ln, src_fn, src_tc = synoname_toolcode(src_ln, src_fn, src_qual) |
||
315 | tar_ln, tar_fn, tar_tc = synoname_toolcode(tar_ln, tar_fn, tar_qual) |
||
316 | |||
317 | src_generation = int(src_tc[2]) |
||
318 | src_romancode = int(src_tc[3:6]) |
||
319 | src_len_fn = int(src_tc[6:8]) |
||
320 | src_tc = src_tc.split('$') |
||
321 | src_specials = _split_special(src_tc[1]) |
||
322 | |||
323 | tar_generation = int(tar_tc[2]) |
||
324 | tar_romancode = int(tar_tc[3:6]) |
||
325 | tar_len_fn = int(tar_tc[6:8]) |
||
326 | tar_tc = tar_tc.split('$') |
||
327 | tar_specials = _split_special(tar_tc[1]) |
||
328 | |||
329 | gen_conflict = ((src_generation != tar_generation) and |
||
330 | bool(src_generation or tar_generation)) |
||
331 | roman_conflict = ((src_romancode != tar_romancode) and |
||
332 | bool(src_romancode or tar_romancode)) |
||
333 | |||
334 | ln_equal = src_ln == tar_ln |
||
335 | fn_equal = src_fn == tar_fn |
||
336 | |||
337 | # approx_c |
||
338 | def _approx_c(): |
||
339 | if gen_conflict or roman_conflict: |
||
340 | return False, 0 |
||
341 | |||
342 | full_src = ' '.join((src_ln, src_fn)) |
||
343 | if full_src.startswith('master '): |
||
344 | full_src = full_src[len('master '):] |
||
345 | for intro in ['of the ', 'of ', 'known as the ', 'with the ', |
||
346 | 'with ']: |
||
347 | if full_src.startswith(intro): |
||
348 | full_src = full_src[len(intro):] |
||
349 | |||
350 | full_tar = ' '.join((tar_ln, tar_fn)) |
||
351 | if full_tar.startswith('master '): |
||
352 | full_tar = full_tar[len('master '):] |
||
353 | for intro in ['of the ', 'of ', 'known as the ', 'with the ', |
||
354 | 'with ']: |
||
355 | if full_tar.startswith(intro): |
||
356 | full_tar = full_tar[len(intro):] |
||
357 | |||
358 | loc_ratio = sim_ratcliff_obershelp(full_src, full_tar) |
||
359 | return loc_ratio >= char_approx_min, loc_ratio |
||
360 | |||
361 | approx_c_result, ca_ratio = _approx_c() |
||
362 | |||
363 | if tests & test_dict['exact'] and fn_equal and ln_equal: |
||
364 | return _fmt_retval(match_type_dict['exact']) |
||
365 | if tests & test_dict['omission']: |
||
366 | if (fn_equal and |
||
367 | levenshtein(src_ln, tar_ln, cost=(1, 1, 99, 99)) == 1): |
||
368 | if not roman_conflict: |
||
369 | return _fmt_retval(match_type_dict['omission']) |
||
370 | elif (ln_equal and |
||
371 | levenshtein(src_fn, tar_fn, cost=(1, 1, 99, 99)) == 1): |
||
372 | return _fmt_retval(match_type_dict['omission']) |
||
373 | if tests & test_dict['substitution']: |
||
374 | if (fn_equal and |
||
375 | levenshtein(src_ln, tar_ln, cost=(99, 99, 1, 99)) == 1): |
||
376 | return _fmt_retval(match_type_dict['substitution']) |
||
377 | elif (ln_equal and |
||
378 | levenshtein(src_fn, tar_fn, cost=(99, 99, 1, 99)) == 1): |
||
379 | return _fmt_retval(match_type_dict['substitution']) |
||
380 | if tests & test_dict['transposition']: |
||
381 | if (fn_equal and |
||
382 | (levenshtein(src_ln, tar_ln, mode='osa', cost=(99, 99, 99, 1)) |
||
383 | == 1)): |
||
384 | return _fmt_retval(match_type_dict['transposition']) |
||
385 | elif (ln_equal and |
||
386 | (levenshtein(src_fn, tar_fn, mode='osa', cost=(99, 99, 99, 1)) |
||
387 | == 1)): |
||
388 | return _fmt_retval(match_type_dict['transposition']) |
||
389 | if tests & test_dict['punctuation']: |
||
390 | np_src_fn = _synoname_strip_punct(src_fn) |
||
391 | np_tar_fn = _synoname_strip_punct(tar_fn) |
||
392 | np_src_ln = _synoname_strip_punct(src_ln) |
||
393 | np_tar_ln = _synoname_strip_punct(tar_ln) |
||
394 | |||
395 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
||
396 | return _fmt_retval(match_type_dict['punctuation']) |
||
397 | |||
398 | np_src_fn = _synoname_strip_punct(src_fn.replace('-', ' ')) |
||
399 | np_tar_fn = _synoname_strip_punct(tar_fn.replace('-', ' ')) |
||
400 | np_src_ln = _synoname_strip_punct(src_ln.replace('-', ' ')) |
||
401 | np_tar_ln = _synoname_strip_punct(tar_ln.replace('-', ' ')) |
||
402 | |||
403 | if (np_src_fn == np_tar_fn) and (np_src_ln == np_tar_ln): |
||
404 | return _fmt_retval(match_type_dict['punctuation']) |
||
405 | |||
406 | if tests & test_dict['initials'] and ln_equal: |
||
407 | if src_fn and tar_fn: |
||
408 | src_initials = _synoname_strip_punct(src_fn).split() |
||
409 | tar_initials = _synoname_strip_punct(tar_fn).split() |
||
410 | initials = bool((len(src_initials) == len(''.join(src_initials))) |
||
411 | or |
||
412 | (len(tar_initials) == len(''.join(tar_initials)))) |
||
413 | if initials: |
||
414 | src_initials = ''.join(_[0] for _ in src_initials) |
||
415 | tar_initials = ''.join(_[0] for _ in tar_initials) |
||
416 | if src_initials == tar_initials: |
||
417 | return _fmt_retval(match_type_dict['initials']) |
||
418 | initial_diff = abs(len(src_initials)-len(tar_initials)) |
||
419 | if (initial_diff and |
||
420 | ((initial_diff == |
||
421 | levenshtein(src_initials, tar_initials, |
||
422 | cost=(1, 99, 99, 99))) or |
||
423 | (initial_diff == |
||
424 | levenshtein(tar_initials, src_initials, |
||
425 | cost=(1, 99, 99, 99))))): |
||
426 | return _fmt_retval(match_type_dict['initials']) |
||
427 | if tests & test_dict['extension']: |
||
428 | if src_ln[1] == tar_ln[1] and (src_ln.startswith(tar_ln) or |
||
429 | tar_ln.startswith(src_ln)): |
||
430 | if (((not src_len_fn and not tar_len_fn) or |
||
431 | (tar_fn and src_fn.startswith(tar_fn)) or |
||
432 | (src_fn and tar_fn.startswith(src_fn))) |
||
433 | and not roman_conflict): |
||
434 | return _fmt_retval(match_type_dict['extension']) |
||
435 | if tests & test_dict['inclusion'] and ln_equal: |
||
436 | if (src_fn and src_fn in tar_fn) or (tar_fn and tar_fn in src_ln): |
||
437 | return _fmt_retval(match_type_dict['inclusion']) |
||
438 | if tests & test_dict['no_first'] and ln_equal: |
||
439 | if src_fn == '' or tar_fn == '': |
||
440 | return _fmt_retval(match_type_dict['no_first']) |
||
441 | if tests & test_dict['word_approx']: |
||
442 | ratio = _synoname_word_approximation(src_ln, tar_ln, src_fn, tar_fn, |
||
443 | {'gen_conflict': gen_conflict, |
||
444 | 'roman_conflict': roman_conflict, |
||
445 | 'src_specials': src_specials, |
||
446 | 'tar_specials': tar_specials}) |
||
447 | if ratio == 1 and tests & test_dict['confusions']: |
||
448 | if (' '.join((src_fn, src_ln)).strip() == |
||
449 | ' '.join((tar_fn, tar_ln)).strip()): |
||
450 | return _fmt_retval(match_type_dict['confusions']) |
||
451 | if ratio >= word_approx_min: |
||
452 | return _fmt_retval(match_type_dict['word_approx']) |
||
453 | if tests & test_dict['char_approx']: |
||
454 | if ca_ratio >= char_approx_min: |
||
455 | return _fmt_retval(match_type_dict['char_approx']) |
||
456 | return _fmt_retval(match_type_dict['no_match']) |
||
457 | |||
462 |