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