Conditions | 15 |
Total Lines | 53 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 15 |
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._clef_german_plus.CLEFGermanPlus.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 -*- |
||
52 | 1 | def stem(self, word): |
|
53 | """Return 'CLEF German stemmer plus' stem. |
||
54 | |||
55 | Parameters |
||
56 | ---------- |
||
57 | word : str |
||
58 | The word to stem |
||
59 | |||
60 | Returns |
||
61 | ------- |
||
62 | str |
||
63 | Word stem |
||
64 | |||
65 | Examples |
||
66 | -------- |
||
67 | >>> stmr = CLEFGermanPlus() |
||
68 | >>> clef_german_plus('lesen') |
||
69 | 'les' |
||
70 | >>> clef_german_plus('graues') |
||
71 | 'grau' |
||
72 | >>> clef_german_plus('buchstabieren') |
||
73 | 'buchstabi' |
||
74 | |||
75 | """ |
||
76 | # lowercase, normalize, and compose |
||
77 | 1 | word = normalize('NFC', text_type(word.lower())) |
|
78 | |||
79 | # remove umlauts |
||
80 | 1 | word = word.translate(self._accents) |
|
81 | |||
82 | # Step 1 |
||
83 | 1 | wlen = len(word) - 1 |
|
84 | 1 | if wlen > 4 and word[-3:] == 'ern': |
|
85 | 1 | word = word[:-3] |
|
86 | 1 | elif wlen > 3 and word[-2:] in {'em', 'en', 'er', 'es'}: |
|
87 | 1 | word = word[:-2] |
|
88 | 1 | elif wlen > 2 and ( |
|
89 | word[-1] == 'e' |
||
90 | or (word[-1] == 's' and word[-2] in self._st_ending) |
||
91 | ): |
||
92 | 1 | word = word[:-1] |
|
93 | |||
94 | # Step 2 |
||
95 | 1 | wlen = len(word) - 1 |
|
96 | 1 | if wlen > 4 and word[-3:] == 'est': |
|
97 | 1 | word = word[:-3] |
|
98 | 1 | elif wlen > 3 and ( |
|
99 | word[-2:] in {'er', 'en'} |
||
100 | or (word[-2:] == 'st' and word[-3] in self._st_ending) |
||
101 | ): |
||
102 | 1 | word = word[:-2] |
|
103 | |||
104 | 1 | return word |
|
105 | |||
140 |