| Conditions | 14 |
| Total Lines | 64 |
| Code Lines | 35 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 33 |
| CRAP Score | 14 |
| 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._sift4.sift4_simplest() 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 -*- |
||
| 33 | 1 | def sift4_simplest(src, tar, max_offset=5): |
|
| 34 | """Return the "simplest" Sift4 distance between two terms. |
||
| 35 | |||
| 36 | This is an approximation of edit distance, described in |
||
| 37 | :cite:`Zackwehdex:2014`. |
||
| 38 | |||
| 39 | :param str src: source string for comparison |
||
| 40 | :param str tar: target string for comparison |
||
| 41 | :param max_offset: the number of characters to search for matching letters |
||
| 42 | :returns: the Sift4 distance according to the simplest formula |
||
| 43 | :rtype: int |
||
| 44 | |||
| 45 | >>> sift4_simplest('cat', 'hat') |
||
| 46 | 1 |
||
| 47 | >>> sift4_simplest('Niall', 'Neil') |
||
| 48 | 2 |
||
| 49 | >>> sift4_simplest('Colin', 'Cuilen') |
||
| 50 | 3 |
||
| 51 | >>> sift4_simplest('ATCG', 'TAGC') |
||
| 52 | 2 |
||
| 53 | """ |
||
| 54 | 1 | if not src: |
|
| 55 | 1 | return len(tar) |
|
| 56 | |||
| 57 | 1 | if not tar: |
|
| 58 | 1 | return len(src) |
|
| 59 | |||
| 60 | 1 | src_len = len(src) |
|
| 61 | 1 | tar_len = len(tar) |
|
| 62 | |||
| 63 | 1 | src_cur = 0 |
|
| 64 | 1 | tar_cur = 0 |
|
| 65 | 1 | lcss = 0 |
|
| 66 | 1 | local_cs = 0 |
|
| 67 | |||
| 68 | 1 | while (src_cur < src_len) and (tar_cur < tar_len): |
|
| 69 | 1 | if src[src_cur] == tar[tar_cur]: |
|
| 70 | 1 | local_cs += 1 |
|
| 71 | else: |
||
| 72 | 1 | lcss += local_cs |
|
| 73 | 1 | local_cs = 0 |
|
| 74 | 1 | if src_cur != tar_cur: |
|
| 75 | 1 | src_cur = tar_cur = max(src_cur, tar_cur) |
|
| 76 | 1 | for i in range(max_offset): |
|
| 77 | 1 | if not ((src_cur + i < src_len) or (tar_cur + i < tar_len)): |
|
| 78 | 1 | break |
|
| 79 | 1 | if (src_cur + i < src_len) and ( |
|
| 80 | src[src_cur + i] == tar[tar_cur] |
||
|
|
|||
| 81 | ): |
||
| 82 | 1 | src_cur += i |
|
| 83 | 1 | local_cs += 1 |
|
| 84 | 1 | break |
|
| 85 | 1 | if (tar_cur + i < tar_len) and ( |
|
| 86 | src[src_cur] == tar[tar_cur + i] |
||
| 87 | ): |
||
| 88 | 1 | tar_cur += i |
|
| 89 | 1 | local_cs += 1 |
|
| 90 | 1 | break |
|
| 91 | |||
| 92 | 1 | src_cur += 1 |
|
| 93 | 1 | tar_cur += 1 |
|
| 94 | |||
| 95 | 1 | lcss += local_cs |
|
| 96 | 1 | return round(max(src_len, tar_len) - lcss) |
|
| 97 | |||
| 255 |