Conditions | 80 |
Total Lines | 205 |
Code Lines | 128 |
Lines | 0 |
Ratio | 0 % |
Tests | 99 |
CRAP Score | 80 |
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._metaphone.Metaphone.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=-1): |
|
51 | """Return the Metaphone code for a word. |
||
52 | |||
53 | Based on Lawrence Philips' Pick BASIC code from 1990 |
||
54 | :cite:`Philips:1990`, as described in :cite:`Philips:1990b`. |
||
55 | This incorporates some corrections to the above code, particularly |
||
56 | some of those suggested by Michael Kuhn in :cite:`Kuhn:1995`. |
||
57 | |||
58 | Parameters |
||
59 | ---------- |
||
60 | word : str |
||
61 | The word to transform |
||
62 | max_length : int |
||
63 | The maximum length of the returned Metaphone code (defaults to 64, |
||
64 | but in Philips' original implementation this was 4) |
||
65 | |||
66 | Returns |
||
67 | ------- |
||
68 | str |
||
69 | The Metaphone value |
||
70 | |||
71 | Examples |
||
72 | -------- |
||
73 | >>> pe = Metaphone() |
||
74 | >>> pe.encode('Christopher') |
||
75 | 'KRSTFR' |
||
76 | >>> pe.encode('Niall') |
||
77 | 'NL' |
||
78 | >>> pe.encode('Smith') |
||
79 | 'SM0' |
||
80 | >>> pe.encode('Schmidt') |
||
81 | 'SKMTT' |
||
82 | |||
83 | """ |
||
84 | # Require a max_length of at least 4 |
||
85 | 1 | if max_length != -1: |
|
86 | 1 | max_length = max(4, max_length) |
|
87 | else: |
||
88 | 1 | max_length = 64 |
|
89 | |||
90 | # As in variable sound--those modified by adding an "h" |
||
91 | 1 | ename = ''.join(c for c in word.upper() if c.isalnum()) |
|
92 | 1 | ename = ename.replace('ß', 'SS') |
|
93 | |||
94 | # Delete non-alphanumeric characters and make all caps |
||
95 | 1 | if not ename: |
|
96 | 1 | return '' |
|
97 | 1 | if ename[0:2] in {'PN', 'AE', 'KN', 'GN', 'WR'}: |
|
98 | 1 | ename = ename[1:] |
|
99 | 1 | elif ename[0] == 'X': |
|
100 | 1 | ename = 'S' + ename[1:] |
|
101 | 1 | elif ename[0:2] == 'WH': |
|
102 | 1 | ename = 'W' + ename[2:] |
|
103 | |||
104 | # Convert to metaphone |
||
105 | 1 | elen = len(ename) - 1 |
|
106 | 1 | metaph = '' |
|
107 | 1 | for i in range(len(ename)): |
|
108 | 1 | if len(metaph) >= max_length: |
|
109 | 1 | break |
|
110 | 1 | if ( |
|
111 | ename[i] not in {'G', 'T'} |
||
112 | and i > 0 |
||
113 | and ename[i - 1] == ename[i] |
||
114 | ): |
||
115 | 1 | continue |
|
116 | |||
117 | 1 | if ename[i] in self._uc_v_set and i == 0: |
|
118 | 1 | metaph = ename[i] |
|
119 | |||
120 | 1 | elif ename[i] == 'B': |
|
121 | 1 | if i != elen or ename[i - 1] != 'M': |
|
122 | 1 | metaph += ename[i] |
|
123 | |||
124 | 1 | elif ename[i] == 'C': |
|
125 | 1 | if not ( |
|
126 | i > 0 |
||
127 | and ename[i - 1] == 'S' |
||
128 | and ename[i + 1 : i + 2] in self._frontv |
||
129 | ): |
||
130 | 1 | if ename[i + 1 : i + 3] == 'IA': |
|
131 | 1 | metaph += 'X' |
|
132 | 1 | elif ename[i + 1 : i + 2] in self._frontv: |
|
133 | 1 | metaph += 'S' |
|
134 | 1 | elif i > 0 and ename[i - 1 : i + 2] == 'SCH': |
|
135 | 1 | metaph += 'K' |
|
136 | 1 | elif ename[i + 1 : i + 2] == 'H': |
|
137 | 1 | if ( |
|
138 | i == 0 |
||
139 | and i + 1 < elen |
||
140 | and ename[i + 2 : i + 3] not in self._uc_v_set |
||
141 | ): |
||
142 | 1 | metaph += 'K' |
|
143 | else: |
||
144 | 1 | metaph += 'X' |
|
145 | else: |
||
146 | 1 | metaph += 'K' |
|
147 | |||
148 | 1 | elif ename[i] == 'D': |
|
149 | 1 | if ( |
|
150 | ename[i + 1 : i + 2] == 'G' |
||
151 | and ename[i + 2 : i + 3] in self._frontv |
||
152 | ): |
||
153 | 1 | metaph += 'J' |
|
154 | else: |
||
155 | 1 | metaph += 'T' |
|
156 | |||
157 | 1 | elif ename[i] == 'G': |
|
158 | 1 | if ename[i + 1 : i + 2] == 'H' and not ( |
|
159 | i + 1 == elen or ename[i + 2 : i + 3] not in self._uc_v_set |
||
160 | ): |
||
161 | 1 | continue |
|
162 | 1 | elif i > 0 and ( |
|
163 | (i + 1 == elen and ename[i + 1] == 'N') |
||
164 | or (i + 3 == elen and ename[i + 1 : i + 4] == 'NED') |
||
165 | ): |
||
166 | 1 | continue |
|
167 | 1 | elif ( |
|
168 | i - 1 > 0 |
||
169 | and i + 1 <= elen |
||
170 | and ename[i - 1] == 'D' |
||
171 | and ename[i + 1] in self._frontv |
||
172 | ): |
||
173 | 1 | continue |
|
174 | 1 | elif ename[i + 1 : i + 2] == 'G': |
|
175 | 1 | continue |
|
176 | 1 | elif ename[i + 1 : i + 2] in self._frontv: |
|
177 | 1 | if i == 0 or ename[i - 1] != 'G': |
|
178 | 1 | metaph += 'J' |
|
179 | else: |
||
180 | 1 | metaph += 'K' |
|
181 | else: |
||
182 | 1 | metaph += 'K' |
|
183 | |||
184 | 1 | elif ename[i] == 'H': |
|
185 | 1 | if ( |
|
186 | i > 0 |
||
187 | and ename[i - 1] in self._uc_v_set |
||
188 | and ename[i + 1 : i + 2] not in self._uc_v_set |
||
189 | ): |
||
190 | 1 | continue |
|
191 | 1 | elif i > 0 and ename[i - 1] in self._varson: |
|
192 | 1 | continue |
|
193 | else: |
||
194 | 1 | metaph += 'H' |
|
195 | |||
196 | 1 | elif ename[i] in {'F', 'J', 'L', 'M', 'N', 'R'}: |
|
197 | 1 | metaph += ename[i] |
|
198 | |||
199 | 1 | elif ename[i] == 'K': |
|
200 | 1 | if i > 0 and ename[i - 1] == 'C': |
|
201 | 1 | continue |
|
202 | else: |
||
203 | 1 | metaph += 'K' |
|
204 | |||
205 | 1 | elif ename[i] == 'P': |
|
206 | 1 | if ename[i + 1 : i + 2] == 'H': |
|
207 | 1 | metaph += 'F' |
|
208 | else: |
||
209 | 1 | metaph += 'P' |
|
210 | |||
211 | 1 | elif ename[i] == 'Q': |
|
212 | 1 | metaph += 'K' |
|
213 | |||
214 | 1 | elif ename[i] == 'S': |
|
215 | 1 | if ( |
|
216 | i > 0 |
||
217 | and i + 2 <= elen |
||
218 | and ename[i + 1] == 'I' |
||
219 | and ename[i + 2] in 'OA' |
||
220 | ): |
||
221 | 1 | metaph += 'X' |
|
222 | 1 | elif ename[i + 1 : i + 2] == 'H': |
|
223 | 1 | metaph += 'X' |
|
224 | else: |
||
225 | 1 | metaph += 'S' |
|
226 | |||
227 | 1 | elif ename[i] == 'T': |
|
228 | 1 | if ( |
|
229 | i > 0 |
||
230 | and i + 2 <= elen |
||
231 | and ename[i + 1] == 'I' |
||
232 | and ename[i + 2] in {'A', 'O'} |
||
233 | ): |
||
234 | 1 | metaph += 'X' |
|
235 | 1 | elif ename[i + 1 : i + 2] == 'H': |
|
236 | 1 | metaph += '0' |
|
237 | 1 | elif ename[i + 1 : i + 3] != 'CH': |
|
238 | 1 | if ename[i - 1 : i] != 'T': |
|
239 | 1 | metaph += 'T' |
|
240 | |||
241 | 1 | elif ename[i] == 'V': |
|
242 | 1 | metaph += 'F' |
|
243 | |||
244 | 1 | elif ename[i] in 'WY': |
|
245 | 1 | if ename[i + 1 : i + 2] in self._uc_v_set: |
|
246 | 1 | metaph += ename[i] |
|
247 | |||
248 | 1 | elif ename[i] == 'X': |
|
249 | 1 | metaph += 'KS' |
|
250 | |||
251 | 1 | elif ename[i] == 'Z': |
|
252 | 1 | metaph += 'S' |
|
253 | |||
254 | 1 | return metaph |
|
255 | |||
294 |