| Conditions | 25 |
| Total Lines | 99 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 59 |
| 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_common() 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 -*- |
||
| 99 | 1 | def sift4_common(src, tar, max_offset=5, max_distance=0): |
|
| 100 | """Return the "common" Sift4 distance between two terms. |
||
| 101 | |||
| 102 | This is an approximation of edit distance, described in |
||
| 103 | :cite:`Zackwehdex:2014`. |
||
| 104 | |||
| 105 | :param str src: source string for comparison |
||
| 106 | :param str tar: target string for comparison |
||
| 107 | :param max_offset: the number of characters to search for matching letters |
||
| 108 | :param max_distance: the distance at which to stop and exit |
||
| 109 | :returns: the Sift4 distance according to the common formula |
||
| 110 | :rtype: int |
||
| 111 | |||
| 112 | >>> sift4_common('cat', 'hat') |
||
| 113 | 1 |
||
| 114 | >>> sift4_common('Niall', 'Neil') |
||
| 115 | 2 |
||
| 116 | >>> sift4_common('Colin', 'Cuilen') |
||
| 117 | 3 |
||
| 118 | >>> sift4_common('ATCG', 'TAGC') |
||
| 119 | 2 |
||
| 120 | """ |
||
| 121 | 1 | if not src: |
|
| 122 | 1 | return len(tar) |
|
| 123 | |||
| 124 | 1 | if not tar: |
|
| 125 | 1 | return len(src) |
|
| 126 | |||
| 127 | 1 | src_len = len(src) |
|
| 128 | 1 | tar_len = len(tar) |
|
| 129 | |||
| 130 | 1 | src_cur = 0 |
|
| 131 | 1 | tar_cur = 0 |
|
| 132 | 1 | lcss = 0 |
|
| 133 | 1 | local_cs = 0 |
|
| 134 | 1 | trans = 0 |
|
| 135 | 1 | offset_arr = [] |
|
| 136 | |||
| 137 | 1 | while (src_cur < src_len) and (tar_cur < tar_len): |
|
| 138 | 1 | if src[src_cur] == tar[tar_cur]: |
|
| 139 | 1 | local_cs += 1 |
|
| 140 | 1 | is_trans = False |
|
| 141 | 1 | i = 0 |
|
| 142 | 1 | while i < len(offset_arr): |
|
| 143 | 1 | ofs = offset_arr[i] |
|
| 144 | 1 | if src_cur <= ofs['src_cur'] or tar_cur <= ofs['tar_cur']: |
|
| 145 | 1 | is_trans = abs(tar_cur - src_cur) >= abs( |
|
| 146 | ofs['tar_cur'] - ofs['src_cur'] |
||
| 147 | ) |
||
| 148 | 1 | if is_trans: |
|
| 149 | 1 | trans += 1 |
|
| 150 | 1 | elif not ofs['trans']: |
|
| 151 | 1 | ofs['trans'] = True |
|
| 152 | 1 | trans += 1 |
|
| 153 | 1 | break |
|
| 154 | 1 | elif src_cur > ofs['tar_cur'] and tar_cur > ofs['src_cur']: |
|
| 155 | 1 | del offset_arr[i] |
|
| 156 | else: |
||
| 157 | 1 | i += 1 |
|
| 158 | |||
| 159 | 1 | offset_arr.append( |
|
| 160 | {'src_cur': src_cur, 'tar_cur': tar_cur, 'trans': is_trans} |
||
| 161 | ) |
||
| 162 | else: |
||
| 163 | 1 | lcss += local_cs |
|
| 164 | 1 | local_cs = 0 |
|
| 165 | 1 | if src_cur != tar_cur: |
|
| 166 | 1 | src_cur = tar_cur = min(src_cur, tar_cur) |
|
| 167 | 1 | for i in range(max_offset): |
|
| 168 | 1 | if not ((src_cur + i < src_len) or (tar_cur + i < tar_len)): |
|
| 169 | 1 | break |
|
| 170 | 1 | if (src_cur + i < src_len) and ( |
|
| 171 | src[src_cur + i] == tar[tar_cur] |
||
| 172 | ): |
||
| 173 | 1 | src_cur += i - 1 |
|
| 174 | 1 | tar_cur -= 1 |
|
| 175 | 1 | break |
|
| 176 | 1 | if (tar_cur + i < tar_len) and ( |
|
| 177 | src[src_cur] == tar[tar_cur + i] |
||
| 178 | ): |
||
| 179 | 1 | src_cur -= 1 |
|
| 180 | 1 | tar_cur += i - 1 |
|
| 181 | 1 | break |
|
| 182 | |||
| 183 | 1 | src_cur += 1 |
|
| 184 | 1 | tar_cur += 1 |
|
| 185 | |||
| 186 | 1 | if max_distance: |
|
| 187 | 1 | temporary_distance = max(src_cur, tar_cur) - lcss + trans |
|
| 188 | 1 | if temporary_distance >= max_distance: |
|
| 189 | 1 | return round(temporary_distance) |
|
| 190 | |||
| 191 | 1 | if (src_cur >= src_len) or (tar_cur >= tar_len): |
|
| 192 | 1 | lcss += local_cs |
|
| 193 | 1 | local_cs = 0 |
|
| 194 | 1 | src_cur = tar_cur = min(src_cur, tar_cur) |
|
| 195 | |||
| 196 | 1 | lcss += local_cs |
|
| 197 | 1 | return round(max(src_len, tar_len) - lcss + trans) |
|
| 198 | |||
| 255 |