Conditions | 11 |
Total Lines | 87 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Tests | 27 |
CRAP Score | 11 |
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._chao_jaccard.ChaoJaccard._get_estimates() 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 -*- |
||
151 | 1 | def _get_estimates(self, src, tar): |
|
152 | """Get the estimates U-hat & V-hat used for Chao's measures. |
||
153 | |||
154 | Parameters |
||
155 | ---------- |
||
156 | src : str |
||
157 | Source string for comparison |
||
158 | tar : str |
||
159 | Target string for comparison |
||
160 | |||
161 | Returns |
||
162 | ------- |
||
163 | tuple(float, float) |
||
164 | The estimates U-hat & V-hat |
||
165 | |||
166 | .. versionadded:: 0.4.1 |
||
167 | |||
168 | """ |
||
169 | 1 | src_card = self._src_card() # n |
|
170 | 1 | tar_card = self._tar_card() # m |
|
171 | |||
172 | 1 | src_token_list = self.params['tokenizer'].tokenize(src).get_list() |
|
173 | 1 | tar_token_list = self.params['tokenizer'].tokenize(tar).get_list() |
|
174 | |||
175 | 1 | src_sampled = Counter(choices(src_token_list, k=src_card)) |
|
176 | 1 | tar_sampled = Counter(choices(tar_token_list, k=tar_card)) |
|
177 | 1 | sample_intersection = src_sampled & tar_sampled |
|
178 | |||
179 | 1 | f_1_plus = sum( |
|
180 | 1 if src_sampled[tok] == 1 and tar_sampled[tok] >= 1 else 0 |
||
181 | for tok in sample_intersection |
||
182 | ) |
||
183 | 1 | f_2_plus = sum( |
|
184 | 1 if src_sampled[tok] == 2 and tar_sampled[tok] >= 1 else 0 |
||
185 | for tok in sample_intersection |
||
186 | ) |
||
187 | 1 | if not f_2_plus: |
|
188 | 1 | f_2_plus = 1 |
|
189 | |||
190 | 1 | f_plus_1 = sum( |
|
191 | 1 if src_sampled[tok] >= 1 and tar_sampled[tok] == 1 else 0 |
||
192 | for tok in sample_intersection |
||
193 | ) |
||
194 | 1 | f_plus_2 = sum( |
|
195 | 1 if src_sampled[tok] >= 1 and tar_sampled[tok] == 2 else 0 |
||
196 | for tok in sample_intersection |
||
197 | ) |
||
198 | 1 | if not f_plus_2: |
|
199 | 1 | f_plus_2 = 1 |
|
200 | |||
201 | 1 | u_hat = 0 |
|
202 | 1 | if src_card: |
|
203 | 1 | u_hat += sum( |
|
204 | src_sampled[tok] / src_card |
||
205 | for tok in sample_intersection.keys() |
||
206 | ) |
||
207 | 1 | if tar_card: |
|
208 | 1 | u_hat += ( |
|
209 | (tar_card - 1) |
||
210 | / tar_card |
||
211 | * f_plus_1 |
||
212 | / (2 * f_plus_2) |
||
213 | * sum( |
||
214 | src_sampled[tok] / src_card * (tar_sampled[tok] == 1) |
||
215 | for tok in sample_intersection.keys() |
||
216 | ) |
||
217 | ) |
||
218 | |||
219 | 1 | v_hat = 0 |
|
220 | 1 | if tar_card: |
|
221 | 1 | v_hat += sum( |
|
222 | tar_sampled[tok] / tar_card |
||
223 | for tok in sample_intersection.keys() |
||
224 | ) |
||
225 | 1 | if src_card: |
|
226 | 1 | v_hat += ( |
|
227 | (src_card - 1) |
||
228 | / src_card |
||
229 | * f_1_plus |
||
230 | / (2 * f_2_plus) |
||
231 | * sum( |
||
232 | tar_sampled[tok] / tar_card * (src_sampled[tok] == 1) |
||
233 | for tok in sample_intersection.keys() |
||
234 | ) |
||
235 | ) |
||
236 | |||
237 | 1 | return u_hat, v_hat |
|
238 | |||
244 |