File Coverage

blib/lib/Lingua/Stem/Pt.pm
Criterion Covered Total %
statement 42 59 71.1
branch 11 24 45.8
condition 1 3 33.3
subroutine 7 9 77.7
pod 3 3 100.0
total 64 98 65.3


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