| Conditions | 37 |
| Total Lines | 150 |
| Code Lines | 77 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 69 |
| CRAP Score | 37 |
| 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._strcmp95.Strcmp95.sim() 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 sim(self, src, tar, long_strings=False): |
|
| 95 | """Return the strcmp95 similarity of two strings. |
||
| 96 | |||
| 97 | Args: |
||
| 98 | src (str): Source string for comparison |
||
| 99 | tar (str): Target string for comparison |
||
| 100 | long_strings (bool): Set to True to increase the probability of a |
||
| 101 | match when the number of matched characters is large. This |
||
| 102 | option allows for a little more tolerance when the strings are |
||
| 103 | large. It is not an appropriate test when comparing fixed |
||
| 104 | length fields such as phone and social security numbers. |
||
| 105 | |||
| 106 | Returns: |
||
| 107 | float: Strcmp95 similarity |
||
| 108 | |||
| 109 | Examples: |
||
| 110 | >>> cmp = Strcmp95() |
||
| 111 | >>> cmp.sim('cat', 'hat') |
||
| 112 | 0.7777777777777777 |
||
| 113 | >>> cmp.sim('Niall', 'Neil') |
||
| 114 | 0.8454999999999999 |
||
| 115 | >>> cmp.sim('aluminum', 'Catalan') |
||
| 116 | 0.6547619047619048 |
||
| 117 | >>> cmp.sim('ATCG', 'TAGC') |
||
| 118 | 0.8333333333333334 |
||
| 119 | |||
| 120 | """ |
||
| 121 | |||
| 122 | 1 | def _in_range(char): |
|
| 123 | """Return True if char is in the range (0, 91). |
||
| 124 | |||
| 125 | Args: |
||
| 126 | char (str): The character to check |
||
| 127 | |||
| 128 | Returns: |
||
| 129 | bool: True if char is in the range (0, 91) |
||
| 130 | |||
| 131 | """ |
||
| 132 | 1 | return 91 > ord(char) > 0 |
|
| 133 | |||
| 134 | 1 | ying = src.strip().upper() |
|
| 135 | 1 | yang = tar.strip().upper() |
|
| 136 | |||
| 137 | 1 | if ying == yang: |
|
| 138 | 1 | return 1.0 |
|
| 139 | # If either string is blank - return - added in Version 2 |
||
| 140 | 1 | if not ying or not yang: |
|
| 141 | 1 | return 0.0 |
|
| 142 | |||
| 143 | 1 | adjwt = defaultdict(int) |
|
| 144 | |||
| 145 | # Initialize the adjwt array on the first call to the function only. |
||
| 146 | # The adjwt array is used to give partial credit for characters that |
||
| 147 | # may be errors due to known phonetic or character recognition errors. |
||
| 148 | # A typical example is to match the letter "O" with the number "0" |
||
| 149 | 1 | for i in self._sp_mx: |
|
| 150 | 1 | adjwt[(i[0], i[1])] = 3 |
|
| 151 | 1 | adjwt[(i[1], i[0])] = 3 |
|
| 152 | |||
| 153 | 1 | if len(ying) > len(yang): |
|
| 154 | 1 | search_range = len(ying) |
|
| 155 | 1 | minv = len(yang) |
|
| 156 | else: |
||
| 157 | 1 | search_range = len(yang) |
|
| 158 | 1 | minv = len(ying) |
|
| 159 | |||
| 160 | # Blank out the flags |
||
| 161 | 1 | ying_flag = [0] * search_range |
|
| 162 | 1 | yang_flag = [0] * search_range |
|
| 163 | 1 | search_range = max(0, search_range // 2 - 1) |
|
| 164 | |||
| 165 | # Looking only within the search range, |
||
| 166 | # count and flag the matched pairs. |
||
| 167 | 1 | num_com = 0 |
|
| 168 | 1 | yl1 = len(yang) - 1 |
|
| 169 | 1 | for i in range(len(ying)): |
|
| 170 | 1 | low_lim = (i - search_range) if (i >= search_range) else 0 |
|
| 171 | 1 | hi_lim = (i + search_range) if ((i + search_range) <= yl1) else yl1 |
|
| 172 | 1 | for j in range(low_lim, hi_lim + 1): |
|
| 173 | 1 | if (yang_flag[j] == 0) and (yang[j] == ying[i]): |
|
| 174 | 1 | yang_flag[j] = 1 |
|
| 175 | 1 | ying_flag[i] = 1 |
|
| 176 | 1 | num_com += 1 |
|
| 177 | 1 | break |
|
| 178 | |||
| 179 | # If no characters in common - return |
||
| 180 | 1 | if num_com == 0: |
|
| 181 | 1 | return 0.0 |
|
| 182 | |||
| 183 | # Count the number of transpositions |
||
| 184 | 1 | k = n_trans = 0 |
|
| 185 | 1 | for i in range(len(ying)): |
|
| 186 | 1 | if ying_flag[i] != 0: |
|
| 187 | 1 | j = 0 |
|
| 188 | 1 | for j in range(k, len(yang)): # pragma: no branch |
|
| 189 | 1 | if yang_flag[j] != 0: |
|
| 190 | 1 | k = j + 1 |
|
| 191 | 1 | break |
|
| 192 | 1 | if ying[i] != yang[j]: |
|
| 193 | 1 | n_trans += 1 |
|
| 194 | 1 | n_trans //= 2 |
|
| 195 | |||
| 196 | # Adjust for similarities in unmatched characters |
||
| 197 | 1 | n_simi = 0 |
|
| 198 | 1 | if minv > num_com: |
|
| 199 | 1 | for i in range(len(ying)): |
|
| 200 | 1 | if ying_flag[i] == 0 and _in_range(ying[i]): |
|
| 201 | 1 | for j in range(len(yang)): |
|
| 202 | 1 | if yang_flag[j] == 0 and _in_range(yang[j]): |
|
| 203 | 1 | if (ying[i], yang[j]) in adjwt: |
|
| 204 | 1 | n_simi += adjwt[(ying[i], yang[j])] |
|
| 205 | 1 | yang_flag[j] = 2 |
|
| 206 | 1 | break |
|
| 207 | 1 | num_sim = n_simi / 10.0 + num_com |
|
| 208 | |||
| 209 | # Main weight computation |
||
| 210 | 1 | weight = ( |
|
| 211 | num_sim / len(ying) |
||
| 212 | + num_sim / len(yang) |
||
| 213 | + (num_com - n_trans) / num_com |
||
| 214 | ) |
||
| 215 | 1 | weight /= 3.0 |
|
| 216 | |||
| 217 | # Continue to boost the weight if the strings are similar |
||
| 218 | 1 | if weight > 0.7: |
|
| 219 | |||
| 220 | # Adjust for having up to the first 4 characters in common |
||
| 221 | 1 | j = 4 if (minv >= 4) else minv |
|
| 222 | 1 | i = 0 |
|
| 223 | 1 | while (i < j) and (ying[i] == yang[i]) and (not ying[i].isdigit()): |
|
| 224 | 1 | i += 1 |
|
| 225 | 1 | if i: |
|
| 226 | 1 | weight += i * 0.1 * (1.0 - weight) |
|
| 227 | |||
| 228 | # Optionally adjust for long strings. |
||
| 229 | |||
| 230 | # After agreeing beginning chars, at least two more must agree and |
||
| 231 | # the agreeing characters must be > .5 of remaining characters. |
||
| 232 | 1 | if ( |
|
| 233 | long_strings |
||
| 234 | and (minv > 4) |
||
| 235 | and (num_com > i + 1) |
||
| 236 | and (2 * num_com >= minv + i) |
||
| 237 | ): |
||
| 238 | 1 | if not ying[0].isdigit(): |
|
| 239 | 1 | weight += (1.0 - weight) * ( |
|
| 240 | (num_com - i - 1) / (len(ying) + len(yang) - i * 2 + 2) |
||
| 241 | ) |
||
| 242 | |||
| 243 | 1 | return weight |
|
| 244 | |||
| 312 |