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