Conditions | 56 |
Total Lines | 175 |
Code Lines | 111 |
Lines | 0 |
Ratio | 0 % |
Tests | 96 |
CRAP Score | 56 |
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._henry_early.HenryEarly.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 -*- |
||
59 | 1 | def encode(self, word, max_length=3): |
|
60 | """Calculate the early version of the Henry code for a word. |
||
61 | |||
62 | Args: |
||
63 | word (str): The word to transform |
||
64 | max_length (int): The length of the code returned (defaults to 3) |
||
65 | |||
66 | Returns: |
||
67 | str: The early Henry code |
||
68 | |||
69 | Examples: |
||
70 | >>> henry_early('Marchand') |
||
71 | 'MRC' |
||
72 | >>> henry_early('Beaulieu') |
||
73 | 'BL' |
||
74 | >>> henry_early('Beaumont') |
||
75 | 'BM' |
||
76 | >>> henry_early('Legrand') |
||
77 | 'LGR' |
||
78 | >>> henry_early('Pelletier') |
||
79 | 'PLT' |
||
80 | |||
81 | """ |
||
82 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
83 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
84 | |||
85 | 1 | if not word: |
|
86 | 1 | return '' |
|
87 | |||
88 | # Rule Ia seems to be covered entirely in II |
||
89 | |||
90 | # Rule Ib |
||
91 | 1 | if word[0] in self._uc_vy_set: |
|
92 | # Ib1 |
||
93 | 1 | if ( |
|
94 | word[1:2] in self._uc_c_set - {'M', 'N'} |
||
95 | and word[2:3] in self._uc_c_set |
||
96 | ) or ( |
||
97 | word[1:2] in self._uc_c_set and word[2:3] not in self._uc_c_set |
||
98 | ): |
||
99 | 1 | if word[0] == 'Y': |
|
100 | 1 | word = 'I' + word[1:] |
|
101 | # Ib2 |
||
102 | 1 | elif word[1:2] in {'M', 'N'} and word[2:3] in self._uc_c_set: |
|
103 | 1 | if word[0] == 'E': |
|
104 | 1 | word = 'A' + word[1:] |
|
105 | 1 | elif word[0] in {'I', 'U', 'Y'}: |
|
106 | 1 | word = 'E' + word[1:] |
|
107 | # Ib3 |
||
108 | 1 | elif word[:2] in self._diph: |
|
109 | 1 | word = self._diph[word[:2]] + word[2:] |
|
110 | # Ib4 |
||
111 | 1 | elif word[1:2] in self._uc_vy_set and word[0] == 'Y': |
|
112 | 1 | word = 'I' + word[1:] |
|
113 | |||
114 | 1 | code = '' |
|
115 | 1 | skip = 0 |
|
116 | |||
117 | # Rule II |
||
118 | 1 | for pos, char in enumerate(word): |
|
119 | 1 | nxch = word[pos + 1 : pos + 2] |
|
120 | 1 | prev = word[pos - 1 : pos] |
|
121 | |||
122 | 1 | if skip: |
|
123 | 1 | skip -= 1 |
|
124 | 1 | elif char in self._uc_vy_set: |
|
125 | 1 | code += char |
|
126 | # IIc |
||
127 | 1 | elif char == nxch: |
|
128 | 1 | skip = 1 |
|
129 | 1 | code += char |
|
130 | 1 | elif word[pos : pos + 2] in {'CQ', 'DT', 'SC'}: |
|
131 | 1 | continue |
|
132 | # IIb |
||
133 | 1 | elif char in self._simple: |
|
134 | 1 | code += self._simple[char] |
|
135 | 1 | elif char in {'C', 'G', 'P', 'Q', 'S'}: |
|
136 | 1 | if char == 'C': |
|
137 | 1 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
|
138 | 1 | code += 'K' |
|
139 | 1 | elif nxch in {'E', 'I', 'Y'}: |
|
140 | 1 | code += 'S' |
|
141 | 1 | elif nxch == 'H': |
|
142 | 1 | if word[pos + 2 : pos + 3] in self._uc_vy_set: |
|
143 | 1 | code += 'C' |
|
144 | else: # CHR, CHL, etc. |
||
145 | 1 | code += 'K' |
|
146 | else: |
||
147 | 1 | code += 'C' |
|
148 | 1 | elif char == 'G': |
|
149 | 1 | if nxch in {'A', 'O', 'U', 'L', 'R'}: |
|
150 | 1 | code += 'G' |
|
151 | 1 | elif nxch in {'E', 'I', 'Y'}: |
|
152 | 1 | code += 'J' |
|
153 | 1 | elif nxch == 'N': |
|
154 | 1 | code += 'N' |
|
155 | 1 | elif char == 'P': |
|
156 | 1 | if nxch != 'H': |
|
157 | 1 | code += 'P' |
|
158 | else: |
||
159 | 1 | code += 'F' |
|
160 | 1 | elif char == 'Q': |
|
161 | 1 | if word[pos + 1 : pos + 3] in {'UE', 'UI', 'UY'}: |
|
162 | 1 | code += 'G' |
|
163 | else: # QUA, QUO, etc. |
||
164 | 1 | code += 'K' |
|
165 | else: # S... |
||
166 | 1 | if word[pos : pos + 6] == 'SAINTE': |
|
167 | 1 | code += 'X' |
|
168 | 1 | skip = 5 |
|
169 | 1 | elif word[pos : pos + 5] == 'SAINT': |
|
170 | 1 | code += 'X' |
|
171 | 1 | skip = 4 |
|
172 | 1 | elif word[pos : pos + 3] == 'STE': |
|
173 | 1 | code += 'X' |
|
174 | 1 | skip = 2 |
|
175 | 1 | elif word[pos : pos + 2] == 'ST': |
|
176 | 1 | code += 'X' |
|
177 | 1 | skip = 1 |
|
178 | 1 | elif nxch in self._uc_c_set: |
|
179 | 1 | continue |
|
180 | else: |
||
181 | 1 | code += 'S' |
|
182 | # IId |
||
183 | 1 | elif char == 'H' and prev in self._uc_c_set: |
|
184 | 1 | continue |
|
185 | 1 | elif char in self._uc_c_set - { |
|
186 | 'L', |
||
187 | 'R', |
||
188 | } and nxch in self._uc_c_set - {'L', 'R'}: |
||
189 | 1 | continue |
|
190 | 1 | elif char == 'L' and nxch in {'M', 'N'}: |
|
191 | 1 | continue |
|
192 | 1 | elif ( |
|
193 | char in {'M', 'N'} |
||
194 | and prev in self._uc_vy_set |
||
195 | and nxch in self._uc_c_set |
||
196 | ): |
||
197 | 1 | continue |
|
198 | # IIa |
||
199 | else: |
||
200 | 1 | code += char |
|
201 | |||
202 | # IIe1 |
||
203 | 1 | if code[-4:] in {'AULT', 'EULT', 'OULT'}: |
|
204 | 1 | code = code[:-2] |
|
205 | # The following are blocked by rules above |
||
206 | # elif code[-4:-3] in _vows and code[-3:] == 'MPS': |
||
207 | # code = code[:-3] |
||
208 | # elif code[-3:-2] in _vows and code[-2:] in {'MB', 'MP', 'ND', |
||
209 | # 'NS', 'NT'}: |
||
210 | # code = code[:-2] |
||
211 | 1 | elif code[-2:-1] == 'R' and code[-1:] in self._uc_c_set: |
|
212 | 1 | code = code[:-1] |
|
213 | # IIe2 |
||
214 | 1 | elif code[-2:-1] in self._uc_vy_set and code[-1:] in { |
|
215 | 'D', |
||
216 | 'M', |
||
217 | 'N', |
||
218 | 'S', |
||
219 | 'T', |
||
220 | }: |
||
221 | 1 | code = code[:-1] |
|
222 | 1 | elif code[-2:] == 'ER': |
|
223 | 1 | code = code[:-1] |
|
224 | |||
225 | # Drop non-initial vowels |
||
226 | 1 | code = code[:1] + code[1:].translate( |
|
227 | {65: '', 69: '', 73: '', 79: '', 85: '', 89: ''} |
||
228 | ) |
||
229 | |||
230 | 1 | if max_length != -1: |
|
231 | 1 | code = code[:max_length] |
|
232 | |||
233 | 1 | return code |
|
234 | |||
268 |