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