Completed
Pull Request — master (#141)
by Chris
11:04
created

abydos.stemmer._snowball_norwegian.sb_norwegian()   A

Complexity

Conditions 1

Size

Total Lines 21
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 21
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_norwegian.
20
21
Snowball Norwegian 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__ = ['SnowballNorwegian', 'sb_norwegian']
38
39
40 1
class SnowballNorwegian(_Snowball):
0 ignored issues
show
Unused Code introduced by
The variable __class__ seems to be unused.
Loading history...
41
    """Snowball Norwegian stemmer.
42
43
    The Snowball Norwegian stemmer is defined at:
44
    http://snowball.tartarus.org/algorithms/norwegian/stemmer.html
45
    """
46
47 1
    _vowels = {'a', 'e', 'i', 'o', 'u', 'y', 'å', 'æ', 'ø'}
48 1
    _s_endings = {
49
        'b',
50
        'c',
51
        'd',
52
        'f',
53
        'g',
54
        'h',
55
        'j',
56
        'l',
57
        'm',
58
        'n',
59
        'o',
60
        'p',
61
        'r',
62
        't',
63
        'v',
64
        'y',
65
        'z',
66
    }
67
68 1
    def stem(self, word):
0 ignored issues
show
Bug introduced by
Parameters differ from overridden 'stem' method
Loading history...
69
        """Return Snowball Norwegian stem.
70
71
        Args:
72
            word (str): The word to stem
73
74
        Returns:
75
            str: Word stem
76
77
        Examples:
78
            >>> stmr = SnowballNorwegian()
79
            >>> stmr.stem('lese')
80
            'les'
81
            >>> stmr.stem('suspensjon')
82
            'suspensjon'
83
            >>> stmr.stem('sikkerhet')
84
            'sikker'
85
86
        """
87
        # lowercase, normalize, and compose
88 1
        word = normalize('NFC', text_type(word.lower()))
89
90 1
        r1_start = min(max(3, self._sb_r1(word)), len(word))
91
92
        # Step 1
93 1
        _r1 = word[r1_start:]
94 1
        if _r1[-7:] == 'hetenes':
95 1
            word = word[:-7]
96 1
        elif _r1[-6:] in {'hetene', 'hetens'}:
97 1
            word = word[:-6]
98 1
        elif _r1[-5:] in {'heten', 'heter', 'endes'}:
99 1
            word = word[:-5]
100 1
        elif _r1[-4:] in {'ande', 'ende', 'edes', 'enes', 'erte'}:
101 1
            if word[-4:] == 'erte':
102 1
                word = word[:-2]
103
            else:
104 1
                word = word[:-4]
105 1
        elif _r1[-3:] in {
106
            'ede',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
107
            'ane',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
108
            'ene',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
109
            'ens',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
110
            'ers',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
111
            'ets',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
112
            'het',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
113
            'ast',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
114
            'ert',
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
115
        }:
116 1
            if word[-3:] == 'ert':
117 1
                word = word[:-1]
118
            else:
119 1
                word = word[:-3]
120 1
        elif _r1[-2:] in {'en', 'ar', 'er', 'as', 'es', 'et'}:
121 1
            word = word[:-2]
122 1
        elif _r1[-1:] in {'a', 'e'}:
123 1
            word = word[:-1]
124 1
        elif _r1[-1:] == 's':
125 1
            if (len(word) > 1 and word[-2] in self._s_endings) or (
126
                len(word) > 2
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
127
                and word[-2] == 'k'
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
128
                and word[-3] not in self._vowels
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block (add 4 spaces).
Loading history...
129
            ):
130 1
                word = word[:-1]
131
132
        # Step 2
133 1
        if word[r1_start:][-2:] in {'dt', 'vt'}:
134 1
            word = word[:-1]
135
136
        # Step 3
137 1
        _r1 = word[r1_start:]
138 1
        if _r1[-7:] == 'hetslov':
139 1
            word = word[:-7]
140 1
        elif _r1[-4:] in {'eleg', 'elig', 'elov', 'slov'}:
141 1
            word = word[:-4]
142 1
        elif _r1[-3:] in {'leg', 'eig', 'lig', 'els', 'lov'}:
143 1
            word = word[:-3]
144 1
        elif _r1[-2:] == 'ig':
145 1
            word = word[:-2]
146
147 1
        return word
148
149
150 1
def sb_norwegian(word):
151
    """Return Snowball Norwegian stem.
152
153
    This is a wrapper for :py:meth:`SnowballNorwegian.stem`.
154
155
    Args:
156
        word (str): The word to stem
157
158
    Returns:
159
        str: Word stem
160
161
    Examples:
162
        >>> sb_norwegian('lese')
163
        'les'
164
        >>> sb_norwegian('suspensjon')
165
        'suspensjon'
166
        >>> sb_norwegian('sikkerhet')
167
        'sikker'
168
169
    """
170 1
    return SnowballNorwegian().stem(word)
171
172
173
if __name__ == '__main__':
174
    import doctest
175
176
    doctest.testmod()
177