Conditions | 32 |
Total Lines | 200 |
Code Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Tests | 76 |
CRAP Score | 32 |
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._dolby.Dolby.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 2018-2020 by Christopher C. Little. |
||
97 | def encode(self, word: str) -> str: |
||
98 | """Return the Dolby Code of a name. |
||
99 | |||
100 | Parameters |
||
101 | ---------- |
||
102 | word : str |
||
103 | The word to transform |
||
104 | 1 | ||
105 | Returns |
||
106 | 1 | ------- |
|
107 | str |
||
108 | The Dolby Code |
||
109 | |||
110 | Examples |
||
111 | -------- |
||
112 | >>> pe = Dolby() |
||
113 | >>> pe.encode('Hansen') |
||
114 | 'H*NSN' |
||
115 | >>> pe.encode('Larsen') |
||
116 | 'L*RSN' |
||
117 | >>> pe.encode('Aagaard') |
||
118 | '*GR' |
||
119 | >>> pe.encode('Braaten') |
||
120 | 'BR*DN' |
||
121 | >>> pe.encode('Sandvik') |
||
122 | 'S*NVK' |
||
123 | |||
124 | >>> pe_6 = Dolby(max_length=6) |
||
125 | >>> pe_6.encode('Hansen') |
||
126 | 'H*NS*N' |
||
127 | >>> pe_6.encode('Larsen') |
||
128 | 'L*RS*N' |
||
129 | >>> pe_6.encode('Aagaard') |
||
130 | '*G*R ' |
||
131 | >>> pe_6.encode('Braaten') |
||
132 | 'BR*D*N' |
||
133 | >>> pe_6.encode('Sandvik') |
||
134 | 'S*NF*K' |
||
135 | |||
136 | >>> pe.encode('Smith') |
||
137 | 'SM*D' |
||
138 | >>> pe.encode('Waters') |
||
139 | 'W*DRS' |
||
140 | >>> pe.encode('James') |
||
141 | 'J*MS' |
||
142 | >>> pe.encode('Schmidt') |
||
143 | 'SM*D' |
||
144 | >>> pe.encode('Ashcroft') |
||
145 | '*SKRFD' |
||
146 | |||
147 | >>> pe_6.encode('Smith') |
||
148 | 'SM*D ' |
||
149 | >>> pe_6.encode('Waters') |
||
150 | 'W*D*RS' |
||
151 | >>> pe_6.encode('James') |
||
152 | 'J*M*S ' |
||
153 | >>> pe_6.encode('Schmidt') |
||
154 | 'SM*D ' |
||
155 | >>> pe_6.encode('Ashcroft') |
||
156 | '*SKRFD' |
||
157 | |||
158 | |||
159 | .. versionadded:: 0.3.0 |
||
160 | .. versionchanged:: 0.3.6 |
||
161 | Encapsulated in class |
||
162 | |||
163 | """ |
||
164 | # uppercase, normalize, decompose, and filter non-A-Z out |
||
165 | word = unicode_normalize('NFKD', word.upper()) |
||
166 | word = ''.join(c for c in word if c in self._uc_set) |
||
167 | |||
168 | # Rule 1 (FL2) |
||
169 | if word[:3] in {'MCG', 'MAG', 'MAC'}: |
||
170 | word = 'MK' + word[3:] |
||
171 | elif word[:2] == 'MC': |
||
172 | word = 'MK' + word[2:] |
||
173 | |||
174 | 1 | # Rule 2 (FL3) |
|
175 | 1 | pos = len(word) - 2 |
|
176 | 1 | while pos > -1: |
|
177 | if word[pos : pos + 2] in { |
||
178 | 'DT', |
||
179 | 1 | 'LD', |
|
180 | 1 | 'ND', |
|
181 | 1 | 'NT', |
|
182 | 1 | 'RC', |
|
183 | 'RD', |
||
184 | 'RT', |
||
185 | 1 | 'SC', |
|
186 | 1 | 'SK', |
|
187 | 1 | 'ST', |
|
188 | }: |
||
189 | word = word[: pos + 1] + word[pos + 2 :] |
||
190 | pos += 1 |
||
191 | pos -= 1 |
||
192 | |||
193 | # Rule 3 (FL4) |
||
194 | # Although the rule indicates "after the first letter", the test cases |
||
195 | # make it clear that these apply to the first letter also. |
||
196 | word = word.replace('X', 'KS') |
||
197 | word = word.replace('CE', 'SE') |
||
198 | word = word.replace('CI', 'SI') |
||
199 | 1 | word = word.replace('CY', 'SI') |
|
200 | 1 | ||
201 | 1 | # not in the rule set, but they seem to have intended it |
|
202 | word = word.replace('TCH', 'CH') |
||
203 | |||
204 | pos = word.find('CH', 1) |
||
205 | while pos != -1: |
||
206 | 1 | if word[pos - 1 : pos] not in self._uc_vy_set: |
|
207 | 1 | word = word[:pos] + 'S' + word[pos + 1 :] |
|
208 | 1 | pos = word.find('CH', pos + 1) |
|
209 | 1 | ||
210 | word = word.replace('C', 'K') |
||
211 | word = word.replace('Z', 'S') |
||
212 | 1 | ||
213 | word = word.replace('WR', 'R') |
||
214 | 1 | word = word.replace('DG', 'G') |
|
215 | 1 | word = word.replace('QU', 'K') |
|
216 | 1 | word = word.replace('T', 'D') |
|
217 | 1 | word = word.replace('PH', 'F') |
|
218 | 1 | ||
219 | # Rule 4 (FL5) |
||
220 | 1 | # Although the rule indicates "after the first letter", the test cases |
|
221 | 1 | # make it clear that these apply to the first letter also. |
|
222 | pos = word.find('K', 0) |
||
223 | 1 | while pos != -1: |
|
224 | 1 | if pos > 1 and word[pos - 1 : pos] not in self._uc_vy_set | { |
|
225 | 1 | 'L', |
|
226 | 1 | 'N', |
|
227 | 1 | 'R', |
|
228 | }: |
||
229 | word = word[: pos - 1] + word[pos:] |
||
230 | pos -= 1 |
||
231 | pos = word.find('K', pos + 1) |
||
232 | 1 | ||
233 | 1 | # Rule FL6 |
|
234 | 1 | if self._max_length > 0 and word[-1:] == 'E': |
|
235 | word = word[:-1] |
||
236 | |||
237 | # Rule 5 (FL7) |
||
238 | word = self._delete_consecutive_repeats(word) |
||
239 | 1 | ||
240 | 1 | # Rule 6 (FL8) |
|
241 | 1 | if word[:2] == 'PF': |
|
242 | word = word[1:] |
||
243 | if word[-2:] == 'PF': |
||
244 | 1 | word = word[:-1] |
|
245 | 1 | elif word[-2:] == 'GH': |
|
246 | if word[-3:-2] in self._uc_vy_set: |
||
247 | word = word[:-2] + 'F' |
||
248 | 1 | else: |
|
249 | word = word[:-2] + 'G' |
||
250 | word = word.replace('GH', '') |
||
251 | 1 | ||
252 | 1 | # Rule FL9 |
|
253 | 1 | if self._max_length > 0: |
|
254 | 1 | word = word.replace('V', 'F') |
|
255 | 1 | ||
256 | 1 | # Rules 7-9 (FL10-FL12) |
|
257 | 1 | first = 1 + (1 if self._max_length > 0 else 0) |
|
258 | code = '' |
||
259 | 1 | for pos, char in enumerate(word): |
|
260 | 1 | if char in self._uc_vy_set: |
|
261 | if first or self._keep_vowels: |
||
262 | code += self._vowel_char |
||
263 | 1 | first -= 1 |
|
264 | 1 | elif pos > 0 and char in {'W', 'H'}: |
|
265 | continue |
||
266 | else: |
||
267 | 1 | code += char |
|
268 | 1 | ||
269 | 1 | if self._max_length > 0: |
|
270 | 1 | # Rule FL13 |
|
271 | 1 | if len(code) > self._max_length and code[-1:] == 'S': |
|
272 | 1 | code = code[:-1] |
|
273 | 1 | if self._keep_vowels: |
|
274 | 1 | code = code[: self._max_length] |
|
275 | 1 | else: |
|
276 | # Rule FL14 |
||
277 | 1 | code = code[: self._max_length + 2] |
|
278 | # Rule FL15 |
||
279 | 1 | while len(code) > self._max_length: |
|
280 | vowels = len(code) - self._max_length |
||
281 | 1 | excess = vowels - 1 |
|
282 | 1 | word = code |
|
283 | 1 | code = '' |
|
284 | 1 | for char in word: |
|
285 | if char == self._vowel_char: |
||
286 | if vowels: |
||
287 | 1 | code += char |
|
288 | vowels -= 1 |
||
289 | 1 | else: |
|
290 | 1 | code += char |
|
291 | 1 | code = code[: self._max_length + excess] |
|
292 | 1 | ||
293 | 1 | # Rule FL16 |
|
294 | 1 | code += ' ' * (self._max_length - len(code)) |
|
295 | 1 | ||
296 | 1 | return code |
|
297 | 1 | ||
303 |