Conditions | 31 |
Total Lines | 143 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
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.jaro.sim_jaro_winkler() 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 -*- |
||
218 | def sim_jaro_winkler(src, tar, qval=1, mode='winkler', long_strings=False, |
||
219 | boost_threshold=0.7, scaling_factor=0.1): |
||
220 | """Return the Jaro or Jaro-Winkler similarity of two strings. |
||
221 | |||
222 | Jaro(-Winkler) distance is a string edit distance initially proposed by |
||
223 | Jaro and extended by Winkler :cite:`Jaro:1989,Winkler:1990`. |
||
224 | |||
225 | This is Python based on the C code for strcmp95: |
||
226 | http://web.archive.org/web/20110629121242/http://www.census.gov/geo/msb/stand/strcmp.c |
||
227 | :cite:`Winkler:1994`. The above file is a US Government publication and, |
||
228 | accordingly, in the public domain. |
||
229 | |||
230 | :param str src: source string for comparison |
||
231 | :param str tar: target string for comparison |
||
232 | :param int qval: the length of each q-gram (defaults to 1: character-wise |
||
233 | matching) |
||
234 | :param str mode: indicates which variant of this distance metric to |
||
235 | compute: |
||
236 | |||
237 | - 'winkler' -- computes the Jaro-Winkler distance (default) which |
||
238 | increases the score for matches near the start of the word |
||
239 | - 'jaro' -- computes the Jaro distance |
||
240 | |||
241 | The following arguments apply only when mode is 'winkler': |
||
242 | |||
243 | :param bool long_strings: set to True to "Increase the probability of a |
||
244 | match when the number of matched characters is large. This option |
||
245 | allows for a little more tolerance when the strings are large. It is |
||
246 | not an appropriate test when comparing fixed length fields such as |
||
247 | phone and social security numbers." |
||
248 | :param float boost_threshold: a value between 0 and 1, below which the |
||
249 | Winkler boost is not applied (defaults to 0.7) |
||
250 | :param float scaling_factor: a value between 0 and 0.25, indicating by how |
||
251 | much to boost scores for matching prefixes (defaults to 0.1) |
||
252 | |||
253 | :returns: Jaro or Jaro-Winkler similarity |
||
254 | :rtype: float |
||
255 | |||
256 | >>> round(sim_jaro_winkler('cat', 'hat'), 12) |
||
257 | 0.777777777778 |
||
258 | >>> round(sim_jaro_winkler('Niall', 'Neil'), 12) |
||
259 | 0.805 |
||
260 | >>> round(sim_jaro_winkler('aluminum', 'Catalan'), 12) |
||
261 | 0.60119047619 |
||
262 | >>> round(sim_jaro_winkler('ATCG', 'TAGC'), 12) |
||
263 | 0.833333333333 |
||
264 | |||
265 | >>> round(sim_jaro_winkler('cat', 'hat', mode='jaro'), 12) |
||
266 | 0.777777777778 |
||
267 | >>> round(sim_jaro_winkler('Niall', 'Neil', mode='jaro'), 12) |
||
268 | 0.783333333333 |
||
269 | >>> round(sim_jaro_winkler('aluminum', 'Catalan', mode='jaro'), 12) |
||
270 | 0.60119047619 |
||
271 | >>> round(sim_jaro_winkler('ATCG', 'TAGC', mode='jaro'), 12) |
||
272 | 0.833333333333 |
||
273 | """ |
||
274 | if mode == 'winkler': |
||
275 | if boost_threshold > 1 or boost_threshold < 0: |
||
276 | raise ValueError('Unsupported boost_threshold assignment; ' + |
||
277 | 'boost_threshold must be between 0 and 1.') |
||
278 | if scaling_factor > 0.25 or scaling_factor < 0: |
||
279 | raise ValueError('Unsupported scaling_factor assignment; ' + |
||
280 | 'scaling_factor must be between 0 and 0.25.') |
||
281 | |||
282 | if src == tar: |
||
283 | return 1.0 |
||
284 | |||
285 | src = QGrams(src.strip(), qval).ordered_list |
||
286 | tar = QGrams(tar.strip(), qval).ordered_list |
||
287 | |||
288 | lens = len(src) |
||
289 | lent = len(tar) |
||
290 | |||
291 | # If either string is blank - return - added in Version 2 |
||
292 | if lens == 0 or lent == 0: |
||
293 | return 0.0 |
||
294 | |||
295 | if lens > lent: |
||
296 | search_range = lens |
||
297 | minv = lent |
||
298 | else: |
||
299 | search_range = lent |
||
300 | minv = lens |
||
301 | |||
302 | # Zero out the flags |
||
303 | src_flag = [0] * search_range |
||
304 | tar_flag = [0] * search_range |
||
305 | search_range = max(0, search_range//2 - 1) |
||
306 | |||
307 | # Looking only within the search range, count and flag the matched pairs. |
||
308 | num_com = 0 |
||
309 | yl1 = lent - 1 |
||
310 | for i in range(lens): |
||
311 | low_lim = (i - search_range) if (i >= search_range) else 0 |
||
312 | hi_lim = (i + search_range) if ((i + search_range) <= yl1) else yl1 |
||
313 | for j in range(low_lim, hi_lim+1): |
||
314 | if (tar_flag[j] == 0) and (tar[j] == src[i]): |
||
315 | tar_flag[j] = 1 |
||
316 | src_flag[i] = 1 |
||
317 | num_com += 1 |
||
318 | break |
||
319 | |||
320 | # If no characters in common - return |
||
321 | if num_com == 0: |
||
322 | return 0.0 |
||
323 | |||
324 | # Count the number of transpositions |
||
325 | k = n_trans = 0 |
||
326 | for i in range(lens): |
||
327 | if src_flag[i] != 0: |
||
328 | j = 0 |
||
329 | for j in range(k, lent): # pragma: no branch |
||
330 | if tar_flag[j] != 0: |
||
331 | k = j + 1 |
||
332 | break |
||
333 | if src[i] != tar[j]: |
||
334 | n_trans += 1 |
||
335 | n_trans //= 2 |
||
336 | |||
337 | # Main weight computation for Jaro distance |
||
338 | weight = num_com / lens + num_com / lent + (num_com - n_trans) / num_com |
||
339 | weight /= 3.0 |
||
340 | |||
341 | # Continue to boost the weight if the strings are similar |
||
342 | # This is the Winkler portion of Jaro-Winkler distance |
||
343 | if mode == 'winkler' and weight > boost_threshold: |
||
344 | |||
345 | # Adjust for having up to the first 4 characters in common |
||
346 | j = 4 if (minv >= 4) else minv |
||
347 | i = 0 |
||
348 | while (i < j) and (src[i] == tar[i]): |
||
349 | i += 1 |
||
350 | weight += i * scaling_factor * (1.0 - weight) |
||
351 | |||
352 | # Optionally adjust for long strings. |
||
353 | |||
354 | # After agreeing beginning chars, at least two more must agree and |
||
355 | # the agreeing characters must be > .5 of remaining characters. |
||
356 | if (long_strings and (minv > 4) and (num_com > i+1) and |
||
357 | (2*num_com >= minv+i)): |
||
358 | weight += (1.0-weight) * ((num_com-i-1) / (lens+lent-i*2+2)) |
||
359 | |||
360 | return weight |
||
361 | |||
421 |