Conditions | 56 |
Total Lines | 225 |
Code Lines | 159 |
Lines | 0 |
Ratio | 0 % |
Tests | 100 |
CRAP Score | 56 |
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.phonetic._fr.henry_early() 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 -*- |
||
258 | 1 | def henry_early(word, max_length=3): |
|
259 | """Calculate the early version of the Henry code for a word. |
||
260 | |||
261 | The early version of Henry coding is given in :cite:`Legare:1972`. This is |
||
262 | different from the later version defined in :cite:`Henry:1976`. |
||
263 | |||
264 | :param str word: the word to transform |
||
265 | :param int max_length: the length of the code returned (defaults to 3) |
||
266 | :returns: the early Henry code |
||
267 | :rtype: str |
||
268 | |||
269 | >>> henry_early('Marchand') |
||
270 | 'MRC' |
||
271 | >>> henry_early('Beaulieu') |
||
272 | 'BL' |
||
273 | >>> henry_early('Beaumont') |
||
274 | 'BM' |
||
275 | >>> henry_early('Legrand') |
||
276 | 'LGR' |
||
277 | >>> henry_early('Pelletier') |
||
278 | 'PLT' |
||
279 | """ |
||
280 | 1 | _cons = { |
|
281 | 'B', |
||
282 | 'C', |
||
283 | 'D', |
||
284 | 'F', |
||
285 | 'G', |
||
286 | 'H', |
||
287 | 'J', |
||
288 | 'K', |
||
289 | 'L', |
||
290 | 'M', |
||
291 | 'N', |
||
292 | 'P', |
||
293 | 'Q', |
||
294 | 'R', |
||
295 | 'S', |
||
296 | 'T', |
||
297 | 'V', |
||
298 | 'W', |
||
299 | 'X', |
||
300 | 'Z', |
||
301 | } |
||
302 | 1 | _vows = {'A', 'E', 'I', 'O', 'U', 'Y'} |
|
303 | 1 | _diph = { |
|
304 | 'AI': 'E', |
||
305 | 'AY': 'E', |
||
306 | 'EI': 'E', |
||
307 | 'AU': 'O', |
||
308 | 'OI': 'O', |
||
309 | 'OU': 'O', |
||
310 | 'EU': 'U', |
||
311 | } |
||
312 | # _unaltered = {'B', 'D', 'F', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'V'} |
||
313 | 1 | _simple = {'W': 'V', 'X': 'S', 'Z': 'S'} |
|
314 | |||
315 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
316 | 1 | word = ''.join( |
|
317 | c |
||
318 | for c in word |
||
319 | if c |
||
320 | in { |
||
321 | 'A', |
||
322 | 'B', |
||
323 | 'C', |
||
324 | 'D', |
||
325 | 'E', |
||
326 | 'F', |
||
327 | 'G', |
||
328 | 'H', |
||
329 | 'I', |
||
330 | 'J', |
||
331 | 'K', |
||
332 | 'L', |
||
333 | 'M', |
||
334 | 'N', |
||
335 | 'O', |
||
336 | 'P', |
||
337 | 'Q', |
||
338 | 'R', |
||
339 | 'S', |
||
340 | 'T', |
||
341 | 'U', |
||
342 | 'V', |
||
343 | 'W', |
||
344 | 'X', |
||
345 | 'Y', |
||
346 | 'Z', |
||
347 | } |
||
348 | ) |
||
349 | |||
350 | 1 | if not word: |
|
351 | 1 | return '' |
|
352 | |||
353 | # Rule Ia seems to be covered entirely in II |
||
354 | |||
355 | # Rule Ib |
||
356 | 1 | if word[0] in _vows: |
|
357 | # Ib1 |
||
358 | 1 | if (word[1:2] in _cons - {'M', 'N'} and word[2:3] in _cons) or ( |
|
359 | word[1:2] in _cons and word[2:3] not in _cons |
||
|
|||
360 | ): |
||
361 | 1 | if word[0] == 'Y': |
|
362 | 1 | word = 'I' + word[1:] |
|
363 | # Ib2 |
||
364 | 1 | elif word[1:2] in {'M', 'N'} and word[2:3] in _cons: |
|
365 | 1 | if word[0] == 'E': |
|
366 | 1 | word = 'A' + word[1:] |
|
367 | 1 | elif word[0] in {'I', 'U', 'Y'}: |
|
368 | 1 | word = 'E' + word[1:] |
|
369 | # Ib3 |
||
370 | 1 | elif word[:2] in _diph: |
|
371 | 1 | word = _diph[word[:2]] + word[2:] |
|
372 | # Ib4 |
||
373 | 1 | elif word[1:2] in _vows and word[0] == 'Y': |
|
374 | 1 | word = 'I' + word[1:] |
|
375 | |||
376 | 1 | code = '' |
|
377 | 1 | skip = 0 |
|
378 | |||
379 | # Rule II |
||
380 | 1 | for pos, char in enumerate(word): |
|
381 | 1 | nxch = word[pos + 1 : pos + 2] |
|
382 | 1 | prev = word[pos - 1 : pos] |
|
383 | |||
384 | 1 | if skip: |
|
385 | 1 | skip -= 1 |
|
386 | 1 | elif char in _vows: |
|
387 | 1 | code += char |
|
388 | # IIc |
||
389 | 1 | elif char == nxch: |
|
390 | 1 | skip = 1 |
|
391 | 1 | code += char |
|
392 | 1 | elif word[pos : pos + 2] in {'CQ', 'DT', 'SC'}: |
|
393 | 1 | continue |
|
394 | # IIb |
||
395 | 1 | elif char in _simple: |
|
396 | 1 | code += _simple[char] |
|
397 | 1 | elif char in {'C', 'G', 'P', 'Q', 'S'}: |
|
398 | 1 | if char == 'C': |
|
399 | 1 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
|
400 | 1 | code += 'K' |
|
401 | 1 | elif nxch in {'E', 'I', 'Y'}: |
|
402 | 1 | code += 'S' |
|
403 | 1 | elif nxch == 'H': |
|
404 | 1 | if word[pos + 2 : pos + 3] in _vows: |
|
405 | 1 | code += 'C' |
|
406 | else: # CHR, CHL, etc. |
||
407 | 1 | code += 'K' |
|
408 | else: |
||
409 | 1 | code += 'C' |
|
410 | 1 | elif char == 'G': |
|
411 | 1 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
|
412 | 1 | code += 'G' |
|
413 | 1 | elif nxch in {'E', 'I', 'Y'}: |
|
414 | 1 | code += 'J' |
|
415 | 1 | elif nxch == 'N': |
|
416 | 1 | code += 'N' |
|
417 | 1 | elif char == 'P': |
|
418 | 1 | if nxch != 'H': |
|
419 | 1 | code += 'P' |
|
420 | else: |
||
421 | 1 | code += 'F' |
|
422 | 1 | elif char == 'Q': |
|
423 | 1 | if word[pos + 1 : pos + 3] in {'UE', 'UI', 'UY'}: |
|
424 | 1 | code += 'G' |
|
425 | else: # QUA, QUO, etc. |
||
426 | 1 | code += 'K' |
|
427 | else: # S... |
||
428 | 1 | if word[pos : pos + 6] == 'SAINTE': |
|
429 | 1 | code += 'X' |
|
430 | 1 | skip = 5 |
|
431 | 1 | elif word[pos : pos + 5] == 'SAINT': |
|
432 | 1 | code += 'X' |
|
433 | 1 | skip = 4 |
|
434 | 1 | elif word[pos : pos + 3] == 'STE': |
|
435 | 1 | code += 'X' |
|
436 | 1 | skip = 2 |
|
437 | 1 | elif word[pos : pos + 2] == 'ST': |
|
438 | 1 | code += 'X' |
|
439 | 1 | skip = 1 |
|
440 | 1 | elif nxch in _cons: |
|
441 | 1 | continue |
|
442 | else: |
||
443 | 1 | code += 'S' |
|
444 | # IId |
||
445 | 1 | elif char == 'H' and prev in _cons: |
|
446 | 1 | continue |
|
447 | 1 | elif char in _cons - {'L', 'R'} and nxch in _cons - {'L', 'R'}: |
|
448 | 1 | continue |
|
449 | 1 | elif char == 'L' and nxch in {'M', 'N'}: |
|
450 | 1 | continue |
|
451 | 1 | elif char in {'M', 'N'} and prev in _vows and nxch in _cons: |
|
452 | 1 | continue |
|
453 | # IIa |
||
454 | else: |
||
455 | 1 | code += char |
|
456 | |||
457 | # IIe1 |
||
458 | 1 | if code[-4:] in {'AULT', 'EULT', 'OULT'}: |
|
459 | 1 | code = code[:-2] |
|
460 | # The following are blocked by rules above |
||
461 | # elif code[-4:-3] in _vows and code[-3:] == 'MPS': |
||
462 | # code = code[:-3] |
||
463 | # elif code[-3:-2] in _vows and code[-2:] in {'MB', 'MP', 'ND', |
||
464 | # 'NS', 'NT'}: |
||
465 | # code = code[:-2] |
||
466 | 1 | elif code[-2:-1] == 'R' and code[-1:] in _cons: |
|
467 | 1 | code = code[:-1] |
|
468 | # IIe2 |
||
469 | 1 | elif code[-2:-1] in _vows and code[-1:] in {'D', 'M', 'N', 'S', 'T'}: |
|
470 | 1 | code = code[:-1] |
|
471 | 1 | elif code[-2:] == 'ER': |
|
472 | 1 | code = code[:-1] |
|
473 | |||
474 | # Drop non-initial vowels |
||
475 | 1 | code = code[:1] + code[1:].translate( |
|
476 | {65: '', 69: '', 73: '', 79: '', 85: '', 89: ''} |
||
477 | ) |
||
478 | |||
479 | 1 | if max_length != -1: |
|
480 | 1 | code = code[:max_length] |
|
481 | |||
482 | 1 | return code |
|
483 | |||
489 |