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