| Conditions | 13 |
| Total Lines | 178 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 47 |
| CRAP Score | 13 |
| 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._typo.Typo.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 -*- |
||
| 94 | 1 | def dist_abs( |
|
| 95 | self, |
||
| 96 | src, |
||
| 97 | tar, |
||
| 98 | metric='euclidean', |
||
| 99 | cost=(1, 1, 0.5, 0.5), |
||
| 100 | layout='QWERTY', |
||
| 101 | ): |
||
| 102 | """Return the typo distance between two strings. |
||
| 103 | |||
| 104 | Parameters |
||
| 105 | ---------- |
||
| 106 | src : str |
||
| 107 | Source string for comparison |
||
| 108 | tar : str |
||
| 109 | Target string for comparison |
||
| 110 | metric : str |
||
| 111 | Supported values include: ``euclidean``, ``manhattan``, |
||
| 112 | ``log-euclidean``, and ``log-manhattan`` |
||
| 113 | cost : tuple |
||
| 114 | A 4-tuple representing the cost of the four possible edits: |
||
| 115 | inserts, deletes, substitutions, and shift, respectively (by |
||
| 116 | default: (1, 1, 0.5, 0.5)) The substitution & shift costs should be |
||
| 117 | significantly less than the cost of an insertion & deletion unless |
||
| 118 | a log metric is used. |
||
| 119 | layout : str |
||
| 120 | Name of the keyboard layout to use (Currently supported: |
||
| 121 | ``QWERTY``, ``Dvorak``, ``AZERTY``, ``QWERTZ``) |
||
| 122 | |||
| 123 | Returns |
||
| 124 | ------- |
||
| 125 | float |
||
| 126 | Typo distance |
||
| 127 | |||
| 128 | Raises |
||
| 129 | ------ |
||
| 130 | ValueError |
||
| 131 | char not found in any keyboard layouts |
||
| 132 | |||
| 133 | Examples |
||
| 134 | -------- |
||
| 135 | >>> cmp = Typo() |
||
| 136 | >>> cmp.dist_abs('cat', 'hat') |
||
| 137 | 1.5811388 |
||
| 138 | >>> cmp.dist_abs('Niall', 'Neil') |
||
| 139 | 2.8251407 |
||
| 140 | >>> cmp.dist_abs('Colin', 'Cuilen') |
||
| 141 | 3.4142137 |
||
| 142 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
| 143 | 2.5 |
||
| 144 | |||
| 145 | >>> cmp.dist_abs('cat', 'hat', metric='manhattan') |
||
| 146 | 2.0 |
||
| 147 | >>> cmp.dist_abs('Niall', 'Neil', metric='manhattan') |
||
| 148 | 3.0 |
||
| 149 | >>> cmp.dist_abs('Colin', 'Cuilen', metric='manhattan') |
||
| 150 | 3.5 |
||
| 151 | >>> cmp.dist_abs('ATCG', 'TAGC', metric='manhattan') |
||
| 152 | 2.5 |
||
| 153 | |||
| 154 | >>> cmp.dist_abs('cat', 'hat', metric='log-manhattan') |
||
| 155 | 0.804719 |
||
| 156 | >>> cmp.dist_abs('Niall', 'Neil', metric='log-manhattan') |
||
| 157 | 2.2424533 |
||
| 158 | >>> cmp.dist_abs('Colin', 'Cuilen', metric='log-manhattan') |
||
| 159 | 2.2424533 |
||
| 160 | >>> cmp.dist_abs('ATCG', 'TAGC', metric='log-manhattan') |
||
| 161 | 2.3465736 |
||
| 162 | |||
| 163 | """ |
||
| 164 | 1 | ins_cost, del_cost, sub_cost, shift_cost = cost |
|
| 165 | |||
| 166 | 1 | if src == tar: |
|
| 167 | 1 | return 0.0 |
|
| 168 | 1 | if not src: |
|
| 169 | 1 | return len(tar) * ins_cost |
|
| 170 | 1 | if not tar: |
|
| 171 | 1 | return len(src) * del_cost |
|
| 172 | |||
| 173 | 1 | keyboard = self._keyboard[layout] |
|
| 174 | 1 | lowercase = {item for sublist in keyboard[0] for item in sublist} |
|
| 175 | 1 | uppercase = {item for sublist in keyboard[1] for item in sublist} |
|
| 176 | |||
| 177 | 1 | def _kb_array_for_char(char): |
|
| 178 | """Return the keyboard layout that contains ch. |
||
| 179 | |||
| 180 | Parameters |
||
| 181 | ---------- |
||
| 182 | char : str |
||
| 183 | The character to lookup |
||
| 184 | |||
| 185 | Returns |
||
| 186 | ------- |
||
| 187 | tuple |
||
| 188 | A keyboard |
||
| 189 | |||
| 190 | Raises |
||
| 191 | ------ |
||
| 192 | ValueError |
||
| 193 | char not found in any keyboard layouts |
||
| 194 | |||
| 195 | """ |
||
| 196 | 1 | if char in lowercase: |
|
| 197 | 1 | return keyboard[0] |
|
| 198 | 1 | elif char in uppercase: |
|
| 199 | 1 | return keyboard[1] |
|
| 200 | 1 | raise ValueError(char + ' not found in any keyboard layouts') |
|
| 201 | |||
| 202 | 1 | def _substitution_cost(char1, char2): |
|
| 203 | 1 | cost = sub_cost |
|
| 204 | 1 | cost *= metric_dict[metric](char1, char2) + shift_cost * ( |
|
| 205 | _kb_array_for_char(char1) != _kb_array_for_char(char2) |
||
| 206 | ) |
||
| 207 | 1 | return cost |
|
| 208 | |||
| 209 | 1 | def _get_char_coord(char, kb_array): |
|
| 210 | """Return the row & column of char in the keyboard. |
||
| 211 | |||
| 212 | Parameters |
||
| 213 | ---------- |
||
| 214 | char : str |
||
| 215 | The character to search for |
||
| 216 | kb_array : tuple of tuples |
||
| 217 | The array of key positions |
||
| 218 | |||
| 219 | Returns |
||
| 220 | ------- |
||
| 221 | tuple |
||
| 222 | The row & column of the key |
||
| 223 | |||
| 224 | """ |
||
| 225 | 1 | for row in kb_array: # pragma: no branch |
|
| 226 | 1 | if char in row: |
|
| 227 | 1 | return kb_array.index(row), row.index(char) |
|
| 228 | |||
| 229 | 1 | def _euclidean_keyboard_distance(char1, char2): |
|
| 230 | 1 | row1, col1 = _get_char_coord(char1, _kb_array_for_char(char1)) |
|
| 231 | 1 | row2, col2 = _get_char_coord(char2, _kb_array_for_char(char2)) |
|
| 232 | 1 | return ((row1 - row2) ** 2 + (col1 - col2) ** 2) ** 0.5 |
|
| 233 | |||
| 234 | 1 | def _manhattan_keyboard_distance(char1, char2): |
|
| 235 | 1 | row1, col1 = _get_char_coord(char1, _kb_array_for_char(char1)) |
|
| 236 | 1 | row2, col2 = _get_char_coord(char2, _kb_array_for_char(char2)) |
|
| 237 | 1 | return abs(row1 - row2) + abs(col1 - col2) |
|
| 238 | |||
| 239 | 1 | def _log_euclidean_keyboard_distance(char1, char2): |
|
| 240 | 1 | return log(1 + _euclidean_keyboard_distance(char1, char2)) |
|
| 241 | |||
| 242 | 1 | def _log_manhattan_keyboard_distance(char1, char2): |
|
| 243 | 1 | return log(1 + _manhattan_keyboard_distance(char1, char2)) |
|
| 244 | |||
| 245 | 1 | metric_dict = { |
|
| 246 | 'euclidean': _euclidean_keyboard_distance, |
||
| 247 | 'manhattan': _manhattan_keyboard_distance, |
||
| 248 | 'log-euclidean': _log_euclidean_keyboard_distance, |
||
| 249 | 'log-manhattan': _log_manhattan_keyboard_distance, |
||
| 250 | } |
||
| 251 | |||
| 252 | 1 | d_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_float32) |
|
| 253 | 1 | for i in range(len(src) + 1): |
|
| 254 | 1 | d_mat[i, 0] = i * del_cost |
|
| 255 | 1 | for j in range(len(tar) + 1): |
|
| 256 | 1 | d_mat[0, j] = j * ins_cost |
|
| 257 | |||
| 258 | 1 | for i in range(len(src)): |
|
| 259 | 1 | for j in range(len(tar)): |
|
| 260 | 1 | d_mat[i + 1, j + 1] = min( |
|
| 261 | d_mat[i + 1, j] + ins_cost, # ins |
||
| 262 | d_mat[i, j + 1] + del_cost, # del |
||
| 263 | d_mat[i, j] |
||
| 264 | + ( |
||
| 265 | _substitution_cost(src[i], tar[j]) |
||
| 266 | if src[i] != tar[j] |
||
| 267 | else 0 |
||
| 268 | ), # sub/== |
||
| 269 | ) |
||
| 270 | |||
| 271 | 1 | return d_mat[len(src), len(tar)] |
|
| 272 | |||
| 488 |