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