Conditions | 32 |
Total Lines | 259 |
Code Lines | 177 |
Lines | 0 |
Ratio | 0 % |
Tests | 87 |
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._sv.sfinxbis() 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 -*- |
||
39 | 1 | def sfinxbis(word, max_length=-1): |
|
40 | """Return the SfinxBis code for a word. |
||
41 | |||
42 | SfinxBis is a Soundex-like algorithm defined in :cite:`Axelsson:2009`. |
||
43 | |||
44 | This implementation follows the reference implementation: |
||
45 | :cite:`Sjoo:2009`. |
||
46 | |||
47 | SfinxBis is intended chiefly for Swedish names. |
||
48 | |||
49 | :param str word: the word to transform |
||
50 | :param int max_length: the length of the code returned (defaults to |
||
51 | unlimited) |
||
52 | :returns: the SfinxBis value |
||
53 | :rtype: tuple |
||
54 | |||
55 | >>> sfinxbis('Christopher') |
||
56 | ('K68376',) |
||
57 | >>> sfinxbis('Niall') |
||
58 | ('N4',) |
||
59 | >>> sfinxbis('Smith') |
||
60 | ('S53',) |
||
61 | >>> sfinxbis('Schmidt') |
||
62 | ('S53',) |
||
63 | |||
64 | >>> sfinxbis('Johansson') |
||
65 | ('J585',) |
||
66 | >>> sfinxbis('Sjöberg') |
||
67 | ('#162',) |
||
68 | """ |
||
69 | 1 | adelstitler = ( |
|
70 | ' DE LA ', |
||
71 | ' DE LAS ', |
||
72 | ' DE LOS ', |
||
73 | ' VAN DE ', |
||
74 | ' VAN DEN ', |
||
75 | ' VAN DER ', |
||
76 | ' VON DEM ', |
||
77 | ' VON DER ', |
||
78 | ' AF ', |
||
79 | ' AV ', |
||
80 | ' DA ', |
||
81 | ' DE ', |
||
82 | ' DEL ', |
||
83 | ' DEN ', |
||
84 | ' DES ', |
||
85 | ' DI ', |
||
86 | ' DO ', |
||
87 | ' DON ', |
||
88 | ' DOS ', |
||
89 | ' DU ', |
||
90 | ' E ', |
||
91 | ' IN ', |
||
92 | ' LA ', |
||
93 | ' LE ', |
||
94 | ' MAC ', |
||
95 | ' MC ', |
||
96 | ' VAN ', |
||
97 | ' VON ', |
||
98 | ' Y ', |
||
99 | ' S:T ', |
||
100 | ) |
||
101 | |||
102 | 1 | _harde_vokaler = {'A', 'O', 'U', 'Å'} |
|
103 | 1 | _mjuka_vokaler = {'E', 'I', 'Y', 'Ä', 'Ö'} |
|
104 | 1 | _konsonanter = { |
|
105 | 'B', |
||
106 | 'C', |
||
107 | 'D', |
||
108 | 'F', |
||
109 | 'G', |
||
110 | 'H', |
||
111 | 'J', |
||
112 | 'K', |
||
113 | 'L', |
||
114 | 'M', |
||
115 | 'N', |
||
116 | 'P', |
||
117 | 'Q', |
||
118 | 'R', |
||
119 | 'S', |
||
120 | 'T', |
||
121 | 'V', |
||
122 | 'W', |
||
123 | 'X', |
||
124 | 'Z', |
||
125 | } |
||
126 | 1 | _alfabet = { |
|
127 | 'A', |
||
128 | 'B', |
||
129 | 'C', |
||
130 | 'D', |
||
131 | 'E', |
||
132 | 'F', |
||
133 | 'G', |
||
134 | 'H', |
||
135 | 'I', |
||
136 | 'J', |
||
137 | 'K', |
||
138 | 'L', |
||
139 | 'M', |
||
140 | 'N', |
||
141 | 'O', |
||
142 | 'P', |
||
143 | 'Q', |
||
144 | 'R', |
||
145 | 'S', |
||
146 | 'T', |
||
147 | 'U', |
||
148 | 'V', |
||
149 | 'W', |
||
150 | 'X', |
||
151 | 'Y', |
||
152 | 'Z', |
||
153 | 'Ä', |
||
154 | 'Å', |
||
155 | 'Ö', |
||
156 | } |
||
157 | |||
158 | 1 | _sfinxbis_translation = dict( |
|
159 | zip( |
||
160 | (ord(_) for _ in 'BCDFGHJKLMNPQRSTVZAOUÅEIYÄÖ'), |
||
|
|||
161 | '123729224551268378999999999', |
||
162 | ) |
||
163 | ) |
||
164 | |||
165 | 1 | _sfinxbis_substitutions = dict( |
|
166 | zip( |
||
167 | (ord(_) for _ in 'WZÀÁÂÃÆÇÈÉÊËÌÍÎÏÑÒÓÔÕØÙÚÛÜÝ'), |
||
168 | 'VSAAAAÄCEEEEIIIINOOOOÖUUUYY', |
||
169 | ) |
||
170 | ) |
||
171 | |||
172 | 1 | def _foersvensker(lokal_ordet): |
|
173 | """Return the Swedish-ized form of the word.""" |
||
174 | 1 | lokal_ordet = lokal_ordet.replace('STIERN', 'STJÄRN') |
|
175 | 1 | lokal_ordet = lokal_ordet.replace('HIE', 'HJ') |
|
176 | 1 | lokal_ordet = lokal_ordet.replace('SIÖ', 'SJÖ') |
|
177 | 1 | lokal_ordet = lokal_ordet.replace('SCH', 'SH') |
|
178 | 1 | lokal_ordet = lokal_ordet.replace('QU', 'KV') |
|
179 | 1 | lokal_ordet = lokal_ordet.replace('IO', 'JO') |
|
180 | 1 | lokal_ordet = lokal_ordet.replace('PH', 'F') |
|
181 | |||
182 | 1 | for i in _harde_vokaler: |
|
183 | 1 | lokal_ordet = lokal_ordet.replace(i + 'Ü', i + 'J') |
|
184 | 1 | lokal_ordet = lokal_ordet.replace(i + 'Y', i + 'J') |
|
185 | 1 | lokal_ordet = lokal_ordet.replace(i + 'I', i + 'J') |
|
186 | 1 | for i in _mjuka_vokaler: |
|
187 | 1 | lokal_ordet = lokal_ordet.replace(i + 'Ü', i + 'J') |
|
188 | 1 | lokal_ordet = lokal_ordet.replace(i + 'Y', i + 'J') |
|
189 | 1 | lokal_ordet = lokal_ordet.replace(i + 'I', i + 'J') |
|
190 | |||
191 | 1 | if 'H' in lokal_ordet: |
|
192 | 1 | for i in _konsonanter: |
|
193 | 1 | lokal_ordet = lokal_ordet.replace('H' + i, i) |
|
194 | |||
195 | 1 | lokal_ordet = lokal_ordet.translate(_sfinxbis_substitutions) |
|
196 | |||
197 | 1 | lokal_ordet = lokal_ordet.replace('Ð', 'ETH') |
|
198 | 1 | lokal_ordet = lokal_ordet.replace('Þ', 'TH') |
|
199 | 1 | lokal_ordet = lokal_ordet.replace('ß', 'SS') |
|
200 | |||
201 | 1 | return lokal_ordet |
|
202 | |||
203 | 1 | def _koda_foersta_ljudet(lokal_ordet): |
|
204 | """Return the word with the first sound coded.""" |
||
205 | 1 | if ( |
|
206 | lokal_ordet[0:1] in _mjuka_vokaler |
||
207 | or lokal_ordet[0:1] in _harde_vokaler |
||
208 | ): |
||
209 | 1 | lokal_ordet = '$' + lokal_ordet[1:] |
|
210 | 1 | elif lokal_ordet[0:2] in ('DJ', 'GJ', 'HJ', 'LJ'): |
|
211 | 1 | lokal_ordet = 'J' + lokal_ordet[2:] |
|
212 | 1 | elif lokal_ordet[0:1] == 'G' and lokal_ordet[1:2] in _mjuka_vokaler: |
|
213 | 1 | lokal_ordet = 'J' + lokal_ordet[1:] |
|
214 | 1 | elif lokal_ordet[0:1] == 'Q': |
|
215 | 1 | lokal_ordet = 'K' + lokal_ordet[1:] |
|
216 | 1 | elif lokal_ordet[0:2] == 'CH' and lokal_ordet[2:3] in frozenset( |
|
217 | _mjuka_vokaler | _harde_vokaler |
||
218 | ): |
||
219 | 1 | lokal_ordet = '#' + lokal_ordet[2:] |
|
220 | 1 | elif lokal_ordet[0:1] == 'C' and lokal_ordet[1:2] in _harde_vokaler: |
|
221 | 1 | lokal_ordet = 'K' + lokal_ordet[1:] |
|
222 | 1 | elif lokal_ordet[0:1] == 'C' and lokal_ordet[1:2] in _konsonanter: |
|
223 | 1 | lokal_ordet = 'K' + lokal_ordet[1:] |
|
224 | 1 | elif lokal_ordet[0:1] == 'X': |
|
225 | 1 | lokal_ordet = 'S' + lokal_ordet[1:] |
|
226 | 1 | elif lokal_ordet[0:1] == 'C' and lokal_ordet[1:2] in _mjuka_vokaler: |
|
227 | 1 | lokal_ordet = 'S' + lokal_ordet[1:] |
|
228 | 1 | elif lokal_ordet[0:3] in ('SKJ', 'STJ', 'SCH'): |
|
229 | 1 | lokal_ordet = '#' + lokal_ordet[3:] |
|
230 | 1 | elif lokal_ordet[0:2] in ('SH', 'KJ', 'TJ', 'SJ'): |
|
231 | 1 | lokal_ordet = '#' + lokal_ordet[2:] |
|
232 | 1 | elif lokal_ordet[0:2] == 'SK' and lokal_ordet[2:3] in _mjuka_vokaler: |
|
233 | 1 | lokal_ordet = '#' + lokal_ordet[2:] |
|
234 | 1 | elif lokal_ordet[0:1] == 'K' and lokal_ordet[1:2] in _mjuka_vokaler: |
|
235 | 1 | lokal_ordet = '#' + lokal_ordet[1:] |
|
236 | 1 | return lokal_ordet |
|
237 | |||
238 | # Steg 1, Versaler |
||
239 | 1 | word = unicode_normalize('NFC', text_type(word.upper())) |
|
240 | 1 | word = word.replace('ß', 'SS') |
|
241 | 1 | word = word.replace('-', ' ') |
|
242 | |||
243 | # Steg 2, Ta bort adelsprefix |
||
244 | 1 | for adelstitel in adelstitler: |
|
245 | 1 | while adelstitel in word: |
|
246 | 1 | word = word.replace(adelstitel, ' ') |
|
247 | 1 | if word.startswith(adelstitel[1:]): |
|
248 | 1 | word = word[len(adelstitel) - 1 :] |
|
249 | |||
250 | # Split word into tokens |
||
251 | 1 | ordlista = word.split() |
|
252 | |||
253 | # Steg 3, Ta bort dubbelteckning i början på namnet |
||
254 | 1 | ordlista = [_delete_consecutive_repeats(ordet) for ordet in ordlista] |
|
255 | 1 | if not ordlista: |
|
256 | # noinspection PyRedundantParentheses |
||
257 | 1 | return ('',) |
|
258 | |||
259 | # Steg 4, Försvenskning |
||
260 | 1 | ordlista = [_foersvensker(ordet) for ordet in ordlista] |
|
261 | |||
262 | # Steg 5, Ta bort alla tecken som inte är A-Ö (65-90,196,197,214) |
||
263 | 1 | ordlista = [ |
|
264 | ''.join(c for c in ordet if c in _alfabet) for ordet in ordlista |
||
265 | ] |
||
266 | |||
267 | # Steg 6, Koda första ljudet |
||
268 | 1 | ordlista = [_koda_foersta_ljudet(ordet) for ordet in ordlista] |
|
269 | |||
270 | # Steg 7, Dela upp namnet i två delar |
||
271 | 1 | rest = [ordet[1:] for ordet in ordlista] |
|
272 | |||
273 | # Steg 8, Utför fonetisk transformation i resten |
||
274 | 1 | rest = [ordet.replace('DT', 'T') for ordet in rest] |
|
275 | 1 | rest = [ordet.replace('X', 'KS') for ordet in rest] |
|
276 | |||
277 | # Steg 9, Koda resten till en sifferkod |
||
278 | 1 | for vokal in _mjuka_vokaler: |
|
279 | 1 | rest = [ordet.replace('C' + vokal, '8' + vokal) for ordet in rest] |
|
280 | 1 | rest = [ordet.translate(_sfinxbis_translation) for ordet in rest] |
|
281 | |||
282 | # Steg 10, Ta bort intilliggande dubbletter |
||
283 | 1 | rest = [_delete_consecutive_repeats(ordet) for ordet in rest] |
|
284 | |||
285 | # Steg 11, Ta bort alla "9" |
||
286 | 1 | rest = [ordet.replace('9', '') for ordet in rest] |
|
287 | |||
288 | # Steg 12, Sätt ihop delarna igen |
||
289 | 1 | ordlista = [ |
|
290 | ''.join(ordet) for ordet in zip((_[0:1] for _ in ordlista), rest) |
||
291 | ] |
||
292 | |||
293 | # truncate, if max_length is set |
||
294 | 1 | if max_length > 0: |
|
295 | 1 | ordlista = [ordet[:max_length] for ordet in ordlista] |
|
296 | |||
297 | 1 | return tuple(ordlista) |
|
298 | |||
409 |