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