| Conditions | 23 |
| Total Lines | 197 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 50 |
| CRAP Score | 23 |
| 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._phonix.Phonix.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. |
||
| 225 | def encode(self, word: str) -> str: |
||
| 226 | """Return the Phonix code for a word. |
||
| 227 | |||
| 228 | Parameters |
||
| 229 | ---------- |
||
| 230 | word : str |
||
| 231 | The word to transform |
||
| 232 | |||
| 233 | Returns |
||
| 234 | ------- |
||
| 235 | str |
||
| 236 | The Phonix value |
||
| 237 | |||
| 238 | Examples |
||
| 239 | 1 | -------- |
|
| 240 | 1 | >>> pe = Phonix() |
|
| 241 | >>> pe.encode('Christopher') |
||
| 242 | 1 | 'K683' |
|
| 243 | >>> pe.encode('Niall') |
||
| 244 | 'N400' |
||
| 245 | >>> pe.encode('Smith') |
||
| 246 | 'S530' |
||
| 247 | >>> pe.encode('Schmidt') |
||
| 248 | 'S530' |
||
| 249 | |||
| 250 | |||
| 251 | .. versionadded:: 0.1.0 |
||
| 252 | .. versionchanged:: 0.3.6 |
||
| 253 | Encapsulated in class |
||
| 254 | |||
| 255 | """ |
||
| 256 | |||
| 257 | def _start_repl( |
||
| 258 | word: str, src: str, tar: str, post: Optional[Set[str]] = None |
||
| 259 | ) -> str: |
||
| 260 | """Replace src with tar at the start of word. |
||
| 261 | |||
| 262 | Parameters |
||
| 263 | ---------- |
||
| 264 | word : str |
||
| 265 | The word to modify |
||
| 266 | src : str |
||
| 267 | Substring to match |
||
| 268 | tar : str |
||
| 269 | Substring to substitute |
||
| 270 | post : set |
||
| 271 | Following characters |
||
| 272 | |||
| 273 | Returns |
||
| 274 | 1 | ------- |
|
| 275 | str |
||
| 276 | Modified string |
||
| 277 | |||
| 278 | .. versionadded:: 0.1.0 |
||
| 279 | |||
| 280 | """ |
||
| 281 | if post: |
||
| 282 | for i in post: |
||
| 283 | if word.startswith(src + i): |
||
| 284 | return tar + word[len(src) :] |
||
| 285 | elif word.startswith(src): |
||
| 286 | return tar + word[len(src) :] |
||
| 287 | return word |
||
| 288 | |||
| 289 | def _end_repl( |
||
| 290 | word: str, src: str, tar: str, pre: Optional[Set[str]] = None |
||
| 291 | ) -> str: |
||
| 292 | """Replace src with tar at the end of word. |
||
| 293 | |||
| 294 | Parameters |
||
| 295 | ---------- |
||
| 296 | 1 | word : str |
|
| 297 | 1 | The word to modify |
|
| 298 | 1 | src : str |
|
| 299 | 1 | Substring to match |
|
| 300 | 1 | tar : str |
|
| 301 | 1 | Substring to substitute |
|
| 302 | 1 | pre : set |
|
| 303 | Preceding characters |
||
| 304 | 1 | ||
| 305 | Returns |
||
| 306 | ------- |
||
| 307 | str |
||
| 308 | Modified string |
||
| 309 | |||
| 310 | .. versionadded:: 0.1.0 |
||
| 311 | |||
| 312 | """ |
||
| 313 | if pre: |
||
| 314 | for i in pre: |
||
| 315 | if word.endswith(i + src): |
||
| 316 | return word[: -len(src)] + tar |
||
| 317 | elif word.endswith(src): |
||
| 318 | return word[: -len(src)] + tar |
||
| 319 | return word |
||
| 320 | |||
| 321 | def _mid_repl( |
||
| 322 | word: str, |
||
| 323 | src: str, |
||
| 324 | tar: str, |
||
| 325 | pre: Optional[Set[str]] = None, |
||
| 326 | 1 | post: Optional[Set[str]] = None, |
|
| 327 | 1 | ) -> str: |
|
| 328 | 1 | """Replace src with tar in the middle of word. |
|
| 329 | 1 | ||
| 330 | 1 | Parameters |
|
| 331 | 1 | ---------- |
|
| 332 | 1 | word : str |
|
| 333 | The word to modify |
||
| 334 | 1 | src : str |
|
| 335 | Substring to match |
||
| 336 | tar : str |
||
| 337 | Substring to substitute |
||
| 338 | pre : set |
||
| 339 | Preceding characters |
||
| 340 | post : set |
||
| 341 | Following characters |
||
| 342 | |||
| 343 | Returns |
||
| 344 | ------- |
||
| 345 | str |
||
| 346 | Modified string |
||
| 347 | |||
| 348 | .. versionadded:: 0.1.0 |
||
| 349 | |||
| 350 | """ |
||
| 351 | if pre or post: |
||
| 352 | if not pre: |
||
| 353 | return word[0] + _all_repl(word[1:], src, tar, pre, post) |
||
| 354 | elif not post: |
||
| 355 | return _all_repl(word[:-1], src, tar, pre, post) + word[-1] |
||
| 356 | return _all_repl(word, src, tar, pre, post) |
||
| 357 | return ( |
||
| 358 | 1 | word[0] + _all_repl(word[1:-1], src, tar, pre, post) + word[-1] |
|
| 359 | 1 | ) |
|
| 360 | 1 | ||
| 361 | 1 | def _all_repl( |
|
| 362 | 1 | word: str, |
|
| 363 | 1 | src: str, |
|
| 364 | 1 | tar: str, |
|
| 365 | pre: Optional[Set[str]] = None, |
||
| 366 | post: Optional[Set[str]] = None, |
||
| 367 | ) -> str: |
||
| 368 | 1 | """Replace src with tar anywhere in word. |
|
| 369 | |||
| 370 | Parameters |
||
| 371 | ---------- |
||
| 372 | word : str |
||
| 373 | The word to modify |
||
| 374 | src : str |
||
| 375 | Substring to match |
||
| 376 | tar : str |
||
| 377 | Substring to substitute |
||
| 378 | pre : set |
||
| 379 | Preceding characters |
||
| 380 | post : set |
||
| 381 | Following characters |
||
| 382 | |||
| 383 | Returns |
||
| 384 | ------- |
||
| 385 | str |
||
| 386 | Modified string |
||
| 387 | |||
| 388 | .. versionadded:: 0.1.0 |
||
| 389 | |||
| 390 | """ |
||
| 391 | if pre or post: |
||
| 392 | 1 | post = post if post else {''} |
|
| 393 | 1 | pre = pre if pre else {''} |
|
| 394 | 1 | ||
| 395 | for i, j in ((i, j) for i in pre for j in post): |
||
| 396 | 1 | word = word.replace(i + src + j, i + tar + j) |
|
| 397 | 1 | return word |
|
| 398 | 1 | else: |
|
| 399 | return word.replace(src, tar) |
||
| 400 | 1 | ||
| 401 | repl_at = (_start_repl, _end_repl, _mid_repl, _all_repl) |
||
| 402 | 1 | ||
| 403 | 1 | sdx = '' |
|
| 404 | 1 | ||
| 405 | word = unicode_normalize('NFKD', word.upper()) |
||
| 406 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
| 407 | if word: |
||
| 408 | 1 | for trans in self._substitutions: |
|
| 409 | word = repl_at[trans[0]](word, *trans[1:]) |
||
| 410 | 1 | if word[0] in self._uc_vy_set: |
|
| 411 | sdx = 'v' + word[1:].translate(self._trans) |
||
| 412 | 1 | else: |
|
| 413 | 1 | sdx = word[0] + word[1:].translate(self._trans) |
|
| 414 | 1 | sdx = self._delete_consecutive_repeats(sdx) |
|
| 415 | 1 | sdx = sdx.replace('0', '') |
|
| 416 | 1 | ||
| 417 | 1 | if self._zero_pad: |
|
| 418 | 1 | sdx += '0' * self._max_length |
|
| 419 | 1 | if not sdx: |
|
| 420 | sdx = '0' |
||
| 421 | 1 | return sdx[: self._max_length] |
|
| 422 | 1 | ||
| 428 |