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): |
|
|
|
|
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): |
|
|
|
|
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
|
|
|
|
154
|
|
|
|
155
|
1 |
|
def sb_norwegian(word): |
156
|
|
|
"""Return Snowball Norwegian stem. |
157
|
|
|
|
158
|
|
|
This is a wrapper for :py:meth:`SnowballNorwegian.stem`. |
159
|
|
|
|
160
|
|
|
Parameters |
161
|
|
|
---------- |
162
|
|
|
word : str |
163
|
|
|
The word to stem |
164
|
|
|
|
165
|
|
|
Returns |
166
|
|
|
------- |
167
|
|
|
str |
168
|
|
|
Word stem |
169
|
|
|
|
170
|
|
|
Examples |
171
|
|
|
-------- |
172
|
|
|
>>> sb_norwegian('lese') |
173
|
|
|
'les' |
174
|
|
|
>>> sb_norwegian('suspensjon') |
175
|
|
|
'suspensjon' |
176
|
|
|
>>> sb_norwegian('sikkerhet') |
177
|
|
|
'sikker' |
178
|
|
|
|
179
|
|
|
""" |
180
|
1 |
|
return SnowballNorwegian().stem(word) |
181
|
|
|
|
182
|
|
|
|
183
|
|
|
if __name__ == '__main__': |
184
|
|
|
import doctest |
185
|
|
|
|
186
|
|
|
doctest.testmod() |
187
|
|
|
|