blib/lib/Lingua/Jspell.pm

filter: Statementstmt 12.2% 59 / 481 Branchbran 3.0% 9 / 298 Conditioncond 0.0% 0 / 72 MC/DCmcdc 0.0% 0 / 41 Subroutinesub 28.8% 13 / 45 Podpod 100% 20 / 20 totaltotal 10.5% 101 / 957 CC 198 SCAR 74.4
CC
198
Cov
8%
_cat2small
5402.0
nlgrep
240.0
fea
210.0
CRAP 1695.5
↑ Summary blib/lib/Lingua/Jspell/DictManager.pm »
1 package Lingua::Jspell;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use POSIX qw(locale_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 setlocale(LC_CTYPE, "pt_PT");
10 2 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 our @EXPORT_OK = (qw.onethat verif nlgrep setstopwords
14 onethatverif any2str hash2str isguess.);
15  
16 2 our %EXPORT_TAGS = (basic => [qw.onethat verif onethatverif
17 any2str hash2str isguess.],
18 greps => [qw.nlgrep setstopwords.]);
19 # use Data::Dumper;
20 2 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Lingua::Jspell::ConfigData;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Lingua::Jspell::EAGLES;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use YAML qw/LoadFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use Data::Compare;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 =head1 NAME
28  
29 =encoding utf8
30  
31 Lingua::Jspell - Perl interface to the Jspell morphological analyser.
32  
33 =cut
34  
35 2 our $VERSION = '1.91';
36 2 our $JSPELL;
37 2 our $JSPELLLIB;
38 2 our $MODE = { nm => "af", flags => 0 };
39 2 our $DELIM = '===';
40 2 our %STOP =();
41  
42 BEGIN {
43 2 ▶ delete @ENV{qw(IFS CD PATH ENV BASH_ENV)}; # Make %ENV safer
Subroutine BEGIN: called CC=8 SCAR=22.4
44  
45 2 my $EXE = "";
46 2 ▶ if ($^O eq "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 2 2
47 0 $ENV{PATH} = "blib\\usrlib";
48 0 $EXE=".exe" ;
49  
50 0 my $dllpath = Lingua::Jspell::ConfigData->config("libdir");
51 0 $ENV{PATH} = join(";", $dllpath, $ENV{PATH});
52 }
53  
54 2 local $_;
55  
56 2 $JSPELL = catfile("blib","bin","jspell$EXE");
57 2 ▶ $JSPELL = Lingua::Jspell::ConfigData->config("jspell") unless -x $JSPELL;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $Lingua::Jspell::JSPELL 0 2 2
58  
59 2 ▶ die "jspell binary cannot be found!\n" unless -x $JSPELL;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $Lingua::Jspell::JSPELL 0 2 2
60  
61 2 local $.;
62 2 ▶ open X, "$JSPELL -vv|" or die "Can't execute $JSPELL";
Branch: 1 branch
BranchTrueFalseTotal
open X, "$Lingua::Jspell::JSPELL -vv|" or die "Can't execute $Lingua::Jspell::JSPELL" 0 2 2
63 2 ▶ while (<X>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline X)) 118 2 120
64 118 ▶ if (/LIBDIR = "([^"]+)"/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/LIBDIR = "([^"]+)"/l) 2 116 118
65 2 $JSPELLLIB = $1;
66 }
67 }
68 2 close X;
69 2 ▶ die "Can't find out jspell lib dir" unless $JSPELLLIB;
Branch: 1 branch
BranchTrueFalseTotal
unless $Lingua::Jspell::JSPELLLIB 0 2 2
70 }
71  
72 =head1 SYNOPSIS
73  
74 use Lingua::Jspell;
75  
76 my $dict = Lingua::Jspell->new( "dict_name");
77 my $dict = Lingua::Jspell->new( "dict_name" , "personal_dict_name");
78  
79 $dict->rad("gatinho"); # list of radicals (gato)
80  
81 $dict->fea("gatinho"); # list of possible analysis
82  
83 $dict->der("gato"); # list of derivated words
84  
85 $dict->flags("gato"); # list of roots and flags
86  
87 =head1 FUNCTIONS
88  
89  
90 =head2 new
91  
92 Use to open a dictionary. Pass it the dictionary name and optionally a
93 personal dictionary name. A new jspell dictionary object will be
94 returned.
95  
96 =cut
97  
98 sub new {
99 0 ▶ my ($self, $dr, $pers, $flag);
Subroutine new: not called CC=12 SCAR=50.5
Pod documented
100 0 local $/="\n";
101 0 my $class = shift;
102  
103 0 $self->{dictionary} = shift;
104 $self->{pdictionary} = shift ||
105 0 ▶ (defined($ENV{HOME})?"$ENV{HOME}/.jspell.$self->{dictionary}":"");
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOME'} ? : 0 0 0
Condition0%0/3
shift() || (defined $ENV{'HOME'} ? "$ENV{'HOME'}/.jspell.$self->{'dictionary'}" : '')
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || (defined $ENV{'HOME'} ? "$ENV{'HOME'}/.jspell.$self->{'dictionary'}" : '')
shift() (defined $ENV{'HOME'} ? "$ENV{'HOME'}/.jspell.$self->{'dictionary'}" : '')
Truth table0%0/3
A || B
A= shift()B= (defined $ENV{'HOME'} ? "$ENV{'HOME'}/.jspell.$self->{'dictionary'}" : '')
ABresult
1X1
011
000
106  
107 0 ▶ $pers = $self->{pdictionary}?"-p $self->{pdictionary}":"";
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pdictionary'} ? : 0 0 0
108 0 ▶ $flag = defined($self->{'undef'})?$self->{'undef'}:"-y";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'undef'} ? : 0 0 0
109  
110 ## Get yaml info ----------------------------------
111 0 my $yaml_file = _yaml_file($self->{dictionary});
112 0 ▶ if (-f $yaml_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $yaml_file) { } 0 0 0
113 0 $self->{yaml} = LoadFile($yaml_file);
114 } else {
115 0 $self->{yaml} = {};
116 }
117  
118  
119 0 my $js = "$JSPELL -d $self->{dictionary} -a $pers -W 0 $flag -o'%s!%s:%s:%s:%s'";
120 0 local $.;
121 0 ▶ $self->{pid} = open3($self->{DW},$self->{DR},$self->{DE},$js) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"pid"} = open3($self->{"DW"}, $self->{"DR"}, $self->{"DE"}, $js) or die $! 0 0 0
122
123 0 binmode($self->{DW},":encoding(iso-8859-1)");
124 0 ▶ if ($^O ne "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne 'MSWin32') { } 0 0 0
125 0 binmode($self->{DR},":encoding(iso-8859-1)");
126 }
127 else {
128 0 binmode($self->{DR},":crlf:encoding(iso-8859-1)");
129 }
130 0 $dr = $self->{DR};
131 0 my $first_line = <$dr>;
132 0 ▶ die "Can't execute jspell with supplied dictionaries ($js)" unless $first_line && $first_line =~ /International Jspell/;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_line and $first_line =~ /International Jspell/l 0 0 0
Condition0%0/3
$first_line and $first_line =~ /International Jspell/l
!ll&&!rl&&r
000
MC/DC0%0/2
$first_line and $first_line =~ /International Jspell/l
$first_line $first_line =~ /International Jspell/l
Truth table0%0/3
A and B
A= $first_lineB= $first_line =~ /International Jspell/l
ABresult
0X0
100
111
133  
134 0 ▶ $self->{mode} ||= $MODE;
Condition0%0/3
$self->{"mode"} ||= $Lingua::Jspell::MODE
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"mode"} ||= $Lingua::Jspell::MODE
$self->{"mode"} $Lingua::Jspell::MODE
Truth table0%0/3
A ||= B
A= $self->{"mode"}B= $Lingua::Jspell::MODE
ABresult
1X1
011
000
135 0 my $dw = $self->{DW};
136 0 print $dw _mode($self->{mode});
137  
138 0 ▶ if ($first_line =~ /Jspell/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first_line =~ /Jspell/l) { } 0 0 0
139 0 return bless $self, $class # amen
140 }
141 else {
142 return undef
143 0 }
144 }
145  
146 =head2 nearmatches
147  
148 This method returns a list of analysis for words that are near-matches
149 to the supplied word. Note that although a word might exist, this
150 method will compute the near-matches as well.
151  
152 @nearmatches = $dictionary->nearmatches('cavale');
153  
154 To compute the list of words to analyze, the method uses a list of
155 equivalence classes that are present on the C<< SNDCLASSES >> section
156 of dictionaries yaml files.
157  
158 It is also possible to specify a list of user-defined classes. These
159 are supplied as a filename that contains, per line, the characters
160 that are equivalent (with spaces separating them):
161  
162 ch x
163 ss ç
164  
165 This example says that if a word uses C<ch>, then it can be replaced
166 by C<x> for near-matches calculation. The inverse is also true.
167  
168 If these rules are stored in a file named C<classes.txt>, you can
169 supply this list with:
170  
171 @nearmatches = $dictionary->nearmatches('chaile', rules => 'classes.txt');
172  
173 =cut
174  
175 sub nearmatches {
176 0 ▶ my ($dict, $word, %ops) = @_;
Subroutine nearmatches: not called CC=12 SCAR=50.5
Pod documented
177 0 my %classes;
178 0 ▶ if ($ops{rules}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ops{'rules'}) { } 0 0 0
179 0 ▶ -f $ops{rules} or die "Can't find file $ops{rules}";
Branch: 1 branch
BranchTrueFalseTotal
-f $ops{"rules"} or die qq[Can't find file $ops{"rules"}] 0 0 0
180 0 local $.;
181 0 ▶ open RULES, $ops{rules} or die "Can't open file $ops{rules}";
Branch: 1 branch
BranchTrueFalseTotal
open RULES, $ops{"rules"} or die qq[Can't open file $ops{"rules"}] 0 0 0
182 0 my @rules;
183 0 ▶ while(<RULES>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline RULES)) 0 0 0
184 0 chomp;
185 0 push @rules, [split /\s+/];
186 }
187 0 close RULES;
188 0 %classes = _expand_classes(@rules);
189 } else {
190 0 ▶ if (exists($dict->{yaml}{META}{SNDCLASSES})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $dict->{'yaml'}{'META'}{'SNDCLASSES'}) { } 0 0 0
191 0 %classes = _expand_classes(@{ $dict->{yaml}{META}{SNDCLASSES} });
192 } else {
193 0 warn "No snd classes defined\n";
194 }
195 }
196  
197 0 my @words = ();
198 0 for my $c (keys %classes) {
199 0 my @where;
200 0 my $l = length($c);
201 0 ▶ push @where, pos($word)-$l while $word =~ /$c/g;
Branch: 1 branch
BranchTrueFalseTotal
$word =~ /$c/gl and do { push @where, pos($word) - $l } 0 0 0
202 0 for my $i (@where) {
203 0 my $o = $word;
204 0 substr($o,$i,length($c), $classes{$c});
205 0 ▶ push @words, $o if $o ne $word;
Branch: 1 branch
BranchTrueFalseTotal
if $o ne $word 0 0 0
206 }
207 }
208  
209 0 my $current_mode = $dict->setmode;
210 0 $dict->setmode({flags => 0, nm => "cc" });
211  
212 0 my @nms;
213 0 for my $w (@words) {
214 0 ▶ my @analysis = map { $_->{guess}||=$w; $_ } $dict->fea($w);
Condition0%0/3
$_->{"guess"} ||= $w
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{"guess"} ||= $w
$_->{"guess"} $w
Truth table0%0/3
A ||= B
A= $_->{"guess"}B= $w
ABresult
1X1
011
000
215 0 push @nms, @analysis;
216 }
217  
218 0 @nms = grep { $_->{guess} ne $word } @nms;
219 # This one is not a guess
220 0 push @nms, $dict->fea($word);
221  
222 0 @nms = _remove_dups(@nms);
223  
224 0 $dict->setmode($current_mode);
225 0 return @nms;
226 }
227  
228 sub _remove_dups {
229 0 ▶ my @new;
Subroutine _remove_dups: not called CC=3 SCAR=24.8
230 0 ▶ while (my $struct = shift @_) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $struct = shift @_) 0 0 0
231 0 ▶ push @new, $struct unless grep { Compare($_,$struct) } @new;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {Compare($_, $struct);} @new 0 0 0
232 }
233 0 @new;
234 }
235  
236 0 ▶sub _expand_classes { map { _expand_class($_) } @_ }
Subroutine _expand_classes: not called CC=1 SCAR=6.9
237  
238 sub _expand_class {
239 0 ▶ my @class = @{ $_[0] };
Subroutine _expand_class: not called CC=3 SCAR=24.8
240 0 my %subs;
241 0 for my $c (@class) {
242 0 my @other = grep { $_ ne $c } @class;
243 0 for (@other) {
244 0 $subs{$c} = $_;
245 }
246 }
247 %subs
248 0 }
249  
250 =head2 setmode
251  
252 $dict->setmode({flags => 0, nm => "off" });
253  
254 =over 4
255  
256 =item af
257  
258 (add flags) Enable parcial near misses, by using rules not officially
259 associated with the current word. Does not give suggestions by
260 changing letters on the original word. (default option)
261  
262 =item full
263  
264 (add flags and change characters) Enable near misses, try to use rules
265 where they are not applied, try to give suggestions by swapping
266 adjacent letters on the original word.
267  
268 =item cc
269  
270 (change characters) Enable parcial near misses, by swapping adjacent,
271 inserting or modifying letters on the original word. Does not use
272 rules not associated with the current word.
273  
274 =item off
275  
276 Disable near misses at all.
277  
278 =back
279  
280 =cut
281  
282 sub setmode {
283 0 ▶ my ($self, $mode) = @_;
Subroutine setmode: not called CC=2 SCAR=17.9
Pod documented
284  
285 0 my $dw = $self->{DW};
286 0 ▶ if (defined($mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mode) { } 0 0 0
287 0 $self->{mode} = $mode;
288 0 print $dw _mode($mode);
289 } else {
290 0 return $self->{mode};
291 }
292 }
293  
294 =head2 fea
295  
296 Returns a list of analisys of a word. Each analisys is a list of
297 attribute value pairs. Attributes available: CAT, T, G, N, P, ....
298  
299 @l = $dic->fea($word)
300 @l = $dic->fea($word,{...att. value pair restriction})
301  
302 If a restriction is provided, just the analisys that verify
303 it are returned.
304  
305 =cut
306  
307  
308 sub fea {
309 0 ▶ my ($self,$w,$res) = @_;
Subroutine fea: not called CC=14 SCAR=53.5
Pod documented
310  
311 0 local $/="\n";
312  
313 0 my @r = ();
314 0 my ($a, $rad, $cla, $flags);
315  
316 0 ▶ return () if $w =~ /\!/;
Branch: 1 branch
BranchTrueFalseTotal
if $w =~ /\!/l 0 0 0
317  
318 0 my ($dw,$dr) = ($self->{DW},$self->{DR});
319  
320 0 local $.;
321  
322 0 print $dw " $w\n";
323 0 $a = <$dr>;
324  
325  
326 0 ▶ for (;($a ne "\n"); $a=<$dr>) { # l^e as respostas
Branch: 1 branch
BranchTrueFalseTotal
if ($a ne "\n") 0 0 0
327 0 for($a){
328 0 chop;
329 0 my ($lixo,$clas);
330 0 ▶ if(/(.*?) :(.*)/){$clas = $2 ; $lixo =$1}
Branch: 1 branch
BranchTrueFalseTotal
if (/(.*?) :(.*)/l) { } 0 0 0
331 0 else {$clas = $_ ; $lixo =""}
332  
333 0 for(split(/[,;] /,$clas)){
334 0 ($rad,$cla)= m{(.+?)\!:*(.*)$};
335  
336 # $cla undef quando nada preenchido...
337  
338 0 ▶ if ($cla) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cla) { } 0 0 0
339 0 ▶ if ($cla =~ s/\/(.*)$//) { $flags = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($cla =~ s[/(.*)$][]l) { } 0 0 0
340 0 else { $flags = "" }
341  
342 0 $cla =~ s/:+$//g;
343 0 $cla =~ s/:+/,/g;
344  
345 0 my %ana =();
346 0 my @attrs = split /,/, $cla;
347 0 for (@attrs) {
348 0 ▶ if (m!=!) {
Branch: 1 branch
BranchTrueFalseTotal
if (/=/l) { } 0 0 0
349 0 $ana{$`}=$';
350 } else {
351 0 print STDERR "** WARNING: Feature-structure parse error: $cla (for word '$w')\n";
352 }
353 }
354  
355 0 ▶ $ana{"flags"} = $flags if $flags;
Branch: 1 branch
BranchTrueFalseTotal
if $flags 0 0 0
356  
357 0 ▶ if ($lixo =~ /^&/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lixo =~ /^&/l) 0 0 0
358 0 $rad =~ s/(.*?)= //;
359 0 $ana{"guess"} = lc($1);
360 0 $ana{"unknown"} = 1;
361 }
362 0 ▶ if ($rad ne "" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rad ne "") 0 0 0
363 0 push(@r,+{"rad" => $rad, %ana});
364 }
365 }
366 0 else {@r=( +{CAT=>"?",rad=>$rad} )}
367 }
368 }
369 }
370 0 ▶ if($res){ return (grep { verif($res,$_) } @r) }
Branch: 1 branch
BranchTrueFalseTotal
if ($res) { } 0 0 0
371 0 else { return @r; }
372 }
373  
374 =head2 flags
375  
376 returns the set of morphological flag associated with the word.
377 Each flag is related with a set of morphological rules.
378  
379 @f = flags("gato")
380  
381 =cut
382  
383 sub flags {
384 0 ▶ my $self = shift;
Subroutine flags: not called CC=1 SCAR=6.9
Pod documented
385 0 my $w = shift;
386 0 my ($a,$dr);
387 0 local $/="\n";
388  
389 0 local $.;
390  
391 0 print {$self->{DW}} "\$\"$w\n";
392 0 $dr = $self->{DR};
393 0 $a = <$dr>;
394  
395 0 chop $a;
396 0 return split(/[# ,]+/,$a);
397 }
398  
399 =head2 rad
400  
401 Returns the list of all possible radicals/lemmas for the supplied word.
402  
403 @l = $dic->rad($word)
404  
405 =cut
406  
407 sub rad {
408 0 ▶ my $self = shift;
Subroutine rad: not called CC=3 SCAR=24.8
Pod documented
409 0 my $word = shift;
410  
411 0 ▶ return () if $word =~ /\!/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /\!/l 0 0 0
412  
413 0 my %rad = ();
414 0 my $a_ = "";
415 0 local $/ = "\n";
416 0 local $.;
417
418 0 my ($dw,$dr) = ($self->{DW},$self->{DR});
419  
420 0 print $dw " $word\n";
421  
422
423 0 ▶ for ($a_ = <$dr>; $a_ ne "\n"; $a_ = <$dr>) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a_ ne "\n") 0 0 0
424 0 chop $a_;
425 0 %rad = ($a_ =~ m/(?: |:)([^ =:,!]+)(\!)/g ) ;
426 }
427  
428 0 return (keys %rad);
429 }
430  
431  
432 =head2 der
433  
434 Returns the list of all possible words using the word as radical.
435  
436 @l = $dic->der($word);
437  
438 =cut
439  
440 sub der {
441 0 ▶ my ($self, $w) = @_;
Subroutine der: not called CC=9 SCAR=45.0
Pod documented
442 0 my @der = $self->flags($w);
443 0 my %res = ();
444 0 my $command;
445  
446 0 local $/ = "\n";
447 0 local $.;
448 0 ▶ my $pid = open3(\*WR, \*RD, \*ERROR, "$JSPELL -d $self->{dictionary} -e -o \"\"") or die "Can't execute jspell.";
Branch: 1 branch
BranchTrueFalseTotal
my $pid = open3(\*WR, \*RD, \*ERROR, qq[$Lingua::Jspell::JSPELL -d $self->{"dictionary"} -e -o ""]) or die "Can't execute jspell." 0 0 0
449 0 print WR join("\n",@der),"\n";
450 0 ▶ print WR "\032" if ($^O =~ /win32/i);
Branch: 1 branch
BranchTrueFalseTotal
if $^O =~ /win32/il 0 0 0
451 0 close WR;
452 0 ▶ while (<RD>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline RD)) 0 0 0
453 0 chomp;
454 0 s/(=|, | $)//g;
455 0 for(split) { $res{$_}++; }
456 }
457 0 close RD;
458 0 close ERROR;
459 0 waitpid $pid, 0;
460
461 0 my $irrcomm;
462 0 my $irr_file = _irr_file($self->{dictionary});
463  
464 0 local $.;
465 0 ▶ if (open IRR, $irr_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (open IRR, $irr_file) 0 0 0
466 0 ▶ while (<IRR>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IRR)) 0 0 0
467 0 ▶ next unless /^\Q$w\E=/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\Q$w\E=/l 0 0 0
468 0 chomp;
469 0 for (split(/[= ]+/,$_)) { $res{$_}++; }
470 }
471 0 close IRR;
472 }
473 0 return keys %res;
474 }
475  
476 =head2 onethat
477  
478 Returns the first Feature Structure from the supplied list that
479 verifies the Feature Structure Pattern used.
480  
481 %analysis = onethat( { CAT=>'adj' }, @features);
482  
483 %analysis = onethat( { CAT=>'adj' }, $pt->fea("espanhol"));
484  
485 =cut
486  
487 sub onethat {
488 0 ▶ my ($a, @b) = @_;
Subroutine onethat: not called CC=3 SCAR=24.8
Pod documented
489 0 for (@b) {
490 0 ▶ return %$_ if verif($a,$_);
Branch: 1 branch
BranchTrueFalseTotal
if verif($a, $_) 0 0 0
491 }
492 0 return () ;
493 }
494  
495 =head2 verif
496  
497 Returns a true value if the second Feature Structure verifies the
498 first Feature Structure Pattern.
499  
500 if (verif( $pattern, $feature) ) { ... }
501  
502 =cut
503  
504 sub verif {
505 0 ▶ my ($a, $b) = @_;
Subroutine verif: not called CC=4 SCAR=30.0
Pod documented
506 0 for (keys %$a) {
507 0 ▶ return 0 if (!defined($b->{$_}) || $a->{$_} ne $b->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if not defined $b->{$_} or $a->{$_} ne $b->{$_} 0 0 0
Condition0%0/3
not defined $b->{$_} or $a->{$_} ne $b->{$_}
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $b->{$_} or $a->{$_} ne $b->{$_}
not defined $b->{$_} $a->{$_} ne $b->{$_}
Truth table0%0/3
not(A) or B
A= not defined $b->{$_}B= $a->{$_} ne $b->{$_}
ABresult
1X1
011
000
508 }
509 0 return 1;
510 }
511  
512 =head2 nlgrep
513  
514 @line = $d->nlgrep( word , files);
515 @line = $d->nlgrep( [word1, wordn] , files);
516  
517 or with options to set a max number of entries, rec. separator, or tu use
518 radtxt files format.
519  
520 @line = $d->nlgrep( {max=>100, sep => "\n", radtxt=>0} , pattern , files);
521  
522 =cut
523  
524 sub nlgrep {
525 0 ▶ my ($self ) = shift;
Subroutine nlgrep: not called CC=15 SCAR=54.8
Pod documented
526 # max=int, sep:str, radtxt:bool
527 0 my %opt = (max=>10000, sep => "\n",radtxt=>0);
528 0 ▶ %opt = (%opt,%{shift(@_)}) if ref($_[0]) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
529  
530 0 my $p = shift;
531  
532 0 ▶ if(!ref($p) && $p =~ /[ ()*,]/){
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $p and $p =~ /[ ()*,]/l) 0 0 0
Condition0%0/3
not ref $p and $p =~ /[ ()*,]/l
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $p and $p =~ /[ ()*,]/l
not ref $p $p =~ /[ ()*,]/l
Truth table0%0/3
not(A) and B
A= not ref $pB= $p =~ /[ ()*,]/l
ABresult
0X0
100
111
533 0 ▶ $p = [map {/\w/ ? ($_):()} split(/[\- ()*\|,]/,$a)];}
Branch: 1 branch
BranchTrueFalseTotal
/\w/l ? : 0 0 0
534  
535 0 my $p2 ;
536  
537 0 ▶ if(ref($p) eq "ARRAY"){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p eq 'ARRAY') { } 0 0 0
538 0 ▶ if($opt{radtxt}){
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'radtxt'}) { } 0 0 0
539 0 my @pat = @$p ;
540 0 ▶ $p2 = sub{ my $x=shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
541 0 ▶ for(@pat){ return 0 unless $x =~ /\b(?:$_)\b/i;}
Branch: 1 branch
BranchTrueFalseTotal
unless $x =~ /\b(?:$_)\b/il 0 0 0
542 0 return 1; };
543 }
544 else {
545 0 my @pat = map {join("|",($_,$self->der($_)))} @$p ;
546 0 ▶ $p2 = sub{ my $x=shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
547 0 ▶ for(@pat){ return 0 unless $x =~ /\b(?:$_)\b/i;}
Branch: 1 branch
BranchTrueFalseTotal
unless $x =~ /\b(?:$_)\b/il 0 0 0
548 0 return 1; }
549 0 }
550 }
551 else {
552 0 ▶ my $pattern = $opt{radtxt} ? $p : join("|",($p,$self->der($p)));
Branch: 1 branch
BranchTrueFalseTotal
$opt{'radtxt'} ? : 0 0 0
553 0 ▶ $p2 = sub{ $_[0] =~ /\b(?:$pattern)\b/i };
Subroutine __ANON__: not called CC=1 SCAR=6.9
554 }
555  
556 0 my @file_list=@_;
557 0 local $/=$opt{sep};
558  
559 0 my @res=();
560 0 my $n = 0;
561 0 for(@file_list) {
562 0 local $.;
563 0 ▶ open(F,$_) or die("cant open $_\n");
Branch: 1 branch
BranchTrueFalseTotal
open F, $_ or die "cant open $_\n" 0 0 0
564 0 ▶ while(<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 0 0 0
565 0 ▶ if ($p2->($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$p2($_)) 0 0 0
566 0 chomp;
567 0 ▶ s/$DELIM.*//g if $opt{radtxt};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"radtxt"} 0 0 0
568 0 push(@res,$_);
569 0 ▶ last if $n++ == $opt{max};
Branch: 1 branch
BranchTrueFalseTotal
if $n++ == $opt{"max"} 0 0 0
570 }
571 }
572 0 close F;
573 0 ▶ last if $n == $opt{max};
Branch: 1 branch
BranchTrueFalseTotal
if $n == $opt{"max"} 0 0 0
574 }
575 0 return @res;
576 }
577  
578 =head2 setstopwords
579  
580 =cut
581  
582 sub setstopwords {
583 0 ▶ $STOP{$_} = 1 for @_;
Subroutine setstopwords: not called CC=2 SCAR=17.9
Pod documented
584 }
585  
586 =head2 eagles
587  
588 =cut
589 sub eagles {
590 0 ▶ my ($dict, $palavra, @ar) = @_;
Subroutine eagles: not called CC=1 SCAR=6.9
Pod documented
591  
592 map {
593 0 my $fea = $_;
594 0 map { $_ . ":$fea->{rad}" } Lingua::Jspell::EAGLES::_cat2eagles(%$fea)
595 } $dict->fea($palavra, @ar);
596 }
597  
598 # NOTA: Esta funcao é específica da língua TUGA!
599 sub _cat2small {
600 0 ▶ my %b = @_;
Subroutine _cat2small: not called CC=73 SCAR=85.9
601 # no warnings;
602  
603 0 ▶ $b{CAT} ||= "HEY!";
Condition0%0/2
$b{"CAT"} ||= "HEY!"
l!l
00
MC/DC0%0/1
$b{"CAT"} ||= "HEY!"
$b{"CAT"}
Truth table0%0/2
A
A= $b{"CAT"}
Aresult
11
00
604 0 ▶ $b{G} ||= "";
Condition0%0/2
$b{"G"} ||= ""
l!l
00
MC/DC0%0/1
$b{"G"} ||= ""
$b{"G"}
Truth table0%0/2
A
A= $b{"G"}
Aresult
11
00
605 0 ▶ $b{N} ||= "";
Condition0%0/2
$b{"N"} ||= ""
l!l
00
MC/DC0%0/1
$b{"N"} ||= ""
$b{"N"}
Truth table0%0/2
A
A= $b{"N"}
Aresult
11
00
606 0 ▶ $b{P} ||= "";
Condition0%0/2
$b{"P"} ||= ""
l!l
00
MC/DC0%0/1
$b{"P"} ||= ""
$b{"P"}
Truth table0%0/2
A
A= $b{"P"}
Aresult
11
00
607 0 ▶ $b{T} ||= "";
Condition0%0/2
$b{"T"} ||= ""
l!l
00
MC/DC0%0/1
$b{"T"} ||= ""
$b{"T"}
Truth table0%0/2
A
A= $b{"T"}
Aresult
11
00
608  
609 0 ▶ if ($b{CAT} eq 'art') {
Branch: 15 branches
BranchTrueFalseTotal
if ($b{'CAT'} eq 'art') { } 0 0 0
elsif ($b{'CAT'} eq 'card') { } 0 0 0
elsif ($b{'CAT'} eq 'nord') { } 0 0 0
elsif ($b{'CAT'} eq 'ppes' or $b{'CAT'} eq 'prel' or $b{'CAT'} eq 'ppos' or $b{'CAT'} eq 'pdem' or $b{'CAT'} eq 'pind' or $b{'CAT'} eq 'pint') { } 0 0 0
elsif ($b{'CAT'} eq 'nc') { } 0 0 0
elsif ($b{'CAT'} eq 'np') { } 0 0 0
elsif ($b{'CAT'} eq 'adj') { } 0 0 0
elsif ($b{'CAT'} eq 'a_nc') { } 0 0 0
elsif ($b{'CAT'} eq 'v') { } 0 0 0
elsif ($b{'CAT'} eq 'prep') { } 0 0 0
elsif ($b{'CAT'} eq 'adv') { } 0 0 0
elsif ($b{'CAT'} eq 'con') { } 0 0 0
elsif ($b{'CAT'} eq 'in') { } 0 0 0
elsif ($b{'CAT'} =~ /^cp(.*)/l) { } 0 0 0
elsif (defined $b{'CAT'} and $b{'CAT'} eq '') { } 0 0 0
610 # Artigos: o léxico já prevê todos...
611 # por isso, NUNCA SE DEVE CHEGAR AQUI!!!
612 0 return "ART";
613 # 16 tags
614  
615 } elsif ($b{CAT} eq 'card') {
616 # Numerais cardinais:
617 0 return "DNCNP";
618 # o léxico já prevê os que flectem (1 e 2); o resto é tudo neutro plural.
619  
620 } elsif ($b{CAT} eq 'nord') {
621 # Numerais ordinais:
622 0 return "\UDNO$b{G}$b{N}";
623  
624 ▶ } elsif ($b{CAT} eq 'ppes' || $b{CAT} eq 'prel' ||
Condition0%0/15
$b{"CAT"} eq "ppes" or $b{"CAT"} eq "prel" or $b{"CAT"} eq "ppos" or $b{"CAT"} eq "pdem" or $b{"CAT"} eq "pind" or $b{"CAT"} eq "pint"
l!l&&r!l&&!r
000
$b{"CAT"} eq "ppes" or $b{"CAT"} eq "prel" or $b{"CAT"} eq "ppos" or $b{"CAT"} eq "pdem" or $b{"CAT"} eq "pind"
l!l&&r!l&&!r
000
$b{"CAT"} eq "ppes" or $b{"CAT"} eq "prel" or $b{"CAT"} eq "ppos" or $b{"CAT"} eq "pdem"
l!l&&r!l&&!r
000
$b{"CAT"} eq "ppes" or $b{"CAT"} eq "prel" or $b{"CAT"} eq "ppos"
l!l&&r!l&&!r
000
$b{"CAT"} eq "ppes" or $b{"CAT"} eq "prel"
l!l&&r!l&&!r
000
MC/DC0%0/6
$b{"CAT"} eq "ppes" or $b{"CAT"} eq "prel" or $b{"CAT"} eq "ppos" or $b{"CAT"} eq "pdem" or $b{"CAT"} eq "pind" or $b{"CAT"} eq "pint"
$b{"CAT"} eq "ppes" $b{"CAT"} eq "prel" $b{"CAT"} eq "ppos" $b{"CAT"} eq "pdem" $b{"CAT"} eq "pind" $b{"CAT"} eq "pint"
Truth table0%0/7
A or B or C or D or E or F
A= $b{"CAT"} eq "ppes"B= $b{"CAT"} eq "prel"C= $b{"CAT"} eq "ppos"D= $b{"CAT"} eq "pdem"E= $b{"CAT"} eq "pind"F= $b{"CAT"} eq "pint"
ABCDEFresult
1XXXXX1
01XXXX1
001XXX1
0001XX1
00001X1
0000011
0000000
625 $b{CAT} eq 'ppos' || $b{CAT} eq 'pdem' ||
626 $b{CAT} eq 'pind' || $b{CAT} eq 'pint') {
627 # Pronomes:
628 0 ▶ if ($b{CAT} eq 'ppes') {
Branch: 6 branches
BranchTrueFalseTotal
if ($b{'CAT'} eq 'ppes') { } 0 0 0
elsif ($b{'CAT'} eq 'prel') { } 0 0 0
elsif ($b{'CAT'} eq 'ppos') { } 0 0 0
elsif ($b{'CAT'} eq 'pdem') { } 0 0 0
elsif ($b{'CAT'} eq 'pint') { } 0 0 0
elsif ($b{'CAT'} eq 'pind') { } 0 0 0
629 # Pronomes pessoais
630 0 $b{CAT} = 'PS';
631 } elsif ($b{CAT} eq 'prel') {
632 # Pronomes relativos
633 0 $b{CAT} = 'PR';
634 } elsif ($b{CAT} eq 'ppos') {
635 # Pronomes possessivos
636 0 $b{CAT} = 'PP';
637 } elsif ($b{CAT} eq 'pdem') {
638 # Pronomes demonstrativos
639 0 $b{CAT} = 'PD';
640 } elsif ($b{CAT} eq 'pint') {
641 # Pronomes interrogativos
642 0 $b{CAT} = 'PI';
643 } elsif ($b{CAT} eq 'pind') {
644 # Pronomes indefinidos
645 0 $b{CAT} = 'PF';
646 }
647  
648 0 ▶ $b{G} = 'N' if $b{G} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq "_" 0 0 0
649 0 ▶ $b{N} = 'N' if $b{N} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"N"} eq "_" 0 0 0
650  
651 # $b{C} esta por inicializar... oops!? vou por como C para já
652 0 $b{C} = "C";
653 0 return "\U$b{CAT}$b{'C'}$b{G}$b{'P'}$b{N}";
654 # $b{'C'}: caso latino.
655  
656 } elsif ($b{CAT} eq 'nc') {
657 # Nomes comuns:
658 0 ▶ $b{G} = 'N' if $b{G} eq '_' || $b{G} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq "_" or $b{"G"} eq "" 0 0 0
Condition0%0/3
$b{"G"} eq "_" or $b{"G"} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$b{"G"} eq "_" or $b{"G"} eq ""
$b{"G"} eq "_" $b{"G"} eq ""
Truth table0%0/3
A or B
A= $b{"G"} eq "_"B= $b{"G"} eq ""
ABresult
1X1
011
000
659 0 ▶ $b{N} = 'N' if $b{N} eq '_' || $b{N} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"N"} eq "_" or $b{"N"} eq "" 0 0 0
Condition0%0/3
$b{"N"} eq "_" or $b{"N"} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$b{"N"} eq "_" or $b{"N"} eq ""
$b{"N"} eq "_" $b{"N"} eq ""
Truth table0%0/3
A or B
A= $b{"N"} eq "_"B= $b{"N"} eq ""
ABresult
1X1
011
000
660 0 ▶ $b{GR} ||= '' ;
Condition0%0/2
$b{"GR"} ||= ""
l!l
00
MC/DC0%0/1
$b{"GR"} ||= ""
$b{"GR"}
Truth table0%0/2
A
A= $b{"GR"}
Aresult
11
00
661 0 ▶ $b{GR}= 'd' if $b{GR} eq 'dim';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"GR"} eq "dim" 0 0 0
662 0 return "\U$b{CAT}$b{G}$b{N}$b{GR}";
663  
664 } elsif ($b{CAT} eq 'np') {
665 # Nomes próprios:
666 0 ▶ $b{G} = 'N' if $b{G} eq '_' || $b{G} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq "_" or $b{"G"} eq "" 0 0 0
Condition0%0/3
$b{"G"} eq "_" or $b{"G"} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$b{"G"} eq "_" or $b{"G"} eq ""
$b{"G"} eq "_" $b{"G"} eq ""
Truth table0%0/3
A or B
A= $b{"G"} eq "_"B= $b{"G"} eq ""
ABresult
1X1
011
000
667 0 ▶ $b{N} = 'N' if $b{N} eq '_' || $b{N} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"N"} eq "_" or $b{"N"} eq "" 0 0 0
Condition0%0/3
$b{"N"} eq "_" or $b{"N"} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
$b{"N"} eq "_" or $b{"N"} eq ""
$b{"N"} eq "_" $b{"N"} eq ""
Truth table0%0/3
A or B
A= $b{"N"} eq "_"B= $b{"N"} eq ""
ABresult
1X1
011
000
668 0 return "\U$b{CAT}$b{G}$b{N}";
669  
670 } elsif ($b{CAT} eq 'adj') {
671 # Adjectivos:
672 0 ▶ $b{G} = 'N' if $b{G} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq "_" 0 0 0
673 0 ▶ $b{G} = 'N' if $b{G} eq '2';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq 2 0 0 0
674 0 ▶ $b{N} = 'N' if $b{N} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"N"} eq "_" 0 0 0
675 0 ▶ $b{GR} ||= '' ;
Condition0%0/2
$b{"GR"} ||= ""
l!l
00
MC/DC0%0/1
$b{"GR"} ||= ""
$b{"GR"}
Truth table0%0/2
A
A= $b{"GR"}
Aresult
11
00
676 0 ▶ $b{GR} = 'd' if $b{GR} eq 'dim';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"GR"} eq "dim" 0 0 0
677 # elsif ($b{N} eq ''){
678 # $b{N} = 'N';
679 # }
680 0 return "\UJ$b{G}$b{N}$b{GR}";
681  
682 } elsif ($b{CAT} eq 'a_nc') {
683 # Adjectivos que podem funcionar como nomes comuns:
684 0 ▶ $b{G} = 'N' if $b{G} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq "_" 0 0 0
685 0 ▶ $b{G} = 'N' if $b{G} eq '2';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq 2 0 0 0
686 0 ▶ $b{N} = 'N' if $b{N} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"N"} eq "_" 0 0 0
687 0 ▶ $b{GR} ||= '' ;
Condition0%0/2
$b{"GR"} ||= ""
l!l
00
MC/DC0%0/1
$b{"GR"} ||= ""
$b{"GR"}
Truth table0%0/2
A
A= $b{"GR"}
Aresult
11
00
688 0 ▶ $b{GR} = 'd' if $b{GR} eq 'dim';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"GR"} eq "dim" 0 0 0
689 # elsif ($b{N} eq ''){
690 # $b{N} = 'N';
691 # }
692 0 return "\UX$b{G}$b{N}$b{GR}";
693  
694 } elsif ($b{CAT} eq 'v') {
695 # Verbos:
696  
697 # formas nominais:
698 0 ▶ if ($b{T} eq 'inf') {
Branch: 14 branches
BranchTrueFalseTotal
if ($b{'T'} eq 'inf') { } 0 0 0
elsif ($b{'T'} eq 'ppa') { } 0 0 0
elsif ($b{'T'} eq 'g') { } 0 0 0
elsif ($b{'T'} eq 'p') { } 0 0 0
elsif ($b{'T'} eq 'pp') { } 0 0 0
elsif ($b{'T'} eq 'pi') { } 0 0 0
elsif ($b{'T'} eq 'pmp') { } 0 0 0
elsif ($b{'T'} eq 'f') { } 0 0 0
elsif ($b{'T'} eq 'pc') { } 0 0 0
elsif ($b{'T'} eq 'pic') { } 0 0 0
elsif ($b{'T'} eq 'fc') { } 0 0 0
elsif ($b{'T'} eq 'i') { } 0 0 0
elsif ($b{'T'} eq 'c') { } 0 0 0
elsif ($b{'T'} eq 'ip') { } 0 0 0
699 # infinitivo impessoal
700 0 $b{T} = 'N';
701  
702 } elsif ($b{T} eq 'ppa') {
703 # Particípio Passado
704 0 $b{T} = 'PP';
705  
706 } elsif ($b{T} eq 'g') {
707 # Gerúndio
708 0 $b{T} = 'G';
709  
710 } elsif ($b{T} eq 'p') {
711 # modo indicativo: presente (Hoje)
712 0 $b{T} = 'IH';
713  
714 } elsif ($b{T} eq 'pp') {
715 # modo indicativo: pretérito Perfeito
716 0 $b{T} = 'IP';
717  
718 } elsif ($b{T} eq 'pi') {
719 # modo indicativo: pretérito Imperfeito
720 0 $b{T} = 'II';
721  
722 } elsif ($b{T} eq 'pmp') {
723 # modo indicativo: pretérito Mais-que-perfeito
724 0 $b{T} = 'IM';
725  
726 } elsif ($b{T} eq 'f') {
727 # modo indicativo: Futuro
728 0 $b{T} = 'IF';
729  
730 } elsif ($b{T} eq 'pc') {
731 # modo conjuntivo (Se): presente (Hoje)
732 0 $b{T} = 'SH';
733  
734 } elsif ($b{T} eq 'pic') {
735 # modo conjuntivo (Se): pretérito Imperfeito
736 0 $b{T} = 'SI';
737  
738 } elsif ($b{T} eq 'fc') {
739 # modo conjuntivo (Se): Futuro
740 0 $b{T} = 'PI';
741  
742 } elsif ($b{T} eq 'i') {
743 # modo Imperativo: presente (Hoje)
744 0 $b{T} = 'MH';
745  
746 } elsif ($b{T} eq 'c') {
747 # modo Condicional: presente (Hoje)
748 0 $b{T} = 'CH';
749  
750 } elsif ($b{T} eq 'ip') {
751 # modo Infinitivo (Pessoal ou Presente):
752 0 $b{T} = 'PI';
753  
754 # Futuro conjuntivo? Só se tiver um "se" antes! -> regras sintácticas...
755 # modo&tempo não previstos ainda...
756  
757 } else {
758 0 $b{T} = '_UNKNOWN';
759 }
760  
761 # converter 'P=1_3' em 'P=_': provisório(?)!
762 0 $b{P} = "";
763 0 ▶ $b{P} = '_' if $b{P} eq '1_3'; # único sítio com '_' como rhs!!!
Branch: 1 branch
BranchTrueFalseTotal
if $b{"P"} eq "1_3" 0 0 0
764  
765
766 0 ▶ if ($b{T} eq "vpp") { return "\U$b{CAT}$b{T}$b{G}$b{P}$b{N}"; }
Branch: 1 branch
BranchTrueFalseTotal
if ($b{'T'} eq 'vpp') { } 0 0 0
767 0 else { return "\U$b{CAT}$b{T}$b{P}$b{N}"; }
768  
769  
770 # Género, só para VPP.
771 # +/- 70 tags
772  
773 } elsif ($b{CAT} eq 'prep') {
774 # Preposições¹:
775 0 return "\UP";
776  
777 } elsif ($b{CAT} eq 'adv') {
778 # Advérbios²:
779 0 return "\UADV";
780  
781 } elsif ($b{CAT} eq 'con') {
782 # Conjunções²:
783 0 return "\UC";
784  
785 } elsif ($b{CAT} eq 'in') {
786 # Interjeições¹:
787 0 return "\UI";
788  
789 # ¹: não sei se a tag devia ser tão atómica, mas para já não há confusão!
790  
791 } elsif ($b{CAT} =~ m/^cp(.*)/) {
792 # Contracções¹:
793 0 ▶ $b{G} = 'N' if $b{G} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"G"} eq "_" 0 0 0
794 0 ▶ $b{N} = 'N' if $b{N} eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if $b{"N"} eq "_" 0 0 0
795 0 return "\U&$b{G}$b{N}";
796  
797 # ²: falta estruturar estes no próprio dicionário...
798 # Palavras do dicionário com categoria vazia ou sem categoria,
799 # palavras não existentes ou sequências aleatórias de caracteres:
800  
801 ▶ } elsif (defined($b{CAT}) && $b{CAT} eq '') {
Condition0%0/3
defined $b{"CAT"} and $b{"CAT"} eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $b{"CAT"} and $b{"CAT"} eq ""
defined $b{"CAT"} $b{"CAT"} eq ""
Truth table0%0/3
A and B
A= defined $b{"CAT"}B= $b{"CAT"} eq ""
ABresult
0X0
100
111
802 0 return "\UUNDEFINED";
803  
804 } else { # restantes categorias (...?)
805 0 return "\UUNTREATED";
806 }
807 }
808  
809 =head2 new_featags
810  
811 =cut
812  
813 sub new_featags {
814 0 ▶ my ($self, $word) = @_;
Subroutine new_featags: not called CC=2 SCAR=17.9
Pod documented
815 0 ▶ if (exists($self->{yaml}{META}{TAG})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'yaml'}{'META'}{'TAG'}) { } 0 0 0
816 0 my $rules = $self->{yaml}{META}{TAG};
817 0 return map { $self->_compact($rules, $_) } $self->fea($word);
818 } else {
819 0 warn "Dictionary without a YAML file, or without rules for fea-compression\n";
820 0 return undef;
821 }
822 }
823  
824 sub _compact {
825 0 ▶ my ($self,$rules, $fs) = @_;
Subroutine _compact: not called CC=10 SCAR=47.0
826 0 my $tag;
827 0 ▶ if (ref($rules) eq "HASH") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $rules eq 'HASH') { } 0 0 0
elsif (ref $rules eq 'ARRAY') { } 0 0 0
elsif (not ref $rules) { } 0 0 0
828 0 my ($key) = (%$rules);
829  
830 0 ▶ if (exists($fs->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fs->{$key}) { } 0 0 0
831 0 $tag = $self->_compact_id($key, $fs->{$key});
832 0 ▶ if (exists($rules->{$key}{$fs->{$key}})) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $rules->{$key}{$fs->{$key}}) { } 0 0 0
elsif (exists $rules->{$key}{'-'}) { } 0 0 0
833 0 $tag.$self->_compact($rules->{$key}{$fs->{$key}}, $fs);
834 }
835 elsif (exists($rules->{$key}{'-'})) {
836 0 $tag.$self->_compact($rules->{$key}{'-'}, $fs);
837 }
838 else {
839 0 $tag
840 }
841 }
842 else {
843 0 ""
844 }
845 }
846 elsif (ref($rules) eq "ARRAY") {
847 0 for my $cat (@$rules) {
848 0 $tag .= $self->_compact($cat, $fs);
849 }
850 $tag
851 0 }
852 elsif (!ref($rules)) {
853 0 ▶ if ($rules && exists($fs->{$rules})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rules and exists $fs->{$rules}) { } 0 0 0
Condition0%0/3
$rules and exists $fs->{$rules}
!ll&&!rl&&r
000
MC/DC0%0/2
$rules and exists $fs->{$rules}
$rules exists $fs->{$rules}
Truth table0%0/3
A and B
A= $rulesB= exists $fs->{$rules}
ABresult
0X0
100
111
854 0 $self->_compact_id($rules, $fs->{$rules})
855 } else {
856 0 ""
857 }
858 }
859 }
860  
861 sub _compact_id {
862 0 ▶ my ($self, $cat, $id) = @_;
Subroutine _compact_id: not called CC=2 SCAR=17.9
863 0 ▶ if (exists($self->{yaml}{"$cat-TAG"}{$id})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'yaml'}{"$cat-TAG"}{$id}) { } 0 0 0
864 0 return $self->{yaml}{"$cat-TAG"}{$id}
865 } else {
866 0 return $id
867 }
868 }
869  
870  
871 =head2 featags
872  
873 Given a word, returns a set of analysis. Each analysis is a morphosintatic tag
874  
875 @l= $pt->featags("lindas")
876 JFS , ...
877 @l= $pt->featags("era",{CAT=>"v"}) ## with a constraint
878  
879  
880 =cut
881  
882 sub featags{
883 0 ▶ my ($self, $palavra,@Ar) = @_;
Subroutine featags: not called CC=1 SCAR=6.9
Pod documented
884 0 return (map {_cat2small(%$_)} ($self->fea($palavra,@Ar)));
885 }
886  
887 =head2 featagsrad
888  
889 Given a word, returns a set of analysis. Each analysis is a morphosintatic tag
890 and the lemma information
891  
892 @l= $pt->featagsrad("lindas")
893 JFS:lindo , ...
894 @l= $pt->featagsrad("era",{CAT=>"v"}) ## with a constraint
895  
896 =cut
897  
898 sub featagsrad{
899 0 ▶ my ($self, $palavra,@Ar) = @_;
Subroutine featagsrad: not called CC=1 SCAR=6.9
Pod documented
900  
901 0 return (map {_cat2small(%$_).":$_->{rad}"} ($self->fea($palavra,@Ar)));
902 }
903  
904  
905 =head2 onethatverif
906  
907 Given a pattern feature structure and a list of analysis (feature
908 structures), returns a true value is there is one analysis that
909 verifies the pattern.
910  
911 # onethatverif( cond:fs , conj:fs-set) :: bool
912 # exists x in conj: verif(cond , x)
913  
914 if(onethatverif({CAT=>"adj"},$pt->fea("linda"))) {
915 ...
916 }
917  
918 =cut
919  
920 sub onethatverif {
921 0 ▶ my ($a, @b) = @_;
Subroutine onethatverif: not called CC=3 SCAR=24.8
Pod documented
922 0 for (@b) {
923 0 ▶ return 1 if verif($a,$_);
Branch: 1 branch
BranchTrueFalseTotal
if verif($a, $_) 0 0 0
924 }
925 0 return 0 ;
926 }
927  
928 =head2 mkradtxt
929  
930 =cut
931  
932 sub mkradtxt {
933 0 ▶ my ($self, $f1, $f2) = @_;
Subroutine mkradtxt: not called CC=6 SCAR=37.4
Pod documented
934 0 local $.;
935 0 ▶ open F1, $f1 or die "Can't open '$f1'\n";
Branch: 1 branch
BranchTrueFalseTotal
open F1, $f1 or die "Can't open '${f1}'\n" 0 0 0
936 0 ▶ open F2, "> $f2" or die "Can't create '$f2'\n";
Branch: 1 branch
BranchTrueFalseTotal
open F2, "> $f2" or die "Can't create '${f2}'\n" 0 0 0
937 0 ▶ while(<F1>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F1)) 0 0 0
938 0 chomp;
939 0 print F2 "$_$DELIM";
940 0 ▶ while (/((\w|-)+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if (/((\w|-)+)/gl) 0 0 0
941 0 ▶ print F2 " ",join(" ",$self->rad($1)) unless $STOP{$1}
Branch: 1 branch
BranchTrueFalseTotal
unless $Lingua::Jspell::STOP{$1} 0 0 0
942 }
943 0 print F2 "\n";
944 }
945 0 close F1;
946 0 close F2;
947 }
948  
949 =head2 isguess
950  
951 Lingua::Jspell::isguess(@ana)
952  
953 returns True if list of analisys are near
954 misses (unknown attribut is 1).
955  
956 =cut
957  
958 sub isguess{
959 0 ▶ my @a=@_;
Subroutine isguess: not called CC=2 SCAR=17.9
Pod documented
960 0 ▶ return @a && $a[0]{unknown};
Condition0%0/3
@a && $a[0]{'unknown'}
!ll&&!rl&&r
000
MC/DC0%0/2
@a && $a[0]{'unknown'}
@a $a[0]{'unknown'}
Truth table0%0/3
A && B
A= @aB= $a[0]{'unknown'}
ABresult
0X0
100
111
961 }
962  
963 =head2 any2str
964  
965 Lingua::Jspell::any2str($ref)
966 Lingua::Jspell::any2str($ref,$indentation)
967 Lingua::Jspell::any2str($ref,"compact")
968  
969 =cut
970  
971 sub any2str {
972 0 ▶ my ($r, $i) = @_;
Subroutine any2str: not called CC=13 SCAR=52.0
Pod documented
973 0 ▶ $i ||= 0;
Condition0%0/2
$i ||= 0
l!l
00
MC/DC0%0/1
$i ||= 0
$i
Truth table0%0/2
A
A= $i
Aresult
11
00
974 0 ▶ if (not $r) {return ""}
Branch: 1 branch
BranchTrueFalseTotal
unless ($r) 0 0 0
975 0 ▶ if (ref $i) { any2str([@_]);}
Branch: 3 branches
BranchTrueFalseTotal
if (ref $i) { } 0 0 0
elsif ($i eq 'compact') { } 0 0 0
elsif ($i eq 'f1') { } 0 0 0
976 elsif ($i eq "compact") {
977 0 ▶ if (ref($r) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'HASH') { } 0 0 0
elsif (ref $r eq 'ARRAY') { } 0 0 0
978 0 return "{". hash2str($r,$i) . "}"
979 } elsif (ref($r) eq "ARRAY") {
980 0 return "[" . join(",", map (any2str($_,$i), @$r)) . "]"
981 } else {
982 0 return "$r"
983 }
984 } elsif ($i eq "f1") {
985 0 ▶ if (ref($r) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'HASH') { } 0 0 0
elsif (ref $r eq 'ARRAY') { } 0 0 0
986 0 return "{". hash2str($r,"f1") . "}"
987 } elsif (ref($r) eq "ARRAY") {
988 0 return "[ " . join(" ,\n ", map (any2str($_,"compact"), @$r)) . "]"
989 } else {
990 0 return "$r"
991 }
992 } else {
993 0 ▶ my $ind = ($i >= 0)? (" " x $i) : "";
Branch: 1 branch
BranchTrueFalseTotal
$i >= 0 ? : 0 0 0
994 0 ▶ if (ref($r) eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $r eq 'HASH') { } 0 0 0
elsif (ref $r eq 'ARRAY') { } 0 0 0
995 0 return "$ind {". hash2str($r,abs($i)+3) . "}"
996 } elsif (ref($r) eq "ARRAY") {
997 0 return "$ind [\n" . join("\n", map (any2str($_,abs($i)+3), @$r)) . "]"
998 } else {
999 0 return "$ind$r"
1000 }
1001 }
1002 }
1003  
1004 =head2 hash2str
1005  
1006 =cut
1007  
1008 sub hash2str {
1009 0 ▶ my ($r, $i) = @_;
Subroutine hash2str: not called CC=6 SCAR=37.4
Pod documented
1010 0 my $c = "";
1011 0 ▶ if ($i eq "compact") {
Branch: 2 branches
BranchTrueFalseTotal
if ($i eq 'compact') { } 0 0 0
elsif ($i eq 'f1') { } 0 0 0
1012 0 for (keys %$r) {
1013 0 $c .= any2str($_,$i). "=". any2str($r->{$_},$i). ",";
1014 }
1015 0 chop($c);
1016 } elsif ($i eq "f1") {
1017 0 for (keys %$r) {
1018 0 $c .= "\n ", any2str($_,"compact"). "=". any2str($r->{$_},"compact"). "\n";
1019 }
1020 0 chop($c);
1021 } else {
1022 0 for (keys %$r) {
1023 0 $c .= "\n". any2str($_,$i). " => ". any2str($r->{$_},-$i);
1024 }
1025 }
1026 0 return $c;
1027 }
1028  
1029 =head1 AUTHOR
1030  
1031 Jose Joao Almeida, C<< <jj@di.uminho.pt> >>
1032 Alberto Simões, C<< <ambs@di.uminho.pt> >>
1033  
1034 =head1 BUGS
1035  
1036 Please report any bugs or feature requests to
1037 C<bug-lingua-jspell@rt.cpan.org>, or through the web interface at
1038 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Lingua-Jspell>. I
1039 will be notified, and then you'll automatically be notified of
1040 progress on your bug as I make changes.
1041  
1042 =head1 COPYRIGHT & LICENSE
1043  
1044 Copyright 2007-2009 Projecto Natura
1045  
1046 This program is free software; licensed under GPL.
1047  
1048 =cut
1049  
1050 sub _yaml_file {
1051 0 ▶ my $dic_file = shift;
Subroutine _yaml_file: not called CC=2 SCAR=17.9
1052 0 ▶ if ($dic_file =~ m!\.hash$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dic_file =~ /\.hash$/l) { } 0 0 0
1053 # we have a local dictionary
1054 0 $dic_file =~ s/\.hash/.yaml/;
1055 } else {
1056 0 $dic_file = "$JSPELLLIB/$dic_file.yaml"
1057 }
1058 0 return $dic_file;
1059 }
1060  
1061 sub _mode {
1062 0 ▶ my $m = shift;
Subroutine _mode: not called CC=7 SCAR=40.3
1063 0 my $r="";
1064 0 ▶ if ($m->{nm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"nm"}) 0 0 0
1065 0 ▶ if ($m->{nm} eq "af") ### af = GPy --> Gym
Branch: 4 branches
BranchTrueFalseTotal
if ($m->{'nm'} eq 'af') { } 0 0 0
elsif ($m->{'nm'} eq 'full') { } 0 0 0
elsif ($m->{'nm'} eq 'cc') { } 0 0 0
elsif ($m->{'nm'} eq 'off') { } 0 0 0
1066 0 { $r .= "\$G\n\$m\n\$y\n" }
1067 elsif ($m->{nm} eq "full") ### full = GYm
1068 0 { $r .= "\$G\n\$Y\n\$m\n" }
1069 elsif ($m->{nm} eq "cc") ### cc = GPY
1070 0 { $r .= "\$G\n\$P\n\$Y\n" }
1071 elsif ($m->{nm} eq "off") ### off = gPy
1072 0 { $r .= "\$g\n\$P\n\$y\n" }
1073 else {}
1074 }
1075 0 ▶ if ($m->{flags}) {$r .= "\$z\n"}
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{'flags'}) { } 0 0 0
1076 0 else {$r .= "\$Z\n"}
1077 0 return $r;
1078 }
1079  
1080  
1081 sub _irr_file {
1082 0 ▶ my $irr_file = shift;
Subroutine _irr_file: not called CC=2 SCAR=17.9
1083 0 ▶ if ($irr_file =~ m!\.hash$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irr_file =~ /\.hash$/l) { } 0 0 0
1084 # we have a local dictionary
1085 0 $irr_file =~ s/\.hash/.irr/;
1086 } else {
1087 0 $irr_file = "$JSPELLLIB/$irr_file.irr"
1088 }
1089 0 return $irr_file;
1090 }
1091  
1092  
1093  
1094  
1095 2 '\o/ yay!'; # End of Lingua::Jspell
1096  
1097 __END__
↑ Summary blib/lib/Lingua/Jspell/DictManager.pm »