| Conditions | 14 |
| Total Lines | 75 |
| Code Lines | 36 |
| 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_simplest.Sift4Simplest.dist_abs() 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 -*- |
||
| 45 | 1 | def dist_abs(self, src, tar, max_offset=5): |
|
| 46 | """Return the "simplest" Sift4 distance between two terms. |
||
| 47 | |||
| 48 | Parameters |
||
| 49 | ---------- |
||
| 50 | src : str |
||
| 51 | Source string for comparison |
||
| 52 | tar : str |
||
| 53 | Target string for comparison |
||
| 54 | max_offset : int |
||
| 55 | The number of characters to search for matching letters |
||
| 56 | |||
| 57 | Returns |
||
| 58 | ------- |
||
| 59 | int |
||
| 60 | The Sift4 distance according to the simplest formula |
||
| 61 | |||
| 62 | Examples |
||
| 63 | -------- |
||
| 64 | >>> cmp = Sift4Simplest() |
||
| 65 | >>> cmp.dist_abs('cat', 'hat') |
||
| 66 | 1 |
||
| 67 | >>> cmp.dist_abs('Niall', 'Neil') |
||
| 68 | 2 |
||
| 69 | >>> cmp.dist_abs('Colin', 'Cuilen') |
||
| 70 | 3 |
||
| 71 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
| 72 | 2 |
||
| 73 | |||
| 74 | """ |
||
| 75 | 1 | if not src: |
|
| 76 | 1 | return len(tar) |
|
| 77 | |||
| 78 | 1 | if not tar: |
|
| 79 | 1 | return len(src) |
|
| 80 | |||
| 81 | 1 | src_len = len(src) |
|
| 82 | 1 | tar_len = len(tar) |
|
| 83 | |||
| 84 | 1 | src_cur = 0 |
|
| 85 | 1 | tar_cur = 0 |
|
| 86 | 1 | lcss = 0 |
|
| 87 | 1 | local_cs = 0 |
|
| 88 | |||
| 89 | 1 | while (src_cur < src_len) and (tar_cur < tar_len): |
|
| 90 | 1 | if src[src_cur] == tar[tar_cur]: |
|
| 91 | 1 | local_cs += 1 |
|
| 92 | else: |
||
| 93 | 1 | lcss += local_cs |
|
| 94 | 1 | local_cs = 0 |
|
| 95 | 1 | if src_cur != tar_cur: |
|
| 96 | 1 | src_cur = tar_cur = max(src_cur, tar_cur) |
|
| 97 | 1 | for i in range(max_offset): |
|
| 98 | 1 | if not ( |
|
| 99 | (src_cur + i < src_len) or (tar_cur + i < tar_len) |
||
| 100 | ): |
||
| 101 | 1 | break |
|
| 102 | 1 | if (src_cur + i < src_len) and ( |
|
| 103 | src[src_cur + i] == tar[tar_cur] |
||
| 104 | ): |
||
| 105 | 1 | src_cur += i |
|
| 106 | 1 | local_cs += 1 |
|
| 107 | 1 | break |
|
| 108 | 1 | if (tar_cur + i < tar_len) and ( |
|
| 109 | src[src_cur] == tar[tar_cur + i] |
||
| 110 | ): |
||
| 111 | 1 | tar_cur += i |
|
| 112 | 1 | local_cs += 1 |
|
| 113 | 1 | break |
|
| 114 | |||
| 115 | 1 | src_cur += 1 |
|
| 116 | 1 | tar_cur += 1 |
|
| 117 | |||
| 118 | 1 | lcss += local_cs |
|
| 119 | 1 | return round(max(src_len, tar_len) - lcss) |
|
| 120 | |||
| 160 |