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