| Conditions | 14 |
| Total Lines | 84 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 19 |
| CRAP Score | 14 |
| 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._needleman_wunsch.NeedlemanWunsch.sim_matrix() 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 -*- |
||
| 55 | 1 | @staticmethod |
|
| 56 | 1 | def sim_matrix( |
|
| 57 | src, |
||
| 58 | tar, |
||
| 59 | mat=None, |
||
| 60 | mismatch_cost=0, |
||
| 61 | match_cost=1, |
||
| 62 | symmetric=True, |
||
| 63 | alphabet=None, |
||
| 64 | ): |
||
| 65 | """Return the matrix similarity of two strings. |
||
| 66 | |||
| 67 | With the default parameters, this is identical to sim_ident. |
||
| 68 | It is possible for sim_matrix to return values outside of the range |
||
| 69 | :math:`[0, 1]`, if values outside that range are present in mat, |
||
| 70 | mismatch_cost, or match_cost. |
||
| 71 | |||
| 72 | Parameters |
||
| 73 | ---------- |
||
| 74 | src : str |
||
| 75 | Source string for comparison |
||
| 76 | tar : str |
||
| 77 | Target string for comparison |
||
| 78 | mat : dict |
||
| 79 | A dict mapping tuples to costs; the tuples are (src, tar) pairs of |
||
| 80 | symbols from the alphabet parameter |
||
| 81 | mismatch_cost : float |
||
| 82 | The value returned if (src, tar) is absent from mat when src does |
||
| 83 | not equal tar |
||
| 84 | match_cost : float |
||
| 85 | The value returned if (src, tar) is absent from mat when src equals |
||
| 86 | tar |
||
| 87 | symmetric : bool |
||
| 88 | True if the cost of src not matching tar is identical to the cost |
||
| 89 | of tar not matching src; in this case, the values in mat need only |
||
| 90 | contain (src, tar) or (tar, src), not both |
||
| 91 | alphabet : str |
||
| 92 | A collection of tokens from which src and tar are drawn; if this is |
||
| 93 | defined a ValueError is raised if either tar or src is not found in |
||
| 94 | alphabet |
||
| 95 | |||
| 96 | Returns |
||
| 97 | ------- |
||
| 98 | float |
||
| 99 | Matrix similarity |
||
| 100 | |||
| 101 | Raises |
||
| 102 | ------ |
||
| 103 | ValueError |
||
| 104 | src value not in alphabet |
||
| 105 | ValueError |
||
| 106 | tar value not in alphabet |
||
| 107 | |||
| 108 | Examples |
||
| 109 | -------- |
||
| 110 | >>> NeedlemanWunsch.sim_matrix('cat', 'hat') |
||
| 111 | 0 |
||
| 112 | >>> NeedlemanWunsch.sim_matrix('hat', 'hat') |
||
| 113 | 1 |
||
| 114 | |||
| 115 | |||
| 116 | .. versionadded:: 0.1.0 |
||
| 117 | .. versionchanged:: 0.3.6 |
||
| 118 | Encapsulated in class |
||
| 119 | |||
| 120 | """ |
||
| 121 | 1 | if alphabet: |
|
| 122 | 1 | alphabet = tuple(alphabet) |
|
| 123 | 1 | for i in src: |
|
| 124 | 1 | if i not in alphabet: |
|
| 125 | 1 | raise ValueError('src value not in alphabet') |
|
| 126 | 1 | for i in tar: |
|
| 127 | 1 | if i not in alphabet: |
|
| 128 | 1 | raise ValueError('tar value not in alphabet') |
|
| 129 | |||
| 130 | 1 | if src == tar: |
|
| 131 | 1 | if mat and (src, src) in mat: |
|
| 132 | 1 | return mat[(src, src)] |
|
| 133 | 1 | return match_cost |
|
| 134 | 1 | if mat and (src, tar) in mat: |
|
| 135 | 1 | return mat[(src, tar)] |
|
| 136 | 1 | elif symmetric and mat and (tar, src) in mat: |
|
| 137 | 1 | return mat[(tar, src)] |
|
| 138 | 1 | return mismatch_cost |
|
| 139 | |||
| 300 |