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