Conditions | 32 |
Total Lines | 193 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Tests | 81 |
CRAP Score | 32 |
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._dolby.Dolby.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 -*- |
||
42 | 1 | def encode(self, word, max_length=-1, keep_vowels=False, vowel_char='*'): |
|
43 | r"""Return the Dolby Code of a name. |
||
44 | |||
45 | Args: |
||
46 | word (str): The word to transform |
||
47 | max_length (int): Maximum length of the returned Dolby code -- this |
||
48 | also activates the fixed-length code mode if it is greater than |
||
49 | 0 |
||
50 | keep_vowels (bool): If True, retains all vowel markers |
||
51 | vowel_char (str): The vowel marker character (default to \*) |
||
52 | |||
53 | Returns: |
||
54 | str: The Dolby Code |
||
55 | |||
56 | Examples: |
||
57 | >>> pe = Dolby() |
||
58 | >>> pe.encode('Hansen') |
||
59 | 'H*NSN' |
||
60 | >>> pe.encode('Larsen') |
||
61 | 'L*RSN' |
||
62 | >>> pe.encode('Aagaard') |
||
63 | '*GR' |
||
64 | >>> pe.encode('Braaten') |
||
65 | 'BR*DN' |
||
66 | >>> pe.encode('Sandvik') |
||
67 | 'S*NVK' |
||
68 | >>> pe.encode('Hansen', max_length=6) |
||
69 | 'H*NS*N' |
||
70 | >>> pe.encode('Larsen', max_length=6) |
||
71 | 'L*RS*N' |
||
72 | >>> pe.encode('Aagaard', max_length=6) |
||
73 | '*G*R ' |
||
74 | >>> pe.encode('Braaten', max_length=6) |
||
75 | 'BR*D*N' |
||
76 | >>> pe.encode('Sandvik', max_length=6) |
||
77 | 'S*NF*K' |
||
78 | |||
79 | >>> pe.encode('Smith') |
||
80 | 'SM*D' |
||
81 | >>> pe.encode('Waters') |
||
82 | 'W*DRS' |
||
83 | >>> pe.encode('James') |
||
84 | 'J*MS' |
||
85 | >>> pe.encode('Schmidt') |
||
86 | 'SM*D' |
||
87 | >>> pe.encode('Ashcroft') |
||
88 | '*SKRFD' |
||
89 | >>> pe.encode('Smith', max_length=6) |
||
90 | 'SM*D ' |
||
91 | >>> pe.encode('Waters', max_length=6) |
||
92 | 'W*D*RS' |
||
93 | >>> pe.encode('James', max_length=6) |
||
94 | 'J*M*S ' |
||
95 | >>> pe.encode('Schmidt', max_length=6) |
||
96 | 'SM*D ' |
||
97 | >>> pe.encode('Ashcroft', max_length=6) |
||
98 | '*SKRFD' |
||
99 | |||
100 | """ |
||
101 | # uppercase, normalize, decompose, and filter non-A-Z out |
||
102 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
103 | 1 | word = word.replace('ß', 'SS') |
|
104 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
105 | |||
106 | # Rule 1 (FL2) |
||
107 | 1 | if word[:3] in {'MCG', 'MAG', 'MAC'}: |
|
108 | 1 | word = 'MK' + word[3:] |
|
109 | 1 | elif word[:2] == 'MC': |
|
110 | 1 | word = 'MK' + word[2:] |
|
111 | |||
112 | # Rule 2 (FL3) |
||
113 | 1 | pos = len(word) - 2 |
|
114 | 1 | while pos > -1: |
|
115 | 1 | if word[pos : pos + 2] in { |
|
116 | 'DT', |
||
117 | 'LD', |
||
118 | 'ND', |
||
119 | 'NT', |
||
120 | 'RC', |
||
121 | 'RD', |
||
122 | 'RT', |
||
123 | 'SC', |
||
124 | 'SK', |
||
125 | 'ST', |
||
126 | }: |
||
127 | 1 | word = word[: pos + 1] + word[pos + 2 :] |
|
128 | 1 | pos += 1 |
|
129 | 1 | pos -= 1 |
|
130 | |||
131 | # Rule 3 (FL4) |
||
132 | # Although the rule indicates "after the first letter", the test cases |
||
133 | # make it clear that these apply to the first letter also. |
||
134 | 1 | word = word.replace('X', 'KS') |
|
135 | 1 | word = word.replace('CE', 'SE') |
|
136 | 1 | word = word.replace('CI', 'SI') |
|
137 | 1 | word = word.replace('CY', 'SI') |
|
138 | |||
139 | # not in the rule set, but they seem to have intended it |
||
140 | 1 | word = word.replace('TCH', 'CH') |
|
141 | |||
142 | 1 | pos = word.find('CH', 1) |
|
143 | 1 | while pos != -1: |
|
144 | 1 | if word[pos - 1 : pos] not in self._uc_vy_set: |
|
145 | 1 | word = word[:pos] + 'S' + word[pos + 1 :] |
|
146 | 1 | pos = word.find('CH', pos + 1) |
|
147 | |||
148 | 1 | word = word.replace('C', 'K') |
|
149 | 1 | word = word.replace('Z', 'S') |
|
150 | |||
151 | 1 | word = word.replace('WR', 'R') |
|
152 | 1 | word = word.replace('DG', 'G') |
|
153 | 1 | word = word.replace('QU', 'K') |
|
154 | 1 | word = word.replace('T', 'D') |
|
155 | 1 | word = word.replace('PH', 'F') |
|
156 | |||
157 | # Rule 4 (FL5) |
||
158 | # Although the rule indicates "after the first letter", the test cases |
||
159 | # make it clear that these apply to the first letter also. |
||
160 | 1 | pos = word.find('K', 0) |
|
161 | 1 | while pos != -1: |
|
162 | 1 | if pos > 1 and word[pos - 1 : pos] not in self._uc_vy_set | { |
|
163 | 'L', |
||
164 | 'N', |
||
165 | 'R', |
||
166 | }: |
||
167 | 1 | word = word[: pos - 1] + word[pos:] |
|
168 | 1 | pos -= 1 |
|
169 | 1 | pos = word.find('K', pos + 1) |
|
170 | |||
171 | # Rule FL6 |
||
172 | 1 | if max_length > 0 and word[-1:] == 'E': |
|
173 | 1 | word = word[:-1] |
|
174 | |||
175 | # Rule 5 (FL7) |
||
176 | 1 | word = self._delete_consecutive_repeats(word) |
|
177 | |||
178 | # Rule 6 (FL8) |
||
179 | 1 | if word[:2] == 'PF': |
|
180 | 1 | word = word[1:] |
|
181 | 1 | if word[-2:] == 'PF': |
|
182 | 1 | word = word[:-1] |
|
183 | 1 | elif word[-2:] == 'GH': |
|
184 | 1 | if word[-3:-2] in self._uc_vy_set: |
|
185 | 1 | word = word[:-2] + 'F' |
|
186 | else: |
||
187 | 1 | word = word[:-2] + 'G' |
|
188 | 1 | word = word.replace('GH', '') |
|
189 | |||
190 | # Rule FL9 |
||
191 | 1 | if max_length > 0: |
|
192 | 1 | word = word.replace('V', 'F') |
|
193 | |||
194 | # Rules 7-9 (FL10-FL12) |
||
195 | 1 | first = 1 + (1 if max_length > 0 else 0) |
|
196 | 1 | code = '' |
|
197 | 1 | for pos, char in enumerate(word): |
|
198 | 1 | if char in self._uc_vy_set: |
|
199 | 1 | if first or keep_vowels: |
|
200 | 1 | code += vowel_char |
|
201 | 1 | first -= 1 |
|
202 | 1 | elif pos > 0 and char in {'W', 'H'}: |
|
203 | 1 | continue |
|
204 | else: |
||
205 | 1 | code += char |
|
206 | |||
207 | 1 | if max_length > 0: |
|
208 | # Rule FL13 |
||
209 | 1 | if len(code) > max_length and code[-1:] == 'S': |
|
210 | 1 | code = code[:-1] |
|
211 | 1 | if keep_vowels: |
|
212 | 1 | code = code[:max_length] |
|
213 | else: |
||
214 | # Rule FL14 |
||
215 | 1 | code = code[: max_length + 2] |
|
216 | # Rule FL15 |
||
217 | 1 | while len(code) > max_length: |
|
218 | 1 | vowels = len(code) - max_length |
|
219 | 1 | excess = vowels - 1 |
|
220 | 1 | word = code |
|
221 | 1 | code = '' |
|
222 | 1 | for char in word: |
|
223 | 1 | if char == vowel_char: |
|
224 | 1 | if vowels: |
|
225 | 1 | code += char |
|
226 | 1 | vowels -= 1 |
|
227 | else: |
||
228 | 1 | code += char |
|
229 | 1 | code = code[: max_length + excess] |
|
230 | |||
231 | # Rule FL16 |
||
232 | 1 | code += ' ' * (max_length - len(code)) |
|
233 | |||
234 | 1 | return code |
|
235 | |||
305 |