Conditions | 36 |
Total Lines | 141 |
Code Lines | 85 |
Lines | 0 |
Ratio | 0 % |
Tests | 80 |
CRAP Score | 36 |
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._pshp_soundex_last.PSHPSoundexLast.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 -*- |
||
58 | 1 | def encode(self, lname, max_length=4, german=False): |
|
59 | """Calculate the PSHP Soundex/Viewex Coding of a last name. |
||
60 | |||
61 | Args: |
||
62 | lname (str): The last name to encode |
||
63 | max_length (int): The length of the code returned (defaults to 4) |
||
64 | german (bool): Set to True if the name is German (different rules |
||
65 | apply) |
||
66 | |||
67 | Returns: |
||
68 | str: The PSHP Soundex/Viewex Coding |
||
69 | |||
70 | Examples: |
||
71 | >>> pe = PSHPSoundexLast() |
||
72 | >>> pe.encode('Smith') |
||
73 | 'S530' |
||
74 | >>> pe.encode('Waters') |
||
75 | 'W350' |
||
76 | >>> pe.encode('James') |
||
77 | 'J500' |
||
78 | >>> pe.encode('Schmidt') |
||
79 | 'S530' |
||
80 | >>> pe.encode('Ashcroft') |
||
81 | 'A225' |
||
82 | |||
83 | """ |
||
84 | 1 | lname = unicode_normalize('NFKD', text_type(lname.upper())) |
|
85 | 1 | lname = lname.replace('ß', 'SS') |
|
86 | 1 | lname = ''.join(c for c in lname if c in self._uc_set) |
|
87 | |||
88 | # A. Prefix treatment |
||
89 | 1 | if lname[:3] == 'VON' or lname[:3] == 'VAN': |
|
90 | 1 | lname = lname[3:].strip() |
|
91 | |||
92 | # The rule implemented below says "MC, MAC become 1". I believe it |
||
93 | # meant to say they become M except in German data (where superscripted |
||
94 | # 1 indicates "except in German data"). It doesn't make sense for them |
||
95 | # to become 1 (BPFV -> 1) or to apply outside German. Unfortunately, |
||
96 | # both articles have this error(?). |
||
97 | 1 | if not german: |
|
98 | 1 | if lname[:3] == 'MAC': |
|
99 | 1 | lname = 'M' + lname[3:] |
|
100 | 1 | elif lname[:2] == 'MC': |
|
101 | 1 | lname = 'M' + lname[2:] |
|
102 | |||
103 | # The non-German-only rule to strip ' is unnecessary due to filtering |
||
104 | |||
105 | 1 | if lname[:1] in {'E', 'I', 'O', 'U'}: |
|
106 | 1 | lname = 'A' + lname[1:] |
|
107 | 1 | elif lname[:2] in {'GE', 'GI', 'GY'}: |
|
108 | 1 | lname = 'J' + lname[1:] |
|
109 | 1 | elif lname[:2] in {'CE', 'CI', 'CY'}: |
|
110 | 1 | lname = 'S' + lname[1:] |
|
111 | 1 | elif lname[:3] == 'CHR': |
|
112 | 1 | lname = 'K' + lname[1:] |
|
113 | 1 | elif lname[:1] == 'C' and lname[:2] != 'CH': |
|
114 | 1 | lname = 'K' + lname[1:] |
|
115 | |||
116 | 1 | if lname[:2] == 'KN': |
|
117 | 1 | lname = 'N' + lname[1:] |
|
118 | 1 | elif lname[:2] == 'PH': |
|
119 | 1 | lname = 'F' + lname[1:] |
|
120 | 1 | elif lname[:3] in {'WIE', 'WEI'}: |
|
121 | 1 | lname = 'V' + lname[1:] |
|
122 | |||
123 | 1 | if german and lname[:1] in {'W', 'M', 'Y', 'Z'}: |
|
124 | 1 | lname = {'W': 'V', 'M': 'N', 'Y': 'J', 'Z': 'S'}[lname[0]] + lname[ |
|
125 | 1: |
||
126 | ] |
||
127 | |||
128 | 1 | code = lname[:1] |
|
129 | |||
130 | # B. Postfix treatment |
||
131 | 1 | if german: # moved from end of postfix treatment due to blocking |
|
132 | 1 | if lname[-3:] == 'TES': |
|
133 | 1 | lname = lname[:-3] |
|
134 | 1 | elif lname[-2:] == 'TS': |
|
135 | 1 | lname = lname[:-2] |
|
136 | 1 | if lname[-3:] == 'TZE': |
|
137 | 1 | lname = lname[:-3] |
|
138 | 1 | elif lname[-2:] == 'ZE': |
|
139 | 1 | lname = lname[:-2] |
|
140 | 1 | if lname[-1:] == 'Z': |
|
141 | 1 | lname = lname[:-1] |
|
142 | 1 | elif lname[-2:] == 'TE': |
|
143 | 1 | lname = lname[:-2] |
|
144 | |||
145 | 1 | if lname[-1:] == 'R': |
|
146 | 1 | lname = lname[:-1] + 'N' |
|
147 | 1 | elif lname[-2:] in {'SE', 'CE'}: |
|
148 | 1 | lname = lname[:-2] |
|
149 | 1 | if lname[-2:] == 'SS': |
|
150 | 1 | lname = lname[:-2] |
|
151 | 1 | elif lname[-1:] == 'S': |
|
152 | 1 | lname = lname[:-1] |
|
153 | |||
154 | 1 | if not german: |
|
155 | 1 | l5_repl = {'STOWN': 'SAWON', 'MPSON': 'MASON'} |
|
156 | 1 | l4_repl = { |
|
157 | 'NSEN': 'ASEN', |
||
158 | 'MSON': 'ASON', |
||
159 | 'STEN': 'SAEN', |
||
160 | 'STON': 'SAON', |
||
161 | } |
||
162 | 1 | if lname[-5:] in l5_repl: |
|
163 | 1 | lname = lname[:-5] + l5_repl[lname[-5:]] |
|
164 | 1 | elif lname[-4:] in l4_repl: |
|
165 | 1 | lname = lname[:-4] + l4_repl[lname[-4:]] |
|
166 | |||
167 | 1 | if lname[-2:] in {'NG', 'ND'}: |
|
168 | 1 | lname = lname[:-1] |
|
169 | 1 | if not german and lname[-3:] in {'GAN', 'GEN'}: |
|
170 | 1 | lname = lname[:-3] + 'A' + lname[-2:] |
|
171 | |||
172 | # C. Infix Treatment |
||
173 | 1 | lname = lname.replace('CK', 'C') |
|
174 | 1 | lname = lname.replace('SCH', 'S') |
|
175 | 1 | lname = lname.replace('DT', 'T') |
|
176 | 1 | lname = lname.replace('ND', 'N') |
|
177 | 1 | lname = lname.replace('NG', 'N') |
|
178 | 1 | lname = lname.replace('LM', 'M') |
|
179 | 1 | lname = lname.replace('MN', 'M') |
|
180 | 1 | lname = lname.replace('WIE', 'VIE') |
|
181 | 1 | lname = lname.replace('WEI', 'VEI') |
|
182 | |||
183 | # D. Soundexing |
||
184 | # code for X & Y are unspecified, but presumably are 2 & 0 |
||
185 | |||
186 | 1 | lname = lname.translate(self._trans) |
|
187 | 1 | lname = self._delete_consecutive_repeats(lname) |
|
188 | |||
189 | 1 | code += lname[1:] |
|
190 | 1 | code = code.replace('0', '') # rule 1 |
|
191 | |||
192 | 1 | if max_length != -1: |
|
193 | 1 | if len(code) < max_length: |
|
194 | 1 | code += '0' * (max_length - len(code)) |
|
195 | else: |
||
196 | 1 | code = code[:max_length] |
|
197 | |||
198 | 1 | return code |
|
199 | |||
235 |