Conditions | 32 |
Total Lines | 174 |
Code Lines | 103 |
Lines | 0 |
Ratio | 0 % |
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 | 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 | adelstitler = (' DE LA ', ' DE LAS ', ' DE LOS ', ' VAN DE ', ' VAN DEN ', |
||
70 | ' VAN DER ', ' VON DEM ', ' VON DER ', |
||
71 | ' AF ', ' AV ', ' DA ', ' DE ', ' DEL ', ' DEN ', ' DES ', |
||
72 | ' DI ', ' DO ', ' DON ', ' DOS ', ' DU ', ' E ', ' IN ', |
||
73 | ' LA ', ' LE ', ' MAC ', ' MC ', ' VAN ', ' VON ', ' Y ', |
||
74 | ' S:T ') |
||
75 | |||
76 | _harde_vokaler = {'A', 'O', 'U', 'Å'} |
||
77 | _mjuka_vokaler = {'E', 'I', 'Y', 'Ä', 'Ö'} |
||
78 | _konsonanter = {'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', |
||
79 | 'Q', 'R', 'S', 'T', 'V', 'W', 'X', 'Z'} |
||
80 | _alfabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', |
||
81 | 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
||
82 | 'Y', 'Z', 'Ä', 'Å', 'Ö'} |
||
83 | |||
84 | _sfinxbis_translation = dict(zip((ord(_) for _ in |
||
|
|||
85 | 'BCDFGHJKLMNPQRSTVZAOUÅEIYÄÖ'), |
||
86 | '123729224551268378999999999')) |
||
87 | |||
88 | _sfinxbis_substitutions = dict(zip((ord(_) for _ in |
||
89 | 'WZÀÁÂÃÆÇÈÉÊËÌÍÎÏÑÒÓÔÕØÙÚÛÜÝ'), |
||
90 | 'VSAAAAÄCEEEEIIIINOOOOÖUUUYY')) |
||
91 | |||
92 | def _foersvensker(lokal_ordet): |
||
93 | """Return the Swedish-ized form of the word.""" |
||
94 | lokal_ordet = lokal_ordet.replace('STIERN', 'STJÄRN') |
||
95 | lokal_ordet = lokal_ordet.replace('HIE', 'HJ') |
||
96 | lokal_ordet = lokal_ordet.replace('SIÖ', 'SJÖ') |
||
97 | lokal_ordet = lokal_ordet.replace('SCH', 'SH') |
||
98 | lokal_ordet = lokal_ordet.replace('QU', 'KV') |
||
99 | lokal_ordet = lokal_ordet.replace('IO', 'JO') |
||
100 | lokal_ordet = lokal_ordet.replace('PH', 'F') |
||
101 | |||
102 | for i in _harde_vokaler: |
||
103 | lokal_ordet = lokal_ordet.replace(i+'Ü', i+'J') |
||
104 | lokal_ordet = lokal_ordet.replace(i+'Y', i+'J') |
||
105 | lokal_ordet = lokal_ordet.replace(i+'I', i+'J') |
||
106 | for i in _mjuka_vokaler: |
||
107 | lokal_ordet = lokal_ordet.replace(i+'Ü', i+'J') |
||
108 | lokal_ordet = lokal_ordet.replace(i+'Y', i+'J') |
||
109 | lokal_ordet = lokal_ordet.replace(i+'I', i+'J') |
||
110 | |||
111 | if 'H' in lokal_ordet: |
||
112 | for i in _konsonanter: |
||
113 | lokal_ordet = lokal_ordet.replace('H'+i, i) |
||
114 | |||
115 | lokal_ordet = lokal_ordet.translate(_sfinxbis_substitutions) |
||
116 | |||
117 | lokal_ordet = lokal_ordet.replace('Ð', 'ETH') |
||
118 | lokal_ordet = lokal_ordet.replace('Þ', 'TH') |
||
119 | lokal_ordet = lokal_ordet.replace('ß', 'SS') |
||
120 | |||
121 | return lokal_ordet |
||
122 | |||
123 | def _koda_foersta_ljudet(lokal_ordet): |
||
124 | """Return the word with the first sound coded.""" |
||
125 | if (lokal_ordet[0:1] in _mjuka_vokaler or |
||
126 | lokal_ordet[0:1] in _harde_vokaler): |
||
127 | lokal_ordet = '$' + lokal_ordet[1:] |
||
128 | elif lokal_ordet[0:2] in ('DJ', 'GJ', 'HJ', 'LJ'): |
||
129 | lokal_ordet = 'J' + lokal_ordet[2:] |
||
130 | elif lokal_ordet[0:1] == 'G' and lokal_ordet[1:2] in _mjuka_vokaler: |
||
131 | lokal_ordet = 'J' + lokal_ordet[1:] |
||
132 | elif lokal_ordet[0:1] == 'Q': |
||
133 | lokal_ordet = 'K' + lokal_ordet[1:] |
||
134 | elif (lokal_ordet[0:2] == 'CH' and |
||
135 | lokal_ordet[2:3] in frozenset(_mjuka_vokaler | _harde_vokaler)): |
||
136 | lokal_ordet = '#' + lokal_ordet[2:] |
||
137 | elif lokal_ordet[0:1] == 'C' and lokal_ordet[1:2] in _harde_vokaler: |
||
138 | lokal_ordet = 'K' + lokal_ordet[1:] |
||
139 | elif lokal_ordet[0:1] == 'C' and lokal_ordet[1:2] in _konsonanter: |
||
140 | lokal_ordet = 'K' + lokal_ordet[1:] |
||
141 | elif lokal_ordet[0:1] == 'X': |
||
142 | lokal_ordet = 'S' + lokal_ordet[1:] |
||
143 | elif lokal_ordet[0:1] == 'C' and lokal_ordet[1:2] in _mjuka_vokaler: |
||
144 | lokal_ordet = 'S' + lokal_ordet[1:] |
||
145 | elif lokal_ordet[0:3] in ('SKJ', 'STJ', 'SCH'): |
||
146 | lokal_ordet = '#' + lokal_ordet[3:] |
||
147 | elif lokal_ordet[0:2] in ('SH', 'KJ', 'TJ', 'SJ'): |
||
148 | lokal_ordet = '#' + lokal_ordet[2:] |
||
149 | elif lokal_ordet[0:2] == 'SK' and lokal_ordet[2:3] in _mjuka_vokaler: |
||
150 | lokal_ordet = '#' + lokal_ordet[2:] |
||
151 | elif lokal_ordet[0:1] == 'K' and lokal_ordet[1:2] in _mjuka_vokaler: |
||
152 | lokal_ordet = '#' + lokal_ordet[1:] |
||
153 | return lokal_ordet |
||
154 | |||
155 | # Steg 1, Versaler |
||
156 | word = unicode_normalize('NFC', text_type(word.upper())) |
||
157 | word = word.replace('ß', 'SS') |
||
158 | word = word.replace('-', ' ') |
||
159 | |||
160 | # Steg 2, Ta bort adelsprefix |
||
161 | for adelstitel in adelstitler: |
||
162 | while adelstitel in word: |
||
163 | word = word.replace(adelstitel, ' ') |
||
164 | if word.startswith(adelstitel[1:]): |
||
165 | word = word[len(adelstitel)-1:] |
||
166 | |||
167 | # Split word into tokens |
||
168 | ordlista = word.split() |
||
169 | |||
170 | # Steg 3, Ta bort dubbelteckning i början på namnet |
||
171 | ordlista = [_delete_consecutive_repeats(ordet) for ordet in ordlista] |
||
172 | if not ordlista: |
||
173 | # noinspection PyRedundantParentheses |
||
174 | return ('',) |
||
175 | |||
176 | # Steg 4, Försvenskning |
||
177 | ordlista = [_foersvensker(ordet) for ordet in ordlista] |
||
178 | |||
179 | # Steg 5, Ta bort alla tecken som inte är A-Ö (65-90,196,197,214) |
||
180 | ordlista = [''.join(c for c in ordet if c in _alfabet) |
||
181 | for ordet in ordlista] |
||
182 | |||
183 | # Steg 6, Koda första ljudet |
||
184 | ordlista = [_koda_foersta_ljudet(ordet) for ordet in ordlista] |
||
185 | |||
186 | # Steg 7, Dela upp namnet i två delar |
||
187 | rest = [ordet[1:] for ordet in ordlista] |
||
188 | |||
189 | # Steg 8, Utför fonetisk transformation i resten |
||
190 | rest = [ordet.replace('DT', 'T') for ordet in rest] |
||
191 | rest = [ordet.replace('X', 'KS') for ordet in rest] |
||
192 | |||
193 | # Steg 9, Koda resten till en sifferkod |
||
194 | for vokal in _mjuka_vokaler: |
||
195 | rest = [ordet.replace('C'+vokal, '8'+vokal) for ordet in rest] |
||
196 | rest = [ordet.translate(_sfinxbis_translation) for ordet in rest] |
||
197 | |||
198 | # Steg 10, Ta bort intilliggande dubbletter |
||
199 | rest = [_delete_consecutive_repeats(ordet) for ordet in rest] |
||
200 | |||
201 | # Steg 11, Ta bort alla "9" |
||
202 | rest = [ordet.replace('9', '') for ordet in rest] |
||
203 | |||
204 | # Steg 12, Sätt ihop delarna igen |
||
205 | ordlista = [''.join(ordet) for ordet in |
||
206 | zip((_[0:1] for _ in ordlista), rest)] |
||
207 | |||
208 | # truncate, if max_length is set |
||
209 | if max_length > 0: |
||
210 | ordlista = [ordet[:max_length] for ordet in ordlista] |
||
211 | |||
212 | return tuple(ordlista) |
||
213 | |||
307 |