| Conditions | 23 |
| Total Lines | 109 |
| Code Lines | 65 |
| Lines | 36 |
| Ratio | 33.03 % |
| Tests | 38 |
| CRAP Score | 23 |
| 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_danish.SnowballDanish.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 | # Copyright 2014-2020 by Christopher C. Little. |
||
| 62 | def stem(self, word: str) -> str: |
||
| 63 | """Return Snowball Danish stem. |
||
| 64 | |||
| 65 | Parameters |
||
| 66 | ---------- |
||
| 67 | word : str |
||
| 68 | The word to stem |
||
| 69 | |||
| 70 | Returns |
||
| 71 | ------- |
||
| 72 | str |
||
| 73 | Word stem |
||
| 74 | |||
| 75 | Examples |
||
| 76 | 1 | -------- |
|
| 77 | >>> stmr = SnowballDanish() |
||
| 78 | >>> stmr.stem('underviser') |
||
| 79 | 'undervis' |
||
| 80 | >>> stmr.stem('suspension') |
||
| 81 | 'suspension' |
||
| 82 | >>> stmr.stem('sikkerhed') |
||
| 83 | 'sikker' |
||
| 84 | |||
| 85 | |||
| 86 | .. versionadded:: 0.1.0 |
||
| 87 | .. versionchanged:: 0.3.6 |
||
| 88 | Encapsulated in class |
||
| 89 | |||
| 90 | """ |
||
| 91 | # lowercase, normalize, and compose |
||
| 92 | word = normalize('NFC', word.lower()) |
||
| 93 | |||
| 94 | r1_start = min(max(3, self._sb_r1(word)), len(word)) |
||
| 95 | |||
| 96 | # Step 1 |
||
| 97 | _r1 = word[r1_start:] |
||
| 98 | View Code Duplication | if _r1[-7:] == 'erendes': |
|
|
|
|||
| 99 | word = word[:-7] |
||
| 100 | elif _r1[-6:] in {'erende', 'hedens'}: |
||
| 101 | word = word[:-6] |
||
| 102 | elif _r1[-5:] in { |
||
| 103 | 'ethed', |
||
| 104 | 'erede', |
||
| 105 | 'heden', |
||
| 106 | 1 | 'heder', |
|
| 107 | 'endes', |
||
| 108 | 1 | 'ernes', |
|
| 109 | 'erens', |
||
| 110 | 'erets', |
||
| 111 | 1 | }: |
|
| 112 | 1 | word = word[:-5] |
|
| 113 | 1 | elif _r1[-4:] in { |
|
| 114 | 1 | 'ered', |
|
| 115 | 1 | 'ende', |
|
| 116 | 1 | 'erne', |
|
| 117 | 'eren', |
||
| 118 | 'erer', |
||
| 119 | 'heds', |
||
| 120 | 'enes', |
||
| 121 | 'eres', |
||
| 122 | 'eret', |
||
| 123 | }: |
||
| 124 | word = word[:-4] |
||
| 125 | elif _r1[-3:] in {'hed', 'ene', 'ere', 'ens', 'ers', 'ets'}: |
||
| 126 | 1 | word = word[:-3] |
|
| 127 | 1 | elif _r1[-2:] in {'en', 'er', 'es', 'et'}: |
|
| 128 | word = word[:-2] |
||
| 129 | elif _r1[-1:] == 'e': |
||
| 130 | word = word[:-1] |
||
| 131 | elif _r1[-1:] == 's': |
||
| 132 | if len(word) > 1 and word[-2] in self._s_endings: |
||
| 133 | word = word[:-1] |
||
| 134 | |||
| 135 | # Step 2 |
||
| 136 | if word[r1_start:][-2:] in {'gd', 'dt', 'gt', 'kt'}: |
||
| 137 | word = word[:-1] |
||
| 138 | 1 | ||
| 139 | 1 | # Step 3 |
|
| 140 | 1 | if word[-4:] == 'igst': |
|
| 141 | 1 | word = word[:-2] |
|
| 142 | 1 | ||
| 143 | 1 | _r1 = word[r1_start:] |
|
| 144 | 1 | repeat_step2 = False |
|
| 145 | 1 | if _r1[-4:] == 'elig': |
|
| 146 | 1 | word = word[:-4] |
|
| 147 | 1 | repeat_step2 = True |
|
| 148 | elif _r1[-4:] == 'løst': |
||
| 149 | word = word[:-1] |
||
| 150 | 1 | elif _r1[-3:] in {'lig', 'els'}: |
|
| 151 | 1 | word = word[:-3] |
|
| 152 | repeat_step2 = True |
||
| 153 | elif _r1[-2:] == 'ig': |
||
| 154 | 1 | word = word[:-2] |
|
| 155 | 1 | repeat_step2 = True |
|
| 156 | |||
| 157 | 1 | if repeat_step2: |
|
| 158 | 1 | if word[r1_start:][-2:] in {'gd', 'dt', 'gt', 'kt'}: |
|
| 159 | 1 | word = word[:-1] |
|
| 160 | 1 | ||
| 161 | 1 | # Step 4 |
|
| 162 | 1 | if ( |
|
| 163 | 1 | len(word[r1_start:]) >= 1 |
|
| 164 | 1 | and len(word) >= 2 |
|
| 165 | 1 | and word[-1] == word[-2] |
|
| 166 | 1 | and word[-1] not in self._vowels |
|
| 167 | 1 | ): |
|
| 168 | 1 | word = word[:-1] |
|
| 169 | 1 | ||
| 170 | return word |
||
| 171 | 1 | ||
| 177 |