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