blib/lib/Lingua/PT/UnConjugate.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 178 CC 21 SCAR 41.8
CC
21
Cov
0%
unconj
132.0
string_entries
42.0
list_entries
42.0
CRAP 65.7
1 #!/usr/bin/perl -w
2 #
3 # Perl package exporting a function "unconj" that un-conjugates
4 # Portuguese verbs.
5 #
6 # Author : Etienne Grossmann (etienne@isr.ist.utl.pt)
7 #
8 # Date : September 1999 onwards.
9 #
10 #
11 package Lingua::PT::UnConjugate ;
12  
13 =head1 NAME
14  
15 Lingua::PT::UnConjugate - Recognition of the conjugated forms of
16 portuguese verbs.
17  
18 =head1 DESCRIPTION
19  
20 This module provides functions for the recognition of the conjugated
21 forms of portuguese verbs.
22  
23 =head1 BUGS
24  
25 Composed tenses are not recognized. The verb list contains many
26 non-verbs that I have not removed yet.
27  
28 =cut
29  
30 use Lingua::PT::Conjugate qw( conjug %long_tense ) ;
31  
32 use Lingua::PT::Infinitives ;
33 use Lingua::PT::VerbSuffixes ;
34 import Lingua::PT::Accent_iso_8859_1 qw(asc2iso);
35 use Exporter ;
36 @ISA = qw(Exporter);
37 # Yes, this package is a namespace polluter.
38 @EXPORT = qw( unconj );
39 @EXPORT_OK = qw( unconj list_entries string_entries );
40  
41  
42 BEGIN {
43 # Suffixes and Infinitives.
44  
45 # ####################### VOCALS, CONSONANTS #####################
46 # Vocals and Consonants
47 $vocs = "aeiouáàäâãéèëêíìïîóòöôõúùüû";
48 $cons = 'qwrtypsdfghjklzxcvbnm';
49 $letter = "ç$vocs$cons";
50 $lpat = "[$letter]" ;
51 # Equivalent accent-matching regexp
52 %equiv = ( "a"=>"[aáàäâã]",
53 "á"=>"[aáàäâã]",
54 "à"=>"[aáàäâã]",
55 "ä"=>"[aáàäâã]",
56 "â"=>"[aáàäâã]",
57 "ã"=>"[aáàäâã]",
58 "e"=>"[eéèëê]",
59 "é"=>"[eéèëê]",
60 "è"=>"[eéèëê]",
61 "ë"=>"[eéèëê]",
62 "ê"=>"[eéèëê]",
63 "i"=>"[iíìïî]",
64 "í"=>"[iíìïî]",
65 "ì"=>"[iíìïî]",
66 "ï"=>"[iíìïî]",
67 "î"=>"[iíìïî]",
68 "o"=>"[oóòöôõ]",
69 "ó"=>"[oóòöôõ]",
70 "ò"=>"[oóòöôõ]",
71 "ö"=>"[oóòöôõ]",
72 "ô"=>"[oóòöôõ]",
73 "õ"=>"[oóòöôõ]",
74 "u"=>"[uúùüû]",
75 "ú"=>"[uúùüû]",
76 "ù"=>"[uúùüû]",
77 "ü"=>"[uúùüû]",
78 "û"=>"[uúùüû]",
79 "c"=>"[cç]",
80 "ç"=>"[cç]",
81 ) ;
82 $equivk = join "", "[", keys(%equiv), "]" ;
83  
84 # Lower_case
85 %mylc = split "",
86 "ÇçÁáÀàÄäÂâÃãÉéÈèËëÊêÍíÌìÏïÎîÓóÒòÖöÔôÕõÚúÙùÜüÛû";
87 $mylck = join "", "[", keys(%mylc), "]" ;
88 # print "$equivk\n$mylck\n" ;
89  
90 }
91  
92 sub my_lc # lc() for accentuated characters too
93 {
94 my $a = shift ;
95 $a = lc($a) ;
96 $a =~ s/($mylck)/$mylc{$1}/g ;
97 return $a ;
98 }
99  
100 # $r = regexify( $w )
101 # $r is a regex that will match any ending substring of $w
102 sub regexify
103 {
104 my $r = shift ;
105 my $r0 = $r ;
106 while( $r =~ s/($lpat+)($lpat)/(\?:$1)\?$2/ ){}
107 # print "regexify : $r0 -> $r\n" ;
108 return $r ;
109 }
110  
111 =head1 SYNOPSIS
112  
113 =head1 C<$verb_forms = unconj( [-a] , $string )>
114  
115 Attempts to recognize a conjugated form of a Portuguese verb, and
116 returns the result as a reference to hash : if the element
117  
118 C<$verb_forms-E<gt>{$infinitive}-E<gt>{$tense}-E<gt>[$person]>
119  
120 is true, then the conjugation of the verb "$infinitive" at the tense
121 "$tense" and the person "$person" should yield "$string".
122  
123 =head2 OPTIONS
124  
125 The first argument may an option :
126  
127 =over 4
128  
129 =item -a : Try to match accentuation errors.
130  
131 =item -A : If no match is found, try matching with option -a.
132  
133 =back
134  
135 =cut
136  
137 sub unconj
138 {
139 my $acc = 0 ; # Check errors in accentuation ?
140 my $ret = 0 ; # Retry in case of failure ?
141 while( $_[0] =~ /^-[aA]$/ ) # Get options
142 {
143 my $opt = shift ;
144 $opt =~ s/-//;
145 $acc = 1 if $opt =~ /a/ ;
146 $ret = 1 if $opt =~ /A/ ;
147 # print "unconj : option $opt\n" ;
148 }
149 my $v0 = shift ;
150 # print "unconj : $v0\n" ;
151 my $v = asc2iso( $v0 ); # No ascii-style accents
152 $v = my_lc($v) ;
153 # my @res = ();
154 my %res = ();
155
156 my $p = regexify( $v ) ;
157 if( $acc )
158 {
159 ## HERE : assume letters are isolated in $p
160 $p =~ s/\b($equivk)\b/$equiv{$1}/g ;
161 ## print "$p\n" ;
162 }
163 my $p2 = "($p .*)" ;
164 my @matches = $verb_suffixes =~ /^$p2/mg ;
165
166 push @matches, " cfut,1,", " cfut,3," if $infinitives =~ /^$v$/m ;
167
168 # print join "\n", @matches,"\n" ;
169 foreach $m (@matches)
170 {
171 my ($s,$t,$p,$r) = $m =~ /^(\S*) (\w+),(\d+),(.*)/ ;
172 my @endings = split ",",$r ;
173 @endings = ("") unless @endings ;
174 # print "Found $m ",0+@endings," endings\n";
175 # print "-- $s, $t, $p, $r\n" ;
176 my $root = $s ? substr( $v, 0, -length($s) ) : $v ;
177 foreach (@endings)
178 {
179 my $i = $root . $_ ;
180 # $i =~ s/r+r$/r/ ; # Why?
181 ##print "Trying : $i, $t, $p, $root + $_\n" ;
182 next unless $infinitives =~ /^$i$/m ;
183 my $check = conjug("xs",$i,$t,$p) ;
184 $check =~ s/($equivk)/$equiv{$1}/g if $acc ;
185 # print "Check $check\n" if $acc ;
186 # print "Checking $i, $t, $p against $check\n" ;
187 next unless $v =~ /^$check$/ ;
188 # print "Found $i, $t, $p in $m\n" ;
189 # push @res , [$i, $t, $p] ;
190 $res{$i}->{$t}->[$p] = 1 ;
191 }
192 }
193 %res = %{unconj("-a",$v0)} if $ret && !$acc && !keys(%res) ;
194 return \%res ;
195 }
196  
197 =head1 C<@res = string_entries( ['l'], \%verb_forms )>
198  
199 Convert a hash of recognized forms into a list of strings
200 C<"$verb, $tense, $person">.
201  
202 If the first argument is a 'l', then long forms of verb names will
203 be used.
204  
205 =cut
206  
207 sub string_entries
208 {
209 my $long = 0 ;
210 if( $_[0] eq 'l' ) # Accept a "long verb name" option
211 {
212 $long = 1 ;
213 shift ;
214 }
215 my $vdb = shift ;
216 my ($w,$x,$y,$z);
217 my @res = ();
218  
219 my $vcnt = 0 ;
220  
221 while( ($w,$x) = each %$vdb ) {
222 next if $w eq " " ;
223 next unless $w ;
224 while( ($t,$y) = each %$x ) {
225 foreach $p (1..6)
226 {
227 # print "." if $vcnt % 50 == 49 ;
228 # print "\n".sprintf("%-6d ",$vcnt)
229 # if $vcnt %1000 == 999 ;
230 next unless defined( $y->[$p] );
231 # HERE : A bug ? If I don't check for "defined" the
232 # first time $long_tense{$t} is used, it is undef'd.
233 $t = $long_tense{$t} if $long &&
234 defined( $long_tense{$t} );
235 push @res, "$w, $t, $p" ;
236 }
237 }
238 }
239 return sort @res ;
240 }
241  
242 =head1 C<@res = list_entries( ['l'], \%verb_forms )>
243  
244 Convert a hash of recognized forms into a list of triplets
245 C<[ $verb, $tense, $person ]>.
246  
247 If the first argument is a 'l', then long forms of verb names will
248 be used.
249  
250 =cut
251
252 sub list_entries
253 {
254 my $long = 0 ;
255 if( $_[0] eq 'l' ) # Accept a "long verb name" option
256 {
257 $long = 1 ;
258 shift ;
259 }
260 my $vdb = shift ;
261 my ($w,$x,$y,$z);
262 my @res = ();
263  
264 my $vcnt = 0 ;
265  
266 while( ($w,$x) = each %$vdb ) {
267 next if $w eq " " ;
268 next unless $w ;
269 while( ($t,$y) = each %$x ) {
270 foreach $p (1..6)
271 {
272 # print "." if $vcnt % 50 == 49 ;
273 # print "\n".sprintf("%-6d ",$vcnt)
274 # if $vcnt %1000 == 999 ;
275 next unless defined( $y->[$p] );
276 $t = $long_tense{$t} if $long &&
277 defined( $long_tense{$t} );
278 push @res, [$w, $t, $p ] ;
279 }
280 }
281 }
282 return sort @res ;
283 }
284  
285 =head1 SEE ALSO : unconj, conjug, treinar.
286  
287 =head1 AUTHOR Etienne Grossmann, 1999 [etienne@isr.ist.utl.pt]
288  
289 =head1 CREDITS
290  
291 Thanks to Soraia Almeida (salmeida@logos.it) from the Logos project
292 (http://www.logos.it) and Ulisses Pinto and José João Almeida from
293 Projecto Natura (http://shiva.di.uminho.pt/~jj/pln) who made Ispell
294 available.
295  
296 A big part of the list of verb infinitives comes from files used in
297 Ispell (http://shiva.di.uminho.pt/~jj/pln) and in Logos
298 (http://www.verba.org, http://www.logos.it). these projects. Some
299 verbs were removed and others added by hand.
300  
301 =cut
302  
303  
304 1;
305  
306  
307  
308  
309