Conditions | 21 |
Total Lines | 85 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Tests | 37 |
CRAP Score | 21 |
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_norwegian.SnowballNorwegian.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 -*- |
||
68 | 1 | def stem(self, word): |
|
69 | """Return Snowball Norwegian stem. |
||
70 | |||
71 | Parameters |
||
72 | ---------- |
||
73 | word : str |
||
74 | The word to stem |
||
75 | |||
76 | Returns |
||
77 | ------- |
||
78 | str |
||
79 | Word stem |
||
80 | |||
81 | Examples |
||
82 | -------- |
||
83 | >>> stmr = SnowballNorwegian() |
||
84 | >>> stmr.stem('lese') |
||
85 | 'les' |
||
86 | >>> stmr.stem('suspensjon') |
||
87 | 'suspensjon' |
||
88 | >>> stmr.stem('sikkerhet') |
||
89 | 'sikker' |
||
90 | |||
91 | """ |
||
92 | # lowercase, normalize, and compose |
||
93 | 1 | word = normalize('NFC', text_type(word.lower())) |
|
94 | |||
95 | 1 | r1_start = min(max(3, self._sb_r1(word)), len(word)) |
|
96 | |||
97 | # Step 1 |
||
98 | 1 | _r1 = word[r1_start:] |
|
99 | 1 | if _r1[-7:] == 'hetenes': |
|
100 | 1 | word = word[:-7] |
|
101 | 1 | elif _r1[-6:] in {'hetene', 'hetens'}: |
|
102 | 1 | word = word[:-6] |
|
103 | 1 | elif _r1[-5:] in {'heten', 'heter', 'endes'}: |
|
104 | 1 | word = word[:-5] |
|
105 | 1 | elif _r1[-4:] in {'ande', 'ende', 'edes', 'enes', 'erte'}: |
|
106 | 1 | if word[-4:] == 'erte': |
|
107 | 1 | word = word[:-2] |
|
108 | else: |
||
109 | 1 | word = word[:-4] |
|
110 | 1 | elif _r1[-3:] in { |
|
111 | 'ede', |
||
112 | 'ane', |
||
113 | 'ene', |
||
114 | 'ens', |
||
115 | 'ers', |
||
116 | 'ets', |
||
117 | 'het', |
||
118 | 'ast', |
||
119 | 'ert', |
||
120 | }: |
||
121 | 1 | if word[-3:] == 'ert': |
|
122 | 1 | word = word[:-1] |
|
123 | else: |
||
124 | 1 | word = word[:-3] |
|
125 | 1 | elif _r1[-2:] in {'en', 'ar', 'er', 'as', 'es', 'et'}: |
|
126 | 1 | word = word[:-2] |
|
127 | 1 | elif _r1[-1:] in {'a', 'e'}: |
|
128 | 1 | word = word[:-1] |
|
129 | 1 | elif _r1[-1:] == 's': |
|
130 | 1 | if (len(word) > 1 and word[-2] in self._s_endings) or ( |
|
131 | len(word) > 2 |
||
132 | and word[-2] == 'k' |
||
133 | and word[-3] not in self._vowels |
||
134 | ): |
||
135 | 1 | word = word[:-1] |
|
136 | |||
137 | # Step 2 |
||
138 | 1 | if word[r1_start:][-2:] in {'dt', 'vt'}: |
|
139 | 1 | word = word[:-1] |
|
140 | |||
141 | # Step 3 |
||
142 | 1 | _r1 = word[r1_start:] |
|
143 | 1 | if _r1[-7:] == 'hetslov': |
|
144 | 1 | word = word[:-7] |
|
145 | 1 | elif _r1[-4:] in {'eleg', 'elig', 'elov', 'slov'}: |
|
146 | 1 | word = word[:-4] |
|
147 | 1 | elif _r1[-3:] in {'leg', 'eig', 'lig', 'els', 'lov'}: |
|
148 | 1 | word = word[:-3] |
|
149 | 1 | elif _r1[-2:] == 'ig': |
|
150 | 1 | word = word[:-2] |
|
151 | |||
152 | 1 | return word |
|
153 | |||
187 |