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