Conditions | 31 |
Total Lines | 155 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Tests | 61 |
CRAP Score | 31 |
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_winkler.JaroWinkler.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 | # Copyright 2014-2020 by Christopher C. Little. |
||
96 | def sim(self, src: str, tar: str) -> float: |
||
97 | """Return the Jaro or Jaro-Winkler similarity of two strings. |
||
98 | |||
99 | Parameters |
||
100 | ---------- |
||
101 | 1 | src : str |
|
102 | 1 | Source string for comparison |
|
103 | 1 | tar : str |
|
104 | 1 | Target string for comparison |
|
105 | 1 | ||
106 | 1 | Returns |
|
107 | ------- |
||
108 | 1 | float |
|
109 | Jaro or Jaro-Winkler similarity |
||
110 | |||
111 | Raises |
||
112 | ------ |
||
113 | ValueError |
||
114 | Unsupported boost_threshold assignment; boost_threshold must be |
||
115 | between 0 and 1. |
||
116 | ValueError |
||
117 | Unsupported scaling_factor assignment; scaling_factor must be |
||
118 | between 0 and 0.25.' |
||
119 | |||
120 | Examples |
||
121 | -------- |
||
122 | >>> cmp = JaroWinkler() |
||
123 | >>> round(cmp.sim('cat', 'hat'), 12) |
||
124 | 0.777777777778 |
||
125 | >>> round(cmp.sim('Niall', 'Neil'), 12) |
||
126 | 0.805 |
||
127 | >>> round(cmp.sim('aluminum', 'Catalan'), 12) |
||
128 | 0.60119047619 |
||
129 | >>> round(cmp.sim('ATCG', 'TAGC'), 12) |
||
130 | 0.833333333333 |
||
131 | |||
132 | >>> cmp = JaroWinkler(mode='jaro') |
||
133 | >>> round(cmp.sim('cat', 'hat'), 12) |
||
134 | 0.777777777778 |
||
135 | >>> round(cmp.sim('Niall', 'Neil'), 12) |
||
136 | 0.783333333333 |
||
137 | >>> round(cmp.sim('aluminum', 'Catalan'), 12) |
||
138 | 0.60119047619 |
||
139 | >>> round(cmp.sim('ATCG', 'TAGC'), 12) |
||
140 | 0.833333333333 |
||
141 | |||
142 | |||
143 | .. versionadded:: 0.1.0 |
||
144 | .. versionchanged:: 0.3.6 |
||
145 | Encapsulated in class |
||
146 | |||
147 | """ |
||
148 | if self._mode == 'winkler': |
||
149 | if self._boost_threshold > 1 or self._boost_threshold < 0: |
||
150 | raise ValueError( |
||
151 | 'Unsupported boost_threshold assignment; ' |
||
152 | + 'boost_threshold must be between 0 and 1.' |
||
153 | ) |
||
154 | if self._scaling_factor > 0.25 or self._scaling_factor < 0: |
||
155 | raise ValueError( |
||
156 | 'Unsupported scaling_factor assignment; ' |
||
157 | + 'scaling_factor must be between 0 and 0.25.' |
||
158 | 1 | ) |
|
159 | 1 | ||
160 | 1 | if src == tar: |
|
161 | return 1.0 |
||
162 | |||
163 | tokenizer = QGrams(self._qval) |
||
164 | 1 | tokenizer.tokenize(src.strip()) |
|
165 | 1 | src_list = tokenizer.get_list() |
|
166 | tokenizer.tokenize(tar.strip()) |
||
167 | tar_list = tokenizer.get_list() |
||
168 | |||
169 | lens = len(src_list) |
||
170 | 1 | lent = len(tar_list) |
|
171 | 1 | ||
172 | # If either string is blank - return - added in Version 2 |
||
173 | 1 | if lens == 0 or lent == 0: |
|
174 | 1 | return 0.0 |
|
175 | |||
176 | 1 | if lens > lent: |
|
177 | 1 | search_range = lens |
|
178 | minv = lent |
||
179 | else: |
||
180 | 1 | search_range = lent |
|
181 | 1 | minv = lens |
|
182 | |||
183 | 1 | # Zero out the flags |
|
184 | 1 | src_flag = [0] * search_range |
|
185 | 1 | tar_flag = [0] * search_range |
|
186 | search_range = max(0, search_range // 2 - 1) |
||
187 | 1 | ||
188 | 1 | # Looking only within the search range, |
|
189 | # count and flag the matched pairs. |
||
190 | num_com = 0 |
||
191 | 1 | yl1 = lent - 1 |
|
192 | 1 | for i in range(lens): |
|
193 | 1 | low_lim = (i - search_range) if (i >= search_range) else 0 |
|
194 | hi_lim = (i + search_range) if ((i + search_range) <= yl1) else yl1 |
||
195 | for j in range(low_lim, hi_lim + 1): |
||
196 | if (tar_flag[j] == 0) and (tar_list[j] == src_list[i]): |
||
197 | 1 | tar_flag[j] = 1 |
|
198 | 1 | src_flag[i] = 1 |
|
199 | 1 | num_com += 1 |
|
200 | 1 | break |
|
201 | 1 | ||
202 | 1 | # If no characters in common - return |
|
203 | 1 | if num_com == 0: |
|
204 | 1 | return 0.0 |
|
205 | 1 | ||
206 | 1 | # Count the number of transpositions |
|
207 | 1 | k = n_trans = 0 |
|
208 | for i in range(lens): |
||
209 | if src_flag[i] != 0: |
||
210 | 1 | j = 0 |
|
211 | 1 | for j in range(k, lent): # pragma: no branch |
|
212 | if tar_flag[j] != 0: |
||
213 | k = j + 1 |
||
214 | 1 | break |
|
215 | 1 | if src_list[i] != tar_list[j]: |
|
216 | 1 | n_trans += 1 |
|
217 | 1 | n_trans //= 2 |
|
218 | 1 | ||
219 | 1 | # Main weight computation for Jaro distance |
|
220 | 1 | weight = ( |
|
221 | 1 | num_com / lens + num_com / lent + (num_com - n_trans) / num_com |
|
222 | 1 | ) |
|
223 | 1 | weight /= 3.0 |
|
224 | 1 | ||
225 | # Continue to boost the weight if the strings are similar |
||
226 | # This is the Winkler portion of Jaro-Winkler distance |
||
227 | 1 | if self._mode == 'winkler' and weight > self._boost_threshold: |
|
228 | |||
229 | # Adjust for having up to the first 4 characters in common |
||
230 | 1 | j = 4 if (minv >= 4) else minv |
|
231 | i = 0 |
||
232 | while (i < j) and (src_list[i] == tar_list[i]): |
||
233 | i += 1 |
||
234 | 1 | weight += i * self._scaling_factor * (1.0 - weight) |
|
235 | |||
236 | # Optionally adjust for long strings. |
||
237 | 1 | ||
238 | 1 | # After agreeing beginning chars, at least two more must agree and |
|
239 | 1 | # the agreeing characters must be > .5 of remaining characters. |
|
240 | 1 | if ( |
|
241 | 1 | self._long_strings |
|
242 | and (minv > 4) |
||
243 | and (num_com > i + 1) |
||
244 | and (2 * num_com >= minv + i) |
||
245 | ): |
||
246 | weight += (1.0 - weight) * ( |
||
247 | 1 | (num_com - i - 1) / (lens + lent - i * 2 + 2) |
|
248 | ) |
||
249 | |||
250 | return weight |
||
251 | |||
257 |