Conditions | 73 |
Total Lines | 310 |
Code Lines | 204 |
Lines | 0 |
Ratio | 0 % |
Tests | 103 |
CRAP Score | 73.0047 |
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._synoname.Synoname._synoname_word_approximation() 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 -*- |
||
114 | 1 | def _synoname_word_approximation( |
|
115 | self, src_ln, tar_ln, src_fn='', tar_fn='', features=None |
||
116 | ): |
||
117 | """Return the Synoname word approximation score for two names. |
||
118 | |||
119 | Parameters |
||
120 | ---------- |
||
121 | src_ln : str |
||
122 | Last name of the source |
||
123 | tar_ln : str |
||
124 | Last name of the target |
||
125 | src_fn : str |
||
126 | First name of the source (optional) |
||
127 | tar_fn : str |
||
128 | First name of the target (optional) |
||
129 | features : dict |
||
130 | A dict containing special features calculated using |
||
131 | :py:class:`fingerprint.SynonameToolcode` (optional) |
||
132 | |||
133 | Returns |
||
134 | ------- |
||
135 | float |
||
136 | The word approximation score |
||
137 | |||
138 | Examples |
||
139 | -------- |
||
140 | >>> pe = Synoname() |
||
141 | >>> pe._synoname_word_approximation('Smith Waterman', 'Waterman', |
||
142 | ... 'Tom Joe Bob', 'Tom Joe') |
||
143 | 0.6 |
||
144 | |||
145 | """ |
||
146 | 1 | if features is None: |
|
147 | 1 | features = {} |
|
148 | 1 | if 'src_specials' not in features: |
|
149 | 1 | features['src_specials'] = [] |
|
150 | 1 | if 'tar_specials' not in features: |
|
151 | 1 | features['tar_specials'] = [] |
|
152 | |||
153 | 1 | src_len_specials = len(features['src_specials']) |
|
154 | 1 | tar_len_specials = len(features['tar_specials']) |
|
155 | |||
156 | # 1 |
||
157 | 1 | if ('gen_conflict' in features and features['gen_conflict']) or ( |
|
158 | 'roman_conflict' in features and features['roman_conflict'] |
||
159 | ): |
||
160 | 1 | return 0 |
|
161 | |||
162 | # 3 & 7 |
||
163 | 1 | full_tar1 = ' '.join((tar_ln, tar_fn)).replace('-', ' ').strip() |
|
164 | 1 | for s_pos, s_type in features['tar_specials']: |
|
165 | 1 | if s_type == 'a': |
|
166 | 1 | full_tar1 = full_tar1[ |
|
167 | : -( |
||
168 | 1 |
||
169 | + len( |
||
170 | self._stc._synoname_special_table[ # noqa: SF01 |
||
171 | s_pos |
||
172 | ][1] |
||
173 | ) |
||
174 | ) |
||
175 | ] |
||
176 | 1 | elif s_type == 'b': |
|
177 | 1 | loc = ( |
|
178 | full_tar1.find( |
||
179 | ' ' |
||
180 | + self._stc._synoname_special_table[ # noqa: SF01 |
||
181 | s_pos |
||
182 | ][1] |
||
183 | + ' ' |
||
184 | ) |
||
185 | + 1 |
||
186 | ) |
||
187 | 1 | full_tar1 = ( |
|
188 | full_tar1[:loc] |
||
189 | + full_tar1[ |
||
190 | loc |
||
191 | + len( |
||
192 | self._stc._synoname_special_table[ # noqa: SF01 |
||
193 | s_pos |
||
194 | ][1] |
||
195 | ) : |
||
196 | ] |
||
197 | ) |
||
198 | 1 | elif s_type == 'c': |
|
199 | 1 | full_tar1 = full_tar1[ |
|
200 | 1 |
||
201 | + len( |
||
202 | self._stc._synoname_special_table[s_pos][ # noqa: SF01 |
||
203 | 1 |
||
204 | ] |
||
205 | ) : |
||
206 | ] |
||
207 | |||
208 | 1 | full_src1 = ' '.join((src_ln, src_fn)).replace('-', ' ').strip() |
|
209 | 1 | for s_pos, s_type in features['src_specials']: |
|
210 | 1 | if s_type == 'a': |
|
211 | 1 | full_src1 = full_src1[ |
|
212 | : -( |
||
213 | 1 |
||
214 | + len( |
||
215 | self._stc._synoname_special_table[ # noqa: SF01 |
||
216 | s_pos |
||
217 | ][1] |
||
218 | ) |
||
219 | ) |
||
220 | ] |
||
221 | 1 | elif s_type == 'b': |
|
222 | 1 | loc = ( |
|
223 | full_src1.find( |
||
224 | ' ' |
||
225 | + self._stc._synoname_special_table[ # noqa: SF01 |
||
226 | s_pos |
||
227 | ][1] |
||
228 | + ' ' |
||
229 | ) |
||
230 | + 1 |
||
231 | ) |
||
232 | 1 | full_src1 = ( |
|
233 | full_src1[:loc] |
||
234 | + full_src1[ |
||
235 | loc |
||
236 | + len( |
||
237 | self._stc._synoname_special_table[ # noqa: SF01 |
||
238 | s_pos |
||
239 | ][1] |
||
240 | ) : |
||
241 | ] |
||
242 | ) |
||
243 | 1 | elif s_type == 'c': |
|
244 | 1 | full_src1 = full_src1[ |
|
245 | 1 |
||
246 | + len( |
||
247 | self._stc._synoname_special_table[s_pos][ # noqa: SF01 |
||
248 | 1 |
||
249 | ] |
||
250 | ) : |
||
251 | ] |
||
252 | |||
253 | 1 | full_tar2 = full_tar1 |
|
254 | 1 | for s_pos, s_type in features['tar_specials']: |
|
255 | 1 | if s_type == 'd': |
|
256 | 1 | full_tar2 = full_tar2[ |
|
257 | len( |
||
258 | self._stc._synoname_special_table[s_pos][ # noqa: SF01 |
||
259 | 1 |
||
260 | ] |
||
261 | ) : |
||
262 | ] |
||
263 | 1 | elif ( |
|
264 | s_type == 'X' |
||
265 | and self._stc._synoname_special_table[s_pos][1] # noqa: SF01 |
||
266 | in full_tar2 |
||
267 | ): |
||
268 | 1 | loc = full_tar2.find( |
|
269 | ' ' |
||
270 | + self._stc._synoname_special_table[s_pos][1] # noqa: SF01 |
||
271 | ) |
||
272 | 1 | full_tar2 = ( |
|
273 | full_tar2[:loc] |
||
274 | + full_tar2[ |
||
275 | loc |
||
276 | + len( |
||
277 | self._stc._synoname_special_table[ # noqa: SF01 |
||
278 | s_pos |
||
279 | ][1] |
||
280 | ) : |
||
281 | ] |
||
282 | ) |
||
283 | |||
284 | 1 | full_src2 = full_src1 |
|
285 | 1 | for s_pos, s_type in features['src_specials']: |
|
286 | 1 | if s_type == 'd': |
|
287 | 1 | full_src2 = full_src2[ |
|
288 | len( |
||
289 | self._stc._synoname_special_table[s_pos][ # noqa: SF01 |
||
290 | 1 |
||
291 | ] |
||
292 | ) : |
||
293 | ] |
||
294 | 1 | elif ( |
|
295 | s_type == 'X' |
||
296 | and self._stc._synoname_special_table[s_pos][1] # noqa: SF01 |
||
297 | in full_src2 |
||
298 | ): |
||
299 | 1 | loc = full_src2.find( |
|
300 | ' ' |
||
301 | + self._stc._synoname_special_table[s_pos][1] # noqa: SF01 |
||
302 | ) |
||
303 | 1 | full_src2 = ( |
|
304 | full_src2[:loc] |
||
305 | + full_src2[ |
||
306 | loc |
||
307 | + len( |
||
308 | self._stc._synoname_special_table[ # noqa: SF01 |
||
309 | s_pos |
||
310 | ][1] |
||
311 | ) : |
||
312 | ] |
||
313 | ) |
||
314 | |||
315 | 1 | full_tar1 = self._synoname_strip_punct(full_tar1) |
|
316 | 1 | tar1_words = full_tar1.split() |
|
317 | 1 | tar1_num_words = len(tar1_words) |
|
318 | |||
319 | 1 | full_src1 = self._synoname_strip_punct(full_src1) |
|
320 | 1 | src1_words = full_src1.split() |
|
321 | 1 | src1_num_words = len(src1_words) |
|
322 | |||
323 | 1 | full_tar2 = self._synoname_strip_punct(full_tar2) |
|
324 | 1 | tar2_words = full_tar2.split() |
|
325 | 1 | tar2_num_words = len(tar2_words) |
|
326 | |||
327 | 1 | full_src2 = self._synoname_strip_punct(full_src2) |
|
328 | 1 | src2_words = full_src2.split() |
|
329 | 1 | src2_num_words = len(src2_words) |
|
330 | |||
331 | # 2 |
||
332 | 1 | if ( |
|
333 | src1_num_words < 2 |
||
334 | and src_len_specials == 0 |
||
335 | and src2_num_words < 2 |
||
336 | and tar_len_specials == 0 |
||
337 | ): |
||
338 | 1 | return 0 |
|
339 | |||
340 | # 4 |
||
341 | 1 | if ( |
|
342 | tar1_num_words == 1 |
||
343 | and src1_num_words == 1 |
||
344 | and tar1_words[0] == src1_words[0] |
||
345 | ): |
||
346 | 1 | return 1 |
|
347 | 1 | if tar1_num_words < 2 and tar_len_specials == 0: |
|
348 | 1 | return 0 |
|
349 | |||
350 | # 5 |
||
351 | 1 | last_found = False |
|
352 | 1 | for word in tar1_words: |
|
353 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
354 | 1 | last_found = True |
|
355 | |||
356 | 1 | if not last_found: |
|
357 | 1 | for word in src1_words: |
|
358 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
359 | 1 | last_found = True |
|
360 | |||
361 | # 6 |
||
362 | 1 | matches = 0 |
|
363 | 1 | if last_found: |
|
364 | 1 | for i, s_word in enumerate(src1_words): |
|
365 | 1 | for j, t_word in enumerate(tar1_words): |
|
366 | 1 | if s_word == t_word: |
|
367 | 1 | src1_words[i] = '@' |
|
368 | 1 | tar1_words[j] = '@' |
|
369 | 1 | matches += 1 |
|
370 | 1 | w_ratio = matches / max(tar1_num_words, src1_num_words) |
|
371 | 1 | if matches > 1 or ( |
|
372 | matches == 1 |
||
373 | and src1_num_words == 1 |
||
374 | and tar1_num_words == 1 |
||
375 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
376 | ): |
||
377 | 1 | return w_ratio |
|
378 | |||
379 | # 8 |
||
380 | 1 | if ( |
|
381 | tar2_num_words == 1 |
||
382 | and src2_num_words == 1 |
||
383 | and tar2_words[0] == src2_words[0] |
||
384 | ): |
||
385 | 1 | return 1 |
|
386 | # I see no way that the following can be True if the equivalent in |
||
387 | # #4 was False. |
||
388 | if tar2_num_words < 2 and tar_len_specials == 0: # pragma: no cover |
||
389 | return 0 |
||
390 | |||
391 | # 9 |
||
392 | 1 | last_found = False |
|
393 | 1 | for word in tar2_words: |
|
394 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
395 | 1 | last_found = True |
|
396 | |||
397 | 1 | if not last_found: |
|
398 | 1 | for word in src2_words: |
|
399 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
400 | 1 | last_found = True |
|
401 | |||
402 | 1 | if not last_found: |
|
403 | 1 | return 0 |
|
404 | |||
405 | # 10 |
||
406 | 1 | matches = 0 |
|
407 | 1 | if last_found: |
|
408 | 1 | for i, s_word in enumerate(src2_words): |
|
409 | 1 | for j, t_word in enumerate(tar2_words): |
|
410 | 1 | if s_word == t_word: |
|
411 | 1 | src2_words[i] = '@' |
|
412 | 1 | tar2_words[j] = '@' |
|
413 | 1 | matches += 1 |
|
414 | 1 | w_ratio = matches / max(tar2_num_words, src2_num_words) |
|
415 | 1 | if matches > 1 or ( |
|
416 | matches == 1 |
||
417 | and src2_num_words == 1 |
||
418 | and tar2_num_words == 1 |
||
419 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
420 | ): |
||
421 | return w_ratio |
||
422 | |||
423 | 1 | return 0 |
|
424 | |||
809 |