Conditions | 23 |
Total Lines | 149 |
Code Lines | 57 |
Lines | 45 |
Ratio | 30.2 % |
Tests | 47 |
CRAP Score | 23 |
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.phonetic._koelner.Koelner.encode() 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 2014-2020 by Christopher C. Little. |
||
45 | def encode(self, word: str) -> str: |
||
46 | """Return the Kölner Phonetik (numeric output) code for a word. |
||
47 | |||
48 | While the output code is numeric, it is still a str because 0s can lead |
||
49 | 1 | the code. |
|
50 | |||
51 | Parameters |
||
52 | ---------- |
||
53 | word : str |
||
54 | The word to transform |
||
55 | |||
56 | Returns |
||
57 | 1 | ------- |
|
58 | str |
||
59 | 1 | The Kölner Phonetik value as a numeric string |
|
60 | 1 | ||
61 | Example |
||
62 | 1 | ------- |
|
63 | >>> pe = Koelner() |
||
64 | >>> pe.encode('Christopher') |
||
65 | '478237' |
||
66 | >>> pe.encode('Niall') |
||
67 | '65' |
||
68 | >>> pe.encode('Smith') |
||
69 | '862' |
||
70 | >>> pe.encode('Schmidt') |
||
71 | '862' |
||
72 | >>> pe.encode('Müller') |
||
73 | '657' |
||
74 | >>> pe.encode('Zimmermann') |
||
75 | '86766' |
||
76 | |||
77 | |||
78 | .. versionadded:: 0.1.0 |
||
79 | .. versionchanged:: 0.3.6 |
||
80 | Encapsulated in class |
||
81 | |||
82 | """ |
||
83 | |||
84 | def _after(word: str, pos: int, letters: Set[str]) -> bool: |
||
85 | """Return True if word[pos] follows one of the supplied letters. |
||
86 | |||
87 | Parameters |
||
88 | ---------- |
||
89 | word : str |
||
90 | The word to check |
||
91 | pos : int |
||
92 | Position within word to check |
||
93 | letters : {str} |
||
94 | Letters to confirm precede word[pos] |
||
95 | |||
96 | Returns |
||
97 | ------- |
||
98 | bool |
||
99 | True if word[pos] follows a value in letters |
||
100 | |||
101 | 1 | .. versionadded:: 0.1.0 |
|
102 | |||
103 | """ |
||
104 | return pos > 0 and word[pos - 1] in letters |
||
105 | |||
106 | def _before(word: str, pos: int, letters: Set[str]) -> bool: |
||
107 | """Return True if word[pos] precedes one of the supplied letters. |
||
108 | |||
109 | Parameters |
||
110 | ---------- |
||
111 | word : str |
||
112 | The word to check |
||
113 | pos : int |
||
114 | Position within word to check |
||
115 | letters : {str} |
||
116 | Letters to confirm follow word[pos] |
||
117 | |||
118 | Returns |
||
119 | ------- |
||
120 | bool |
||
121 | 1 | True if word[pos] precedes a value in letters |
|
122 | |||
123 | 1 | .. versionadded:: 0.1.0 |
|
124 | |||
125 | """ |
||
126 | return pos + 1 < len(word) and word[pos + 1] in letters |
||
127 | |||
128 | sdx = '' |
||
129 | |||
130 | word = unicode_normalize('NFKD', word.upper()) |
||
131 | |||
132 | word = word.replace('Ä', 'AE') |
||
133 | word = word.replace('Ö', 'OE') |
||
134 | word = word.replace('Ü', 'UE') |
||
135 | word = ''.join(c for c in word if c in self._uc_set) |
||
136 | |||
137 | # Nothing to convert, return base case |
||
138 | if not word: |
||
139 | return sdx |
||
140 | |||
141 | for i in range(len(word)): |
||
142 | View Code Duplication | if word[i] in self._uc_v_set: |
|
143 | 1 | sdx += '0' |
|
144 | elif word[i] == 'B': |
||
145 | 1 | sdx += '1' |
|
146 | elif word[i] == 'P': |
||
147 | 1 | if _before(word, i, {'H'}): |
|
148 | 1 | sdx += '3' |
|
149 | else: |
||
150 | 1 | sdx += '1' |
|
151 | 1 | elif word[i] in {'D', 'T'}: |
|
152 | 1 | if _before(word, i, {'C', 'S', 'Z'}): |
|
153 | 1 | sdx += '8' |
|
154 | else: |
||
155 | sdx += '2' |
||
156 | 1 | elif word[i] in {'F', 'V', 'W'}: |
|
157 | 1 | sdx += '3' |
|
158 | elif word[i] in {'G', 'K', 'Q'}: |
||
159 | 1 | sdx += '4' |
|
160 | 1 | elif word[i] == 'C': |
|
161 | 1 | if _after(word, i, {'S', 'Z'}): |
|
162 | 1 | sdx += '8' |
|
163 | 1 | elif i == 0: |
|
164 | 1 | if _before( |
|
165 | 1 | word, i, {'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X'} |
|
166 | 1 | ): |
|
167 | sdx += '4' |
||
168 | 1 | else: |
|
169 | 1 | sdx += '8' |
|
170 | 1 | elif _before(word, i, {'A', 'H', 'K', 'O', 'Q', 'U', 'X'}): |
|
171 | 1 | sdx += '4' |
|
172 | else: |
||
173 | 1 | sdx += '8' |
|
174 | 1 | elif word[i] == 'X': |
|
175 | 1 | if _after(word, i, {'C', 'K', 'Q'}): |
|
176 | 1 | sdx += '8' |
|
177 | 1 | else: |
|
178 | 1 | sdx += '48' |
|
179 | 1 | elif word[i] == 'L': |
|
180 | 1 | sdx += '5' |
|
181 | 1 | elif word[i] in {'M', 'N'}: |
|
182 | 1 | sdx += '6' |
|
183 | elif word[i] == 'R': |
||
184 | sdx += '7' |
||
185 | 1 | elif word[i] in {'S', 'Z'}: |
|
186 | sdx += '8' |
||
187 | 1 | ||
188 | 1 | sdx = self._delete_consecutive_repeats(sdx) |
|
189 | 1 | ||
190 | if sdx: |
||
191 | 1 | sdx = sdx[:1] + sdx[1:].replace('0', '') |
|
192 | 1 | ||
193 | 1 | return sdx |
|
194 | 1 | ||
234 |