Completed
Pull Request — master (#149)
by Chris
11:34
created

abydos.stemmer._snowball_danish.sb_danish()   A

Complexity

Conditions 1

Size

Total Lines 26
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 1
dl 0
loc 26
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2014-2018 by Christopher C. Little.
4
# This file is part of Abydos.
5
#
6
# Abydos is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Abydos is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Abydos. If not, see <http://www.gnu.org/licenses/>.
18
19 1
"""abydos.stemmer._snowball_danish.
20
21
Snowball Danish stemmer
22
"""
23
24 1
from __future__ import (
25
    absolute_import,
26
    division,
27
    print_function,
28
    unicode_literals,
29
)
30
31 1
from unicodedata import normalize
32
33 1
from six import text_type
34
35 1
from ._snowball import _Snowball
36
37 1
__all__ = ['SnowballDanish', 'sb_danish']
38
39
40 1
class SnowballDanish(_Snowball):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
41
    """Snowball Danish stemmer.
42
43
    The Snowball Danish stemmer is defined at:
44
    http://snowball.tartarus.org/algorithms/danish/stemmer.html
45
    """
46
47 1
    _vowels = {'a', 'e', 'i', 'o', 'u', 'y', 'å', 'æ', 'ø'}
48 1
    _s_endings = {
49
        'a',
50
        'b',
51
        'c',
52
        'd',
53
        'f',
54
        'g',
55
        'h',
56
        'j',
57
        'k',
58
        'l',
59
        'm',
60
        'n',
61
        'o',
62
        'p',
63
        'r',
64
        't',
65
        'v',
66
        'y',
67
        'z',
68
        'å',
69
    }
70
71 1
    def stem(self, word):
0 ignored issues
show
Bug introduced by
Parameters differ from overridden 'stem' method
Loading history...
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':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
108
            'erede',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
109
            'heden',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
110
            'heder',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
111
            'endes',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
112
            'ernes',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
113
            'erens',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
114
            'erets',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
115
        }:
116 1
            word = word[:-5]
117 1
        elif _r1[-4:] in {
118
            'ered',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
119
            'ende',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
120
            'erne',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
121
            'eren',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
122
            'erer',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
123
            'heds',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
124
            'enes',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
125
            'eres',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
126
            'eret',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
168
            and len(word) >= 2
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
169
            and word[-1] == word[-2]
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
170
            and word[-1] not in self._vowels
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
171
        ):
172 1
            word = word[:-1]
173
174 1
        return word
175
176
177 1
def sb_danish(word):
178
    """Return Snowball Danish stem.
179
180
    This is a wrapper for :py:meth:`SnowballDanish.stem`.
181
182
    Parameters
183
    ----------
184
    word : str
185
        The word to stem
186
187
    Returns
188
    -------
189
    str
190
        Word stem
191
192
    Examples
193
    --------
194
    >>> sb_danish('underviser')
195
    'undervis'
196
    >>> sb_danish('suspension')
197
    'suspension'
198
    >>> sb_danish('sikkerhed')
199
    'sikker'
200
201
    """
202 1
    return SnowballDanish().stem(word)
203
204
205
if __name__ == '__main__':
206
    import doctest
207
208
    doctest.testmod()
209