Conditions | 17 |
Total Lines | 63 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Tests | 27 |
CRAP Score | 17 |
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._guth.Guth.sim_score() 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 2019-2020 by Christopher C. Little. |
||
106 | 1 | def sim_score(self, src: str, tar: str) -> float: |
|
107 | 1 | """Return the Guth matching score of two strings. |
|
108 | |||
109 | 1 | Parameters |
|
110 | ---------- |
||
111 | src : str |
||
112 | Source string for comparison |
||
113 | tar : str |
||
114 | Target string for comparison |
||
115 | |||
116 | Returns |
||
117 | ------- |
||
118 | float |
||
119 | Guth matching score (1.0 if matching, otherwise 0.0) |
||
120 | |||
121 | Examples |
||
122 | -------- |
||
123 | >>> cmp = Guth() |
||
124 | >>> cmp.sim_score('cat', 'hat') |
||
125 | 1.0 |
||
126 | >>> cmp.sim_score('Niall', 'Neil') |
||
127 | 1.0 |
||
128 | >>> cmp.sim_score('aluminum', 'Catalan') |
||
129 | 0.0 |
||
130 | >>> cmp.sim_score('ATCG', 'TAGC') |
||
131 | 1.0 |
||
132 | |||
133 | |||
134 | .. versionadded:: 0.4.1 |
||
135 | |||
136 | """ |
||
137 | if src == tar: |
||
138 | return 1.0 |
||
139 | if not src or not tar: |
||
140 | 1 | return 0.0 |
|
141 | 1 | ||
142 | 1 | if self.params['tokenizer']: |
|
143 | 1 | src = self.params['tokenizer'].tokenize(src).get_list() |
|
144 | tar = self.params['tokenizer'].tokenize(tar).get_list() |
||
145 | 1 | ||
146 | 1 | for pos in range(len(src)): |
|
147 | 1 | s = self._token_at(src, pos) |
|
148 | if s and s in set(tar[max(0, pos - 1) : pos + 3]): |
||
149 | 1 | continue |
|
150 | 1 | ||
151 | 1 | t = self._token_at(tar, pos) |
|
152 | 1 | if t and t in set(src[max(0, pos - 1) : pos + 3]): |
|
153 | 1 | continue |
|
154 | |||
155 | 1 | s = self._token_at(src, pos + 1) |
|
156 | 1 | t = self._token_at(tar, pos + 1) |
|
157 | 1 | if s and t and s == t: |
|
158 | 1 | continue |
|
159 | |||
160 | 1 | s = self._token_at(src, pos + 2) |
|
161 | 1 | t = self._token_at(tar, pos + 2) |
|
162 | 1 | if s and t and s == t: |
|
163 | 1 | continue |
|
164 | |||
165 | 1 | break |
|
166 | 1 | else: |
|
167 | 1 | return 1.0 |
|
168 | 1 | return 0.0 |
|
169 | |||
276 |