Conditions | 29 |
Total Lines | 172 |
Code Lines | 101 |
Lines | 0 |
Ratio | 0 % |
Tests | 87 |
CRAP Score | 29 |
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._es.SpanishMetaphone.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 -*- |
||
138 | 1 | def encode(self, word, max_length=6, modified=False): |
|
139 | """Return the Spanish Metaphone of a word. |
||
140 | |||
141 | Args: |
||
142 | word (str): The word to transform |
||
143 | max_length (int): The length of the code returned (defaults to 6) |
||
144 | modified (bool): Set to True to use del Pilar Angeles & |
||
145 | Bailón-Miguel's modified version of the algorithm |
||
146 | |||
147 | Returns: |
||
148 | str: The Spanish Metaphone code |
||
149 | |||
150 | Examples: |
||
151 | >>> pe = SpanishMetaphone() |
||
152 | >>> pe.encode('Perez') |
||
153 | 'PRZ' |
||
154 | >>> pe.encode('Martinez') |
||
155 | 'MRTNZ' |
||
156 | >>> pe.encode('Gutierrez') |
||
157 | 'GTRRZ' |
||
158 | >>> pe.encode('Santiago') |
||
159 | 'SNTG' |
||
160 | >>> pe.encode('Nicolás') |
||
161 | 'NKLS' |
||
162 | |||
163 | """ |
||
164 | |||
165 | 1 | def _is_vowel(pos): |
|
166 | """Return True if the character at word[pos] is a vowel. |
||
167 | |||
168 | Args: |
||
169 | pos (int): Position to check for a vowel |
||
170 | |||
171 | Returns: |
||
172 | bool: True if word[pos] is a vowel |
||
173 | |||
174 | """ |
||
175 | 1 | return pos < len(word) and word[pos] in {'A', 'E', 'I', 'O', 'U'} |
|
176 | |||
177 | 1 | word = unicode_normalize('NFC', text_type(word.upper())) |
|
178 | |||
179 | 1 | meta_key = '' |
|
180 | 1 | pos = 0 |
|
181 | |||
182 | # do some replacements for the modified version |
||
183 | 1 | if modified: |
|
184 | 1 | word = word.replace('MB', 'NB') |
|
185 | 1 | word = word.replace('MP', 'NP') |
|
186 | 1 | word = word.replace('BS', 'S') |
|
187 | 1 | if word[:2] == 'PS': |
|
188 | 1 | word = word[1:] |
|
189 | |||
190 | # simple replacements |
||
191 | 1 | word = word.replace('Á', 'A') |
|
192 | 1 | word = word.replace('CH', 'X') |
|
193 | 1 | word = word.replace('Ç', 'S') |
|
194 | 1 | word = word.replace('É', 'E') |
|
195 | 1 | word = word.replace('Í', 'I') |
|
196 | 1 | word = word.replace('Ó', 'O') |
|
197 | 1 | word = word.replace('Ú', 'U') |
|
198 | 1 | word = word.replace('Ñ', 'NY') |
|
199 | 1 | word = word.replace('GÜ', 'W') |
|
200 | 1 | word = word.replace('Ü', 'U') |
|
201 | 1 | word = word.replace('B', 'V') |
|
202 | 1 | word = word.replace('LL', 'Y') |
|
203 | |||
204 | 1 | while len(meta_key) < max_length: |
|
205 | 1 | if pos >= len(word): |
|
206 | 1 | break |
|
207 | |||
208 | # get the next character |
||
209 | 1 | current_char = word[pos] |
|
210 | |||
211 | # if a vowel in pos 0, add to key |
||
212 | 1 | if _is_vowel(pos) and pos == 0: |
|
213 | 1 | meta_key += current_char |
|
214 | 1 | pos += 1 |
|
215 | # otherwise, do consonant rules |
||
216 | else: |
||
217 | # simple consonants (unmutated) |
||
218 | 1 | if current_char in { |
|
219 | 'D', |
||
220 | 'F', |
||
221 | 'J', |
||
222 | 'K', |
||
223 | 'M', |
||
224 | 'N', |
||
225 | 'P', |
||
226 | 'T', |
||
227 | 'V', |
||
228 | 'L', |
||
229 | 'Y', |
||
230 | }: |
||
231 | 1 | meta_key += current_char |
|
232 | # skip doubled consonants |
||
233 | 1 | if word[pos + 1 : pos + 2] == current_char: |
|
234 | 1 | pos += 2 |
|
235 | else: |
||
236 | 1 | pos += 1 |
|
237 | else: |
||
238 | 1 | if current_char == 'C': |
|
239 | # special case 'acción', 'reacción',etc. |
||
240 | 1 | if word[pos + 1 : pos + 2] == 'C': |
|
241 | 1 | meta_key += 'X' |
|
242 | 1 | pos += 2 |
|
243 | # special case 'cesar', 'cien', 'cid', 'conciencia' |
||
244 | 1 | elif word[pos + 1 : pos + 2] in {'E', 'I'}: |
|
245 | 1 | meta_key += 'Z' |
|
246 | 1 | pos += 2 |
|
247 | # base case |
||
248 | else: |
||
249 | 1 | meta_key += 'K' |
|
250 | 1 | pos += 1 |
|
251 | 1 | elif current_char == 'G': |
|
252 | # special case 'gente', 'ecologia',etc |
||
253 | 1 | if word[pos + 1 : pos + 2] in {'E', 'I'}: |
|
254 | 1 | meta_key += 'J' |
|
255 | 1 | pos += 2 |
|
256 | # base case |
||
257 | else: |
||
258 | 1 | meta_key += 'G' |
|
259 | 1 | pos += 1 |
|
260 | 1 | elif current_char == 'H': |
|
261 | # since the letter 'H' is silent in Spanish, |
||
262 | # set the meta key to the vowel after the letter 'H' |
||
263 | 1 | if _is_vowel(pos + 1): |
|
264 | 1 | meta_key += word[pos + 1] |
|
265 | 1 | pos += 2 |
|
266 | else: |
||
267 | 1 | meta_key += 'H' |
|
268 | 1 | pos += 1 |
|
269 | 1 | elif current_char == 'Q': |
|
270 | 1 | if word[pos + 1 : pos + 2] == 'U': |
|
271 | 1 | pos += 2 |
|
272 | else: |
||
273 | 1 | pos += 1 |
|
274 | 1 | meta_key += 'K' |
|
275 | 1 | elif current_char == 'W': |
|
276 | 1 | meta_key += 'U' |
|
277 | 1 | pos += 1 |
|
278 | 1 | elif current_char == 'R': |
|
279 | 1 | meta_key += 'R' |
|
280 | 1 | pos += 1 |
|
281 | 1 | elif current_char == 'S': |
|
282 | 1 | if not _is_vowel(pos + 1) and pos == 0: |
|
283 | 1 | meta_key += 'ES' |
|
284 | 1 | pos += 1 |
|
285 | else: |
||
286 | 1 | meta_key += 'S' |
|
287 | 1 | pos += 1 |
|
288 | 1 | elif current_char == 'Z': |
|
289 | 1 | meta_key += 'Z' |
|
290 | 1 | pos += 1 |
|
291 | 1 | elif current_char == 'X': |
|
292 | 1 | if ( |
|
293 | len(word) > 1 |
||
294 | and pos == 0 |
||
295 | and not _is_vowel(pos + 1) |
||
296 | ): |
||
297 | 1 | meta_key += 'EX' |
|
298 | 1 | pos += 1 |
|
299 | else: |
||
300 | 1 | meta_key += 'X' |
|
301 | 1 | pos += 1 |
|
302 | else: |
||
303 | 1 | pos += 1 |
|
304 | |||
305 | # Final change from S to Z in modified version |
||
306 | 1 | if modified: |
|
307 | 1 | meta_key = meta_key.replace('S', 'Z') |
|
308 | |||
309 | 1 | return meta_key |
|
310 | |||
346 |