| Conditions | 73 |
| Total Lines | 202 |
| Code Lines | 140 |
| 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_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 -*- |
||
| 53 | 1 | def _synoname_word_approximation( |
|
| 54 | src_ln, tar_ln, src_fn='', tar_fn='', features=None |
||
| 55 | ): |
||
| 56 | """Return the Synoname word approximation score for two names. |
||
| 57 | |||
| 58 | :param str src_ln: last name of the source |
||
| 59 | :param str tar_ln: last name of the target |
||
| 60 | :param str src_fn: first name of the source (optional) |
||
| 61 | :param str tar_fn: first name of the target (optional) |
||
| 62 | :param features: a dict containing special features calculated via |
||
| 63 | fingerprint.synoname_toolcode() (optional) |
||
| 64 | :returns: The word approximation score |
||
| 65 | :rtype: float |
||
| 66 | |||
| 67 | >>> _synoname_word_approximation('Smith Waterman', 'Waterman', |
||
| 68 | ... 'Tom Joe Bob', 'Tom Joe') |
||
| 69 | 0.6 |
||
| 70 | """ |
||
| 71 | 1 | if features is None: |
|
| 72 | 1 | features = {} |
|
| 73 | 1 | if 'src_specials' not in features: |
|
| 74 | 1 | features['src_specials'] = [] |
|
| 75 | 1 | if 'tar_specials' not in features: |
|
| 76 | 1 | features['tar_specials'] = [] |
|
| 77 | |||
| 78 | 1 | src_len_specials = len(features['src_specials']) |
|
| 79 | 1 | tar_len_specials = len(features['tar_specials']) |
|
| 80 | |||
| 81 | # 1 |
||
| 82 | 1 | if ('gen_conflict' in features and features['gen_conflict']) or ( |
|
| 83 | 'roman_conflict' in features and features['roman_conflict'] |
||
| 84 | ): |
||
| 85 | 1 | return 0 |
|
| 86 | |||
| 87 | # 3 & 7 |
||
| 88 | 1 | full_tar1 = ' '.join((tar_ln, tar_fn)).replace('-', ' ').strip() |
|
| 89 | 1 | for s_pos, s_type in features['tar_specials']: |
|
| 90 | 1 | if s_type == 'a': |
|
| 91 | 1 | full_tar1 = full_tar1[ |
|
| 92 | : -(1 + len(_synoname_special_table[s_pos][1])) |
||
| 93 | ] |
||
| 94 | 1 | elif s_type == 'b': |
|
| 95 | 1 | loc = ( |
|
| 96 | full_tar1.find(' ' + _synoname_special_table[s_pos][1] + ' ') |
||
| 97 | + 1 |
||
| 98 | ) |
||
| 99 | 1 | full_tar1 = ( |
|
| 100 | full_tar1[:loc] |
||
| 101 | + full_tar1[loc + len(_synoname_special_table[s_pos][1]) :] |
||
| 102 | ) |
||
| 103 | 1 | elif s_type == 'c': |
|
| 104 | 1 | full_tar1 = full_tar1[1 + len(_synoname_special_table[s_pos][1]) :] |
|
| 105 | |||
| 106 | 1 | full_src1 = ' '.join((src_ln, src_fn)).replace('-', ' ').strip() |
|
| 107 | 1 | for s_pos, s_type in features['src_specials']: |
|
| 108 | 1 | if s_type == 'a': |
|
| 109 | 1 | full_src1 = full_src1[ |
|
| 110 | : -(1 + len(_synoname_special_table[s_pos][1])) |
||
| 111 | ] |
||
| 112 | 1 | elif s_type == 'b': |
|
| 113 | 1 | loc = ( |
|
| 114 | full_src1.find(' ' + _synoname_special_table[s_pos][1] + ' ') |
||
| 115 | + 1 |
||
| 116 | ) |
||
| 117 | 1 | full_src1 = ( |
|
| 118 | full_src1[:loc] |
||
| 119 | + full_src1[loc + len(_synoname_special_table[s_pos][1]) :] |
||
| 120 | ) |
||
| 121 | 1 | elif s_type == 'c': |
|
| 122 | 1 | full_src1 = full_src1[1 + len(_synoname_special_table[s_pos][1]) :] |
|
| 123 | |||
| 124 | 1 | full_tar2 = full_tar1 |
|
| 125 | 1 | for s_pos, s_type in features['tar_specials']: |
|
| 126 | 1 | if s_type == 'd': |
|
| 127 | 1 | full_tar2 = full_tar2[len(_synoname_special_table[s_pos][1]) :] |
|
| 128 | 1 | elif s_type == 'X' and _synoname_special_table[s_pos][1] in full_tar2: |
|
| 129 | 1 | loc = full_tar2.find(' ' + _synoname_special_table[s_pos][1]) |
|
| 130 | 1 | full_tar2 = ( |
|
| 131 | full_tar2[:loc] |
||
| 132 | + full_tar2[loc + len(_synoname_special_table[s_pos][1]) :] |
||
| 133 | ) |
||
| 134 | |||
| 135 | 1 | full_src2 = full_src1 |
|
| 136 | 1 | for s_pos, s_type in features['src_specials']: |
|
| 137 | 1 | if s_type == 'd': |
|
| 138 | 1 | full_src2 = full_src2[len(_synoname_special_table[s_pos][1]) :] |
|
| 139 | 1 | elif s_type == 'X' and _synoname_special_table[s_pos][1] in full_src2: |
|
| 140 | 1 | loc = full_src2.find(' ' + _synoname_special_table[s_pos][1]) |
|
| 141 | 1 | full_src2 = ( |
|
| 142 | full_src2[:loc] |
||
| 143 | + full_src2[loc + len(_synoname_special_table[s_pos][1]) :] |
||
| 144 | ) |
||
| 145 | |||
| 146 | 1 | full_tar1 = _synoname_strip_punct(full_tar1) |
|
| 147 | 1 | tar1_words = full_tar1.split() |
|
| 148 | 1 | tar1_num_words = len(tar1_words) |
|
| 149 | |||
| 150 | 1 | full_src1 = _synoname_strip_punct(full_src1) |
|
| 151 | 1 | src1_words = full_src1.split() |
|
| 152 | 1 | src1_num_words = len(src1_words) |
|
| 153 | |||
| 154 | 1 | full_tar2 = _synoname_strip_punct(full_tar2) |
|
| 155 | 1 | tar2_words = full_tar2.split() |
|
| 156 | 1 | tar2_num_words = len(tar2_words) |
|
| 157 | |||
| 158 | 1 | full_src2 = _synoname_strip_punct(full_src2) |
|
| 159 | 1 | src2_words = full_src2.split() |
|
| 160 | 1 | src2_num_words = len(src2_words) |
|
| 161 | |||
| 162 | # 2 |
||
| 163 | 1 | if ( |
|
| 164 | src1_num_words < 2 |
||
| 165 | and src_len_specials == 0 |
||
| 166 | and src2_num_words < 2 |
||
| 167 | and tar_len_specials == 0 |
||
| 168 | ): |
||
| 169 | 1 | return 0 |
|
| 170 | |||
| 171 | # 4 |
||
| 172 | 1 | if ( |
|
| 173 | tar1_num_words == 1 |
||
| 174 | and src1_num_words == 1 |
||
| 175 | and tar1_words[0] == src1_words[0] |
||
| 176 | ): |
||
| 177 | 1 | return 1 |
|
| 178 | 1 | if tar1_num_words < 2 and tar_len_specials == 0: |
|
| 179 | 1 | return 0 |
|
| 180 | |||
| 181 | # 5 |
||
| 182 | 1 | last_found = False |
|
| 183 | 1 | for word in tar1_words: |
|
| 184 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
| 185 | 1 | last_found = True |
|
| 186 | |||
| 187 | 1 | if not last_found: |
|
| 188 | 1 | for word in src1_words: |
|
| 189 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
| 190 | 1 | last_found = True |
|
| 191 | |||
| 192 | # 6 |
||
| 193 | 1 | matches = 0 |
|
| 194 | 1 | if last_found: |
|
| 195 | 1 | for i, s_word in enumerate(src1_words): |
|
| 196 | 1 | for j, t_word in enumerate(tar1_words): |
|
| 197 | 1 | if s_word == t_word: |
|
| 198 | 1 | src1_words[i] = '@' |
|
| 199 | 1 | tar1_words[j] = '@' |
|
| 200 | 1 | matches += 1 |
|
| 201 | 1 | w_ratio = matches / max(tar1_num_words, src1_num_words) |
|
| 202 | 1 | if matches > 1 or ( |
|
| 203 | matches == 1 |
||
| 204 | and src1_num_words == 1 |
||
| 205 | and tar1_num_words == 1 |
||
| 206 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
| 207 | ): |
||
| 208 | 1 | return w_ratio |
|
| 209 | |||
| 210 | # 8 |
||
| 211 | 1 | if ( |
|
| 212 | tar2_num_words == 1 |
||
| 213 | and src2_num_words == 1 |
||
| 214 | and tar2_words[0] == src2_words[0] |
||
| 215 | ): |
||
| 216 | 1 | return 1 |
|
| 217 | # I see no way that the following can be True if the equivalent in |
||
| 218 | # #4 was False. |
||
| 219 | if tar2_num_words < 2 and tar_len_specials == 0: # pragma: no cover |
||
| 220 | return 0 |
||
| 221 | |||
| 222 | # 9 |
||
| 223 | 1 | last_found = False |
|
| 224 | 1 | for word in tar2_words: |
|
| 225 | 1 | if src_ln.endswith(word) or word + ' ' in src_ln: |
|
| 226 | 1 | last_found = True |
|
| 227 | |||
| 228 | 1 | if not last_found: |
|
| 229 | 1 | for word in src2_words: |
|
| 230 | 1 | if tar_ln.endswith(word) or word + ' ' in tar_ln: |
|
| 231 | 1 | last_found = True |
|
| 232 | |||
| 233 | 1 | if not last_found: |
|
| 234 | 1 | return 0 |
|
| 235 | |||
| 236 | # 10 |
||
| 237 | 1 | matches = 0 |
|
| 238 | 1 | if last_found: |
|
| 239 | 1 | for i, s_word in enumerate(src2_words): |
|
| 240 | 1 | for j, t_word in enumerate(tar2_words): |
|
| 241 | 1 | if s_word == t_word: |
|
| 242 | 1 | src2_words[i] = '@' |
|
| 243 | 1 | tar2_words[j] = '@' |
|
| 244 | 1 | matches += 1 |
|
| 245 | 1 | w_ratio = matches / max(tar2_num_words, src2_num_words) |
|
| 246 | 1 | if matches > 1 or ( |
|
| 247 | matches == 1 |
||
| 248 | and src2_num_words == 1 |
||
| 249 | and tar2_num_words == 1 |
||
| 250 | and (tar_len_specials > 0 or src_len_specials > 0) |
||
| 251 | ): |
||
| 252 | return w_ratio |
||
| 253 | |||
| 254 | 1 | return 0 |
|
| 255 | |||
| 560 |