Conditions | 19 |
Total Lines | 83 |
Code Lines | 45 |
Lines | 0 |
Ratio | 0 % |
Tests | 44 |
CRAP Score | 19 |
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._norphone.Norphone.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 -*- |
||
73 | 1 | def encode(self, word): |
|
74 | """Return the Norphone code. |
||
75 | |||
76 | Parameters |
||
77 | ---------- |
||
78 | word : str |
||
79 | The word to transform |
||
80 | |||
81 | Returns |
||
82 | ------- |
||
83 | str |
||
84 | The Norphone code |
||
85 | |||
86 | Examples |
||
87 | -------- |
||
88 | >>> pe = Norphone() |
||
89 | >>> pe.encode('Hansen') |
||
90 | 'HNSN' |
||
91 | >>> pe.encode('Larsen') |
||
92 | 'LRSN' |
||
93 | >>> pe.encode('Aagaard') |
||
94 | 'ÅKRT' |
||
95 | >>> pe.encode('Braaten') |
||
96 | 'BRTN' |
||
97 | >>> pe.encode('Sandvik') |
||
98 | 'SNVK' |
||
99 | |||
100 | """ |
||
101 | 1 | word = word.upper() |
|
102 | |||
103 | 1 | code = '' |
|
104 | 1 | skip = 0 |
|
105 | |||
106 | 1 | if word[0:2] == 'AA': |
|
107 | 1 | code = 'Å' |
|
108 | 1 | skip = 2 |
|
109 | 1 | elif word[0:2] == 'GI': |
|
110 | 1 | code = 'J' |
|
111 | 1 | skip = 2 |
|
112 | 1 | elif word[0:3] == 'SKY': |
|
113 | 1 | code = 'X' |
|
114 | 1 | skip = 3 |
|
115 | 1 | elif word[0:2] == 'EI': |
|
116 | 1 | code = 'Æ' |
|
117 | 1 | skip = 2 |
|
118 | 1 | elif word[0:2] == 'KY': |
|
119 | 1 | code = 'X' |
|
120 | 1 | skip = 2 |
|
121 | 1 | elif word[:1] == 'C': |
|
122 | 1 | code = 'K' |
|
123 | 1 | skip = 1 |
|
124 | 1 | elif word[:1] == 'Ä': |
|
125 | 1 | code = 'Æ' |
|
126 | 1 | skip = 1 |
|
127 | 1 | elif word[:1] == 'Ö': |
|
128 | 1 | code = 'Ø' |
|
129 | 1 | skip = 1 |
|
130 | |||
131 | 1 | if word[-2:] == 'DT': |
|
132 | 1 | word = word[:-2] + 'T' |
|
133 | # Though the rules indicate this rule applies in all positions, the |
||
134 | # reference implementation indicates it applies only in final position. |
||
135 | 1 | elif word[-2:-1] in self._uc_v_set and word[-1:] == 'D': |
|
136 | 1 | word = word[:-2] |
|
137 | |||
138 | 1 | for pos, char in enumerate(word): |
|
139 | 1 | if skip: |
|
140 | 1 | skip -= 1 |
|
141 | else: |
||
142 | 1 | for length in sorted(self._replacements, reverse=True): |
|
143 | 1 | if word[pos : pos + length] in self._replacements[length]: |
|
144 | 1 | code += self._replacements[length][ |
|
145 | word[pos : pos + length] |
||
146 | ] |
||
147 | 1 | skip = length - 1 |
|
148 | 1 | break |
|
149 | else: |
||
150 | 1 | if not pos or char not in self._uc_v_set: |
|
151 | 1 | code += char |
|
152 | |||
153 | 1 | code = self._delete_consecutive_repeats(code) |
|
154 | |||
155 | 1 | return code |
|
156 | |||
194 |