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