Conditions | 36 |
Total Lines | 156 |
Code Lines | 95 |
Lines | 0 |
Ratio | 0 % |
Tests | 78 |
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._caverphone.Caverphone.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 -*- |
||
46 | 1 | def encode(self, word, version=2): |
|
47 | """Return the Caverphone code for a word. |
||
48 | |||
49 | Parameters |
||
50 | ---------- |
||
51 | word : str |
||
52 | The word to transform |
||
53 | version : int |
||
54 | The version of Caverphone to employ for encoding (defaults to 2) |
||
55 | |||
56 | Returns |
||
57 | ------- |
||
58 | str |
||
59 | The Caverphone value |
||
60 | |||
61 | Examples |
||
62 | -------- |
||
63 | >>> pe = Caverphone() |
||
64 | >>> pe.encode('Christopher') |
||
65 | 'KRSTFA1111' |
||
66 | >>> pe.encode('Niall') |
||
67 | 'NA11111111' |
||
68 | >>> pe.encode('Smith') |
||
69 | 'SMT1111111' |
||
70 | >>> pe.encode('Schmidt') |
||
71 | 'SKMT111111' |
||
72 | |||
73 | >>> pe.encode('Christopher', 1) |
||
74 | 'KRSTF1' |
||
75 | >>> pe.encode('Niall', 1) |
||
76 | 'N11111' |
||
77 | >>> pe.encode('Smith', 1) |
||
78 | 'SMT111' |
||
79 | >>> pe.encode('Schmidt', 1) |
||
80 | 'SKMT11' |
||
81 | |||
82 | """ |
||
83 | 1 | word = word.lower() |
|
84 | 1 | word = ''.join(c for c in word if c in self._lc_set) |
|
85 | |||
86 | 1 | def _squeeze_replace(word, char): |
|
87 | """Convert strings of char in word to one instance. |
||
88 | |||
89 | Parameters |
||
90 | ---------- |
||
91 | word : str |
||
92 | The partially converted word |
||
93 | char : str |
||
94 | A character to 'squeeze' |
||
95 | |||
96 | Returns |
||
97 | ------- |
||
98 | str |
||
99 | The word with instances of char squeezed down to one |
||
100 | |||
101 | """ |
||
102 | 1 | while char * 2 in word: |
|
103 | 1 | word = word.replace(char * 2, char) |
|
104 | 1 | return word.replace(char, char.upper()) |
|
105 | |||
106 | # the main replacement algorithm |
||
107 | 1 | if version != 1 and word[-1:] == 'e': |
|
108 | 1 | word = word[:-1] |
|
109 | 1 | if word: |
|
110 | 1 | if word[:5] == 'cough': |
|
111 | 1 | word = 'cou2f' + word[5:] |
|
112 | 1 | if word[:5] == 'rough': |
|
113 | 1 | word = 'rou2f' + word[5:] |
|
114 | 1 | if word[:5] == 'tough': |
|
115 | 1 | word = 'tou2f' + word[5:] |
|
116 | 1 | if word[:6] == 'enough': |
|
117 | 1 | word = 'enou2f' + word[6:] |
|
118 | 1 | if version != 1 and word[:6] == 'trough': |
|
119 | 1 | word = 'trou2f' + word[6:] |
|
120 | 1 | if word[:2] == 'gn': |
|
121 | 1 | word = '2n' + word[2:] |
|
122 | 1 | if word[-2:] == 'mb': |
|
123 | 1 | word = word[:-1] + '2' |
|
124 | 1 | for src, tar in ( |
|
125 | ('cq', '2q'), |
||
126 | ('ci', 'si'), |
||
127 | ('ce', 'se'), |
||
128 | ('cy', 'sy'), |
||
129 | ('tch', '2ch'), |
||
130 | ('c', 'k'), |
||
131 | ('q', 'k'), |
||
132 | ('x', 'k'), |
||
133 | ('v', 'f'), |
||
134 | ('dg', '2g'), |
||
135 | ('tio', 'sio'), |
||
136 | ('tia', 'sia'), |
||
137 | ('d', 't'), |
||
138 | ('ph', 'fh'), |
||
139 | ('b', 'p'), |
||
140 | ('sh', 's2'), |
||
141 | ('z', 's'), |
||
142 | ): |
||
143 | 1 | word = word.replace(src, tar) |
|
144 | 1 | if word[0] in self._lc_v_set: |
|
145 | 1 | word = 'A' + word[1:] |
|
146 | 1 | for vowel in 'aeiou': |
|
147 | 1 | word = word.replace(vowel, '3') |
|
148 | 1 | if version != 1: |
|
149 | 1 | word = word.replace('j', 'y') |
|
150 | 1 | if word[:2] == 'y3': |
|
151 | 1 | word = 'Y3' + word[2:] |
|
152 | 1 | if word[:1] == 'y': |
|
153 | 1 | word = 'A' + word[1:] |
|
154 | 1 | word = word.replace('y', '3') |
|
155 | 1 | for src, tar in (('3gh3', '3kh3'), ('gh', '22'), ('g', 'k')): |
|
156 | 1 | word = word.replace(src, tar) |
|
157 | |||
158 | 1 | for char in 'stpkfmn': |
|
159 | 1 | word = _squeeze_replace(word, char) |
|
160 | |||
161 | 1 | word = word.replace('w3', 'W3') |
|
162 | 1 | if version == 1: |
|
163 | 1 | word = word.replace('wy', 'Wy') |
|
164 | 1 | word = word.replace('wh3', 'Wh3') |
|
165 | 1 | if version == 1: |
|
166 | 1 | word = word.replace('why', 'Why') |
|
167 | 1 | if version != 1 and word[-1:] == 'w': |
|
168 | 1 | word = word[:-1] + '3' |
|
169 | 1 | word = word.replace('w', '2') |
|
170 | 1 | if word[:1] == 'h': |
|
171 | 1 | word = 'A' + word[1:] |
|
172 | 1 | word = word.replace('h', '2') |
|
173 | 1 | word = word.replace('r3', 'R3') |
|
174 | 1 | if version == 1: |
|
175 | 1 | word = word.replace('ry', 'Ry') |
|
176 | 1 | if version != 1 and word[-1:] == 'r': |
|
177 | 1 | word = word[:-1] + '3' |
|
178 | 1 | word = word.replace('r', '2') |
|
179 | 1 | word = word.replace('l3', 'L3') |
|
180 | 1 | if version == 1: |
|
181 | 1 | word = word.replace('ly', 'Ly') |
|
182 | 1 | if version != 1 and word[-1:] == 'l': |
|
183 | 1 | word = word[:-1] + '3' |
|
184 | 1 | word = word.replace('l', '2') |
|
185 | 1 | if version == 1: |
|
186 | 1 | word = word.replace('j', 'y') |
|
187 | 1 | word = word.replace('y3', 'Y3') |
|
188 | 1 | word = word.replace('y', '2') |
|
189 | 1 | word = word.replace('2', '') |
|
190 | 1 | if version != 1 and word[-1:] == '3': |
|
191 | 1 | word = word[:-1] + 'A' |
|
192 | 1 | word = word.replace('3', '') |
|
193 | |||
194 | # pad with 1s, then extract the necessary length of code |
||
195 | 1 | word += '1' * 10 |
|
196 | 1 | if version != 1: |
|
197 | 1 | word = word[:10] |
|
198 | else: |
||
199 | 1 | word = word[:6] |
|
200 | |||
201 | 1 | return word |
|
202 | |||
249 |