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