Conditions | 73 |
Total Lines | 237 |
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 -*- |
||
109 | 1 | def _synoname_word_approximation( |
|
110 | self, src_ln, tar_ln, src_fn='', tar_fn='', features=None |
||
111 | ): |
||
112 | """Return the Synoname word approximation score for two names. |
||
113 | |||
114 | Args: |
||
115 | src_ln (str): Last name of the source |
||
116 | tar_ln (str): Last name of the target |
||
117 | src_fn (str): First name of the source (optional) |
||
118 | tar_fn (str): First name of the target (optional) |
||
119 | features (dict): A dict containing special features calculated |
||
120 | using :py:class:`fingerprint.SynonameToolcode` (optional) |
||
121 | |||
122 | Returns: |
||
123 | float: The word approximation score |
||
124 | |||
125 | Examples: |
||
126 | >>> pe = Synoname() |
||
127 | >>> pe._synoname_word_approximation('Smith Waterman', 'Waterman', |
||
128 | ... 'Tom Joe Bob', 'Tom Joe') |
||
129 | 0.6 |
||
130 | |||
131 | """ |
||
132 | 1 | if features is None: |
|
133 | 1 | features = {} |
|
134 | 1 | if 'src_specials' not in features: |
|
135 | 1 | features['src_specials'] = [] |
|
136 | 1 | if 'tar_specials' not in features: |
|
137 | 1 | features['tar_specials'] = [] |
|
138 | |||
139 | 1 | src_len_specials = len(features['src_specials']) |
|
140 | 1 | tar_len_specials = len(features['tar_specials']) |
|
141 | |||
142 | # 1 |
||
143 | 1 | if ('gen_conflict' in features and features['gen_conflict']) or ( |
|
144 | 'roman_conflict' in features and features['roman_conflict'] |
||
145 | ): |
||
146 | 1 | return 0 |
|
147 | |||
148 | # 3 & 7 |
||
149 | 1 | full_tar1 = ' '.join((tar_ln, tar_fn)).replace('-', ' ').strip() |
|
150 | 1 | for s_pos, s_type in features['tar_specials']: |
|
151 | 1 | if s_type == 'a': |
|
152 | 1 | full_tar1 = full_tar1[ |
|
153 | : -(1 + len(self.stc.synoname_special_table[s_pos][1])) |
||
154 | ] |
||
155 | 1 | elif s_type == 'b': |
|
156 | 1 | loc = ( |
|
157 | full_tar1.find( |
||
158 | ' ' + self.stc.synoname_special_table[s_pos][1] + ' ' |
||
159 | ) |
||
160 | + 1 |
||
161 | ) |
||
162 | 1 | full_tar1 = ( |
|
163 | full_tar1[:loc] |
||
164 | + full_tar1[ |
||
165 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
166 | ] |
||
167 | ) |
||
168 | 1 | elif s_type == 'c': |
|
169 | 1 | full_tar1 = full_tar1[ |
|
170 | 1 + len(self.stc.synoname_special_table[s_pos][1]) : |
||
171 | ] |
||
172 | |||
173 | 1 | full_src1 = ' '.join((src_ln, src_fn)).replace('-', ' ').strip() |
|
174 | 1 | for s_pos, s_type in features['src_specials']: |
|
175 | 1 | if s_type == 'a': |
|
176 | 1 | full_src1 = full_src1[ |
|
177 | : -(1 + len(self.stc.synoname_special_table[s_pos][1])) |
||
178 | ] |
||
179 | 1 | elif s_type == 'b': |
|
180 | 1 | loc = ( |
|
181 | full_src1.find( |
||
182 | ' ' + self.stc.synoname_special_table[s_pos][1] + ' ' |
||
183 | ) |
||
184 | + 1 |
||
185 | ) |
||
186 | 1 | full_src1 = ( |
|
187 | full_src1[:loc] |
||
188 | + full_src1[ |
||
189 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
190 | ] |
||
191 | ) |
||
192 | 1 | elif s_type == 'c': |
|
193 | 1 | full_src1 = full_src1[ |
|
194 | 1 + len(self.stc.synoname_special_table[s_pos][1]) : |
||
195 | ] |
||
196 | |||
197 | 1 | full_tar2 = full_tar1 |
|
198 | 1 | for s_pos, s_type in features['tar_specials']: |
|
199 | 1 | if s_type == 'd': |
|
200 | 1 | full_tar2 = full_tar2[ |
|
201 | len(self.stc.synoname_special_table[s_pos][1]) : |
||
202 | ] |
||
203 | 1 | elif ( |
|
204 | s_type == 'X' |
||
205 | and self.stc.synoname_special_table[s_pos][1] in full_tar2 |
||
206 | ): |
||
207 | 1 | loc = full_tar2.find( |
|
208 | ' ' + self.stc.synoname_special_table[s_pos][1] |
||
209 | ) |
||
210 | 1 | full_tar2 = ( |
|
211 | full_tar2[:loc] |
||
212 | + full_tar2[ |
||
213 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
214 | ] |
||
215 | ) |
||
216 | |||
217 | 1 | full_src2 = full_src1 |
|
218 | 1 | for s_pos, s_type in features['src_specials']: |
|
219 | 1 | if s_type == 'd': |
|
220 | 1 | full_src2 = full_src2[ |
|
221 | len(self.stc.synoname_special_table[s_pos][1]) : |
||
222 | ] |
||
223 | 1 | elif ( |
|
224 | s_type == 'X' |
||
225 | and self.stc.synoname_special_table[s_pos][1] in full_src2 |
||
226 | ): |
||
227 | 1 | loc = full_src2.find( |
|
228 | ' ' + self.stc.synoname_special_table[s_pos][1] |
||
229 | ) |
||
230 | 1 | full_src2 = ( |
|
231 | full_src2[:loc] |
||
232 | + full_src2[ |
||
233 | loc + len(self.stc.synoname_special_table[s_pos][1]) : |
||
234 | ] |
||
235 | ) |
||
236 | |||
237 | 1 | full_tar1 = self._synoname_strip_punct(full_tar1) |
|
238 | 1 | tar1_words = full_tar1.split() |
|
239 | 1 | tar1_num_words = len(tar1_words) |
|
240 | |||
241 | 1 | full_src1 = self._synoname_strip_punct(full_src1) |
|
242 | 1 | src1_words = full_src1.split() |
|
243 | 1 | src1_num_words = len(src1_words) |
|
244 | |||
245 | 1 | full_tar2 = self._synoname_strip_punct(full_tar2) |
|
246 | 1 | tar2_words = full_tar2.split() |
|
247 | 1 | tar2_num_words = len(tar2_words) |
|
248 | |||
249 | 1 | full_src2 = self._synoname_strip_punct(full_src2) |
|
250 | 1 | src2_words = full_src2.split() |
|
251 | 1 | src2_num_words = len(src2_words) |
|
252 | |||
253 | # 2 |
||
254 | 1 | if ( |
|
255 | src1_num_words < 2 |
||
256 | and src_len_specials == 0 |
||
257 | and src2_num_words < 2 |
||
258 | and tar_len_specials == 0 |
||
259 | ): |
||
260 | 1 | return 0 |
|
261 | |||
262 | # 4 |
||
263 | 1 | if ( |
|
264 | tar1_num_words == 1 |
||
265 | and src1_num_words == 1 |
||
266 | and tar1_words[0] == src1_words[0] |
||
267 | ): |
||
268 | 1 | return 1 |
|
269 | 1 | if tar1_num_words < 2 and tar_len_specials == 0: |
|
270 | 1 | return 0 |
|
271 | |||
272 | # 5 |
||
273 | 1 | last_found = False |
|
274 | 1 | for word in tar1_words: |
|
275 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
276 | 1 | last_found = True |
|
277 | |||
278 | 1 | if not last_found: |
|
279 | 1 | for word in src1_words: |
|
280 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
281 | 1 | last_found = True |
|
282 | |||
283 | # 6 |
||
284 | 1 | matches = 0 |
|
285 | 1 | if last_found: |
|
286 | 1 | for i, s_word in enumerate(src1_words): |
|
287 | 1 | for j, t_word in enumerate(tar1_words): |
|
288 | 1 | if s_word == t_word: |
|
289 | 1 | src1_words[i] = '@' |
|
290 | 1 | tar1_words[j] = '@' |
|
291 | 1 | matches += 1 |
|
292 | 1 | w_ratio = matches / max(tar1_num_words, src1_num_words) |
|
293 | 1 | if matches > 1 or ( |
|
294 | matches == 1 |
||
295 | and src1_num_words == 1 |
||
296 | and tar1_num_words == 1 |
||
297 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
298 | ): |
||
299 | 1 | return w_ratio |
|
300 | |||
301 | # 8 |
||
302 | 1 | if ( |
|
303 | tar2_num_words == 1 |
||
304 | and src2_num_words == 1 |
||
305 | and tar2_words[0] == src2_words[0] |
||
306 | ): |
||
307 | 1 | return 1 |
|
308 | # I see no way that the following can be True if the equivalent in |
||
309 | # #4 was False. |
||
310 | if tar2_num_words < 2 and tar_len_specials == 0: # pragma: no cover |
||
311 | return 0 |
||
312 | |||
313 | # 9 |
||
314 | 1 | last_found = False |
|
315 | 1 | for word in tar2_words: |
|
316 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
317 | 1 | last_found = True |
|
318 | |||
319 | 1 | if not last_found: |
|
320 | 1 | for word in src2_words: |
|
321 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
322 | 1 | last_found = True |
|
323 | |||
324 | 1 | if not last_found: |
|
325 | 1 | return 0 |
|
326 | |||
327 | # 10 |
||
328 | 1 | matches = 0 |
|
329 | 1 | if last_found: |
|
330 | 1 | for i, s_word in enumerate(src2_words): |
|
331 | 1 | for j, t_word in enumerate(tar2_words): |
|
332 | 1 | if s_word == t_word: |
|
333 | 1 | src2_words[i] = '@' |
|
334 | 1 | tar2_words[j] = '@' |
|
335 | 1 | matches += 1 |
|
336 | 1 | w_ratio = matches / max(tar2_num_words, src2_num_words) |
|
337 | 1 | if matches > 1 or ( |
|
338 | matches == 1 |
||
339 | and src2_num_words == 1 |
||
340 | and tar2_num_words == 1 |
||
341 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
342 | ): |
||
343 | return w_ratio |
||
344 | |||
345 | 1 | return 0 |
|
346 | |||
706 |