1package Lingua::Stem::Snowball::Se;
2use strict;
3use bytes;
4# -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5# Lingua::Stem::Snowball::Se - Swedish stemmer
6# :: based upon the swedish stemmer algorithm at snowball.tartarus.org.
7#	 by Martin Porter.
8# (c) 2001-2007 Ask Solem Hoel <ask@0x61736b.net>
9#
10#   This program is free software; you can redistribute it and/or modify
11#   it under the terms of the GNU General Public License version 2,
12#   *NOT* "earlier versions", as published by the Free Software Foundation.
13#
14#   This program is distributed in the hope that it will be useful,
15#   but WITHOUT ANY WARRANTY; without even the implied warranty of
16#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17#   GNU General Public License for more details.
18#
19#   You should have received a copy of the GNU General Public License
20#   along with this program; if not, write to the Free Software
21#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22# -+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23#####
24
25
26use vars qw($VERSION);
27$VERSION = 1.2;
28
29# special characters
30my $ae = chr(0xe4); # a"
31my $ao = chr(0xe5); # ao
32my $oe = chr(0xf6); # o"
33
34# delete the s if a "s ending" is preceeded by one
35# of these characters.
36my $s_ending = "bcdfghjklmnoprtvy";
37
38# swedish vowels.
39my $vowels = "aeiouy$ae$ao$oe";
40
41# ####
42# the endings in step 1
43# XXX: these must be sorted by length
44# to save time we've done it already, you can do it like this:
45#	my $bylength = sub {
46#		length $a <=> length $b;
47#	}
48#	@endings = reverse sort $bylength @endings;
49my @endings = qw/
50   heterna hetens ornas ernas andet heter arnas arens heten andes
51   anden orna erna erns arna ades aren aste arne ande ast het ens
52   ern ade are er at es ad as en or ar e a s
53/;
54
55# the endings in step 2
56# XXX: these must be sorted by length, like @endings in step 1.
57my @endings2 = ('fullt', "l${oe}st", 'els', 'lig', 'ig');
58
59my %cache = ( );
60
61sub new {
62	my $pkg = shift;
63	$pkg = ref $pkg || $pkg;
64	my %arg = @_;
65	my $self = {};
66	bless $self, $pkg;
67	if($arg{use_cache}) {
68		$self->use_cache(1);
69	}
70	return $self;
71}
72
73sub use_cache {
74	my($self, $use_cache) = @_;
75	if($use_cache) {
76		$self->{USE_CACHE} = 1;
77	}
78	return $self->{USE_CACHE};
79}
80
81sub stem {
82	my ($self, $word) = @_;
83    no warnings;
84	my $orig_word;
85
86	if($self->use_cache()) {
87		$orig_word = $word;
88		my $cached_word = $cache{$word};
89		return $cached_word if $cached_word;
90	}
91
92	my ($ls, $rs, $wlen, $lslen, $rslen) = getsides($word);
93	return $word unless $lslen >= 3;
94
95	# ### STEP 1
96	# only need to refresh wlen each time we change the word.
97	foreach my $ending (@endings)  {
98		my $endinglen = length $ending; # do this once.
99
100		# only continue if the word has this ending at all.
101		if(substr($rs, $rslen - $endinglen, $rslen) eq $ending) {
102			if($ending eq 's') { # b)
103				# check if it has a valid "s ending"...
104				my $valid_s_ending = 0;
105				if($rslen == 1) {
106					my $wmr1 = substr($word, 0, $wlen - $rslen);
107					if($wmr1 =~ /[$s_ending]$/o) {
108						$valid_s_ending = 1;
109					}
110				}
111				else {
112					if(substr($rs, $rslen - 2, $rslen - 1) =~ /[$s_ending]/o) {
113						$valid_s_ending = 1;
114					}
115				}
116				if($valid_s_ending) {
117					# ...delete the last character (which is a s)
118					$word = substr($word, 0, $wlen - 1);
119					($ls, $rs, $wlen, $lslen, $rslen) = getsides($word);
120					last;
121				}
122			}
123			else { # a)
124				# delete the ending.
125				$word = substr($word, 0, $wlen - $endinglen);
126				($ls, $rs, $wlen, $lslen, $rslen) = getsides($word);
127				last;
128			}
129		}
130	}
131	return $word unless $lslen >= 3;
132
133	# ### STEP 2
134	my $ending = substr($rs, $rslen - 2, $rslen);
135	if($ending =~ /dd|gd|nn|dt|gt|kt|tt/) {
136		$word = substr($word, 0, $wlen - 1);
137		($ls, $rs, $wlen, $lslen, $rslen) = getsides($word);
138	}
139	return $word unless $lslen >= 3;
140
141	# ### STEP 3
142	foreach my $ending (@endings2) {
143		if($rs =~ /\Q$ending\E$/)
144		{
145			if($ending =~ /lig|ig|els/)
146			{
147				my $res = substr($word, 0, $wlen - length($ending));
148				$word = $res; #if length $res > 2;
149				last
150			}
151			if($ending eq "l${oe}st")
152			{
153				my $res = substr($word, 0, $wlen - 1);
154				$word = $res; #if length $res > 2;
155				last
156			}
157			if($ending eq 'fullt')
158			{
159				my $res = substr($word, 0, $wlen - 1);
160				$word = $res; #if length $res > 2;
161				last
162			}
163		}
164	}
165
166	if($self->use_cache()) {
167		$cache{$orig_word} = $word;
168	}
169
170	return $word;
171}
172
173sub getsides {
174    my $word = shift;
175    no warnings;
176    my $wlen = length $word;
177
178    my($ls, $rs) = (undef, undef); # left side and right side.
179
180    # ###
181    # find the first vowel with a non-vowel after it.
182    my($found_vowel, $nonv_position, $curpos) = (-1, -1, 0);
183    foreach(split//, $word) {
184        if($found_vowel> 0) {
185			if(/[^$vowels]/o) {
186				if($curpos > 0) {
187				$nonv_position = $curpos + 1;
188				last;
189				}
190			}
191        }
192        if(/[$vowels]/o) {
193            $found_vowel = 1;
194        }
195        $curpos++;
196    }
197
198	# got nothing: return false
199	return undef if $nonv_position < 0;
200
201    # ###
202    # length of the left side must be atleast 3 chars.
203    my $leftlen = $wlen - ($wlen - $nonv_position);
204    if($leftlen < 3) {
205        $ls = substr($word, 0, 3);
206        $rs = substr($word, 3, $wlen);
207    }
208    else {
209        $ls = substr($word, 0, $leftlen);
210        $rs = substr($word, $nonv_position, $wlen);
211    }
212    return($ls, $rs, $wlen, length $ls, length $rs);
213}
214
2151;
216
217__END__
218
219=head1 NAME
220
221Lingua::Stem::Snowball::Se - Porters stemming algorithm for Swedish
222
223=head1 VERSION
224
225This document describes version 1.1.
226
227=head1 SYNOPSIS
228
229  use Lingua::Stem::Snowball::Se
230  my $stemmer = new Lingua::Stem::Snowball::Se (use_cache => 1);
231
232  foreach my $word (@words) {
233	my $stemmed = $stemmer->stem($word);
234	print $stemmed, "\n";
235  }
236
237=head1 DESCRIPTION
238
239The stem function takes a scalar as a parameter and stems the word
240according to Martin Porters Swedish stemming algorithm,
241which can be found at the Snowball website: L<http://snowball.tartarus.org/>.
242
243It also supports caching if you pass the use_cache option when constructing
244a new L:S:S:S object.
245
246=head2 EXPORT
247
248Lingua::Stem::Snowball::Se has nothing to export.
249
250=head1 AUTHOR
251
252Ask Solem Hoel, E<lt>ask@0x61736b.netE<gt>
253
254=head1 SEE ALSO
255
256L<perl>. L<Lingua::Stem::Snowball>. L<Lingua::Stem>. L<http://snowball.tartarus.org>.
257
258=head1 LICENSE AND COPYRIGHT
259
260Copyright (c), 2007 Ask Solem C<< ask@0x61736b.net >>.
261
262All rights reserved.
263
264This library is free software; you can redistribute it and/or modify
265it under the same terms as Perl itself, either Perl version 5.8.6 or,
266at your option, any later version of Perl 5 you may have available.
267
268=head1 DISCLAIMER OF WARRANTY
269
270BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
271SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
272STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE
273SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
274INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
275FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
276PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE,
277YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
278
279IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
280COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE
281SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES,
282INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
283OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO
284LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
285THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER
286SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
287POSSIBILITY OF SUCH DAMAGES.
288
289=cut
290