Conditions | 73 |
Total Lines | 246 |
Code Lines | 156 |
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 | : -(1 + len(self.stc.synoname_special_table[s_pos][1])) |
||
168 | ] |
||
169 | 1 | elif s_type == 'b': |
|
170 | 1 | loc = ( |
|
171 | full_tar1.find( |
||
172 | ' ' + self.stc.synoname_special_table[s_pos][1] + ' ' |
||
173 | ) |
||
174 | + 1 |
||
175 | ) |
||
176 | 1 | full_tar1 = ( |
|
177 | full_tar1[:loc] |
||
178 | + full_tar1[ |
||
179 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
180 | ] |
||
181 | ) |
||
182 | 1 | elif s_type == 'c': |
|
183 | 1 | full_tar1 = full_tar1[ |
|
184 | 1 + len(self.stc.synoname_special_table[s_pos][1]) : |
||
185 | ] |
||
186 | |||
187 | 1 | full_src1 = ' '.join((src_ln, src_fn)).replace('-', ' ').strip() |
|
188 | 1 | for s_pos, s_type in features['src_specials']: |
|
189 | 1 | if s_type == 'a': |
|
190 | 1 | full_src1 = full_src1[ |
|
191 | : -(1 + len(self.stc.synoname_special_table[s_pos][1])) |
||
192 | ] |
||
193 | 1 | elif s_type == 'b': |
|
194 | 1 | loc = ( |
|
195 | full_src1.find( |
||
196 | ' ' + self.stc.synoname_special_table[s_pos][1] + ' ' |
||
197 | ) |
||
198 | + 1 |
||
199 | ) |
||
200 | 1 | full_src1 = ( |
|
201 | full_src1[:loc] |
||
202 | + full_src1[ |
||
203 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
204 | ] |
||
205 | ) |
||
206 | 1 | elif s_type == 'c': |
|
207 | 1 | full_src1 = full_src1[ |
|
208 | 1 + len(self.stc.synoname_special_table[s_pos][1]) : |
||
209 | ] |
||
210 | |||
211 | 1 | full_tar2 = full_tar1 |
|
212 | 1 | for s_pos, s_type in features['tar_specials']: |
|
213 | 1 | if s_type == 'd': |
|
214 | 1 | full_tar2 = full_tar2[ |
|
215 | len(self.stc.synoname_special_table[s_pos][1]) : |
||
216 | ] |
||
217 | 1 | elif ( |
|
218 | s_type == 'X' |
||
219 | and self.stc.synoname_special_table[s_pos][1] in full_tar2 |
||
220 | ): |
||
221 | 1 | loc = full_tar2.find( |
|
222 | ' ' + self.stc.synoname_special_table[s_pos][1] |
||
223 | ) |
||
224 | 1 | full_tar2 = ( |
|
225 | full_tar2[:loc] |
||
226 | + full_tar2[ |
||
227 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
228 | ] |
||
229 | ) |
||
230 | |||
231 | 1 | full_src2 = full_src1 |
|
232 | 1 | for s_pos, s_type in features['src_specials']: |
|
233 | 1 | if s_type == 'd': |
|
234 | 1 | full_src2 = full_src2[ |
|
235 | len(self.stc.synoname_special_table[s_pos][1]) : |
||
236 | ] |
||
237 | 1 | elif ( |
|
238 | s_type == 'X' |
||
239 | and self.stc.synoname_special_table[s_pos][1] in full_src2 |
||
240 | ): |
||
241 | 1 | loc = full_src2.find( |
|
242 | ' ' + self.stc.synoname_special_table[s_pos][1] |
||
243 | ) |
||
244 | 1 | full_src2 = ( |
|
245 | full_src2[:loc] |
||
246 | + full_src2[ |
||
247 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
248 | ] |
||
249 | ) |
||
250 | |||
251 | 1 | full_tar1 = self._synoname_strip_punct(full_tar1) |
|
252 | 1 | tar1_words = full_tar1.split() |
|
253 | 1 | tar1_num_words = len(tar1_words) |
|
254 | |||
255 | 1 | full_src1 = self._synoname_strip_punct(full_src1) |
|
256 | 1 | src1_words = full_src1.split() |
|
257 | 1 | src1_num_words = len(src1_words) |
|
258 | |||
259 | 1 | full_tar2 = self._synoname_strip_punct(full_tar2) |
|
260 | 1 | tar2_words = full_tar2.split() |
|
261 | 1 | tar2_num_words = len(tar2_words) |
|
262 | |||
263 | 1 | full_src2 = self._synoname_strip_punct(full_src2) |
|
264 | 1 | src2_words = full_src2.split() |
|
265 | 1 | src2_num_words = len(src2_words) |
|
266 | |||
267 | # 2 |
||
268 | 1 | if ( |
|
269 | src1_num_words < 2 |
||
270 | and src_len_specials == 0 |
||
271 | and src2_num_words < 2 |
||
272 | and tar_len_specials == 0 |
||
273 | ): |
||
274 | 1 | return 0 |
|
275 | |||
276 | # 4 |
||
277 | 1 | if ( |
|
278 | tar1_num_words == 1 |
||
279 | and src1_num_words == 1 |
||
280 | and tar1_words[0] == src1_words[0] |
||
281 | ): |
||
282 | 1 | return 1 |
|
283 | 1 | if tar1_num_words < 2 and tar_len_specials == 0: |
|
284 | 1 | return 0 |
|
285 | |||
286 | # 5 |
||
287 | 1 | last_found = False |
|
288 | 1 | for word in tar1_words: |
|
289 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
290 | 1 | last_found = True |
|
291 | |||
292 | 1 | if not last_found: |
|
293 | 1 | for word in src1_words: |
|
294 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
295 | 1 | last_found = True |
|
296 | |||
297 | # 6 |
||
298 | 1 | matches = 0 |
|
299 | 1 | if last_found: |
|
300 | 1 | for i, s_word in enumerate(src1_words): |
|
301 | 1 | for j, t_word in enumerate(tar1_words): |
|
302 | 1 | if s_word == t_word: |
|
303 | 1 | src1_words[i] = '@' |
|
304 | 1 | tar1_words[j] = '@' |
|
305 | 1 | matches += 1 |
|
306 | 1 | w_ratio = matches / max(tar1_num_words, src1_num_words) |
|
307 | 1 | if matches > 1 or ( |
|
308 | matches == 1 |
||
309 | and src1_num_words == 1 |
||
310 | and tar1_num_words == 1 |
||
311 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
312 | ): |
||
313 | 1 | return w_ratio |
|
314 | |||
315 | # 8 |
||
316 | 1 | if ( |
|
317 | tar2_num_words == 1 |
||
318 | and src2_num_words == 1 |
||
319 | and tar2_words[0] == src2_words[0] |
||
320 | ): |
||
321 | 1 | return 1 |
|
322 | # I see no way that the following can be True if the equivalent in |
||
323 | # #4 was False. |
||
324 | if tar2_num_words < 2 and tar_len_specials == 0: # pragma: no cover |
||
325 | return 0 |
||
326 | |||
327 | # 9 |
||
328 | 1 | last_found = False |
|
329 | 1 | for word in tar2_words: |
|
330 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
331 | 1 | last_found = True |
|
332 | |||
333 | 1 | if not last_found: |
|
334 | 1 | for word in src2_words: |
|
335 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
336 | 1 | last_found = True |
|
337 | |||
338 | 1 | if not last_found: |
|
339 | 1 | return 0 |
|
340 | |||
341 | # 10 |
||
342 | 1 | matches = 0 |
|
343 | 1 | if last_found: |
|
344 | 1 | for i, s_word in enumerate(src2_words): |
|
345 | 1 | for j, t_word in enumerate(tar2_words): |
|
346 | 1 | if s_word == t_word: |
|
347 | 1 | src2_words[i] = '@' |
|
348 | 1 | tar2_words[j] = '@' |
|
349 | 1 | matches += 1 |
|
350 | 1 | w_ratio = matches / max(tar2_num_words, src2_num_words) |
|
351 | 1 | if matches > 1 or ( |
|
352 | matches == 1 |
||
353 | and src2_num_words == 1 |
||
354 | and tar2_num_words == 1 |
||
355 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
356 | ): |
||
357 | return w_ratio |
||
358 | |||
359 | 1 | return 0 |
|
360 | |||
740 |