Conditions | 19 |
Total Lines | 118 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Tests | 45 |
CRAP Score | 19 |
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._damerau_levenshtein.DamerauLevenshtein.dist_abs() 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 -*- |
||
57 | 1 | def dist_abs(self, src, tar, cost=(1, 1, 1, 1)): |
|
58 | """Return the Damerau-Levenshtein distance between two strings. |
||
59 | |||
60 | Parameters |
||
61 | ---------- |
||
62 | src : str |
||
63 | Source string for comparison |
||
64 | tar : str |
||
65 | Target string for comparison |
||
66 | cost : tuple |
||
67 | A 4-tuple representing the cost of the four possible edits: |
||
68 | inserts, deletes, substitutions, and transpositions, respectively |
||
69 | (by default: (1, 1, 1, 1)) |
||
70 | |||
71 | Returns |
||
72 | ------- |
||
73 | int (may return a float if cost has float values) |
||
74 | The Damerau-Levenshtein distance between src & tar |
||
75 | |||
76 | Raises |
||
77 | ------ |
||
78 | ValueError |
||
79 | Unsupported cost assignment; the cost of two transpositions must |
||
80 | not be less than the cost of an insert plus a delete. |
||
81 | |||
82 | Examples |
||
83 | -------- |
||
84 | >>> cmp = DamerauLevenshtein() |
||
85 | >>> cmp.dist_abs('cat', 'hat') |
||
86 | 1 |
||
87 | >>> cmp.dist_abs('Niall', 'Neil') |
||
88 | 3 |
||
89 | >>> cmp.dist_abs('aluminum', 'Catalan') |
||
90 | 7 |
||
91 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
92 | 2 |
||
93 | |||
94 | """ |
||
95 | 1 | ins_cost, del_cost, sub_cost, trans_cost = cost |
|
96 | |||
97 | 1 | if src == tar: |
|
98 | 1 | return 0 |
|
99 | 1 | if not src: |
|
100 | 1 | return len(tar) * ins_cost |
|
101 | 1 | if not tar: |
|
102 | 1 | return len(src) * del_cost |
|
103 | |||
104 | 1 | if 2 * trans_cost < ins_cost + del_cost: |
|
105 | 1 | raise ValueError( |
|
106 | 'Unsupported cost assignment; the cost of two transpositions ' |
||
107 | + 'must not be less than the cost of an insert plus a delete.' |
||
108 | ) |
||
109 | |||
110 | 1 | d_mat = np_zeros((len(src)) * (len(tar)), dtype=np_int).reshape( |
|
111 | (len(src), len(tar)) |
||
112 | ) |
||
113 | |||
114 | 1 | if src[0] != tar[0]: |
|
115 | 1 | d_mat[0, 0] = min(sub_cost, ins_cost + del_cost) |
|
116 | |||
117 | 1 | src_index_by_character = {src[0]: 0} |
|
118 | 1 | for i in range(1, len(src)): |
|
119 | 1 | del_distance = d_mat[i - 1, 0] + del_cost |
|
120 | 1 | ins_distance = (i + 1) * del_cost + ins_cost |
|
121 | 1 | match_distance = i * del_cost + ( |
|
122 | 0 if src[i] == tar[0] else sub_cost |
||
123 | ) |
||
124 | 1 | d_mat[i, 0] = min(del_distance, ins_distance, match_distance) |
|
125 | |||
126 | 1 | for j in range(1, len(tar)): |
|
127 | 1 | del_distance = (j + 1) * ins_cost + del_cost |
|
128 | 1 | ins_distance = d_mat[0, j - 1] + ins_cost |
|
129 | 1 | match_distance = j * ins_cost + ( |
|
130 | 0 if src[0] == tar[j] else sub_cost |
||
131 | ) |
||
132 | 1 | d_mat[0, j] = min(del_distance, ins_distance, match_distance) |
|
133 | |||
134 | 1 | for i in range(1, len(src)): |
|
135 | 1 | max_src_letter_match_index = 0 if src[i] == tar[0] else -1 |
|
136 | 1 | for j in range(1, len(tar)): |
|
137 | 1 | candidate_swap_index = ( |
|
138 | -1 |
||
139 | if tar[j] not in src_index_by_character |
||
140 | else src_index_by_character[tar[j]] |
||
141 | ) |
||
142 | 1 | j_swap = max_src_letter_match_index |
|
143 | 1 | del_distance = d_mat[i - 1, j] + del_cost |
|
144 | 1 | ins_distance = d_mat[i, j - 1] + ins_cost |
|
145 | 1 | match_distance = d_mat[i - 1, j - 1] |
|
146 | 1 | if src[i] != tar[j]: |
|
147 | 1 | match_distance += sub_cost |
|
148 | else: |
||
149 | 1 | max_src_letter_match_index = j |
|
150 | |||
151 | 1 | if candidate_swap_index != -1 and j_swap != -1: |
|
152 | 1 | i_swap = candidate_swap_index |
|
153 | |||
154 | 1 | if i_swap == 0 and j_swap == 0: |
|
155 | 1 | pre_swap_cost = 0 |
|
156 | else: |
||
157 | 1 | pre_swap_cost = d_mat[ |
|
158 | max(0, i_swap - 1), max(0, j_swap - 1) |
||
159 | ] |
||
160 | 1 | swap_distance = ( |
|
161 | pre_swap_cost |
||
162 | + (i - i_swap - 1) * del_cost |
||
163 | + (j - j_swap - 1) * ins_cost |
||
164 | + trans_cost |
||
165 | ) |
||
166 | else: |
||
167 | 1 | swap_distance = maxsize |
|
168 | |||
169 | 1 | d_mat[i, j] = min( |
|
170 | del_distance, ins_distance, match_distance, swap_distance |
||
171 | ) |
||
172 | 1 | src_index_by_character[src[i]] = i |
|
173 | |||
174 | 1 | return d_mat[len(src) - 1, len(tar) - 1] |
|
175 | |||
337 |