| Conditions | 25 |
| Total Lines | 112 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 54 |
| CRAP Score | 25 |
| 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.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 | # Copyright 2018-2020 by Christopher C. Little. |
||
| 60 | def dist_abs(self, src: str, tar: str) -> float: |
||
| 61 | """Return the "common" Sift4 distance between two terms. |
||
| 62 | |||
| 63 | Parameters |
||
| 64 | ---------- |
||
| 65 | src : str |
||
| 66 | 1 | Source string for comparison |
|
| 67 | 1 | tar : str |
|
| 68 | 1 | Target string for comparison |
|
| 69 | |||
| 70 | 1 | Returns |
|
| 71 | ------- |
||
| 72 | int |
||
| 73 | The Sift4 distance according to the common formula |
||
| 74 | |||
| 75 | Examples |
||
| 76 | -------- |
||
| 77 | >>> cmp = Sift4() |
||
| 78 | >>> cmp.dist_abs('cat', 'hat') |
||
| 79 | 1 |
||
| 80 | >>> cmp.dist_abs('Niall', 'Neil') |
||
| 81 | 2 |
||
| 82 | >>> cmp.dist_abs('Colin', 'Cuilen') |
||
| 83 | 3 |
||
| 84 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
| 85 | 2 |
||
| 86 | |||
| 87 | |||
| 88 | .. versionadded:: 0.3.0 |
||
| 89 | .. versionchanged:: 0.3.6 |
||
| 90 | Encapsulated in class |
||
| 91 | |||
| 92 | """ |
||
| 93 | if not src: |
||
| 94 | return len(tar) |
||
| 95 | |||
| 96 | if not tar: |
||
| 97 | return len(src) |
||
| 98 | |||
| 99 | src_len = len(src) |
||
| 100 | tar_len = len(tar) |
||
| 101 | |||
| 102 | src_cur = 0 |
||
| 103 | 1 | tar_cur = 0 |
|
| 104 | 1 | lcss = 0 |
|
| 105 | local_cs = 0 |
||
| 106 | 1 | trans = 0 |
|
| 107 | 1 | offset_arr = [] # type: List[Dict[str, Union[int, bool]]] |
|
| 108 | |||
| 109 | 1 | while (src_cur < src_len) and (tar_cur < tar_len): |
|
| 110 | 1 | if src[src_cur] == tar[tar_cur]: |
|
| 111 | local_cs += 1 |
||
| 112 | 1 | is_trans = False |
|
| 113 | 1 | i = 0 |
|
| 114 | 1 | while i < len(offset_arr): |
|
| 115 | 1 | ofs = offset_arr[i] |
|
| 116 | 1 | if src_cur <= ofs['src_cur'] or tar_cur <= ofs['tar_cur']: |
|
| 117 | 1 | is_trans = abs(tar_cur - src_cur) >= abs( |
|
| 118 | ofs['tar_cur'] - ofs['src_cur'] |
||
| 119 | 1 | ) |
|
| 120 | 1 | if is_trans: |
|
| 121 | 1 | trans += 1 |
|
| 122 | 1 | elif not ofs['trans']: |
|
| 123 | 1 | ofs['trans'] = True |
|
| 124 | 1 | trans += 1 |
|
| 125 | 1 | break |
|
| 126 | 1 | elif src_cur > ofs['tar_cur'] and tar_cur > ofs['src_cur']: |
|
| 127 | 1 | del offset_arr[i] |
|
| 128 | else: |
||
| 129 | i += 1 |
||
| 130 | 1 | ||
| 131 | 1 | offset_arr.append( |
|
| 132 | 1 | {'src_cur': src_cur, 'tar_cur': tar_cur, 'trans': is_trans} |
|
| 133 | 1 | ) |
|
| 134 | 1 | else: |
|
| 135 | 1 | lcss += local_cs |
|
| 136 | 1 | local_cs = 0 |
|
| 137 | 1 | if src_cur != tar_cur: |
|
| 138 | src_cur = tar_cur = min(src_cur, tar_cur) |
||
| 139 | 1 | for i in range(self._max_offset): |
|
| 140 | if not ( |
||
| 141 | 1 | (src_cur + i < src_len) or (tar_cur + i < tar_len) |
|
| 142 | ): |
||
| 143 | break |
||
| 144 | if (src_cur + i < src_len) and ( |
||
| 145 | 1 | src[src_cur + i] == tar[tar_cur] |
|
| 146 | 1 | ): |
|
| 147 | 1 | src_cur += i - 1 |
|
| 148 | 1 | tar_cur -= 1 |
|
| 149 | 1 | break |
|
| 150 | 1 | if (tar_cur + i < tar_len) and ( |
|
| 151 | src[src_cur] == tar[tar_cur + i] |
||
| 152 | ): |
||
| 153 | 1 | src_cur -= 1 |
|
| 154 | 1 | tar_cur += i - 1 |
|
| 155 | break |
||
| 156 | |||
| 157 | 1 | src_cur += 1 |
|
| 158 | 1 | tar_cur += 1 |
|
| 159 | 1 | ||
| 160 | 1 | if self._max_distance: |
|
| 161 | temporary_distance = max(src_cur, tar_cur) - lcss + trans |
||
| 162 | if temporary_distance >= self._max_distance: |
||
| 163 | 1 | return round(temporary_distance) |
|
| 164 | 1 | ||
| 165 | 1 | if (src_cur >= src_len) or (tar_cur >= tar_len): |
|
| 166 | lcss += local_cs |
||
| 167 | 1 | local_cs = 0 |
|
| 168 | 1 | src_cur = tar_cur = min(src_cur, tar_cur) |
|
| 169 | |||
| 170 | 1 | lcss += local_cs |
|
| 171 | 1 | return round(max(src_len, tar_len) - lcss + trans) |
|
| 172 | 1 | ||
| 215 |