Conditions | 33 |
Total Lines | 100 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Tests | 50 |
CRAP Score | 33 |
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.distance._guth.Guth.sim() 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 | # Copyright 2019-2020 by Christopher C. Little. |
||
170 | 1 | def sim(self, src: str, tar: str) -> float: |
|
171 | """Return the relative Guth similarity of two strings. |
||
172 | 1 | ||
173 | 1 | This deviates from the algorithm described in :cite:`Guth:1976` in that |
|
174 | more distant matches are penalized, so that less similar terms score |
||
175 | 1 | lower that more similar terms. |
|
176 | |||
177 | If no match is found for a particular token in the source string, this |
||
178 | does not result in an automatic 0.0 score. Rather, the score is further |
||
179 | penalized towards 0.0. |
||
180 | |||
181 | Parameters |
||
182 | ---------- |
||
183 | src : str |
||
184 | Source string for comparison |
||
185 | tar : str |
||
186 | Target string for comparison |
||
187 | |||
188 | Returns |
||
189 | ------- |
||
190 | float |
||
191 | Relative Guth matching score |
||
192 | |||
193 | Examples |
||
194 | -------- |
||
195 | >>> cmp = Guth() |
||
196 | >>> cmp.sim('cat', 'hat') |
||
197 | 0.8666666666666667 |
||
198 | >>> cmp.sim('Niall', 'Neil') |
||
199 | 0.8800000000000001 |
||
200 | >>> cmp.sim('aluminum', 'Catalan') |
||
201 | 0.4 |
||
202 | >>> cmp.sim('ATCG', 'TAGC') |
||
203 | 0.8 |
||
204 | |||
205 | |||
206 | .. versionadded:: 0.4.1 |
||
207 | |||
208 | """ |
||
209 | if src == tar: |
||
210 | return 1.0 |
||
211 | if not src or not tar: |
||
212 | return 0.0 |
||
213 | |||
214 | 1 | if self.params['tokenizer']: |
|
215 | 1 | src = self.params['tokenizer'].tokenize(src).get_list() |
|
216 | 1 | tar = self.params['tokenizer'].tokenize(tar).get_list() |
|
217 | 1 | ||
218 | score = 0.0 |
||
219 | 1 | for pos in range(len(src)): |
|
220 | 1 | s = self._token_at(src, pos) |
|
221 | 1 | t = self._token_at(tar, pos) |
|
222 | if s and t and s == t: |
||
223 | 1 | score += 1.0 |
|
224 | 1 | continue |
|
225 | 1 | ||
226 | 1 | t = self._token_at(tar, pos + 1) |
|
227 | 1 | if s and t and s == t: |
|
228 | 1 | score += 0.8 |
|
229 | 1 | continue |
|
230 | |||
231 | 1 | t = self._token_at(tar, pos + 2) |
|
232 | 1 | if s and t and s == t: |
|
233 | 1 | score += 0.6 |
|
234 | 1 | continue |
|
235 | |||
236 | 1 | t = self._token_at(tar, pos - 1) |
|
237 | 1 | if s and t and s == t: |
|
238 | 1 | score += 0.8 |
|
239 | 1 | continue |
|
240 | |||
241 | 1 | s = self._token_at(src, pos - 1) |
|
242 | 1 | t = self._token_at(tar, pos) |
|
243 | 1 | if s and t and s == t: |
|
244 | 1 | score += 0.8 |
|
245 | continue |
||
246 | 1 | ||
247 | 1 | s = self._token_at(src, pos + 1) |
|
248 | 1 | if s and t and s == t: |
|
249 | 1 | score += 0.8 |
|
250 | 1 | continue |
|
251 | |||
252 | 1 | s = self._token_at(src, pos + 2) |
|
253 | 1 | if s and t and s == t: |
|
254 | 1 | score += 0.6 |
|
255 | 1 | continue |
|
256 | |||
257 | 1 | s = self._token_at(src, pos + 1) |
|
258 | 1 | t = self._token_at(tar, pos + 1) |
|
259 | 1 | if s and t and s == t: |
|
260 | 1 | score += 0.6 |
|
261 | continue |
||
262 | 1 | ||
263 | 1 | s = self._token_at(src, pos + 2) |
|
264 | 1 | t = self._token_at(tar, pos + 2) |
|
265 | 1 | if s and t and s == t: |
|
266 | 1 | score += 0.2 |
|
267 | continue |
||
268 | 1 | ||
269 | 1 | return score / len(src) |
|
270 | 1 | ||
276 |