Conditions | 10 |
Total Lines | 75 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Tests | 19 |
CRAP Score | 10 |
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._soft_cosine.SoftCosine.sim() 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 | # Copyright 2018-2020 by Christopher C. Little. |
||
144 | def sim(self, src: str, tar: str) -> float: |
||
145 | r"""Return the Soft Cosine similarity of two strings. |
||
146 | |||
147 | 1 | Parameters |
|
148 | 1 | ---------- |
|
149 | src : str |
||
150 | 1 | Source string (or QGrams/Counter objects) for comparison |
|
151 | tar : str |
||
152 | 1 | Target string (or QGrams/Counter objects) for comparison |
|
153 | 1 | ||
154 | Returns |
||
155 | 1 | ------- |
|
156 | float |
||
157 | Fuzzy Cosine similarity |
||
158 | |||
159 | Examples |
||
160 | -------- |
||
161 | >>> cmp = SoftCosine() |
||
162 | >>> cmp.sim('cat', 'hat') |
||
163 | 0.8750000000000001 |
||
164 | >>> cmp.sim('Niall', 'Neil') |
||
165 | 0.8844691709074513 |
||
166 | >>> cmp.sim('aluminum', 'Catalan') |
||
167 | 0.831348688760277 |
||
168 | >>> cmp.sim('ATCG', 'TAGC') |
||
169 | 0.8571428571428572 |
||
170 | |||
171 | |||
172 | .. versionadded:: 0.4.0 |
||
173 | |||
174 | """ |
||
175 | if src == tar: |
||
176 | return 1.0 |
||
177 | |||
178 | self._tokenize(src, tar) |
||
179 | |||
180 | if not self._src_card() or not self._tar_card(): |
||
181 | 1 | return 0.0 |
|
182 | 1 | ||
183 | 1 | similarity = { |
|
184 | 'a': self._sim_a, |
||
185 | 1 | 'b': self._sim_b, |
|
186 | 1 | 'c': self._sim_c, |
|
187 | 1 | 'd': self._sim_d, |
|
188 | } |
||
189 | |||
190 | nom = 0.0 |
||
191 | denom_left = 0.0 |
||
192 | denom_right = 0.0 |
||
193 | 1 | ||
194 | 1 | for src in self._src_tokens.keys(): |
|
195 | 1 | for tar in self._tar_tokens.keys(): |
|
196 | nom += ( |
||
197 | self._src_tokens[src] |
||
198 | * self._tar_tokens[tar] |
||
199 | * similarity[self.params['sim_method']](src, tar) |
||
200 | ) |
||
201 | 1 | ||
202 | 1 | for src in self._src_tokens.keys(): |
|
203 | 1 | for tar in self._src_tokens.keys(): |
|
204 | denom_left += ( |
||
205 | self._src_tokens[src] |
||
206 | * self._src_tokens[tar] |
||
207 | * similarity[self.params['sim_method']](src, tar) |
||
208 | ) |
||
209 | 1 | ||
210 | for src in self._tar_tokens.keys(): |
||
211 | for tar in self._tar_tokens.keys(): |
||
212 | denom_right += ( |
||
213 | self._tar_tokens[src] |
||
214 | * self._tar_tokens[tar] |
||
215 | * similarity[self.params['sim_method']](src, tar) |
||
216 | ) |
||
217 | |||
218 | return nom / (denom_left ** 0.5 * denom_right ** 0.5) |
||
219 | |||
225 |