Conditions | 32 |
Total Lines | 149 |
Code Lines | 106 |
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.caverphone.caverphone() 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 -*- |
||
29 | def caverphone(word, version=2): |
||
30 | """Return the Caverphone code for a word. |
||
31 | |||
32 | A description of version 1 of the algorithm can be found in |
||
33 | :cite:`Hood:2002`. |
||
34 | |||
35 | A description of version 2 of the algorithm can be found in |
||
36 | :cite:`Hood:2004`. |
||
37 | |||
38 | :param str word: the word to transform |
||
39 | :param int version: the version of Caverphone to employ for encoding |
||
40 | (defaults to 2) |
||
41 | :returns: the Caverphone value |
||
42 | :rtype: str |
||
43 | |||
44 | >>> caverphone('Christopher') |
||
45 | 'KRSTFA1111' |
||
46 | >>> caverphone('Niall') |
||
47 | 'NA11111111' |
||
48 | >>> caverphone('Smith') |
||
49 | 'SMT1111111' |
||
50 | >>> caverphone('Schmidt') |
||
51 | 'SKMT111111' |
||
52 | |||
53 | >>> caverphone('Christopher', 1) |
||
54 | 'KRSTF1' |
||
55 | >>> caverphone('Niall', 1) |
||
56 | 'N11111' |
||
57 | >>> caverphone('Smith', 1) |
||
58 | 'SMT111' |
||
59 | >>> caverphone('Schmidt', 1) |
||
60 | 'SKMT11' |
||
61 | """ |
||
62 | _vowels = {'a', 'e', 'i', 'o', 'u'} |
||
63 | |||
64 | word = word.lower() |
||
65 | word = ''.join(c for c in word if c in |
||
66 | {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', |
||
67 | 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', |
||
68 | 'y', 'z'}) |
||
69 | |||
70 | def _squeeze_replace(word, char, new_char): |
||
71 | """Convert strings of char in word to one instance of new_char.""" |
||
72 | while char * 2 in word: |
||
73 | word = word.replace(char * 2, char) |
||
74 | return word.replace(char, new_char) |
||
75 | |||
76 | # the main replacement algorithm |
||
77 | if version != 1 and word[-1:] == 'e': |
||
78 | word = word[:-1] |
||
79 | if word: |
||
80 | if word[:5] == 'cough': |
||
81 | word = 'cou2f'+word[5:] |
||
82 | if word[:5] == 'rough': |
||
83 | word = 'rou2f'+word[5:] |
||
84 | if word[:5] == 'tough': |
||
85 | word = 'tou2f'+word[5:] |
||
86 | if word[:6] == 'enough': |
||
87 | word = 'enou2f'+word[6:] |
||
88 | if version != 1 and word[:6] == 'trough': |
||
89 | word = 'trou2f'+word[6:] |
||
90 | if word[:2] == 'gn': |
||
91 | word = '2n'+word[2:] |
||
92 | if word[-2:] == 'mb': |
||
93 | word = word[:-1]+'2' |
||
94 | word = word.replace('cq', '2q') |
||
95 | word = word.replace('ci', 'si') |
||
96 | word = word.replace('ce', 'se') |
||
97 | word = word.replace('cy', 'sy') |
||
98 | word = word.replace('tch', '2ch') |
||
99 | word = word.replace('c', 'k') |
||
100 | word = word.replace('q', 'k') |
||
101 | word = word.replace('x', 'k') |
||
102 | word = word.replace('v', 'f') |
||
103 | word = word.replace('dg', '2g') |
||
104 | word = word.replace('tio', 'sio') |
||
105 | word = word.replace('tia', 'sia') |
||
106 | word = word.replace('d', 't') |
||
107 | word = word.replace('ph', 'fh') |
||
108 | word = word.replace('b', 'p') |
||
109 | word = word.replace('sh', 's2') |
||
110 | word = word.replace('z', 's') |
||
111 | if word[0] in _vowels: |
||
112 | word = 'A'+word[1:] |
||
113 | word = word.replace('a', '3') |
||
114 | word = word.replace('e', '3') |
||
115 | word = word.replace('i', '3') |
||
116 | word = word.replace('o', '3') |
||
117 | word = word.replace('u', '3') |
||
118 | if version != 1: |
||
119 | word = word.replace('j', 'y') |
||
120 | if word[:2] == 'y3': |
||
121 | word = 'Y3'+word[2:] |
||
122 | if word[:1] == 'y': |
||
123 | word = 'A'+word[1:] |
||
124 | word = word.replace('y', '3') |
||
125 | word = word.replace('3gh3', '3kh3') |
||
126 | word = word.replace('gh', '22') |
||
127 | word = word.replace('g', 'k') |
||
128 | |||
129 | word = _squeeze_replace(word, 's', 'S') |
||
130 | word = _squeeze_replace(word, 't', 'T') |
||
131 | word = _squeeze_replace(word, 'p', 'P') |
||
132 | word = _squeeze_replace(word, 'k', 'K') |
||
133 | word = _squeeze_replace(word, 'f', 'F') |
||
134 | word = _squeeze_replace(word, 'm', 'M') |
||
135 | word = _squeeze_replace(word, 'n', 'N') |
||
136 | |||
137 | word = word.replace('w3', 'W3') |
||
138 | if version == 1: |
||
139 | word = word.replace('wy', 'Wy') |
||
140 | word = word.replace('wh3', 'Wh3') |
||
141 | if version == 1: |
||
142 | word = word.replace('why', 'Why') |
||
143 | if version != 1 and word[-1:] == 'w': |
||
144 | word = word[:-1]+'3' |
||
145 | word = word.replace('w', '2') |
||
146 | if word[:1] == 'h': |
||
147 | word = 'A'+word[1:] |
||
148 | word = word.replace('h', '2') |
||
149 | word = word.replace('r3', 'R3') |
||
150 | if version == 1: |
||
151 | word = word.replace('ry', 'Ry') |
||
152 | if version != 1 and word[-1:] == 'r': |
||
153 | word = word[:-1]+'3' |
||
154 | word = word.replace('r', '2') |
||
155 | word = word.replace('l3', 'L3') |
||
156 | if version == 1: |
||
157 | word = word.replace('ly', 'Ly') |
||
158 | if version != 1 and word[-1:] == 'l': |
||
159 | word = word[:-1]+'3' |
||
160 | word = word.replace('l', '2') |
||
161 | if version == 1: |
||
162 | word = word.replace('j', 'y') |
||
163 | word = word.replace('y3', 'Y3') |
||
164 | word = word.replace('y', '2') |
||
165 | word = word.replace('2', '') |
||
166 | if version != 1 and word[-1:] == '3': |
||
167 | word = word[:-1]+'A' |
||
168 | word = word.replace('3', '') |
||
169 | |||
170 | # pad with 1s, then extract the necessary length of code |
||
171 | word += '1'*10 |
||
172 | if version != 1: |
||
173 | word = word[:10] |
||
174 | else: |
||
175 | word = word[:6] |
||
176 | |||
177 | return word |
||
178 | |||
183 |