Conditions | 29 |
Total Lines | 102 |
Code Lines | 67 |
Lines | 13 |
Ratio | 12.75 % |
Tests | 56 |
CRAP Score | 29 |
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._discounted_levenshtein.DiscountedLevenshtein._alignment_matrix() 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 -*- |
||
130 | 1 | def _alignment_matrix(self, src, tar, backtrace=True): |
|
131 | """Return the Levenshtein alignment matrix. |
||
132 | |||
133 | Parameters |
||
134 | ---------- |
||
135 | src : str |
||
136 | Source string for comparison |
||
137 | tar : str |
||
138 | Target string for comparison |
||
139 | backtrace : bool |
||
140 | Return the backtrace matrix as well |
||
141 | |||
142 | Returns |
||
143 | ------- |
||
144 | numpy.ndarray or tuple(numpy.ndarray, numpy.ndarray) |
||
145 | The alignment matrix and (optionally) the backtrace matrix |
||
146 | |||
147 | |||
148 | .. versionadded:: 0.4.1 |
||
149 | |||
150 | """ |
||
151 | 1 | src_len = len(src) |
|
152 | 1 | tar_len = len(tar) |
|
153 | |||
154 | 1 | if self._discount_from == 'coda': |
|
155 | 1 | discount_from = [0, 0] |
|
156 | |||
157 | 1 | src_voc = src.lower() |
|
158 | 1 | for i in range(len(src_voc)): |
|
159 | 1 | if src_voc[i] in self._vowels: |
|
160 | 1 | discount_from[0] = i |
|
161 | 1 | break |
|
162 | 1 | for i in range(discount_from[0], len(src_voc)): |
|
163 | 1 | if src_voc[i] not in self._vowels: |
|
164 | 1 | discount_from[0] = i |
|
165 | 1 | break |
|
166 | else: |
||
167 | 1 | discount_from[0] += 1 |
|
168 | |||
169 | 1 | tar_voc = tar.lower() |
|
170 | 1 | for i in range(len(tar_voc)): |
|
171 | 1 | if tar_voc[i] in self._vowels: |
|
172 | 1 | discount_from[1] = i |
|
173 | 1 | break |
|
174 | 1 | for i in range(discount_from[1], len(tar_voc)): |
|
175 | 1 | if tar_voc[i] not in self._vowels: |
|
176 | 1 | discount_from[1] = i |
|
177 | 1 | break |
|
178 | else: |
||
179 | 1 | discount_from[1] += 1 |
|
180 | |||
181 | 1 | elif isinstance(self._discount_from, int): |
|
182 | 1 | discount_from = [self._discount_from, self._discount_from] |
|
183 | else: |
||
184 | 1 | discount_from = [1, 1] |
|
185 | |||
186 | 1 | d_mat = np.zeros((src_len + 1, tar_len + 1), dtype=np.float) |
|
187 | 1 | if backtrace: |
|
188 | 1 | trace_mat = np.zeros((src_len + 1, tar_len + 1), dtype=np.int8) |
|
189 | 1 | for i in range(1, src_len + 1): |
|
190 | 1 | d_mat[i, 0] = d_mat[i - 1, 0] + self._cost( |
|
191 | max(0, i - discount_from[0]) |
||
192 | ) |
||
193 | 1 | if backtrace: |
|
194 | 1 | trace_mat[i, 0] = 1 |
|
195 | 1 | for j in range(1, tar_len + 1): |
|
196 | 1 | d_mat[0, j] = d_mat[0, j - 1] + self._cost( |
|
197 | max(0, j - discount_from[1]) |
||
198 | ) |
||
199 | 1 | if backtrace: |
|
200 | 1 | trace_mat[0, j] = 0 |
|
201 | 1 | for i in range(src_len): |
|
202 | 1 | i_extend = self._cost(max(0, i - discount_from[0])) |
|
203 | 1 | for j in range(tar_len): |
|
204 | 1 | traces = ((i + 1, j), (i, j + 1), (i, j)) |
|
205 | 1 | cost = min(i_extend, self._cost(max(0, j - discount_from[1]))) |
|
206 | 1 | opts = ( |
|
207 | d_mat[traces[0]] + cost, # ins |
||
208 | d_mat[traces[1]] + cost, # del |
||
209 | d_mat[traces[2]] |
||
210 | + (cost if src[i] != tar[j] else 0), # sub/== |
||
211 | ) |
||
212 | 1 | d_mat[i + 1, j + 1] = min(opts) |
|
213 | 1 | if backtrace: |
|
214 | 1 | trace_mat[i + 1, j + 1] = int(np.argmin(opts)) |
|
215 | |||
216 | 1 | View Code Duplication | if self._mode == 'osa': |
|
|||
217 | 1 | if ( |
|
218 | i + 1 > 1 |
||
219 | and j + 1 > 1 |
||
220 | and src[i] == tar[j - 1] |
||
221 | and src[i - 1] == tar[j] |
||
222 | ): |
||
223 | # transposition |
||
224 | 1 | d_mat[i + 1, j + 1] = min( |
|
225 | d_mat[i + 1, j + 1], d_mat[i - 1, j - 1] + cost |
||
226 | ) |
||
227 | 1 | if backtrace: |
|
228 | 1 | trace_mat[i + 1, j + 1] = 2 |
|
229 | 1 | if backtrace: |
|
230 | 1 | return d_mat, trace_mat |
|
231 | 1 | return d_mat |
|
232 | |||
368 |