| Conditions | 36 |
| Total Lines | 147 |
| Code Lines | 84 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 73 |
| CRAP Score | 36 |
| 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._pshp_soundex_last.PSHPSoundexLast.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 | # Copyright 2014-2020 by Christopher C. Little. |
||
| 102 | def encode(self, lname: str) -> str: |
||
| 103 | """Calculate the PSHP Soundex/Viewex Coding of a last name. |
||
| 104 | |||
| 105 | Parameters |
||
| 106 | ---------- |
||
| 107 | lname : str |
||
| 108 | The last name to encode |
||
| 109 | |||
| 110 | Returns |
||
| 111 | ------- |
||
| 112 | str |
||
| 113 | 1 | The PSHP Soundex/Viewex Coding |
|
| 114 | 1 | ||
| 115 | Examples |
||
| 116 | 1 | -------- |
|
| 117 | >>> pe = PSHPSoundexLast() |
||
| 118 | >>> pe.encode('Smith') |
||
| 119 | 'S530' |
||
| 120 | >>> pe.encode('Waters') |
||
| 121 | 'W350' |
||
| 122 | >>> pe.encode('James') |
||
| 123 | 'J500' |
||
| 124 | >>> pe.encode('Schmidt') |
||
| 125 | 'S530' |
||
| 126 | >>> pe.encode('Ashcroft') |
||
| 127 | 'A225' |
||
| 128 | |||
| 129 | |||
| 130 | .. versionadded:: 0.3.0 |
||
| 131 | .. versionchanged:: 0.3.6 |
||
| 132 | Encapsulated in class |
||
| 133 | |||
| 134 | """ |
||
| 135 | lname = unicode_normalize('NFKD', lname.upper()) |
||
| 136 | lname = ''.join(c for c in lname if c in self._uc_set) |
||
| 137 | |||
| 138 | # A. Prefix treatment |
||
| 139 | if lname[:3] == 'VON' or lname[:3] == 'VAN': |
||
| 140 | lname = lname[3:].strip() |
||
| 141 | |||
| 142 | # The rule implemented below says "MC, MAC become 1". I believe it |
||
| 143 | # meant to say they become M except in German data (where superscripted |
||
| 144 | # 1 indicates "except in German data"). It doesn't make sense for them |
||
| 145 | # to become 1 (BPFV -> 1) or to apply outside German. Unfortunately, |
||
| 146 | # both articles have this error(?). |
||
| 147 | if not self._german: |
||
| 148 | if lname[:3] == 'MAC': |
||
| 149 | 1 | lname = 'M' + lname[3:] |
|
| 150 | 1 | elif lname[:2] == 'MC': |
|
| 151 | 1 | lname = 'M' + lname[2:] |
|
| 152 | |||
| 153 | # The non-German-only rule to strip ' is unnecessary due to filtering |
||
| 154 | 1 | ||
| 155 | 1 | if lname[:1] in {'E', 'I', 'O', 'U'}: |
|
| 156 | lname = 'A' + lname[1:] |
||
| 157 | elif lname[:2] in {'GE', 'GI', 'GY'}: |
||
| 158 | lname = 'J' + lname[1:] |
||
| 159 | elif lname[:2] in {'CE', 'CI', 'CY'}: |
||
| 160 | lname = 'S' + lname[1:] |
||
| 161 | elif lname[:3] == 'CHR': |
||
| 162 | 1 | lname = 'K' + lname[1:] |
|
| 163 | 1 | elif lname[:1] == 'C' and lname[:2] != 'CH': |
|
| 164 | 1 | lname = 'K' + lname[1:] |
|
| 165 | 1 | ||
| 166 | 1 | if lname[:2] == 'KN': |
|
| 167 | lname = 'N' + lname[1:] |
||
| 168 | elif lname[:2] == 'PH': |
||
| 169 | lname = 'F' + lname[1:] |
||
| 170 | 1 | elif lname[:3] in {'WIE', 'WEI'}: |
|
| 171 | 1 | lname = 'V' + lname[1:] |
|
| 172 | 1 | ||
| 173 | 1 | if self._german and lname[:1] in {'W', 'M', 'Y', 'Z'}: |
|
| 174 | 1 | lname = {'W': 'V', 'M': 'N', 'Y': 'J', 'Z': 'S'}[lname[0]] + lname[ |
|
| 175 | 1 | 1: |
|
| 176 | 1 | ] |
|
| 177 | 1 | ||
| 178 | 1 | code = lname[:1] |
|
| 179 | 1 | ||
| 180 | # B. Postfix treatment |
||
| 181 | 1 | if self._german: # moved from end of postfix treatment due to blocking |
|
| 182 | 1 | if lname[-3:] == 'TES': |
|
| 183 | 1 | lname = lname[:-3] |
|
| 184 | 1 | elif lname[-2:] == 'TS': |
|
| 185 | 1 | lname = lname[:-2] |
|
| 186 | 1 | if lname[-3:] == 'TZE': |
|
| 187 | lname = lname[:-3] |
||
| 188 | 1 | elif lname[-2:] == 'ZE': |
|
| 189 | 1 | lname = lname[:-2] |
|
| 190 | if lname[-1:] == 'Z': |
||
| 191 | lname = lname[:-1] |
||
| 192 | elif lname[-2:] == 'TE': |
||
| 193 | 1 | lname = lname[:-2] |
|
| 194 | |||
| 195 | if lname[-1:] == 'R': |
||
| 196 | 1 | lname = lname[:-1] + 'N' |
|
| 197 | 1 | elif lname[-2:] in {'SE', 'CE'}: |
|
| 198 | 1 | lname = lname[:-2] |
|
| 199 | 1 | if lname[-2:] == 'SS': |
|
| 200 | 1 | lname = lname[:-2] |
|
| 201 | 1 | elif lname[-1:] == 'S': |
|
| 202 | 1 | lname = lname[:-1] |
|
| 203 | 1 | ||
| 204 | 1 | if not self._german: |
|
| 205 | 1 | l5_repl = {'STOWN': 'SAWON', 'MPSON': 'MASON'} |
|
| 206 | 1 | l4_repl = { |
|
| 207 | 1 | 'NSEN': 'ASEN', |
|
| 208 | 1 | 'MSON': 'ASON', |
|
| 209 | 'STEN': 'SAEN', |
||
| 210 | 1 | 'STON': 'SAON', |
|
| 211 | 1 | } |
|
| 212 | 1 | if lname[-5:] in l5_repl: |
|
| 213 | 1 | lname = lname[:-5] + l5_repl[lname[-5:]] |
|
| 214 | 1 | elif lname[-4:] in l4_repl: |
|
| 215 | 1 | lname = lname[:-4] + l4_repl[lname[-4:]] |
|
| 216 | 1 | ||
| 217 | 1 | if lname[-2:] in {'NG', 'ND'}: |
|
| 218 | lname = lname[:-1] |
||
| 219 | 1 | if not self._german and lname[-3:] in {'GAN', 'GEN'}: |
|
| 220 | 1 | lname = lname[:-3] + 'A' + lname[-2:] |
|
| 221 | 1 | ||
| 222 | # C. Infix Treatment |
||
| 223 | lname = lname.replace('CK', 'C') |
||
| 224 | lname = lname.replace('SCH', 'S') |
||
| 225 | lname = lname.replace('DT', 'T') |
||
| 226 | lname = lname.replace('ND', 'N') |
||
| 227 | 1 | lname = lname.replace('NG', 'N') |
|
| 228 | 1 | lname = lname.replace('LM', 'M') |
|
| 229 | 1 | lname = lname.replace('MN', 'M') |
|
| 230 | 1 | lname = lname.replace('WIE', 'VIE') |
|
| 231 | lname = lname.replace('WEI', 'VEI') |
||
| 232 | 1 | ||
| 233 | 1 | # D. Soundexing |
|
| 234 | 1 | # code for X & Y are unspecified, but presumably are 2 & 0 |
|
| 235 | 1 | ||
| 236 | lname = lname.translate(self._trans) |
||
| 237 | lname = self._delete_consecutive_repeats(lname) |
||
| 238 | 1 | ||
| 239 | 1 | code += lname[1:] |
|
| 240 | 1 | code = code.replace('0', '') # rule 1 |
|
| 241 | 1 | ||
| 242 | 1 | if self._max_length != -1: |
|
| 243 | 1 | if len(code) < self._max_length: |
|
| 244 | 1 | code += '0' * (self._max_length - len(code)) |
|
| 245 | 1 | else: |
|
| 246 | 1 | code = code[: self._max_length] |
|
| 247 | |||
| 248 | return code |
||
| 249 | |||
| 255 |