Conditions | 63 |
Total Lines | 140 |
Code Lines | 89 |
Lines | 0 |
Ratio | 0 % |
Tests | 67 |
CRAP Score | 63 |
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.stemmer._snowball_dutch.SnowballDutch.stem() 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 -*- |
||
74 | 1 | def stem(self, word): |
|
75 | """Return Snowball Dutch stem. |
||
76 | |||
77 | Parameters |
||
78 | ---------- |
||
79 | word : str |
||
80 | The word to stem |
||
81 | |||
82 | Returns |
||
83 | ------- |
||
84 | str |
||
85 | Word stem |
||
86 | |||
87 | Examples |
||
88 | -------- |
||
89 | >>> stmr = SnowballDutch() |
||
90 | >>> stmr.stem('lezen') |
||
91 | 'lez' |
||
92 | >>> stmr.stem('opschorting') |
||
93 | 'opschort' |
||
94 | >>> stmr.stem('ongrijpbaarheid') |
||
95 | 'ongrijp' |
||
96 | |||
97 | """ |
||
98 | # lowercase, normalize, decompose, filter umlauts & acutes out, and |
||
99 | # compose |
||
100 | 1 | word = normalize('NFC', text_type(word.lower())) |
|
101 | 1 | word = word.translate(self._accented) |
|
102 | |||
103 | 1 | for i in range(len(word)): |
|
104 | 1 | if i == 0 and word[0] == 'y': |
|
105 | 1 | word = 'Y' + word[1:] |
|
106 | 1 | elif word[i] == 'y' and word[i - 1] in self._vowels: |
|
107 | 1 | word = word[:i] + 'Y' + word[i + 1 :] |
|
108 | 1 | elif ( |
|
109 | word[i] == 'i' |
||
110 | and word[i - 1] in self._vowels |
||
111 | and i + 1 < len(word) |
||
112 | and word[i + 1] in self._vowels |
||
113 | ): |
||
114 | 1 | word = word[:i] + 'I' + word[i + 1 :] |
|
115 | |||
116 | 1 | r1_start = max(3, self._sb_r1(word)) |
|
117 | 1 | r2_start = self._sb_r2(word) |
|
118 | |||
119 | # Step 1 |
||
120 | 1 | if word[-5:] == 'heden': |
|
121 | 1 | if len(word[r1_start:]) >= 5: |
|
122 | 1 | word = word[:-3] + 'id' |
|
123 | 1 | elif word[-3:] == 'ene': |
|
124 | 1 | if len(word[r1_start:]) >= 3 and ( |
|
125 | word[-4] not in self._vowels and word[-6:-3] != 'gem' |
||
126 | ): |
||
127 | 1 | word = self._undouble(word[:-3]) |
|
128 | 1 | elif word[-2:] == 'en': |
|
129 | 1 | if len(word[r1_start:]) >= 2 and ( |
|
130 | word[-3] not in self._vowels and word[-5:-2] != 'gem' |
||
131 | ): |
||
132 | 1 | word = self._undouble(word[:-2]) |
|
133 | 1 | elif word[-2:] == 'se': |
|
134 | 1 | if ( |
|
135 | len(word[r1_start:]) >= 2 |
||
136 | and word[-3] not in self._not_s_endings |
||
137 | ): |
||
138 | 1 | word = word[:-2] |
|
139 | 1 | elif word[-1:] == 's': |
|
140 | 1 | if ( |
|
141 | len(word[r1_start:]) >= 1 |
||
142 | and word[-2] not in self._not_s_endings |
||
143 | ): |
||
144 | 1 | word = word[:-1] |
|
145 | |||
146 | # Step 2 |
||
147 | 1 | e_removed = False |
|
148 | 1 | if word[-1:] == 'e': |
|
149 | 1 | if len(word[r1_start:]) >= 1 and word[-2] not in self._vowels: |
|
150 | 1 | word = self._undouble(word[:-1]) |
|
151 | 1 | e_removed = True |
|
152 | |||
153 | # Step 3a |
||
154 | 1 | if word[-4:] == 'heid': |
|
155 | 1 | if len(word[r2_start:]) >= 4 and word[-5] != 'c': |
|
156 | 1 | word = word[:-4] |
|
157 | 1 | if word[-2:] == 'en': |
|
158 | 1 | if len(word[r1_start:]) >= 2 and ( |
|
159 | word[-3] not in self._vowels and word[-5:-2] != 'gem' |
||
160 | ): |
||
161 | 1 | word = self._undouble(word[:-2]) |
|
162 | |||
163 | # Step 3b |
||
164 | 1 | if word[-4:] == 'lijk': |
|
165 | 1 | if len(word[r2_start:]) >= 4: |
|
166 | 1 | word = word[:-4] |
|
167 | # Repeat step 2 |
||
168 | 1 | if word[-1:] == 'e': |
|
169 | 1 | if ( |
|
170 | len(word[r1_start:]) >= 1 |
||
171 | and word[-2] not in self._vowels |
||
172 | ): |
||
173 | 1 | word = self._undouble(word[:-1]) |
|
174 | 1 | elif word[-4:] == 'baar': |
|
175 | 1 | if len(word[r2_start:]) >= 4: |
|
176 | 1 | word = word[:-4] |
|
177 | 1 | elif word[-3:] in ('end', 'ing'): |
|
178 | 1 | if len(word[r2_start:]) >= 3: |
|
179 | 1 | word = word[:-3] |
|
180 | 1 | if ( |
|
181 | word[-2:] == 'ig' |
||
182 | and len(word[r2_start:]) >= 2 |
||
183 | and word[-3] != 'e' |
||
184 | ): |
||
185 | 1 | word = word[:-2] |
|
186 | else: |
||
187 | 1 | word = self._undouble(word) |
|
188 | 1 | elif word[-3:] == 'bar': |
|
189 | 1 | if len(word[r2_start:]) >= 3 and e_removed: |
|
190 | 1 | word = word[:-3] |
|
191 | 1 | elif word[-2:] == 'ig': |
|
192 | 1 | if len(word[r2_start:]) >= 2 and word[-3] != 'e': |
|
193 | 1 | word = word[:-2] |
|
194 | |||
195 | # Step 4 |
||
196 | 1 | if ( |
|
197 | len(word) >= 4 |
||
198 | and word[-3] == word[-2] |
||
199 | and word[-2] in {'a', 'e', 'o', 'u'} |
||
200 | and word[-4] not in self._vowels |
||
201 | and word[-1] not in self._vowels |
||
202 | and word[-1] != 'I' |
||
203 | ): |
||
204 | 1 | word = word[:-2] + word[-1] |
|
205 | |||
206 | # Change 'Y' and 'U' back to lowercase if survived stemming |
||
207 | 1 | for i in range(0, len(word)): |
|
208 | 1 | if word[i] == 'Y': |
|
209 | 1 | word = word[:i] + 'y' + word[i + 1 :] |
|
210 | 1 | elif word[i] == 'I': |
|
211 | 1 | word = word[:i] + 'i' + word[i + 1 :] |
|
212 | |||
213 | 1 | return word |
|
214 | |||
248 |