| Conditions | 18 |
| Total Lines | 97 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 38 |
| CRAP Score | 18 |
| 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_first.PSHPSoundexFirst.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 -*- |
||
| 58 | 1 | def encode(self, fname, max_length=4, german=False): |
|
| 59 | """Calculate the PSHP Soundex/Viewex Coding of a first name. |
||
| 60 | |||
| 61 | Parameters |
||
| 62 | ---------- |
||
| 63 | fname : str |
||
| 64 | The first name to encode |
||
| 65 | max_length : int |
||
| 66 | The length of the code returned (defaults to 4) |
||
| 67 | german : bool |
||
| 68 | Set to True if the name is German (different rules apply) |
||
| 69 | |||
| 70 | Returns |
||
| 71 | ------- |
||
| 72 | str |
||
| 73 | The PSHP Soundex/Viewex Coding |
||
| 74 | |||
| 75 | Examples |
||
| 76 | -------- |
||
| 77 | >>> pe = PSHPSoundexFirst() |
||
| 78 | >>> pe.encode('Smith') |
||
| 79 | 'S530' |
||
| 80 | >>> pe.encode('Waters') |
||
| 81 | 'W352' |
||
| 82 | >>> pe.encode('James') |
||
| 83 | 'J700' |
||
| 84 | >>> pe.encode('Schmidt') |
||
| 85 | 'S500' |
||
| 86 | >>> pe.encode('Ashcroft') |
||
| 87 | 'A220' |
||
| 88 | >>> pe.encode('John') |
||
| 89 | 'J500' |
||
| 90 | >>> pe.encode('Colin') |
||
| 91 | 'K400' |
||
| 92 | >>> pe.encode('Niall') |
||
| 93 | 'N400' |
||
| 94 | >>> pe.encode('Sally') |
||
| 95 | 'S400' |
||
| 96 | >>> pe.encode('Jane') |
||
| 97 | 'J500' |
||
| 98 | |||
| 99 | """ |
||
| 100 | 1 | fname = unicode_normalize('NFKD', text_type(fname.upper())) |
|
| 101 | 1 | fname = fname.replace('ß', 'SS') |
|
| 102 | 1 | fname = ''.join(c for c in fname if c in self._uc_set) |
|
| 103 | |||
| 104 | # special rules |
||
| 105 | 1 | if fname == 'JAMES': |
|
| 106 | 1 | code = 'J7' |
|
| 107 | 1 | elif fname == 'PAT': |
|
| 108 | 1 | code = 'P7' |
|
| 109 | |||
| 110 | else: |
||
| 111 | # A. Prefix treatment |
||
| 112 | 1 | if fname[:2] in {'GE', 'GI', 'GY'}: |
|
| 113 | 1 | fname = 'J' + fname[1:] |
|
| 114 | 1 | elif fname[:2] in {'CE', 'CI', 'CY'}: |
|
| 115 | 1 | fname = 'S' + fname[1:] |
|
| 116 | 1 | elif fname[:3] == 'CHR': |
|
| 117 | 1 | fname = 'K' + fname[1:] |
|
| 118 | 1 | elif fname[:1] == 'C' and fname[:2] != 'CH': |
|
| 119 | 1 | fname = 'K' + fname[1:] |
|
| 120 | |||
| 121 | 1 | if fname[:2] == 'KN': |
|
| 122 | 1 | fname = 'N' + fname[1:] |
|
| 123 | 1 | elif fname[:2] == 'PH': |
|
| 124 | 1 | fname = 'F' + fname[1:] |
|
| 125 | 1 | elif fname[:3] in {'WIE', 'WEI'}: |
|
| 126 | 1 | fname = 'V' + fname[1:] |
|
| 127 | |||
| 128 | 1 | if german and fname[:1] in {'W', 'M', 'Y', 'Z'}: |
|
| 129 | 1 | fname = {'W': 'V', 'M': 'N', 'Y': 'J', 'Z': 'S'}[ |
|
| 130 | fname[0] |
||
| 131 | ] + fname[1:] |
||
| 132 | |||
| 133 | 1 | code = fname[:1] |
|
| 134 | |||
| 135 | # B. Soundex coding |
||
| 136 | # code for Y unspecified, but presumably is 0 |
||
| 137 | 1 | fname = fname.translate(self._trans) |
|
| 138 | 1 | fname = self._delete_consecutive_repeats(fname) |
|
| 139 | |||
| 140 | 1 | code += fname[1:] |
|
| 141 | 1 | syl_ptr = code.find('0') |
|
| 142 | 1 | syl2_ptr = code[syl_ptr + 1 :].find('0') |
|
| 143 | 1 | if syl_ptr != -1 and syl2_ptr != -1 and syl2_ptr - syl_ptr > -1: |
|
| 144 | 1 | code = code[: syl_ptr + 2] |
|
| 145 | |||
| 146 | 1 | code = code.replace('0', '') # rule 1 |
|
| 147 | |||
| 148 | 1 | if max_length != -1: |
|
| 149 | 1 | if len(code) < max_length: |
|
| 150 | 1 | code += '0' * (max_length - len(code)) |
|
| 151 | else: |
||
| 152 | 1 | code = code[:max_length] |
|
| 153 | |||
| 154 | 1 | return code |
|
| 155 | |||
| 207 |