Conditions | 11 |
Total Lines | 114 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Tests | 25 |
CRAP Score | 11 |
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._eudex.Eudex.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 -*- |
||
87 | 1 | def dist_abs( |
|
88 | self, src, tar, weights='exponential', max_length=8, normalized=False |
||
89 | ): |
||
90 | """Calculate the distance between the Eudex hashes of two terms. |
||
91 | |||
92 | Parameters |
||
93 | ---------- |
||
94 | src : str |
||
95 | Source string for comparison |
||
96 | tar : str |
||
97 | Target string for comparison |
||
98 | weights : str, iterable, or generator function |
||
99 | The weights or weights generator function |
||
100 | |||
101 | - If set to ``None``, a simple Hamming distance is calculated. |
||
102 | - If set to ``exponential``, weight decays by powers of 2, as |
||
103 | proposed in the eudex specification: |
||
104 | https://github.com/ticki/eudex. |
||
105 | - If set to ``fibonacci``, weight decays through the Fibonacci |
||
106 | series, as in the eudex reference implementation. |
||
107 | - If set to a callable function, this assumes it creates a |
||
108 | generator and the generator is used to populate a series of |
||
109 | weights. |
||
110 | - If set to an iterable, the iterable's values should be |
||
111 | integers and will be used as the weights. |
||
112 | |||
113 | max_length : int |
||
114 | The number of characters to encode as a eudex hash |
||
115 | normalized : bool |
||
116 | Normalizes to [0, 1] if True |
||
117 | |||
118 | Returns |
||
119 | ------- |
||
120 | int |
||
121 | The Eudex Hamming distance |
||
122 | |||
123 | Examples |
||
124 | -------- |
||
125 | >>> cmp = Eudex() |
||
126 | >>> cmp.dist_abs('cat', 'hat') |
||
127 | 128 |
||
128 | >>> cmp.dist_abs('Niall', 'Neil') |
||
129 | 2 |
||
130 | >>> cmp.dist_abs('Colin', 'Cuilen') |
||
131 | 10 |
||
132 | >>> cmp.dist_abs('ATCG', 'TAGC') |
||
133 | 403 |
||
134 | |||
135 | >>> cmp.dist_abs('cat', 'hat', weights='fibonacci') |
||
136 | 34 |
||
137 | >>> cmp.dist_abs('Niall', 'Neil', weights='fibonacci') |
||
138 | 2 |
||
139 | >>> cmp.dist_abs('Colin', 'Cuilen', weights='fibonacci') |
||
140 | 7 |
||
141 | >>> cmp.dist_abs('ATCG', 'TAGC', weights='fibonacci') |
||
142 | 117 |
||
143 | |||
144 | >>> cmp.dist_abs('cat', 'hat', weights=None) |
||
145 | 1 |
||
146 | >>> cmp.dist_abs('Niall', 'Neil', weights=None) |
||
147 | 1 |
||
148 | >>> cmp.dist_abs('Colin', 'Cuilen', weights=None) |
||
149 | 2 |
||
150 | >>> cmp.dist_abs('ATCG', 'TAGC', weights=None) |
||
151 | 9 |
||
152 | |||
153 | >>> # Using the OEIS A000142: |
||
154 | >>> cmp.dist_abs('cat', 'hat', [1, 1, 2, 6, 24, 120, 720, 5040]) |
||
155 | 1 |
||
156 | >>> cmp.dist_abs('Niall', 'Neil', [1, 1, 2, 6, 24, 120, 720, 5040]) |
||
157 | 720 |
||
158 | >>> cmp.dist_abs('Colin', 'Cuilen', |
||
159 | ... [1, 1, 2, 6, 24, 120, 720, 5040]) |
||
160 | 744 |
||
161 | >>> cmp.dist_abs('ATCG', 'TAGC', [1, 1, 2, 6, 24, 120, 720, 5040]) |
||
162 | 6243 |
||
163 | |||
164 | """ |
||
165 | # Calculate the eudex hashes and XOR them |
||
166 | 1 | xored = eudex(src, max_length=max_length) ^ eudex( |
|
167 | tar, max_length=max_length |
||
168 | ) |
||
169 | |||
170 | # Simple hamming distance (all bits are equal) |
||
171 | 1 | if not weights: |
|
172 | 1 | binary = bin(xored) |
|
173 | 1 | distance = binary.count('1') |
|
174 | 1 | if normalized: |
|
175 | 1 | return distance / (len(binary) - 2) |
|
176 | 1 | return distance |
|
177 | |||
178 | # If weights is a function, it should create a generator, |
||
179 | # which we now use to populate a list |
||
180 | 1 | if callable(weights): |
|
181 | 1 | weights = weights() |
|
182 | 1 | elif weights == 'exponential': |
|
183 | 1 | weights = Eudex.gen_exponential() |
|
184 | 1 | elif weights == 'fibonacci': |
|
185 | 1 | weights = Eudex.gen_fibonacci() |
|
186 | 1 | if isinstance(weights, GeneratorType): |
|
187 | 1 | weights = [next(weights) for _ in range(max_length)][::-1] |
|
188 | |||
189 | # Sum the weighted hamming distance |
||
190 | 1 | distance = 0 |
|
191 | 1 | max_distance = 0 |
|
192 | 1 | while (xored or normalized) and weights: |
|
193 | 1 | max_distance += 8 * weights[-1] |
|
194 | 1 | distance += bin(xored & 0xFF).count('1') * weights.pop() |
|
195 | 1 | xored >>= 8 |
|
196 | |||
197 | 1 | if normalized: |
|
198 | 1 | distance /= max_distance |
|
199 | |||
200 | 1 | return distance |
|
201 | |||
383 |