Conditions | 66 |
Total Lines | 172 |
Code Lines | 114 |
Lines | 0 |
Ratio | 0 % |
Tests | 109 |
CRAP Score | 66 |
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._nysiis.NYSIIS.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 -*- |
||
49 | 1 | def encode(self, word, max_length=6, modified=False): |
|
50 | """Return the NYSIIS code for a word. |
||
51 | |||
52 | Parameters |
||
53 | ---------- |
||
54 | word : str |
||
55 | The word to transform |
||
56 | max_length : int |
||
57 | The maximum length (default 6) of the code to return |
||
58 | modified : bool |
||
59 | Indicates whether to use USDA modified NYSIIS |
||
60 | |||
61 | Returns |
||
62 | ------- |
||
63 | str |
||
64 | The NYSIIS value |
||
65 | |||
66 | Examples |
||
67 | -------- |
||
68 | >>> pe = NYSIIS() |
||
69 | >>> pe.encode('Christopher') |
||
70 | 'CRASTA' |
||
71 | >>> pe.encode('Niall') |
||
72 | 'NAL' |
||
73 | >>> pe.encode('Smith') |
||
74 | 'SNAT' |
||
75 | >>> pe.encode('Schmidt') |
||
76 | 'SNAD' |
||
77 | |||
78 | >>> pe.encode('Christopher', max_length=-1) |
||
79 | 'CRASTAFAR' |
||
80 | |||
81 | >>> pe.encode('Christopher', max_length=8, modified=True) |
||
82 | 'CRASTAFA' |
||
83 | >>> pe.encode('Niall', max_length=8, modified=True) |
||
84 | 'NAL' |
||
85 | >>> pe.encode('Smith', max_length=8, modified=True) |
||
86 | 'SNAT' |
||
87 | >>> pe.encode('Schmidt', max_length=8, modified=True) |
||
88 | 'SNAD' |
||
89 | |||
90 | """ |
||
91 | # Require a max_length of at least 6 |
||
92 | 1 | if max_length > -1: |
|
93 | 1 | max_length = max(6, max_length) |
|
94 | |||
95 | 1 | word = ''.join(c for c in word.upper() if c.isalpha()) |
|
96 | 1 | word = word.replace('ß', 'SS') |
|
97 | |||
98 | # exit early if there are no alphas |
||
99 | 1 | if not word: |
|
100 | 1 | return '' |
|
101 | |||
102 | 1 | original_first_char = word[0] |
|
103 | |||
104 | 1 | if word[:3] == 'MAC': |
|
105 | 1 | word = 'MCC' + word[3:] |
|
106 | 1 | elif word[:2] == 'KN': |
|
107 | 1 | word = 'NN' + word[2:] |
|
108 | 1 | elif word[:1] == 'K': |
|
109 | 1 | word = 'C' + word[1:] |
|
110 | 1 | elif word[:2] in {'PH', 'PF'}: |
|
111 | 1 | word = 'FF' + word[2:] |
|
112 | 1 | elif word[:3] == 'SCH': |
|
113 | 1 | word = 'SSS' + word[3:] |
|
114 | 1 | elif modified: |
|
115 | 1 | if word[:2] == 'WR': |
|
116 | 1 | word = 'RR' + word[2:] |
|
117 | 1 | elif word[:2] == 'RH': |
|
118 | 1 | word = 'RR' + word[2:] |
|
119 | 1 | elif word[:2] == 'DG': |
|
120 | 1 | word = 'GG' + word[2:] |
|
121 | 1 | elif word[:1] in self._uc_v_set: |
|
122 | 1 | word = 'A' + word[1:] |
|
123 | |||
124 | 1 | if modified and word[-1:] in {'S', 'Z'}: |
|
125 | 1 | word = word[:-1] |
|
126 | |||
127 | 1 | if ( |
|
128 | word[-2:] == 'EE' |
||
129 | or word[-2:] == 'IE' |
||
130 | or (modified and word[-2:] == 'YE') |
||
131 | ): |
||
132 | 1 | word = word[:-2] + 'Y' |
|
133 | 1 | elif word[-2:] in {'DT', 'RT', 'RD'}: |
|
134 | 1 | word = word[:-2] + 'D' |
|
135 | 1 | elif word[-2:] in {'NT', 'ND'}: |
|
136 | 1 | word = word[:-2] + ('N' if modified else 'D') |
|
137 | 1 | elif modified: |
|
138 | 1 | if word[-2:] == 'IX': |
|
139 | 1 | word = word[:-2] + 'ICK' |
|
140 | 1 | elif word[-2:] == 'EX': |
|
141 | 1 | word = word[:-2] + 'ECK' |
|
142 | 1 | elif word[-2:] in {'JR', 'SR'}: |
|
143 | 1 | return 'ERROR' |
|
144 | |||
145 | 1 | key = word[:1] |
|
146 | |||
147 | 1 | skip = 0 |
|
148 | 1 | for i in range(1, len(word)): |
|
149 | 1 | if i >= len(word): |
|
150 | 1 | continue |
|
151 | 1 | elif skip: |
|
152 | 1 | skip -= 1 |
|
153 | 1 | continue |
|
154 | 1 | elif word[i : i + 2] == 'EV': |
|
155 | 1 | word = word[:i] + 'AF' + word[i + 2 :] |
|
156 | 1 | skip = 1 |
|
157 | 1 | elif word[i] in self._uc_v_set: |
|
158 | 1 | word = word[:i] + 'A' + word[i + 1 :] |
|
159 | 1 | elif modified and i != len(word) - 1 and word[i] == 'Y': |
|
160 | 1 | word = word[:i] + 'A' + word[i + 1 :] |
|
161 | 1 | elif word[i] == 'Q': |
|
162 | 1 | word = word[:i] + 'G' + word[i + 1 :] |
|
163 | 1 | elif word[i] == 'Z': |
|
164 | 1 | word = word[:i] + 'S' + word[i + 1 :] |
|
165 | 1 | elif word[i] == 'M': |
|
166 | 1 | word = word[:i] + 'N' + word[i + 1 :] |
|
167 | 1 | elif word[i : i + 2] == 'KN': |
|
168 | 1 | word = word[:i] + 'N' + word[i + 2 :] |
|
169 | 1 | elif word[i] == 'K': |
|
170 | 1 | word = word[:i] + 'C' + word[i + 1 :] |
|
171 | 1 | elif modified and i == len(word) - 3 and word[i : i + 3] == 'SCH': |
|
172 | 1 | word = word[:i] + 'SSA' |
|
173 | 1 | skip = 2 |
|
174 | 1 | elif word[i : i + 3] == 'SCH': |
|
175 | 1 | word = word[:i] + 'SSS' + word[i + 3 :] |
|
176 | 1 | skip = 2 |
|
177 | 1 | elif modified and i == len(word) - 2 and word[i : i + 2] == 'SH': |
|
178 | 1 | word = word[:i] + 'SA' |
|
179 | 1 | skip = 1 |
|
180 | 1 | elif word[i : i + 2] == 'SH': |
|
181 | 1 | word = word[:i] + 'SS' + word[i + 2 :] |
|
182 | 1 | skip = 1 |
|
183 | 1 | elif word[i : i + 2] == 'PH': |
|
184 | 1 | word = word[:i] + 'FF' + word[i + 2 :] |
|
185 | 1 | skip = 1 |
|
186 | 1 | elif modified and word[i : i + 3] == 'GHT': |
|
187 | 1 | word = word[:i] + 'TTT' + word[i + 3 :] |
|
188 | 1 | skip = 2 |
|
189 | 1 | elif modified and word[i : i + 2] == 'DG': |
|
190 | 1 | word = word[:i] + 'GG' + word[i + 2 :] |
|
191 | 1 | skip = 1 |
|
192 | 1 | elif modified and word[i : i + 2] == 'WR': |
|
193 | 1 | word = word[:i] + 'RR' + word[i + 2 :] |
|
194 | 1 | skip = 1 |
|
195 | 1 | elif word[i] == 'H' and ( |
|
196 | word[i - 1] not in self._uc_v_set |
||
197 | or word[i + 1 : i + 2] not in self._uc_v_set |
||
198 | ): |
||
199 | 1 | word = word[:i] + word[i - 1] + word[i + 1 :] |
|
200 | 1 | elif word[i] == 'W' and word[i - 1] in self._uc_v_set: |
|
201 | 1 | word = word[:i] + word[i - 1] + word[i + 1 :] |
|
202 | |||
203 | 1 | if word[i : i + skip + 1] != key[-1:]: |
|
204 | 1 | key += word[i : i + skip + 1] |
|
205 | |||
206 | 1 | key = self._delete_consecutive_repeats(key) |
|
207 | |||
208 | 1 | if key[-1:] == 'S': |
|
209 | 1 | key = key[:-1] |
|
210 | 1 | if key[-2:] == 'AY': |
|
211 | 1 | key = key[:-2] + 'Y' |
|
212 | 1 | if key[-1:] == 'A': |
|
213 | 1 | key = key[:-1] |
|
214 | 1 | if modified and key[:1] == 'A': |
|
215 | 1 | key = original_first_char + key[1:] |
|
216 | |||
217 | 1 | if max_length > 0: |
|
218 | 1 | key = key[:max_length] |
|
219 | |||
220 | 1 | return key |
|
221 | |||
273 |