SnowballDanish.stem()   F
last analyzed

Complexity

Conditions 23

Size

Total Lines 109
Code Lines 65

Duplication

Lines 36
Ratio 33.03 %

Code Coverage

Tests 38
CRAP Score 23

Importance

Changes 0
Metric Value
eloc 65
dl 36
loc 109
ccs 38
cts 38
cp 1
rs 0
c 0
b 0
f 0
cc 23
nop 2
crap 23

How to fix   Long Method    Complexity   

Long Method

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:

Complexity

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.
2
# This file is part of Abydos.
3
#
4
# Abydos is free software: you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# Abydos is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with Abydos. If not, see <http://www.gnu.org/licenses/>.
16
17
"""abydos.stemmer._snowball_danish.
18
19 1
Snowball Danish stemmer
20
"""
21
22
from unicodedata import normalize
23
24 1
from ._snowball import _Snowball
25
26
__all__ = ['SnowballDanish']
27
28
29
class SnowballDanish(_Snowball):
30
    """Snowball Danish stemmer.
31 1
32
    The Snowball Danish stemmer is defined at:
33 1
    http://snowball.tartarus.org/algorithms/danish/stemmer.html
34
35 1
    .. versionadded:: 0.3.6
36
    """
37 1
38 1
    _vowels = {'a', 'e', 'i', 'o', 'u', 'y', 'å', 'æ', 'ø'}
39
    _s_endings = {
40 1
        'a',
41
        'b',
42
        'c',
43 1
        'd',
44
        'f',
45
        'g',
46
        'h',
47
        'j',
48
        'k',
49
        'l',
50
        'm',
51
        'n',
52 1
        'o',
53 1
        'p',
54
        'r',
55
        't',
56
        'v',
57
        'y',
58
        'z',
59
        'å',
60
    }
61
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':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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
172 1
173 1
if __name__ == '__main__':
174
    import doctest
175
176
    doctest.testmod()
177