Conditions | 56 |
Total Lines | 171 |
Code Lines | 111 |
Lines | 0 |
Ratio | 0 % |
Tests | 96 |
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.HenryEarly.encode() 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 -*- |
||
276 | 1 | def encode(self, word, max_length=3): |
|
277 | """Calculate the early version of the Henry code for a word. |
||
278 | |||
279 | :param str word: the word to transform |
||
280 | :param int max_length: the length of the code returned (defaults to 3) |
||
281 | :returns: the early Henry code |
||
282 | :rtype: str |
||
283 | |||
284 | >>> henry_early('Marchand') |
||
285 | 'MRC' |
||
286 | >>> henry_early('Beaulieu') |
||
287 | 'BL' |
||
288 | >>> henry_early('Beaumont') |
||
289 | 'BM' |
||
290 | >>> henry_early('Legrand') |
||
291 | 'LGR' |
||
292 | >>> henry_early('Pelletier') |
||
293 | 'PLT' |
||
294 | """ |
||
295 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
296 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
297 | |||
298 | 1 | if not word: |
|
299 | 1 | return '' |
|
300 | |||
301 | # Rule Ia seems to be covered entirely in II |
||
302 | |||
303 | # Rule Ib |
||
304 | 1 | if word[0] in self._uc_vy_set: |
|
305 | # Ib1 |
||
306 | 1 | if ( |
|
307 | word[1:2] in self._uc_c_set - {'M', 'N'} |
||
308 | and word[2:3] in self._uc_c_set |
||
309 | ) or ( |
||
310 | word[1:2] in self._uc_c_set and word[2:3] not in self._uc_c_set |
||
311 | ): |
||
312 | 1 | if word[0] == 'Y': |
|
313 | 1 | word = 'I' + word[1:] |
|
314 | # Ib2 |
||
315 | 1 | elif word[1:2] in {'M', 'N'} and word[2:3] in self._uc_c_set: |
|
316 | 1 | if word[0] == 'E': |
|
317 | 1 | word = 'A' + word[1:] |
|
318 | 1 | elif word[0] in {'I', 'U', 'Y'}: |
|
319 | 1 | word = 'E' + word[1:] |
|
320 | # Ib3 |
||
321 | 1 | elif word[:2] in self._diph: |
|
322 | 1 | word = self._diph[word[:2]] + word[2:] |
|
323 | # Ib4 |
||
324 | 1 | elif word[1:2] in self._uc_vy_set and word[0] == 'Y': |
|
325 | 1 | word = 'I' + word[1:] |
|
326 | |||
327 | 1 | code = '' |
|
328 | 1 | skip = 0 |
|
329 | |||
330 | # Rule II |
||
331 | 1 | for pos, char in enumerate(word): |
|
332 | 1 | nxch = word[pos + 1 : pos + 2] |
|
333 | 1 | prev = word[pos - 1 : pos] |
|
334 | |||
335 | 1 | if skip: |
|
336 | 1 | skip -= 1 |
|
337 | 1 | elif char in self._uc_vy_set: |
|
338 | 1 | code += char |
|
339 | # IIc |
||
340 | 1 | elif char == nxch: |
|
341 | 1 | skip = 1 |
|
342 | 1 | code += char |
|
343 | 1 | elif word[pos : pos + 2] in {'CQ', 'DT', 'SC'}: |
|
344 | 1 | continue |
|
345 | # IIb |
||
346 | 1 | elif char in self._simple: |
|
347 | 1 | code += self._simple[char] |
|
348 | 1 | elif char in {'C', 'G', 'P', 'Q', 'S'}: |
|
349 | 1 | if char == 'C': |
|
350 | 1 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
|
351 | 1 | code += 'K' |
|
352 | 1 | elif nxch in {'E', 'I', 'Y'}: |
|
353 | 1 | code += 'S' |
|
354 | 1 | elif nxch == 'H': |
|
355 | 1 | if word[pos + 2 : pos + 3] in self._uc_vy_set: |
|
356 | 1 | code += 'C' |
|
357 | else: # CHR, CHL, etc. |
||
358 | 1 | code += 'K' |
|
359 | else: |
||
360 | 1 | code += 'C' |
|
361 | 1 | elif char == 'G': |
|
362 | 1 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
|
363 | 1 | code += 'G' |
|
364 | 1 | elif nxch in {'E', 'I', 'Y'}: |
|
365 | 1 | code += 'J' |
|
366 | 1 | elif nxch == 'N': |
|
367 | 1 | code += 'N' |
|
368 | 1 | elif char == 'P': |
|
369 | 1 | if nxch != 'H': |
|
370 | 1 | code += 'P' |
|
371 | else: |
||
372 | 1 | code += 'F' |
|
373 | 1 | elif char == 'Q': |
|
374 | 1 | if word[pos + 1 : pos + 3] in {'UE', 'UI', 'UY'}: |
|
375 | 1 | code += 'G' |
|
376 | else: # QUA, QUO, etc. |
||
377 | 1 | code += 'K' |
|
378 | else: # S... |
||
379 | 1 | if word[pos : pos + 6] == 'SAINTE': |
|
380 | 1 | code += 'X' |
|
381 | 1 | skip = 5 |
|
382 | 1 | elif word[pos : pos + 5] == 'SAINT': |
|
383 | 1 | code += 'X' |
|
384 | 1 | skip = 4 |
|
385 | 1 | elif word[pos : pos + 3] == 'STE': |
|
386 | 1 | code += 'X' |
|
387 | 1 | skip = 2 |
|
388 | 1 | elif word[pos : pos + 2] == 'ST': |
|
389 | 1 | code += 'X' |
|
390 | 1 | skip = 1 |
|
391 | 1 | elif nxch in self._uc_c_set: |
|
392 | 1 | continue |
|
393 | else: |
||
394 | 1 | code += 'S' |
|
395 | # IId |
||
396 | 1 | elif char == 'H' and prev in self._uc_c_set: |
|
397 | 1 | continue |
|
398 | 1 | elif char in self._uc_c_set - { |
|
399 | 'L', |
||
400 | 'R', |
||
401 | } and nxch in self._uc_c_set - {'L', 'R'}: |
||
402 | 1 | continue |
|
403 | 1 | elif char == 'L' and nxch in {'M', 'N'}: |
|
404 | 1 | continue |
|
405 | 1 | elif ( |
|
406 | char in {'M', 'N'} |
||
407 | and prev in self._uc_vy_set |
||
408 | and nxch in self._uc_c_set |
||
409 | ): |
||
410 | 1 | continue |
|
411 | # IIa |
||
412 | else: |
||
413 | 1 | code += char |
|
414 | |||
415 | # IIe1 |
||
416 | 1 | if code[-4:] in {'AULT', 'EULT', 'OULT'}: |
|
417 | 1 | code = code[:-2] |
|
418 | # The following are blocked by rules above |
||
419 | # elif code[-4:-3] in _vows and code[-3:] == 'MPS': |
||
420 | # code = code[:-3] |
||
421 | # elif code[-3:-2] in _vows and code[-2:] in {'MB', 'MP', 'ND', |
||
422 | # 'NS', 'NT'}: |
||
423 | # code = code[:-2] |
||
424 | 1 | elif code[-2:-1] == 'R' and code[-1:] in self._uc_c_set: |
|
425 | 1 | code = code[:-1] |
|
426 | # IIe2 |
||
427 | 1 | elif code[-2:-1] in self._uc_vy_set and code[-1:] in { |
|
428 | 'D', |
||
429 | 'M', |
||
430 | 'N', |
||
431 | 'S', |
||
432 | 'T', |
||
433 | }: |
||
434 | 1 | code = code[:-1] |
|
435 | 1 | elif code[-2:] == 'ER': |
|
436 | 1 | code = code[:-1] |
|
437 | |||
438 | # Drop non-initial vowels |
||
439 | 1 | code = code[:1] + code[1:].translate( |
|
440 | {65: '', 69: '', 73: '', 79: '', 85: '', 89: ''} |
||
441 | ) |
||
442 | |||
443 | 1 | if max_length != -1: |
|
444 | 1 | code = code[:max_length] |
|
445 | |||
446 | 1 | return code |
|
447 | |||
477 |