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