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