Conditions | 36 |
Total Lines | 147 |
Code Lines | 85 |
Lines | 0 |
Ratio | 0 % |
Tests | 80 |
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 | # -*- coding: utf-8 -*- |
||
58 | 1 | def encode(self, lname, max_length=4, german=False): |
|
59 | """Calculate the PSHP Soundex/Viewex Coding of a last name. |
||
60 | |||
61 | Parameters |
||
62 | ---------- |
||
63 | lname : str |
||
64 | The last 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 = PSHPSoundexLast() |
||
78 | >>> pe.encode('Smith') |
||
79 | 'S530' |
||
80 | >>> pe.encode('Waters') |
||
81 | 'W350' |
||
82 | >>> pe.encode('James') |
||
83 | 'J500' |
||
84 | >>> pe.encode('Schmidt') |
||
85 | 'S530' |
||
86 | >>> pe.encode('Ashcroft') |
||
87 | 'A225' |
||
88 | |||
89 | """ |
||
90 | 1 | lname = unicode_normalize('NFKD', text_type(lname.upper())) |
|
91 | 1 | lname = lname.replace('ß', 'SS') |
|
92 | 1 | lname = ''.join(c for c in lname if c in self._uc_set) |
|
93 | |||
94 | # A. Prefix treatment |
||
95 | 1 | if lname[:3] == 'VON' or lname[:3] == 'VAN': |
|
96 | 1 | lname = lname[3:].strip() |
|
97 | |||
98 | # The rule implemented below says "MC, MAC become 1". I believe it |
||
99 | # meant to say they become M except in German data (where superscripted |
||
100 | # 1 indicates "except in German data"). It doesn't make sense for them |
||
101 | # to become 1 (BPFV -> 1) or to apply outside German. Unfortunately, |
||
102 | # both articles have this error(?). |
||
103 | 1 | if not german: |
|
104 | 1 | if lname[:3] == 'MAC': |
|
105 | 1 | lname = 'M' + lname[3:] |
|
106 | 1 | elif lname[:2] == 'MC': |
|
107 | 1 | lname = 'M' + lname[2:] |
|
108 | |||
109 | # The non-German-only rule to strip ' is unnecessary due to filtering |
||
110 | |||
111 | 1 | if lname[:1] in {'E', 'I', 'O', 'U'}: |
|
112 | 1 | lname = 'A' + lname[1:] |
|
113 | 1 | elif lname[:2] in {'GE', 'GI', 'GY'}: |
|
114 | 1 | lname = 'J' + lname[1:] |
|
115 | 1 | elif lname[:2] in {'CE', 'CI', 'CY'}: |
|
116 | 1 | lname = 'S' + lname[1:] |
|
117 | 1 | elif lname[:3] == 'CHR': |
|
118 | 1 | lname = 'K' + lname[1:] |
|
119 | 1 | elif lname[:1] == 'C' and lname[:2] != 'CH': |
|
120 | 1 | lname = 'K' + lname[1:] |
|
121 | |||
122 | 1 | if lname[:2] == 'KN': |
|
123 | 1 | lname = 'N' + lname[1:] |
|
124 | 1 | elif lname[:2] == 'PH': |
|
125 | 1 | lname = 'F' + lname[1:] |
|
126 | 1 | elif lname[:3] in {'WIE', 'WEI'}: |
|
127 | 1 | lname = 'V' + lname[1:] |
|
128 | |||
129 | 1 | if german and lname[:1] in {'W', 'M', 'Y', 'Z'}: |
|
130 | 1 | lname = {'W': 'V', 'M': 'N', 'Y': 'J', 'Z': 'S'}[lname[0]] + lname[ |
|
131 | 1: |
||
132 | ] |
||
133 | |||
134 | 1 | code = lname[:1] |
|
135 | |||
136 | # B. Postfix treatment |
||
137 | 1 | if german: # moved from end of postfix treatment due to blocking |
|
138 | 1 | if lname[-3:] == 'TES': |
|
139 | 1 | lname = lname[:-3] |
|
140 | 1 | elif lname[-2:] == 'TS': |
|
141 | 1 | lname = lname[:-2] |
|
142 | 1 | if lname[-3:] == 'TZE': |
|
143 | 1 | lname = lname[:-3] |
|
144 | 1 | elif lname[-2:] == 'ZE': |
|
145 | 1 | lname = lname[:-2] |
|
146 | 1 | if lname[-1:] == 'Z': |
|
147 | 1 | lname = lname[:-1] |
|
148 | 1 | elif lname[-2:] == 'TE': |
|
149 | 1 | lname = lname[:-2] |
|
150 | |||
151 | 1 | if lname[-1:] == 'R': |
|
152 | 1 | lname = lname[:-1] + 'N' |
|
153 | 1 | elif lname[-2:] in {'SE', 'CE'}: |
|
154 | 1 | lname = lname[:-2] |
|
155 | 1 | if lname[-2:] == 'SS': |
|
156 | 1 | lname = lname[:-2] |
|
157 | 1 | elif lname[-1:] == 'S': |
|
158 | 1 | lname = lname[:-1] |
|
159 | |||
160 | 1 | if not german: |
|
161 | 1 | l5_repl = {'STOWN': 'SAWON', 'MPSON': 'MASON'} |
|
162 | 1 | l4_repl = { |
|
163 | 'NSEN': 'ASEN', |
||
164 | 'MSON': 'ASON', |
||
165 | 'STEN': 'SAEN', |
||
166 | 'STON': 'SAON', |
||
167 | } |
||
168 | 1 | if lname[-5:] in l5_repl: |
|
169 | 1 | lname = lname[:-5] + l5_repl[lname[-5:]] |
|
170 | 1 | elif lname[-4:] in l4_repl: |
|
171 | 1 | lname = lname[:-4] + l4_repl[lname[-4:]] |
|
172 | |||
173 | 1 | if lname[-2:] in {'NG', 'ND'}: |
|
174 | 1 | lname = lname[:-1] |
|
175 | 1 | if not german and lname[-3:] in {'GAN', 'GEN'}: |
|
176 | 1 | lname = lname[:-3] + 'A' + lname[-2:] |
|
177 | |||
178 | # C. Infix Treatment |
||
179 | 1 | lname = lname.replace('CK', 'C') |
|
180 | 1 | lname = lname.replace('SCH', 'S') |
|
181 | 1 | lname = lname.replace('DT', 'T') |
|
182 | 1 | lname = lname.replace('ND', 'N') |
|
183 | 1 | lname = lname.replace('NG', 'N') |
|
184 | 1 | lname = lname.replace('LM', 'M') |
|
185 | 1 | lname = lname.replace('MN', 'M') |
|
186 | 1 | lname = lname.replace('WIE', 'VIE') |
|
187 | 1 | lname = lname.replace('WEI', 'VEI') |
|
188 | |||
189 | # D. Soundexing |
||
190 | # code for X & Y are unspecified, but presumably are 2 & 0 |
||
191 | |||
192 | 1 | lname = lname.translate(self._trans) |
|
193 | 1 | lname = self._delete_consecutive_repeats(lname) |
|
194 | |||
195 | 1 | code += lname[1:] |
|
196 | 1 | code = code.replace('0', '') # rule 1 |
|
197 | |||
198 | 1 | if max_length != -1: |
|
199 | 1 | if len(code) < max_length: |
|
200 | 1 | code += '0' * (max_length - len(code)) |
|
201 | else: |
||
202 | 1 | code = code[:max_length] |
|
203 | |||
204 | 1 | return code |
|
205 | |||
247 |