| Conditions | 15 |
| Total Lines | 106 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 61 |
| CRAP Score | 15 |
| 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._fuzzy_soundex.FuzzySoundex.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 -*- |
||
| 54 | 1 | def encode(self, word, max_length=5, zero_pad=True): |
|
| 55 | """Return the Fuzzy Soundex code for a word. |
||
| 56 | |||
| 57 | Parameters |
||
| 58 | ---------- |
||
| 59 | word : str |
||
| 60 | The word to transform |
||
| 61 | max_length : int |
||
| 62 | The length of the code returned (defaults to 4) |
||
| 63 | zero_pad : bool |
||
| 64 | Pad the end of the return value with 0s to achieve a max_length |
||
| 65 | string |
||
| 66 | |||
| 67 | Returns |
||
| 68 | ------- |
||
| 69 | str |
||
| 70 | The Fuzzy Soundex value |
||
| 71 | |||
| 72 | Examples |
||
| 73 | -------- |
||
| 74 | >>> pe = FuzzySoundex() |
||
| 75 | >>> pe.encode('Christopher') |
||
| 76 | 'K6931' |
||
| 77 | >>> pe.encode('Niall') |
||
| 78 | 'N4000' |
||
| 79 | >>> pe.encode('Smith') |
||
| 80 | 'S5300' |
||
| 81 | >>> pe.encode('Smith') |
||
| 82 | 'S5300' |
||
| 83 | |||
| 84 | """ |
||
| 85 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
| 86 | 1 | word = word.replace('ß', 'SS') |
|
| 87 | |||
| 88 | # Clamp max_length to [4, 64] |
||
| 89 | 1 | if max_length != -1: |
|
| 90 | 1 | max_length = min(max(4, max_length), 64) |
|
| 91 | else: |
||
| 92 | 1 | max_length = 64 |
|
| 93 | |||
| 94 | 1 | if not word: |
|
| 95 | 1 | if zero_pad: |
|
| 96 | 1 | return '0' * max_length |
|
| 97 | 1 | return '0' |
|
| 98 | |||
| 99 | 1 | if word[:2] in {'CS', 'CZ', 'TS', 'TZ'}: |
|
| 100 | 1 | word = 'SS' + word[2:] |
|
| 101 | 1 | elif word[:2] == 'GN': |
|
| 102 | 1 | word = 'NN' + word[2:] |
|
| 103 | 1 | elif word[:2] in {'HR', 'WR'}: |
|
| 104 | 1 | word = 'RR' + word[2:] |
|
| 105 | 1 | elif word[:2] == 'HW': |
|
| 106 | 1 | word = 'WW' + word[2:] |
|
| 107 | 1 | elif word[:2] in {'KN', 'NG'}: |
|
| 108 | 1 | word = 'NN' + word[2:] |
|
| 109 | |||
| 110 | 1 | if word[-2:] == 'CH': |
|
| 111 | 1 | word = word[:-2] + 'KK' |
|
| 112 | 1 | elif word[-2:] == 'NT': |
|
| 113 | 1 | word = word[:-2] + 'TT' |
|
| 114 | 1 | elif word[-2:] == 'RT': |
|
| 115 | 1 | word = word[:-2] + 'RR' |
|
| 116 | 1 | elif word[-3:] == 'RDT': |
|
| 117 | 1 | word = word[:-3] + 'RR' |
|
| 118 | |||
| 119 | 1 | word = word.replace('CA', 'KA') |
|
| 120 | 1 | word = word.replace('CC', 'KK') |
|
| 121 | 1 | word = word.replace('CK', 'KK') |
|
| 122 | 1 | word = word.replace('CE', 'SE') |
|
| 123 | 1 | word = word.replace('CHL', 'KL') |
|
| 124 | 1 | word = word.replace('CL', 'KL') |
|
| 125 | 1 | word = word.replace('CHR', 'KR') |
|
| 126 | 1 | word = word.replace('CR', 'KR') |
|
| 127 | 1 | word = word.replace('CI', 'SI') |
|
| 128 | 1 | word = word.replace('CO', 'KO') |
|
| 129 | 1 | word = word.replace('CU', 'KU') |
|
| 130 | 1 | word = word.replace('CY', 'SY') |
|
| 131 | 1 | word = word.replace('DG', 'GG') |
|
| 132 | 1 | word = word.replace('GH', 'HH') |
|
| 133 | 1 | word = word.replace('MAC', 'MK') |
|
| 134 | 1 | word = word.replace('MC', 'MK') |
|
| 135 | 1 | word = word.replace('NST', 'NSS') |
|
| 136 | 1 | word = word.replace('PF', 'FF') |
|
| 137 | 1 | word = word.replace('PH', 'FF') |
|
| 138 | 1 | word = word.replace('SCH', 'SSS') |
|
| 139 | 1 | word = word.replace('TIO', 'SIO') |
|
| 140 | 1 | word = word.replace('TIA', 'SIO') |
|
| 141 | 1 | word = word.replace('TCH', 'CHH') |
|
| 142 | |||
| 143 | 1 | sdx = word.translate(self._trans) |
|
| 144 | 1 | sdx = sdx.replace('-', '') |
|
| 145 | |||
| 146 | # remove repeating characters |
||
| 147 | 1 | sdx = self._delete_consecutive_repeats(sdx) |
|
| 148 | |||
| 149 | 1 | if word[0] in {'H', 'W', 'Y'}: |
|
| 150 | 1 | sdx = word[0] + sdx |
|
| 151 | else: |
||
| 152 | 1 | sdx = word[0] + sdx[1:] |
|
| 153 | |||
| 154 | 1 | sdx = sdx.replace('0', '') |
|
| 155 | |||
| 156 | 1 | if zero_pad: |
|
| 157 | 1 | sdx += '0' * max_length |
|
| 158 | |||
| 159 | 1 | return sdx[:max_length] |
|
| 160 | |||
| 200 |