File Coverage

blib/lib/Lingua/Stem/Pt.pm
Criterion Covered Total %
statement 45 62 72.5
branch 10 24 41.6
condition 1 3 33.3
subroutine 8 10 80.0
pod 3 3 100.0
total 67 102 65.6


line stmt bran cond sub pod time code
1             package Lingua::Stem::Pt;
2              
3             =head1 NAME
4              
5             Lingua::Stem::Pt - Stemming algorithm for Portuguese
6              
7             =head1 SYNOPSIS
8              
9             use Lingua::Stem::Pt;
10             my $stems = Lingua::Stem::Pt::stem({ -words => $word_list_reference,
11             -locale => 'pt',
12             -exceptions => $exceptions_hash,
13             });
14              
15             =head1 DESCRIPTION
16              
17             This routine applies a stemming slgorithm to a passed anon array of Portuguese words,
18             returning the stemmed words as an anon array.
19              
20             It is a 'convienence' wrapper for 'Lingua::Stemmer::PT' that provides
21             a standardized interface and caching.
22              
23             =head1 CHANGES
24              
25             2.31 2020.09.26 - Fix for Latin1/UTF8 issue in documentation
26              
27             2.30 2020.06.20 - Version renumber for module consistency
28              
29             1.01 2003.09.28 - Documenation fix
30              
31             1.00 2003.04.05 - Initial release
32              
33             =cut
34              
35             #######################################################################
36             # Initialization
37             #######################################################################
38              
39 1     1   6 use strict;
  1         2  
  1         72  
40 1     1   41 use warnings;
  1         4  
  1         34  
41              
42 1     1   527 use Lingua::PT::Stemmer;
  1         1945  
  1         38  
43              
44 1     1   6 use Exporter;
  1         2  
  1         34  
45 1     1   5 use Carp;
  1         3  
  1         53  
46 1     1   6 use vars qw (@ISA @EXPORT_OK @EXPORT %EXPORT_TAGS $VERSION);
  1         2  
  1         98  
47             BEGIN {
48 1     1   18 @ISA = qw (Exporter);
49 1         3 @EXPORT = ();
50 1         2 @EXPORT_OK = qw (stem clear_stem_cache stem_caching);
51 1         494 %EXPORT_TAGS = ();
52             }
53             $VERSION = "2.31";
54              
55             my $Stem_Caching = 0;
56             my $Stem_Cache = {};
57              
58             =head1 METHODS
59              
60             =cut
61              
62             #######################################################################
63              
64             =over 4
65              
66             =item stem({ -words => \@words, -locale => 'pt', -exceptions => \%exceptions });
67              
68             Stems a list of passed words using the rules of Portuguese. Returns
69             an anonymous array reference to the stemmed words.
70              
71             Example:
72              
73             my $stemmed_words = Lingua::Stem::Pt::stem({ -words => \@words,
74             -locale => 'pt',
75             -exceptions => \%exceptions,
76             });
77              
78             =back
79              
80             =cut
81              
82             sub stem {
83 1 50   1 1 4 return [] if ($#_ == -1);
84 1         1 my $parm_ref;
85 1 50       4 if (ref $_[0]) {
86 1         2 $parm_ref = shift;
87             } else {
88 0         0 $parm_ref = { @_ };
89             }
90            
91 1         1 my $words = [];
92 1         2 my $locale = 'pt';
93 1         1 my $exceptions = {};
94 1         4 foreach (keys %$parm_ref) {
95 3         5 my $key = lc ($_);
96 3 100       8 if ($key eq '-words') {
    100          
    50          
97 1         2 @$words = @{$parm_ref->{$key}};
  1         4  
98             } elsif ($key eq '-exceptions') {
99 1         3 $exceptions = $parm_ref->{$key};
100             } elsif ($key eq '-locale') {
101 1         2 $locale = $parm_ref->{$key};
102             } else {
103 0         0 croak (__PACKAGE__ . "::stem() - Unknown parameter '$key' with value '$parm_ref->{$key}'\n");
104             }
105             }
106            
107 1         2 local $_;
108 1         2 foreach (@$words) {
109              
110             # Check against exceptions list
111 5 50       12 if (exists $exceptions->{$_}) {
112 0         0 $_ = $exceptions->{$_};
113 0         0 next;
114             }
115              
116             # Check against cache of stemmed words
117 5         6 my $original_word = $_;
118 5 0 33     10 if ($Stem_Caching && exists $Stem_Cache->{$original_word}) {
119 0         0 $_ = $Stem_Cache->{$original_word};
120 0         0 next;
121             }
122 5         13 ($_) = Lingua::PT::Stemmer::stem("$_");
123 5 50       12072 $Stem_Cache->{$original_word} = $_ if $Stem_Caching;
124             }
125 1 50       5 $Stem_Cache = {} if ($Stem_Caching < 2);
126            
127 1         5 return $words;
128             }
129              
130             ##############################################################
131              
132             =over 4
133              
134             =item stem_caching({ -level => 0|1|2 });
135              
136             Sets the level of stem caching.
137              
138             '0' means 'no caching'. This is the default level.
139              
140             '1' means 'cache per run'. This caches stemming results during a single
141             call to 'stem'.
142              
143             '2' means 'cache indefinitely'. This caches stemming results until
144             either the process exits or the 'clear_stem_cache' method is called.
145              
146             =back
147              
148             =cut
149              
150             sub stem_caching {
151 0     0 1   my $parm_ref;
152 0 0         if (ref $_[0]) {
153 0           $parm_ref = shift;
154             } else {
155 0           $parm_ref = { @_ };
156             }
157 0           my $caching_level = $parm_ref->{-level};
158 0 0         if (defined $caching_level) {
159 0 0         if ($caching_level !~ m/^[012]$/) {
160 0           croak(__PACKAGE__ . "::stem_caching() - Legal values are '0','1' or '2'. '$caching_level' is not a legal value");
161             }
162 0           $Stem_Caching = $caching_level;
163             }
164 0           return $Stem_Caching;
165             }
166            
167             ##############################################################
168              
169             =over 4
170              
171             =item clear_stem_cache;
172              
173             Clears the cache of stemmed words
174              
175             =back
176              
177             =cut
178              
179             sub clear_stem_cache {
180 0     0 1   $Stem_Cache = {};
181             }
182              
183             ##############################################################
184              
185             =head1 NOTES
186              
187             This code is a wrapper around Lingua::Stem::Snowball::Pt written by
188             Ask Solem Hoel,
189              
190             =head1 SEE ALSO
191              
192             Lingua::Stem Lingua::PT::Stemmer;
193              
194             =head1 AUTHOR
195              
196             Integration in Lingua::Stem by
197             Jerilyn Franz, FreeRun Technologies,
198            
199              
200             =head1 COPYRIGHT
201              
202             Jerilyn Franz, FreeRun Technologies
203              
204             This code is freely available under the same terms as Perl.
205              
206             =head1 BUGS
207              
208             =head1 TODO
209              
210             =cut
211              
212             1;