Conditions | 18 |
Total Lines | 114 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Tests | 38 |
CRAP Score | 18 |
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._editex.Editex.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 -*- |
||
66 | 1 | def dist_abs(self, src, tar, cost=(0, 1, 2), local=False): |
|
67 | """Return the Editex distance between two strings. |
||
68 | |||
69 | Parameters |
||
70 | ---------- |
||
71 | src : str |
||
72 | Source string for comparison |
||
73 | tar : str |
||
74 | Target string for comparison |
||
75 | cost : tuple |
||
76 | A 3-tuple representing the cost of the four possible edits: match, |
||
77 | same-group, and mismatch respectively (by default: (0, 1, 2)) |
||
78 | local : bool |
||
79 | If True, the local variant of Editex is used |
||
80 | |||
81 | Returns |
||
82 | ------- |
||
83 | int |
||
84 | Editex distance |
||
85 | |||
86 | Examples |
||
87 | -------- |
||
88 | >>> cmp = Editex() |
||
89 | >>> cmp.dist_abs('cat', 'hat') |
||
90 | 2 |
||
91 | >>> cmp.dist_abs('Niall', 'Neil') |
||
92 | 2 |
||
93 | >>> cmp.dist_abs('aluminum', 'Catalan') |
||
94 | 12 |
||
95 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
96 | 6 |
||
97 | |||
98 | """ |
||
99 | 1 | match_cost, group_cost, mismatch_cost = cost |
|
100 | |||
101 | 1 | def r_cost(ch1, ch2): |
|
102 | """Return r(a,b) according to Zobel & Dart's definition. |
||
103 | |||
104 | Parameters |
||
105 | ---------- |
||
106 | ch1 : str |
||
107 | The first character to compare |
||
108 | ch2 : str |
||
109 | The second character to compare |
||
110 | |||
111 | Returns |
||
112 | ------- |
||
113 | int |
||
114 | r(a,b) according to Zobel & Dart's definition |
||
115 | |||
116 | """ |
||
117 | 1 | if ch1 == ch2: |
|
118 | 1 | return match_cost |
|
119 | 1 | if ch1 in self._all_letters and ch2 in self._all_letters: |
|
120 | 1 | for group in self._letter_groups: |
|
121 | 1 | if ch1 in group and ch2 in group: |
|
122 | 1 | return group_cost |
|
123 | 1 | return mismatch_cost |
|
124 | |||
125 | 1 | def d_cost(ch1, ch2): |
|
126 | """Return d(a,b) according to Zobel & Dart's definition. |
||
127 | |||
128 | Parameters |
||
129 | ---------- |
||
130 | ch1 : str |
||
131 | The first character to compare |
||
132 | ch2 : str |
||
133 | The second character to compare |
||
134 | |||
135 | Returns |
||
136 | ------- |
||
137 | int |
||
138 | d(a,b) according to Zobel & Dart's definition |
||
139 | |||
140 | """ |
||
141 | 1 | if ch1 != ch2 and (ch1 == 'H' or ch1 == 'W'): |
|
142 | 1 | return group_cost |
|
143 | 1 | return r_cost(ch1, ch2) |
|
144 | |||
145 | # convert both src & tar to NFKD normalized unicode |
||
146 | 1 | src = unicode_normalize('NFKD', text_type(src.upper())) |
|
147 | 1 | tar = unicode_normalize('NFKD', text_type(tar.upper())) |
|
148 | # convert ß to SS (for Python2) |
||
149 | 1 | src = src.replace('ß', 'SS') |
|
150 | 1 | tar = tar.replace('ß', 'SS') |
|
151 | |||
152 | 1 | if src == tar: |
|
153 | 1 | return 0.0 |
|
154 | 1 | if not src: |
|
155 | 1 | return len(tar) * mismatch_cost |
|
156 | 1 | if not tar: |
|
157 | 1 | return len(src) * mismatch_cost |
|
158 | |||
159 | 1 | d_mat = np_zeros((len(src) + 1, len(tar) + 1), dtype=np_int) |
|
160 | 1 | lens = len(src) |
|
161 | 1 | lent = len(tar) |
|
162 | 1 | src = ' ' + src |
|
163 | 1 | tar = ' ' + tar |
|
164 | |||
165 | 1 | if not local: |
|
166 | 1 | for i in range(1, lens + 1): |
|
167 | 1 | d_mat[i, 0] = d_mat[i - 1, 0] + d_cost(src[i - 1], src[i]) |
|
168 | 1 | for j in range(1, lent + 1): |
|
169 | 1 | d_mat[0, j] = d_mat[0, j - 1] + d_cost(tar[j - 1], tar[j]) |
|
170 | |||
171 | 1 | for i in range(1, lens + 1): |
|
172 | 1 | for j in range(1, lent + 1): |
|
173 | 1 | d_mat[i, j] = min( |
|
174 | d_mat[i - 1, j] + d_cost(src[i - 1], src[i]), |
||
175 | d_mat[i, j - 1] + d_cost(tar[j - 1], tar[j]), |
||
176 | d_mat[i - 1, j - 1] + r_cost(src[i], tar[j]), |
||
177 | ) |
||
178 | |||
179 | 1 | return d_mat[lens, lent] |
|
180 | |||
344 |