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