| Conditions | 11 |
| Total Lines | 62 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 24 |
| 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._vps.VPS.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 | # -*- coding: utf-8 -*- |
||
| 46 | 1 | def sim(self, src, tar): |
|
| 47 | """Return the Victorian Panel Study score of two words. |
||
| 48 | |||
| 49 | Parameters |
||
| 50 | ---------- |
||
| 51 | src : str |
||
| 52 | Source string for comparison |
||
| 53 | tar : str |
||
| 54 | Target string for comparison |
||
| 55 | |||
| 56 | Returns |
||
| 57 | ------- |
||
| 58 | float |
||
| 59 | The VPS score |
||
| 60 | |||
| 61 | Examples |
||
| 62 | -------- |
||
| 63 | >>> cmp = VPS() |
||
| 64 | >>> cmp.sim('cat', 'hat') |
||
| 65 | 0.5 |
||
| 66 | >>> cmp.sim('Niall', 'Neil') |
||
| 67 | 0.3 |
||
| 68 | >>> cmp.sim('aluminum', 'Catalan') |
||
| 69 | 0.14285714285714285 |
||
| 70 | >>> cmp.sim('ATCG', 'TAGC') |
||
| 71 | 0.3333333333333333 |
||
| 72 | |||
| 73 | |||
| 74 | .. versionadded:: 0.4.1 |
||
| 75 | |||
| 76 | """ |
||
| 77 | 1 | if src == tar: |
|
| 78 | 1 | return 1.0 |
|
| 79 | 1 | if len(src) < len(tar): |
|
| 80 | 1 | src, tar = tar, src |
|
| 81 | |||
| 82 | 1 | score = 0 |
|
| 83 | 1 | discount = 0 |
|
| 84 | |||
| 85 | 1 | src_tokens = defaultdict(set) |
|
| 86 | 1 | tar_tokens = defaultdict(set) |
|
| 87 | 1 | for slen in range(1, 4): |
|
| 88 | 1 | for i in range(len(src) - slen + 1): |
|
| 89 | 1 | src_tokens[src[i : i + slen]].add(i) |
|
| 90 | 1 | for i in range(len(tar) - slen + 1): |
|
| 91 | 1 | tar_tokens[tar[i : i + slen]].add(i) |
|
| 92 | |||
| 93 | 1 | for token in src_tokens.keys(): |
|
| 94 | 1 | if token in tar_tokens: |
|
| 95 | 1 | for src_pos in src_tokens[token]: |
|
| 96 | 1 | score += 1 |
|
| 97 | 1 | if src_pos not in tar_tokens[token]: |
|
| 98 | 1 | discount += min( |
|
| 99 | abs(src_pos - tar_pos) |
||
| 100 | for tar_pos in tar_tokens[token] |
||
| 101 | ) |
||
| 102 | |||
| 103 | 1 | score -= discount / max(len(src), len(tar)) |
|
| 104 | 1 | if score: |
|
| 105 | 1 | score /= 3 * len(src) - 3 |
|
| 106 | |||
| 107 | 1 | return score |
|
| 108 | |||
| 114 |