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