| Conditions | 37 |
| Total Lines | 185 |
| Code Lines | 114 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 70 |
| 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._jaro.sim_strcmp95() 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 -*- |
||
| 46 | 1 | def sim_strcmp95(src, tar, long_strings=False): |
|
|
|
|||
| 47 | """Return the strcmp95 similarity of two strings. |
||
| 48 | |||
| 49 | This is a Python translation of the C code for strcmp95: |
||
| 50 | http://web.archive.org/web/20110629121242/http://www.census.gov/geo/msb/stand/strcmp.c |
||
| 51 | :cite:`Winkler:1994`. |
||
| 52 | The above file is a US Government publication and, accordingly, |
||
| 53 | in the public domain. |
||
| 54 | |||
| 55 | This is based on the Jaro-Winkler distance, but also attempts to correct |
||
| 56 | for some common typos and frequently confused characters. It is also |
||
| 57 | limited to uppercase ASCII characters, so it is appropriate to American |
||
| 58 | names, but not much else. |
||
| 59 | |||
| 60 | :param str src: source string for comparison |
||
| 61 | :param str tar: target string for comparison |
||
| 62 | :param bool long_strings: set to True to "Increase the probability of a |
||
| 63 | match when the number of matched characters is large. This option |
||
| 64 | allows for a little more tolerance when the strings are large. It is |
||
| 65 | not an appropriate test when comparing fixed length fields such as |
||
| 66 | phone and social security numbers." |
||
| 67 | :returns: strcmp95 similarity |
||
| 68 | :rtype: float |
||
| 69 | |||
| 70 | >>> sim_strcmp95('cat', 'hat') |
||
| 71 | 0.7777777777777777 |
||
| 72 | >>> sim_strcmp95('Niall', 'Neil') |
||
| 73 | 0.8454999999999999 |
||
| 74 | >>> sim_strcmp95('aluminum', 'Catalan') |
||
| 75 | 0.6547619047619048 |
||
| 76 | >>> sim_strcmp95('ATCG', 'TAGC') |
||
| 77 | 0.8333333333333334 |
||
| 78 | """ |
||
| 79 | |||
| 80 | 1 | def _in_range(char): |
|
| 81 | """Return True if char is in the range (0, 91).""" |
||
| 82 | 1 | return 91 > ord(char) > 0 |
|
| 83 | |||
| 84 | 1 | ying = src.strip().upper() |
|
| 85 | 1 | yang = tar.strip().upper() |
|
| 86 | |||
| 87 | 1 | if ying == yang: |
|
| 88 | 1 | return 1.0 |
|
| 89 | # If either string is blank - return - added in Version 2 |
||
| 90 | 1 | if not ying or not yang: |
|
| 91 | 1 | return 0.0 |
|
| 92 | |||
| 93 | 1 | adjwt = defaultdict(int) |
|
| 94 | 1 | sp_mx = ( |
|
| 95 | ('A', 'E'), |
||
| 96 | ('A', 'I'), |
||
| 97 | ('A', 'O'), |
||
| 98 | ('A', 'U'), |
||
| 99 | ('B', 'V'), |
||
| 100 | ('E', 'I'), |
||
| 101 | ('E', 'O'), |
||
| 102 | ('E', 'U'), |
||
| 103 | ('I', 'O'), |
||
| 104 | ('I', 'U'), |
||
| 105 | ('O', 'U'), |
||
| 106 | ('I', 'Y'), |
||
| 107 | ('E', 'Y'), |
||
| 108 | ('C', 'G'), |
||
| 109 | ('E', 'F'), |
||
| 110 | ('W', 'U'), |
||
| 111 | ('W', 'V'), |
||
| 112 | ('X', 'K'), |
||
| 113 | ('S', 'Z'), |
||
| 114 | ('X', 'S'), |
||
| 115 | ('Q', 'C'), |
||
| 116 | ('U', 'V'), |
||
| 117 | ('M', 'N'), |
||
| 118 | ('L', 'I'), |
||
| 119 | ('Q', 'O'), |
||
| 120 | ('P', 'R'), |
||
| 121 | ('I', 'J'), |
||
| 122 | ('2', 'Z'), |
||
| 123 | ('5', 'S'), |
||
| 124 | ('8', 'B'), |
||
| 125 | ('1', 'I'), |
||
| 126 | ('1', 'L'), |
||
| 127 | ('0', 'O'), |
||
| 128 | ('0', 'Q'), |
||
| 129 | ('C', 'K'), |
||
| 130 | ('G', 'J'), |
||
| 131 | ) |
||
| 132 | |||
| 133 | # Initialize the adjwt array on the first call to the function only. |
||
| 134 | # The adjwt array is used to give partial credit for characters that |
||
| 135 | # may be errors due to known phonetic or character recognition errors. |
||
| 136 | # A typical example is to match the letter "O" with the number "0" |
||
| 137 | 1 | for i in sp_mx: |
|
| 138 | 1 | adjwt[(i[0], i[1])] = 3 |
|
| 139 | 1 | adjwt[(i[1], i[0])] = 3 |
|
| 140 | |||
| 141 | 1 | if len(ying) > len(yang): |
|
| 142 | 1 | search_range = len(ying) |
|
| 143 | 1 | minv = len(yang) |
|
| 144 | else: |
||
| 145 | 1 | search_range = len(yang) |
|
| 146 | 1 | minv = len(ying) |
|
| 147 | |||
| 148 | # Blank out the flags |
||
| 149 | 1 | ying_flag = [0] * search_range |
|
| 150 | 1 | yang_flag = [0] * search_range |
|
| 151 | 1 | search_range = max(0, search_range // 2 - 1) |
|
| 152 | |||
| 153 | # Looking only within the search range, count and flag the matched pairs. |
||
| 154 | 1 | num_com = 0 |
|
| 155 | 1 | yl1 = len(yang) - 1 |
|
| 156 | 1 | for i in range(len(ying)): |
|
| 157 | 1 | low_lim = (i - search_range) if (i >= search_range) else 0 |
|
| 158 | 1 | hi_lim = (i + search_range) if ((i + search_range) <= yl1) else yl1 |
|
| 159 | 1 | for j in range(low_lim, hi_lim + 1): |
|
| 160 | 1 | if (yang_flag[j] == 0) and (yang[j] == ying[i]): |
|
| 161 | 1 | yang_flag[j] = 1 |
|
| 162 | 1 | ying_flag[i] = 1 |
|
| 163 | 1 | num_com += 1 |
|
| 164 | 1 | break |
|
| 165 | |||
| 166 | # If no characters in common - return |
||
| 167 | 1 | if num_com == 0: |
|
| 168 | 1 | return 0.0 |
|
| 169 | |||
| 170 | # Count the number of transpositions |
||
| 171 | 1 | k = n_trans = 0 |
|
| 172 | 1 | for i in range(len(ying)): |
|
| 173 | 1 | if ying_flag[i] != 0: |
|
| 174 | 1 | j = 0 |
|
| 175 | 1 | for j in range(k, len(yang)): # pragma: no branch |
|
| 176 | 1 | if yang_flag[j] != 0: |
|
| 177 | 1 | k = j + 1 |
|
| 178 | 1 | break |
|
| 179 | 1 | if ying[i] != yang[j]: |
|
| 180 | 1 | n_trans += 1 |
|
| 181 | 1 | n_trans //= 2 |
|
| 182 | |||
| 183 | # Adjust for similarities in unmatched characters |
||
| 184 | 1 | n_simi = 0 |
|
| 185 | 1 | if minv > num_com: |
|
| 186 | 1 | for i in range(len(ying)): |
|
| 187 | 1 | if ying_flag[i] == 0 and _in_range(ying[i]): |
|
| 188 | 1 | for j in range(len(yang)): |
|
| 189 | 1 | if yang_flag[j] == 0 and _in_range(yang[j]): |
|
| 190 | 1 | if (ying[i], yang[j]) in adjwt: |
|
| 191 | 1 | n_simi += adjwt[(ying[i], yang[j])] |
|
| 192 | 1 | yang_flag[j] = 2 |
|
| 193 | 1 | break |
|
| 194 | 1 | num_sim = n_simi / 10.0 + num_com |
|
| 195 | |||
| 196 | # Main weight computation |
||
| 197 | 1 | weight = ( |
|
| 198 | num_sim / len(ying) |
||
| 199 | + num_sim / len(yang) |
||
| 200 | + (num_com - n_trans) / num_com |
||
| 201 | ) |
||
| 202 | 1 | weight /= 3.0 |
|
| 203 | |||
| 204 | # Continue to boost the weight if the strings are similar |
||
| 205 | 1 | if weight > 0.7: |
|
| 206 | |||
| 207 | # Adjust for having up to the first 4 characters in common |
||
| 208 | 1 | j = 4 if (minv >= 4) else minv |
|
| 209 | 1 | i = 0 |
|
| 210 | 1 | while (i < j) and (ying[i] == yang[i]) and (not ying[i].isdigit()): |
|
| 211 | 1 | i += 1 |
|
| 212 | 1 | if i: |
|
| 213 | 1 | weight += i * 0.1 * (1.0 - weight) |
|
| 214 | |||
| 215 | # Optionally adjust for long strings. |
||
| 216 | |||
| 217 | # After agreeing beginning chars, at least two more must agree and |
||
| 218 | # the agreeing characters must be > .5 of remaining characters. |
||
| 219 | 1 | if ( |
|
| 220 | long_strings |
||
| 221 | and (minv > 4) |
||
| 222 | and (num_com > i + 1) |
||
| 223 | and (2 * num_com >= minv + i) |
||
| 224 | ): |
||
| 225 | 1 | if not ying[0].isdigit(): |
|
| 226 | 1 | weight += (1.0 - weight) * ( |
|
| 227 | (num_com - i - 1) / (len(ying) + len(yang) - i * 2 + 2) |
||
| 228 | ) |
||
| 229 | |||
| 230 | 1 | return weight |
|
| 231 | |||
| 490 |