| Conditions | 25 |
| Total Lines | 111 |
| Code Lines | 64 |
| 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.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, max_distance=0): |
|
| 46 | """Return the "common" 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 | max_distance : int |
||
| 57 | The distance at which to stop and exit |
||
| 58 | |||
| 59 | Returns |
||
| 60 | ------- |
||
| 61 | int |
||
| 62 | The Sift4 distance according to the common formula |
||
| 63 | |||
| 64 | Examples |
||
| 65 | -------- |
||
| 66 | >>> cmp = Sift4() |
||
| 67 | >>> cmp.dist_abs('cat', 'hat') |
||
| 68 | 1 |
||
| 69 | >>> cmp.dist_abs('Niall', 'Neil') |
||
| 70 | 2 |
||
| 71 | >>> cmp.dist_abs('Colin', 'Cuilen') |
||
| 72 | 3 |
||
| 73 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
| 74 | 2 |
||
| 75 | |||
| 76 | """ |
||
| 77 | 1 | if not src: |
|
| 78 | 1 | return len(tar) |
|
| 79 | |||
| 80 | 1 | if not tar: |
|
| 81 | 1 | return len(src) |
|
| 82 | |||
| 83 | 1 | src_len = len(src) |
|
| 84 | 1 | tar_len = len(tar) |
|
| 85 | |||
| 86 | 1 | src_cur = 0 |
|
| 87 | 1 | tar_cur = 0 |
|
| 88 | 1 | lcss = 0 |
|
| 89 | 1 | local_cs = 0 |
|
| 90 | 1 | trans = 0 |
|
| 91 | 1 | offset_arr = [] |
|
| 92 | |||
| 93 | 1 | while (src_cur < src_len) and (tar_cur < tar_len): |
|
| 94 | 1 | if src[src_cur] == tar[tar_cur]: |
|
| 95 | 1 | local_cs += 1 |
|
| 96 | 1 | is_trans = False |
|
| 97 | 1 | i = 0 |
|
| 98 | 1 | while i < len(offset_arr): |
|
| 99 | 1 | ofs = offset_arr[i] |
|
| 100 | 1 | if src_cur <= ofs['src_cur'] or tar_cur <= ofs['tar_cur']: |
|
| 101 | 1 | is_trans = abs(tar_cur - src_cur) >= abs( |
|
| 102 | ofs['tar_cur'] - ofs['src_cur'] |
||
| 103 | ) |
||
| 104 | 1 | if is_trans: |
|
| 105 | 1 | trans += 1 |
|
| 106 | 1 | elif not ofs['trans']: |
|
| 107 | 1 | ofs['trans'] = True |
|
| 108 | 1 | trans += 1 |
|
| 109 | 1 | break |
|
| 110 | 1 | elif src_cur > ofs['tar_cur'] and tar_cur > ofs['src_cur']: |
|
| 111 | 1 | del offset_arr[i] |
|
| 112 | else: |
||
| 113 | 1 | i += 1 |
|
| 114 | |||
| 115 | 1 | offset_arr.append( |
|
| 116 | {'src_cur': src_cur, 'tar_cur': tar_cur, 'trans': is_trans} |
||
| 117 | ) |
||
| 118 | else: |
||
| 119 | 1 | lcss += local_cs |
|
| 120 | 1 | local_cs = 0 |
|
| 121 | 1 | if src_cur != tar_cur: |
|
| 122 | 1 | src_cur = tar_cur = min(src_cur, tar_cur) |
|
| 123 | 1 | for i in range(max_offset): |
|
| 124 | 1 | if not ( |
|
| 125 | (src_cur + i < src_len) or (tar_cur + i < tar_len) |
||
| 126 | ): |
||
| 127 | 1 | break |
|
| 128 | 1 | if (src_cur + i < src_len) and ( |
|
| 129 | src[src_cur + i] == tar[tar_cur] |
||
| 130 | ): |
||
| 131 | 1 | src_cur += i - 1 |
|
| 132 | 1 | tar_cur -= 1 |
|
| 133 | 1 | break |
|
| 134 | 1 | if (tar_cur + i < tar_len) and ( |
|
| 135 | src[src_cur] == tar[tar_cur + i] |
||
| 136 | ): |
||
| 137 | 1 | src_cur -= 1 |
|
| 138 | 1 | tar_cur += i - 1 |
|
| 139 | 1 | break |
|
| 140 | |||
| 141 | 1 | src_cur += 1 |
|
| 142 | 1 | tar_cur += 1 |
|
| 143 | |||
| 144 | 1 | if max_distance: |
|
| 145 | 1 | temporary_distance = max(src_cur, tar_cur) - lcss + trans |
|
| 146 | 1 | if temporary_distance >= max_distance: |
|
| 147 | 1 | return round(temporary_distance) |
|
| 148 | |||
| 149 | 1 | if (src_cur >= src_len) or (tar_cur >= tar_len): |
|
| 150 | 1 | lcss += local_cs |
|
| 151 | 1 | local_cs = 0 |
|
| 152 | 1 | src_cur = tar_cur = min(src_cur, tar_cur) |
|
| 153 | |||
| 154 | 1 | lcss += local_cs |
|
| 155 | 1 | return round(max(src_len, tar_len) - lcss + trans) |
|
| 156 | |||
| 308 |