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