| Conditions | 56 |
| Total Lines | 167 |
| Code Lines | 107 |
| Lines | 0 |
| Ratio | 0 % |
| 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 -*- |
||
| 168 | def henry_early(word, max_length=3): |
||
| 169 | """Calculate the early version of the Henry code for a word. |
||
| 170 | |||
| 171 | The early version of Henry coding is given in :cite:`Legare:1972`. This is |
||
| 172 | different from the later version defined in :cite:`Henry:1976`. |
||
| 173 | |||
| 174 | :param str word: the word to transform |
||
| 175 | :param int max_length: the length of the code returned (defaults to 3) |
||
| 176 | :returns: the early Henry code |
||
| 177 | :rtype: str |
||
| 178 | |||
| 179 | >>> henry_early('Marchand') |
||
| 180 | 'MRC' |
||
| 181 | >>> henry_early('Beaulieu') |
||
| 182 | 'BL' |
||
| 183 | >>> henry_early('Beaumont') |
||
| 184 | 'BM' |
||
| 185 | >>> henry_early('Legrand') |
||
| 186 | 'LGR' |
||
| 187 | >>> henry_early('Pelletier') |
||
| 188 | 'PLT' |
||
| 189 | """ |
||
| 190 | _cons = {'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', |
||
| 191 | 'R', 'S', 'T', 'V', 'W', 'X', 'Z'} |
||
| 192 | _vows = {'A', 'E', 'I', 'O', 'U', 'Y'} |
||
| 193 | _diph = {'AI': 'E', 'AY': 'E', 'EI': 'E', 'AU': 'O', 'OI': 'O', 'OU': 'O', |
||
| 194 | 'EU': 'U'} |
||
| 195 | # _unaltered = {'B', 'D', 'F', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'V'} |
||
| 196 | _simple = {'W': 'V', 'X': 'S', 'Z': 'S'} |
||
| 197 | |||
| 198 | word = unicode_normalize('NFKD', text_type(word.upper())) |
||
| 199 | word = ''.join(c for c in word if c in |
||
| 200 | {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
||
| 201 | 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
||
| 202 | 'Y', 'Z'}) |
||
| 203 | |||
| 204 | if not word: |
||
| 205 | return '' |
||
| 206 | |||
| 207 | # Rule Ia seems to be covered entirely in II |
||
| 208 | |||
| 209 | # Rule Ib |
||
| 210 | if word[0] in _vows: |
||
| 211 | # Ib1 |
||
| 212 | if (((word[1:2] in _cons-{'M', 'N'} and word[2:3] in _cons) or |
||
| 213 | (word[1:2] in _cons and word[2:3] not in _cons))): |
||
| 214 | if word[0] == 'Y': |
||
| 215 | word = 'I'+word[1:] |
||
| 216 | # Ib2 |
||
| 217 | elif word[1:2] in {'M', 'N'} and word[2:3] in _cons: |
||
| 218 | if word[0] == 'E': |
||
| 219 | word = 'A'+word[1:] |
||
| 220 | elif word[0] in {'I', 'U', 'Y'}: |
||
| 221 | word = 'E'+word[1:] |
||
| 222 | # Ib3 |
||
| 223 | elif word[:2] in _diph: |
||
| 224 | word = _diph[word[:2]]+word[2:] |
||
| 225 | # Ib4 |
||
| 226 | elif word[1:2] in _vows and word[0] == 'Y': |
||
| 227 | word = 'I' + word[1:] |
||
| 228 | |||
| 229 | code = '' |
||
| 230 | skip = 0 |
||
| 231 | |||
| 232 | # Rule II |
||
| 233 | for pos, char in enumerate(word): |
||
| 234 | nxch = word[pos+1:pos+2] |
||
| 235 | prev = word[pos-1:pos] |
||
| 236 | |||
| 237 | if skip: |
||
| 238 | skip -= 1 |
||
| 239 | elif char in _vows: |
||
| 240 | code += char |
||
| 241 | # IIc |
||
| 242 | elif char == nxch: |
||
| 243 | skip = 1 |
||
| 244 | code += char |
||
| 245 | elif word[pos:pos+2] in {'CQ', 'DT', 'SC'}: |
||
| 246 | continue |
||
| 247 | # IIb |
||
| 248 | elif char in _simple: |
||
| 249 | code += _simple[char] |
||
| 250 | elif char in {'C', 'G', 'P', 'Q', 'S'}: |
||
| 251 | if char == 'C': |
||
| 252 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
||
| 253 | code += 'K' |
||
| 254 | elif nxch in {'E', 'I', 'Y'}: |
||
| 255 | code += 'S' |
||
| 256 | elif nxch == 'H': |
||
| 257 | if word[pos+2:pos+3] in _vows: |
||
| 258 | code += 'C' |
||
| 259 | else: # CHR, CHL, etc. |
||
| 260 | code += 'K' |
||
| 261 | else: |
||
| 262 | code += 'C' |
||
| 263 | elif char == 'G': |
||
| 264 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
||
| 265 | code += 'G' |
||
| 266 | elif nxch in {'E', 'I', 'Y'}: |
||
| 267 | code += 'J' |
||
| 268 | elif nxch == 'N': |
||
| 269 | code += 'N' |
||
| 270 | elif char == 'P': |
||
| 271 | if nxch != 'H': |
||
| 272 | code += 'P' |
||
| 273 | else: |
||
| 274 | code += 'F' |
||
| 275 | elif char == 'Q': |
||
| 276 | if word[pos+1:pos+3] in {'UE', 'UI', 'UY'}: |
||
| 277 | code += 'G' |
||
| 278 | else: # QUA, QUO, etc. |
||
| 279 | code += 'K' |
||
| 280 | else: # S... |
||
| 281 | if word[pos:pos+6] == 'SAINTE': |
||
| 282 | code += 'X' |
||
| 283 | skip = 5 |
||
| 284 | elif word[pos:pos+5] == 'SAINT': |
||
| 285 | code += 'X' |
||
| 286 | skip = 4 |
||
| 287 | elif word[pos:pos+3] == 'STE': |
||
| 288 | code += 'X' |
||
| 289 | skip = 2 |
||
| 290 | elif word[pos:pos+2] == 'ST': |
||
| 291 | code += 'X' |
||
| 292 | skip = 1 |
||
| 293 | elif nxch in _cons: |
||
| 294 | continue |
||
| 295 | else: |
||
| 296 | code += 'S' |
||
| 297 | # IId |
||
| 298 | elif char == 'H' and prev in _cons: |
||
| 299 | continue |
||
| 300 | elif char in _cons-{'L', 'R'} and nxch in _cons-{'L', 'R'}: |
||
| 301 | continue |
||
| 302 | elif char == 'L' and nxch in {'M', 'N'}: |
||
| 303 | continue |
||
| 304 | elif char in {'M', 'N'} and prev in _vows and nxch in _cons: |
||
| 305 | continue |
||
| 306 | # IIa |
||
| 307 | else: |
||
| 308 | code += char |
||
| 309 | |||
| 310 | # IIe1 |
||
| 311 | if code[-4:] in {'AULT', 'EULT', 'OULT'}: |
||
| 312 | code = code[:-2] |
||
| 313 | # The following are blocked by rules above |
||
| 314 | # elif code[-4:-3] in _vows and code[-3:] == 'MPS': |
||
| 315 | # code = code[:-3] |
||
| 316 | # elif code[-3:-2] in _vows and code[-2:] in {'MB', 'MP', 'ND', |
||
| 317 | # 'NS', 'NT'}: |
||
| 318 | # code = code[:-2] |
||
| 319 | elif code[-2:-1] == 'R' and code[-1:] in _cons: |
||
| 320 | code = code[:-1] |
||
| 321 | # IIe2 |
||
| 322 | elif code[-2:-1] in _vows and code[-1:] in {'D', 'M', 'N', 'S', 'T'}: |
||
| 323 | code = code[:-1] |
||
| 324 | elif code[-2:] == 'ER': |
||
| 325 | code = code[:-1] |
||
| 326 | |||
| 327 | # Drop non-initial vowels |
||
| 328 | code = code[:1]+code[1:].translate({65: '', 69: '', 73: '', 79: '', 85: '', |
||
| 329 | 89: ''}) |
||
| 330 | |||
| 331 | if max_length != -1: |
||
| 332 | code = code[:max_length] |
||
| 333 | |||
| 334 | return code |
||
| 335 | |||
| 340 |