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): |
|
|
|
|
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): |
|
|
|
|
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': |
|
|
|
|
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', |
|
|
|
|
108
|
|
|
'erede', |
|
|
|
|
109
|
|
|
'heden', |
|
|
|
|
110
|
|
|
'heder', |
|
|
|
|
111
|
|
|
'endes', |
|
|
|
|
112
|
|
|
'ernes', |
|
|
|
|
113
|
|
|
'erens', |
|
|
|
|
114
|
|
|
'erets', |
|
|
|
|
115
|
|
|
}: |
116
|
1 |
|
word = word[:-5] |
117
|
1 |
|
elif _r1[-4:] in { |
118
|
|
|
'ered', |
|
|
|
|
119
|
|
|
'ende', |
|
|
|
|
120
|
|
|
'erne', |
|
|
|
|
121
|
|
|
'eren', |
|
|
|
|
122
|
|
|
'erer', |
|
|
|
|
123
|
|
|
'heds', |
|
|
|
|
124
|
|
|
'enes', |
|
|
|
|
125
|
|
|
'eres', |
|
|
|
|
126
|
|
|
'eret', |
|
|
|
|
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 |
|
|
|
|
168
|
|
|
and len(word) >= 2 |
|
|
|
|
169
|
|
|
and word[-1] == word[-2] |
|
|
|
|
170
|
|
|
and word[-1] not in self._vowels |
|
|
|
|
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
|
|
|
|