Conditions | 23 |
Total Lines | 124 |
Code Lines | 58 |
Lines | 45 |
Ratio | 36.29 % |
Tests | 57 |
CRAP Score | 23 |
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._koelner.Koelner.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 -*- |
||
57 | 1 | def encode(self, word): |
|
58 | """Return the Kölner Phonetik (numeric output) code for a word. |
||
59 | |||
60 | While the output code is numeric, it is still a str because 0s can lead |
||
61 | the code. |
||
62 | |||
63 | Args: |
||
64 | word (str): The word to transform |
||
65 | |||
66 | Returns: |
||
67 | str: The Kölner Phonetik value as a numeric string |
||
68 | |||
69 | Example: |
||
70 | >>> pe = Koelner() |
||
71 | >>> pe.encode('Christopher') |
||
72 | '478237' |
||
73 | >>> pe.encode('Niall') |
||
74 | '65' |
||
75 | >>> pe.encode('Smith') |
||
76 | '862' |
||
77 | >>> pe.encode('Schmidt') |
||
78 | '862' |
||
79 | >>> pe.encode('Müller') |
||
80 | '657' |
||
81 | >>> pe.encode('Zimmermann') |
||
82 | '86766' |
||
83 | |||
84 | """ |
||
85 | |||
86 | 1 | def _after(word, pos, letters): |
|
87 | """Return True if word[pos] follows one of the supplied letters. |
||
88 | |||
89 | Args: |
||
90 | word (str): The word to check |
||
91 | pos (int): Position within word to check |
||
92 | letters (str): Letters to confirm precede word[pos] |
||
93 | |||
94 | Returns: |
||
95 | bool: True if word[pos] follows a value in letters |
||
96 | |||
97 | """ |
||
98 | 1 | return pos > 0 and word[pos - 1] in letters |
|
99 | |||
100 | 1 | def _before(word, pos, letters): |
|
101 | """Return True if word[pos] precedes one of the supplied letters. |
||
102 | |||
103 | Args: |
||
104 | word (str): The word to check |
||
105 | pos (int): Position within word to check |
||
106 | letters (str): Letters to confirm follow word[pos] |
||
107 | |||
108 | Returns: |
||
109 | bool: True if word[pos] precedes a value in letters |
||
110 | |||
111 | """ |
||
112 | 1 | return pos + 1 < len(word) and word[pos + 1] in letters |
|
113 | |||
114 | 1 | sdx = '' |
|
115 | |||
116 | 1 | word = unicode_normalize('NFKD', text_type(word.upper())) |
|
117 | 1 | word = word.replace('ß', 'SS') |
|
118 | |||
119 | 1 | word = word.replace('Ä', 'AE') |
|
120 | 1 | word = word.replace('Ö', 'OE') |
|
121 | 1 | word = word.replace('Ü', 'UE') |
|
122 | 1 | word = ''.join(c for c in word if c in self._uc_set) |
|
123 | |||
124 | # Nothing to convert, return base case |
||
125 | 1 | if not word: |
|
126 | 1 | return sdx |
|
127 | |||
128 | 1 | for i in range(len(word)): |
|
129 | 1 | View Code Duplication | if word[i] in self._uc_v_set: |
130 | 1 | sdx += '0' |
|
131 | 1 | elif word[i] == 'B': |
|
132 | 1 | sdx += '1' |
|
133 | 1 | elif word[i] == 'P': |
|
134 | 1 | if _before(word, i, {'H'}): |
|
135 | 1 | sdx += '3' |
|
136 | else: |
||
137 | 1 | sdx += '1' |
|
138 | 1 | elif word[i] in {'D', 'T'}: |
|
139 | 1 | if _before(word, i, {'C', 'S', 'Z'}): |
|
140 | 1 | sdx += '8' |
|
141 | else: |
||
142 | 1 | sdx += '2' |
|
143 | 1 | elif word[i] in {'F', 'V', 'W'}: |
|
144 | 1 | sdx += '3' |
|
145 | 1 | elif word[i] in {'G', 'K', 'Q'}: |
|
146 | 1 | sdx += '4' |
|
147 | 1 | elif word[i] == 'C': |
|
148 | 1 | if _after(word, i, {'S', 'Z'}): |
|
149 | 1 | sdx += '8' |
|
150 | 1 | elif i == 0: |
|
151 | 1 | if _before( |
|
152 | word, i, {'A', 'H', 'K', 'L', 'O', 'Q', 'R', 'U', 'X'} |
||
153 | ): |
||
154 | 1 | sdx += '4' |
|
155 | else: |
||
156 | 1 | sdx += '8' |
|
157 | 1 | elif _before(word, i, {'A', 'H', 'K', 'O', 'Q', 'U', 'X'}): |
|
158 | 1 | sdx += '4' |
|
159 | else: |
||
160 | 1 | sdx += '8' |
|
161 | 1 | elif word[i] == 'X': |
|
162 | 1 | if _after(word, i, {'C', 'K', 'Q'}): |
|
163 | 1 | sdx += '8' |
|
164 | else: |
||
165 | 1 | sdx += '48' |
|
166 | 1 | elif word[i] == 'L': |
|
167 | 1 | sdx += '5' |
|
168 | 1 | elif word[i] in {'M', 'N'}: |
|
169 | 1 | sdx += '6' |
|
170 | 1 | elif word[i] == 'R': |
|
171 | 1 | sdx += '7' |
|
172 | 1 | elif word[i] in {'S', 'Z'}: |
|
173 | 1 | sdx += '8' |
|
174 | |||
175 | 1 | sdx = self._delete_consecutive_repeats(sdx) |
|
176 | |||
177 | 1 | if sdx: |
|
178 | 1 | sdx = sdx[:1] + sdx[1:].replace('0', '') |
|
179 | |||
180 | 1 | return sdx |
|
181 | |||
309 |