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