/usr/share/perl5/Lingua/Stem/It.pm is in liblingua-stem-perl 0.84-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | package Lingua::Stem::It;
use strict;
use Exporter;
use Carp;
use vars qw (@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION);
BEGIN {
@ISA = qw (Exporter);
@EXPORT = ();
@EXPORT_OK = qw (stem stem_word clear_stem_cache stem_caching);
%EXPORT_TAGS = ();
}
$VERSION = "0.02";
my $Stem_Caching = 0;
my $Stem_Cache = {};
sub stem {
return [] if ($#_ == -1);
my $parm_ref;
if (ref $_[0]) {
$parm_ref = shift;
} else {
$parm_ref = { @_ };
}
my $words = [];
my $locale = 'it';
my $exceptions = {};
foreach (keys %$parm_ref) {
my $key = lc ($_);
if ($key eq '-words') {
@$words = @{$parm_ref->{$key}};
} elsif ($key eq '-exceptions') {
$exceptions = $parm_ref->{$key};
} elsif ($key eq '-locale') {
$locale = $parm_ref->{$key};
} else {
croak (__PACKAGE__ . "::stem() - Unknown parameter '$key' with value '$parm_ref->{$key}'\n");
}
}
local( $_ );
foreach (@$words) {
# Flatten case
$_ = lc $_;
# Check against exceptions list
if (exists $exceptions->{$_}) {
$_ = $exceptions->{$_};
next;
}
# Check against cache of stemmed words
my $original_word = $_;
if ($Stem_Caching && exists $Stem_Cache->{$original_word}) {
$_ = $Stem_Cache->{$original_word};
next;
}
$_ = stem_word($_);
$Stem_Cache->{$original_word} = $_ if $Stem_Caching;
}
$Stem_Cache = {} if ($Stem_Caching < 2);
return $words;
}
sub stem_word {
our($word) = @_;
my @suffix;
$word = lc $word;
# Check against cache of stemmed words
if ($Stem_Caching && exists $Stem_Cache->{$word}) {
return $Stem_Cache->{$word};
}
our($RV, $R1, $R2);
#### First, replace all acute accents by grave accents.
$word =~ s/é/è/g;
### put u after q, and u, i between vowels into upper case.
$word =~ s/([aàeèiìoòuù])([ui])([aàeèiìoòuù])/$1.uc($2).$3/eg;
#### RV is defined as follows
$RV = $word;
#### If the second letter is a consonant,
if($word =~ /^.[^aàeèiìoòuù]/) {
#### RV is the region after the next following vowel
$RV =~ s/^..[^aàeèiìoòuù]*[aàeèiìoòuù]//;
#### or if the first two letters are vowels
} elsif ($word =~ /^[aàeèiìoòuù][^aàeèiìoòuù]/) {
#### RV is the region after the next consonant
$RV =~ s/^..[aàeèiìoòuù]*[^aàeèiìoòuù]//;
#### and otherwise (consonant-vowel case)
} else {
#### RV is the region after the third letter
$RV =~ s/^...//;
}
#print "RV=$RV\n";
#### Defining R1 and R2
$R1 = $word;
#### R1 is the region after the first non-vowel following a
#### vowel, or is the null region at the end of the word if
#### there is no such non-vowel.
unless($R1 =~ s/^.*?[aàeèiìoòuù][^aàeèiìoòuù]//) {
$R1 = "";
}
#print "R1=$R1\n";
#### R2 is the region after the first non-vowel following a
#### vowel in R1, or is the null region at the end of the
#### word if there is no such non-vowel.
$R2 = $R1;
if($R2) {
unless($R2 =~ s/^.*?[aàeèiìoòuù][^aàeèiìoòuù]//) {
$R2 = "";
}
}
#print "R2=$R2\n";
#### Step 0: Attached pronoun
##### Search for the longest among the following suffixes
my @pronoun = qw(
ci gli la le li lo mi ne si ti vi
sene gliela gliele glieli glielo gliene
mela mele meli melo mene
tela tele teli telo tene
cela cele celi celo cene
vela vele veli velo vene
);
#### following one of
#### (a) ando endo
#### (b) ar er ir
#### in RV.
#### In case of (a) the suffix is deleted,
#### in case (b) it is replace by e
stem_killer( $RV, "[ae]ndo", "", @pronoun )
or stem_killer( $RV, "[aei]r", "e", @pronoun );
#### Step 1: Standard suffix removal
my $step1 = 0;
#### Search for the longest among the following suffixes,
#### and perform the action indicated
@suffix = qw(
anza anze
ico ici ica ice iche ichi
ismo ismi
abile abili ibile ibili
ista iste isti istà istè istì
oso osi osa ose
mente
atrice atrici
);
#### delete if in R2
$step1 += stem_killer( $R2, "", "", @suffix );
@suffix = qw(
icazione icazioni icatore icatori
azione azioni atore atori
);
#### delete if in R2
#### if preceded by ic, delete if in R2
$step1 += stem_killer( $R2, "", "", @suffix );
@suffix = qw(
logia logie
);
#### replace with log if in R2
$step1 += stem_killer( $R2, "", "log", @suffix );
@suffix = qw(
uzione uzioni usione usioni
);
#### replace with u if in R2
$step1 += stem_killer( $R2, "", "u", @suffix );
@suffix = qw(
enza enze
);
#### replace with ente if in R2
$step1 += stem_killer( $R2, "", "ente", @suffix );
@suffix = qw(
amento amenti imento imenti
);
#### delete if in RV
$step1 += stem_killer( $RV, "", "", @suffix );
@suffix = qw(
amente
);
#### delete if in R1
#### if preceded by iv, delete if in R2
#### (and if further preceded by at, delete if in R2), otherwise,
#### if preceded by os, ic or abil, delete if in R2
$step1 += stem_killer( $R2, "ativ", "", @suffix )
|| stem_killer( $R2, "iv", "", @suffix )
|| stem_killer( $R2, "(os|ic|abil)", "", @suffix )
|| stem_killer( $R1, "", "", @suffix );
@suffix = qw(
ità
);
#### delete if in R2
#### if preceded by abil, ic or iv, delete if in R2
$step1 += stem_killer( $R2, "(abil|ic|iv)", "", @suffix )
|| stem_killer( $R2, "", "", @suffix );
@suffix = qw(
ivo ivi iva ive
);
#### delete if in R2
#### if preceded by at, delete if in R2
#### (and if further preceded by ic, delete if in R2)
$step1 += stem_killer( $R2, "icat", "", @suffix)
|| stem_killer( $R2, "at", "", @suffix)
|| stem_killer( $R2, "", "", @suffix);
#### Step 2: Verb suffixes
#### Do step 2 if no ending was removed by step 1.
if($step1 == 0) {
#### Search for the longest among the following suffixes in RV,
#### and if found, delete.
stem_killer( $RV, "", "", qw(
ammo ando ano are arono asse assero assi assimo
ata ate ati ato ava avamo avano avate avi avo
emmo enda ende endi endo erà erai eranno ere
erebbe erebbero erei eremmo eremo ereste eresti
erete erò erono essero ete eva evamo evano evate
evi evo Yamo iamo immo irà irai iranno ire
irebbe irebbero irei iremmo iremo ireste iresti
irete irò irono isca iscano isce isci isco iscono
issero ita ite iti ito iva ivamo ivano ivate
ivi ivo ono uta ute uti uto ar er
));
}
#### Step 3a
#### Delete a final a, e, i, o, à, è, ì or ò if it is in RV,
#### and a preceding i if it is in RV
if($RV =~ s/i?[aeioàèìò]$//) {
$word =~ s/i?[aeioàèìò]$//;
#} else {
# if($RV =~ s/[aeioàèìò]$//) {
# $word =~ s/[aeioàèìò]$//;
# }
}
#### Step 3b
#### Replace final ch (or gh) with c (or g) if in RV
if($RV =~ s/([cg])h$/$1/) {
$word =~ s/([cg])h$/$1/;
}
#### Finally,
#### turn I and U back into lower case
$word =~ s/([IU])/lc($1)/eg;
return $word;
}
sub stem_killer {
my($where, $pre, $with, @list) = @_;
use vars qw($RV $R1 $R2 $word);
my $done = 0;
foreach my $P (sort { length($b) <=> length($a) } @list) {
if($where =~ /$pre$P$/) {
$R2 =~ s/$pre$P$/$with/;
$R1 =~ s/$pre$P$/$with/;
$RV =~ s/$pre$P$/$with/;
$word =~ s/$pre$P$/$with/;
$done = 1;
last;
}
}
return $done;
}
sub stem_caching {
my $parm_ref;
if (ref $_[0]) {
$parm_ref = shift;
} else {
$parm_ref = { @_ };
}
my $caching_level = $parm_ref->{-level};
if (defined $caching_level) {
if ($caching_level !~ m/^[012]$/) {
croak(__PACKAGE__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value");
}
$Stem_Caching = $caching_level;
}
return $Stem_Caching;
}
sub clear_stem_cache {
$Stem_Cache = {};
}
1;
__END__
=head1 NAME
Lingua::Stem::It - Porter's stemming algorithm for Italian
=head1 SYNOPSIS
use Lingua::Stem::It;
my $stems = Lingua::Stem::It::stem({ -words => $word_list_reference,
-locale => 'it',
-exceptions => $exceptions_hash,
});
my $stem = Lingua::Stem::It::stem_word( $word );
=head1 DESCRIPTION
This module applies the Porter Stemming Algorithm to its parameters,
returning the stemmed words.
The algorithm is implemented exactly (I hope :-) as described in:
http://snowball.tartarus.org/algorithms/italian/stemmer.html
The code is carefully crafted to work in conjunction with the L<Lingua::Stem>
module by Benjamin Franz, from which I've also borrowed some functionalities
(caching and exception list).
=head1 METHODS
=over 4
=item stem({ -words => \@words, -locale => 'it', -exceptions => \%exceptions });
Stems a list of passed words. Returns an anonymous list reference to the stemmed
words.
Example:
my $stemmed_words = Lingua::Stem::It::stem({ -words => \@words,
-locale => 'it',
-exceptions => \%exceptions,
});
=item stem_word( $word );
Stems a single word and returns the stem directly.
Example:
my $stem = Lingua::Stem::It::stem_word( $word );
=item stem_caching({ -level => 0|1|2 });
Sets the level of stem caching.
'0' means 'no caching'. This is the default level.
'1' means 'cache per run'. This caches stemming results during a single
call to 'stem'.
'2' means 'cache indefinitely'. This caches stemming results until
either the process exits or the 'clear_stem_cache' method is called.
=item clear_stem_cache;
Clears the cache of stemmed words
=back
=cut
=head2 EXPORT
None by default.
=head1 AUTHOR
Aldo Calpini, dada@perl.it
=head1 SEE ALSO
Lingua::Stem
=head1 COPYRIGHT
Copyright (c) Aldo Calpini, dada@perl.it. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
=cut
|