PML/FSFormat.pm

filter: Statementstmt 54.0% 127 / 235 Branchbran 36.8% 45 / 122 Conditioncond 24.3% 10 / 41 MC/DCmcdc 8.3% 2 / 24 Subroutinesub 51.2% 20 / 39 Podpod 93.9% 31 / 33 totaltotal 47.5% 235 / 494 CC 80 SCAR 27.1
CC
80
Cov
46%
isHidden
56.0
addNewAttribute
30.0
color
30.0
CRAP 15.1
« PML/Schema/XMLNode.pm ↑ Summary PML/Backend/FS.pm »
1  
2 ############################################################
3 #
4 # FS Format
5 # =========
6 #
7 #
8  
9 package Treex::PML::FSFormat;
10 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 $VERSION='2.29'; # version template
16 }
17 9 ▶use UNIVERSAL::DOES;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 my $attr_name_re='[^\\\\ \\n\\r\\t{}(),=|]+';
20  
21 # this is extendible
22 9 our $SpecialTypes='WNVH';
23 9 our %Specials = (sentord => 'W', order => 'N', value => 'V', hide => 'H');
24  
25 =head1 NAME
26  
27 Treex::PML::FSFormat - Treex::PML class representing the file header of a FS file.
28  
29 =over 4
30  
31 =item Treex::PML::FSFormat->new (array_ref_or_GLOB)
32  
33 NOTE: Don't call this constructor directly, use
34 Treex::PML::Factory->createFSFormat() instead!
35  
36 Create a new FS format instance object by parsing a given input as a
37 FS file header. If the argument is an ARRAY reference, each element is
38 assumed to represent a single line.
39  
40 =item Treex::PML::FSFormat->new (attributes_hash_ref?, ordered_names_list_ref?, unparsed_header?)
41  
42 NOTE: Don't call this constructor directly, use
43 Treex::PML::Factory->createFSFormat() instead!
44  
45 Create a new FS format instance object and C<initialize> it with the
46 optional values.
47  
48 =cut
49  
50 27 ▶sub new {
Subroutine new: called CC=5 SCAR=19.8
Pod documented
51 27 my $self = shift;
52 27 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
0270
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
53 27 my $new = [];
54 27 bless $new, $class;
55 27 ▶ if (@_==1 and ref($_[0]) and !UNIVERSAL::isa($_[0],'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and not UNIVERSAL::isa($_[0], 'HASH')) { } 0 27 27
Condition33%2/6
@_ == 1 and ref $_[0] and not UNIVERSAL::isa($_[0], "HASH")
!ll&&!rl&&r
2700
@_ == 1 and ref $_[0]
!ll&&!rl&&r
2700
MC/DC0%0/3
@_ == 1 and ref $_[0] and not UNIVERSAL::isa($_[0], "HASH")
@_ == 1 ref $_[0] not UNIVERSAL::isa($_[0], "HASH")
Truth table25%1/4
A and B and not(C)
A= @_ == 1B= ref $_[0]C= not UNIVERSAL::isa($_[0], "HASH")
ABCresult
0XX0
10X0
1100
1111
56 0 $new->initialize();
57 0 $new->readFrom(@_);
58 } else {
59 27 $new->initialize(@_);
60 }
61 27 return $new;
62 }
63  
64 =item Treex::PML::FSFormat->create (@header)
65  
66 NOTE: Don't call this constructor directly, use
67 Treex::PML::Factory->createFSFormat() instead!
68  
69 Same as Treex::PML::FSFormat->new (\@header).
70  
71 =cut
72  
73 0 ▶sub create {
Subroutine create: not called CC=1 SCAR=6.9
Pod documented
74 0 my $self = shift;
75 0 my $new=$self->new();
76 0 $new->readFrom([@_]);
77 0 return $new;
78 }
79  
80 =item $format->clone
81  
82 Duplicate FS format instance object.
83  
84 =cut
85  
86 0 ▶sub clone {
Subroutine clone: not called CC=2 SCAR=17.9
Pod documented
87 0 my ($self) = @_;
88 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
89 return $self->new(
90 0 {%{$self->defs()}},
91 [$self->attributes()],
92 0 [@{$self->unparsed()}],
93 undef, # specials
94 );
95 }
96  
97  
98 =pod
99  
100 =item $format->initialize (attributes_hash_ref?, ordered_names_list_ref?, unparsed_header?)
101  
102 Initialize a new FS format instance with given values. See L<Treex::PML>
103 for more information about attribute hash, ordered names list and unparsed headers.
104  
105 =cut
106  
107 27 ▶sub initialize {
Subroutine initialize: called CC=6 SCAR=19.3
Pod documented
108 27 my $self = $_[0];
109 27 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 27 27
110  
111 27 ▶ $self->[0] = ref($_[1]) ? $_[1] : { }; # attribs (hash)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 27 27
112 27 ▶ $self->[1] = ref($_[2]) ? $_[2] : [ ]; # atord (sorted array)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[2] ? : 0 27 27
113 27 ▶ $self->[2] = ref($_[3]) ? $_[3] : [ ]; # unparsed (sorted array)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[3] ? : 0 27 27
114 27 ▶ $self->[3] = ref($_[4]) ? $_[4] : undef; # specials
Branch: 1 branch
BranchTrueFalseTotal
ref $_[4] ? : 0 27 27
115 27 return $self;
116 }
117  
118 =pod
119  
120 =item $format->addNewAttribute (type, colour, name, list)
121  
122 Adds a new attribute definition to the Treex::PML::FSFormat. Type must be one of
123 the letters [KPOVNWLH], colour one of characters [A-Z0-9]. If the type
124 is L, the fourth parameter is a string containing a list of possible
125 values separated by |.
126  
127 =cut
128  
129 0 ▶sub addNewAttribute {
Subroutine addNewAttribute: not called CC=5 SCAR=34.0
Pod documented
130 0 my ($self,$type,$color,$name,$list)=@_;
131 0 ▶ $self->list->[$self->count()]=$name if (!defined($self->defs->{$name}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->defs->{$name} 0 0 0
132 0 ▶ if (index($SpecialTypes, $type)+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($Treex::PML::FSFormat::SpecialTypes, $type) + 1) 0 0 0
133 0 $self->set_special($type,$name);
134 }
135 0 ▶ if ($list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list) { } 0 0 0
136 0 $self->defs->{$name}.=" $type=$list"; # so we create a list of defchars separated by spaces
137 } else { # a value-list may follow the equation mark
138 0 $self->defs->{$name}.=" $type";
139 }
140 0 ▶ if ($color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($color) 0 0 0
141 0 $self->defs->{$name}.=" $color"; # we add a special defchar for color
142 }
143 }
144  
145 =pod
146  
147 =item $format->readFrom (source,output?)
148  
149 Reads FS format instance definition from given source, optionally
150 echoing the unparsed input on the given output. The obligatory
151 argument C<source> must be either a GLOB or list reference.
152 Argument C<output> is optional and if given, it must be a GLOB reference.
153  
154 =cut
155  
156 2 ▶sub readFrom {
Subroutine readFrom: called CC=14 SCAR=30.9
Pod documented
157 2 my ($self,$handle,$out) = @_;
158 2 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 2 2
159 2 require Treex::PML::Backend::FS;
160 2 my $read = \&Treex::PML::Backend::FS::ReadEscapedLine;
161 2 my %result;
162 2 my $count=0;
163 2 local $_;
164 2 ▶ while ($_=$read->($handle)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ = &$read($handle)) 114 0 114
165 114 s/\r$//o;
166 114 ▶ if (ref($out)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $out) { } 0 114 114
167 0 print $out $_;
168 } else {
169 114 push @{$self->unparsed}, $_;
170 }
171 114 ▶ if (/^\@([KPOVNWLHE])([A-Z0-9])* (${attr_name_re})(?:\|(.*))?/o) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\@([KPOVNWLHE])([A-Z0-9])* ($attr_name_re)(?:\|(.*))?/o) { } 112 2 114
elsif (/^\r*$/o) { } 2 0 2
172 112 ▶ if ($1 eq 'E') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "E") 0 112 112
173 0 ▶ unless (defined $self->special('E')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->special('E')) { } 0 0 0
174 0 $self->set_special('E',$3);
175 0 ▶ if (ref($handle) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handle ne "ARRAY") 0 0 0
176 0 binmode $handle, ':raw:perlio:encoding('.$3.')';
177 0 ▶ if ($count>0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count > 0) 0 0 0
178 0 warn "\@E should be on the first line!\n";
179 }
180 }
181 } else {
182 0 warn __PACKAGE__.": There should be just one encoding (\@E) and that should occur on the very first line. Ignoring $_!\n";
183 }
184 0 next;
185 }
186 112 ▶ if (index($SpecialTypes, $1)+1) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($Treex::PML::FSFormat::SpecialTypes, $1) + 1) 6 106 112
187 6 $self->set_special($1,$3);
188 }
189 112 ▶ $self->list->[$count++]=$3 if (!defined($self->defs->{$3}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->defs->{$3} 100 12 112
190 112 ▶ if ($4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($4) { } 2 110 112
191 2 $self->defs->{$3}.=" $1=$4"; # so we create a list of defchars separated by spaces
192 } else { # a value-list may follow the equation mark
193 110 $self->defs->{$3}.=" $1";
194 }
195 112 ▶ if ($2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2) 10 102 112
196 10 $self->defs->{$3}.=" $2"; # we add a special defchar for color
197 }
198 112 next;
199 } elsif (/^\r*$/o) {
200 2 last;
201 } else {
202 0 return 0;
203 }
204 }
205 2 return 1;
206 }
207  
208 =item $format->toArray
209  
210 Return FS declaration as an array of FS header declarations.
211  
212 =cut
213  
214 1 ▶sub toArray {
Subroutine toArray: called CC=10 SCAR=23.2
Pod documented
215 1 my ($self) = @_;
216 1 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
217 1 my $defs = $self->defs;
218 1 my @ad;
219 my @result;
220 1 my $l;
221 1 my $vals;
222 1 foreach (@{$self->list}) {
223 50 @ad=split ' ',$defs->{$_};
224 50 ▶ while (@ad) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ad) 56 50 106
225 56 $l='@';
226 56 ▶ if ($ad[0]=~/^L=(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ad[0] =~ /^L=(.*)/) { } 1 55 56
227 1 $vals=$1;
228 1 shift @ad;
229 1 $l.="L";
230 1 ▶ $l.=shift @ad if (@ad and $ad[0]=~/^[A0-3]/);
Branch: 1 branch
BranchTrueFalseTotal
if @ad and $ad[0] =~ /^[A0-3]/ 1 0 1
Condition33%1/3
@ad and $ad[0] =~ /^[A0-3]/
!ll&&!rl&&r
001
MC/DC0%0/2
@ad and $ad[0] =~ /^[A0-3]/
@ad $ad[0] =~ /^[A0-3]/
Truth table33%1/3
A and B
A= @adB= $ad[0] =~ /^[A0-3]/
ABresult
0X0
100
111
231 1 $l.=" $_|$vals\n";
232 } else {
233 55 ▶ $l.=shift @ad if @ad;
Branch: 1 branch
BranchTrueFalseTotal
if @ad 55 0 55
234 55 ▶ $l.=shift @ad if (@ad and $ad[0]=~/^[A0-3]/);
Branch: 1 branch
BranchTrueFalseTotal
if @ad and $ad[0] =~ /^[A0-3]/ 4 51 55
Condition100%3/3
@ad and $ad[0] =~ /^[A0-3]/
!ll&&!rl&&r
4654
MC/DC100%2/2
@ad and $ad[0] =~ /^[A0-3]/
@ad $ad[0] =~ /^[A0-3]/
Truth table100%3/3
A and B
A= @adB= $ad[0] =~ /^[A0-3]/
ABresult
0X0
100
111
235 55 $l.=" $_\n";
236 }
237 56 push @result, $l;
238 }
239 }
240 1 push @result,"\n";
241 1 return @result;
242 }
243  
244 =item $format->writeTo (glob_ref)
245  
246 Write FS declaration to a given file (file handle open for
247 reading must be passed as a GLOB reference).
248  
249 =cut
250  
251 1 ▶sub writeTo {
Subroutine writeTo: called CC=2 SCAR=7.0
Pod documented
252 1 my ($self,$fileref) = @_;
253 1 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 1 1
254 1 print $fileref $self->toArray;
255 1 return 1;
256 }
257  
258  
259 =pod
260  
261 =item $format->sentord (), order(), value(), hide()
262  
263 Return names of special attributes declared in FS format as @W, @N,
264 @V, @H respectively.
265  
266 =cut
267  
268 {
269 9 my ($sub, $key);
270 9 ▶ while (($sub,$key)= each %Specials) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Treex::PML::FSFormat::Specials) 36 9 45
271 0 ▶ eval "sub $sub { \$_[0]->special('$key'); }";
Subroutine hide: not called CC=1 SCAR=4.1
Subroutine order: not called CC=1 SCAR=4.1
Subroutine sentord: not called CC=1 SCAR=4.1
Subroutine value: not called CC=1 SCAR=4.1
Pod documented
Pod documented
Pod documented
Pod documented
272 }
273 }
274  
275 27 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
276 27 my ($self) = @_;
277 27 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 27 27
278 27 $self->[0]=undef;
279 27 $self->[1]=undef;
280 27 $self->[2]=undef;
281 27 $self=undef;
282 }
283  
284 =pod
285  
286 =item $format->isHidden (node)
287  
288 Return the lowest ancestor-or-self of the given node whose value of
289 the FS attribute declared as @H is either C<'hide'> or 1. Return
290 undef, if no such node exists.
291  
292 =cut
293  
294 0 ▶sub isHidden {
Subroutine isHidden: not called CC=7 SCAR=40.3
Pod documented
295 # Tests if given node is hidden or not
296 # Returns the ancesor that hides it or undef
297 0 my ($self,$node)=@_;
298 0 my $hide=$self->special('H');
299 0 ▶ return unless defined $hide;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hide 0 0 0
300 0 my $h;
301 0 ▶ while ($node and !(($h = $node->get_member($hide)) eq 'hide'
Branch: 1 branch
BranchTrueFalseTotal
if ($node and not +($h = $node->get_member($hide)) eq "hide" || $h eq "true" || $h == 1) 0 0 0
Condition0%0/9
$node and not +($h = $node->get_member($hide)) eq "hide" || $h eq "true" || $h == 1
!ll&&!rl&&r
000
($h = $node->get_member($hide)) eq 'hide' || $h eq 'true' || $h == 1
l!l&&r!l&&!r
000
($h = $node->get_member($hide)) eq "hide" or $h eq "true"
l!l&&r!l&&!r
000
MC/DC0%0/4
$node and not +($h = $node->get_member($hide)) eq "hide" || $h eq "true" || $h == 1
$node ($h = $node->get_member($hide)) eq "hide" $h eq "true" $h == 1
Truth table0%0/5
A and not(B or C || D)
A= $nodeB= ($h = $node->get_member($hide)) eq "hide"C= $h eq "true"D= $h == 1
ABCDresult
0XXX0
11XX0
101X0
10010
10001
302 or $h eq 'true'
303 or $h == 1 )) {
304 0 $node=$node->parent;
305 }
306 0 ▶ return ($node||undef);
Condition0%0/2
$node || undef
l!l
00
MC/DC0%0/1
$node || undef
$node
Truth table0%0/2
A
A= $node
Aresult
11
00
307 }
308  
309 =pod
310  
311 =item $format->defs
312  
313 Return a reference to the internally stored attribute hash.
314  
315 =cut
316  
317 403 ▶sub defs {
Subroutine defs: called CC=2 SCAR=7.2
Pod documented
318 403 my ($self) = @_;
319 403 ▶ return ref($self) ? $self->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 403 0 403
320 }
321  
322 =pod
323  
324 =item $format->list
325  
326 Return a reference to the internally stored attribute names list.
327  
328 =cut
329  
330 147 ▶sub list {
Subroutine list: called CC=2 SCAR=7.2
Pod documented
331 147 my ($self) = @_;
332 147 ▶ return ref($self) ? $self->[1] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 147 0 147
333 }
334  
335 =pod
336  
337 =item $format->unparsed
338  
339 Return a reference to the internally stored unparsed FS header. Note,
340 that this header must B<not> correspond to the defs and attributes if
341 any changes are made to the definitions or names at run-time by hand.
342  
343 =cut
344  
345 114 ▶sub unparsed {
Subroutine unparsed: called CC=2 SCAR=7.2
Pod documented
346 114 my ($self) = @_;
347 114 ▶ return ref($self) ? $self->[2] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 114 0 114
348 }
349  
350  
351 =pod
352  
353 =item $format->renew_specials
354  
355 Refresh special attribute hash.
356  
357 =cut
358  
359 2 ▶sub renew_specials {
Subroutine renew_specials: called CC=3 SCAR=11.0
Pod documented
360 2 my ($self)=@_;
361 2 my $re = " ([$SpecialTypes])";
362 2 my %spec;
363 2 my $defs = $self->[0]; # defs
364 2 my ($k,$v);
365 2 ▶ while (($k,$v)=each %$defs) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$defs) 12 2 14
366 12 ▶ $spec{$1} = $k if $v=~/$re/o;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /$re/o 0 12 12
367 }
368 2 return $self->[3] = \%spec;
369 }
370  
371 # obsolete
372 0 ▶sub findSpecialDef {
Subroutine findSpecialDef: not called CC=3 SCAR=24.8
Pod undocumented
373 0 my ($self,$defchar)=@_;
374 0 my $defs = $self->defs;
375 0 foreach (keys %{$defs}) {
376 0 ▶ return $_ if (index($defs->{$_}," $defchar")>=0);
Branch: 1 branch
BranchTrueFalseTotal
if (index($defs->{$_}, " $defchar") != -1) 0 0 0
377 }
378 0 return undef; # we want an explicit undef here!!
379 }
380  
381 =item $format->specials
382  
383 Return a reference to a hash of attributes of special types. Keys
384 of the hash are special attribute types and values are their names.
385  
386 =cut
387  
388 0 ▶sub specials {
Subroutine specials: not called CC=2 SCAR=17.9
Pod documented
389 0 my ($self) = @_;
390 0 ▶ return ($self->[3] || $self->renew_specials());
Condition0%0/3
$self->[3] || $self->renew_specials
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->[3] || $self->renew_specials
$self->[3] $self->renew_specials
Truth table0%0/3
A || B
A= $self->[3]B= $self->renew_specials
ABresult
1X1
011
000
391 }
392  
393 =pod
394  
395 =item $format->attributes
396  
397 Return a list of all attribute names (in the order given by FS
398 instance declaration).
399  
400 =cut
401  
402 4 ▶sub attributes {
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
403 4 my ($self) = @_;
404 4 return @{$self->list};
405 }
406  
407 =pod
408  
409 =item $format->atno (n)
410  
411 Return the n'th attribute name (in the order given by FS
412 instance declaration).
413  
414 =cut
415  
416  
417 0 ▶sub atno {
Subroutine atno: not called CC=2 SCAR=17.9
Pod documented
418 0 my ($self,$index) = @_;
419 0 ▶ return ref($self) ? $self->list->[$index] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
420 }
421  
422 =pod
423  
424 =item $format->atdef (attribute_name)
425  
426 Return the definition string for the given attribute.
427  
428 =cut
429  
430 0 ▶sub atdef {
Subroutine atdef: not called CC=2 SCAR=17.9
Pod documented
431 0 my ($self,$name) = @_;
432 0 ▶ return ref($self) ? $self->defs->{$name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
433 }
434  
435 =pod
436  
437 =item $format->count
438  
439 Return the number of declared attributes.
440  
441 =cut
442  
443 0 ▶sub count {
Subroutine count: not called CC=2 SCAR=17.9
Pod documented
444 0 my ($self) = @_;
445 0 ▶ return ref($self) ? $#{$self->list}+1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
446 }
447  
448 =pod
449  
450 =item $format->isList (attribute_name)
451  
452 Return true if given attribute is assigned a list of all possible
453 values.
454  
455 =cut
456  
457 100 ▶sub isList {
Subroutine isList: called CC=2 SCAR=6.9
Pod documented
458 100 my ($self,$attrib)=@_;
459 100 ▶ return (index($self->defs->{$attrib}," L")>=0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(index($self->defs->{$attrib}, ' L') != -1) ? : 2 98 100
460 }
461  
462 =pod
463  
464 =item $format->listValues (attribute_name)
465  
466 Return the list of all possible values for the given attribute.
467  
468 =cut
469  
470 2 ▶sub listValues {
Subroutine listValues: called CC=4 SCAR=14.8
Pod documented
471 2 my ($self,$attrib)=@_;
472 2 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 2 2
473  
474 2 my $defs = $self->defs;
475 2 my ($I,$b,$e);
476 2 $b=index($defs->{$attrib}," L=");
477 2 ▶ if ($b>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b >= 0) { } 2 0 2
478 2 $e=index($defs->{$attrib}," ",$b+1);
479 2 ▶ if ($e>=0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e >= 0) { } 2 0 2
480 2 return split /\|/,substr($defs->{$attrib},$b+3,$e-$b-3);
481 } else {
482 0 return split /\|/,substr($defs->{$attrib},$b+3);
483 }
484 0 } else { return (); }
485 }
486  
487 =pod
488  
489 =item $format->color (attribute_name)
490  
491 Return one of C<Shadow>, C<Hilite> and C<XHilite> depending on the
492 color assigned to the given attribute in the FS format instance.
493  
494 =cut
495  
496 0 ▶sub color {
Subroutine color: not called CC=5 SCAR=34.0
Pod documented
497 0 my ($self,$arg) = @_;
498 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
499  
500 0 ▶ if (index($self->defs->{$arg}," 1")>=0) {
Branch: 3 branches
BranchTrueFalseTotal
if (index($self->defs->{$arg}, ' 1') != -1) { } 0 0 0
elsif (index($self->defs->{$arg}, ' 2') != -1) { } 0 0 0
elsif (index($self->defs->{$arg}, ' 3') != -1) { } 0 0 0
501 0 return "Shadow";
502 } elsif (index($self->defs->{$arg}," 2")>=0) {
503 0 return "Hilite";
504 } elsif (index($self->defs->{$arg}," 3")>=0) {
505 0 return "XHilite";
506 } else {
507 0 return "normal";
508 }
509 }
510  
511 =pod
512  
513 =item $format->special (letter)
514  
515 Return name of a special attribute declared in FS definition with a
516 given letter. See also sentord() and similar.
517  
518 =cut
519  
520 2 ▶sub special {
Subroutine special: called CC=2 SCAR=8.1
Pod documented
521 2 my ($self,$defchar)=@_;
522 2 ▶ return ($self->[3]||$self->renew_specials)->{$defchar};
Condition33%1/3
$self->[3] || $self->renew_specials
l!l&&r!l&&!r
200
MC/DC0%0/2
$self->[3] || $self->renew_specials
$self->[3] $self->renew_specials
Truth table33%1/3
A || B
A= $self->[3]B= $self->renew_specials
ABresult
1X1
011
000
523 }
524  
525 6 ▶sub set_special {
Subroutine set_special: called CC=2 SCAR=7.0
Pod undocumented
526 6 my ($self,$defchar,$value)=@_;
527 6 ▶ my $spec = ($self->[3]||$self->renew_specials);
Condition66%2/3
$self->[3] || $self->renew_specials
l!l&&r!l&&!r
420
MC/DC0%0/2
$self->[3] || $self->renew_specials
$self->[3] $self->renew_specials
Truth table66%2/3
A || B
A= $self->[3]B= $self->renew_specials
ABresult
1X1
011
000
528 6 $spec->{$defchar}=$value;
529 6 return;
530 }
531  
532 =pod
533  
534 =item $format->indexOf (attribute_name)
535  
536 Return index of the given attribute (in the order given by FS
537 instance declaration).
538  
539 =cut
540  
541 0 ▶sub indexOf {
Subroutine indexOf: not called CC=2 SCAR=17.9
Pod documented
542 0 my ($self,$arg)=@_;
543 return
544 0 ▶ ref($self) ? Treex::PML::Index($self->list,$arg) : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
545 }
546  
547 =item $format->exists (attribute_name)
548  
549 Return true if an attribute of the given name exists.
550  
551 =cut
552  
553 0 ▶sub exists {
Subroutine exists: not called CC=3 SCAR=24.8
Pod documented
554 0 my ($self,$arg)=@_;
555 return
556 ref($self) ?
557 (exists($self->defs->{$arg}) &&
558 0 ▶ defined($self->defs->{$arg})) : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
Condition0%0/3
exists $self->defs->{$arg} and defined $self->defs->{$arg}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->defs->{$arg} and defined $self->defs->{$arg}
exists $self->defs->{$arg} defined $self->defs->{$arg}
Truth table0%0/3
A and B
A= exists $self->defs->{$arg}B= defined $self->defs->{$arg}
ABresult
0X0
100
111
559 }
560  
561  
562 =pod
563  
564 =item $format->make_sentence (root_node,separator)
565  
566 Return a string containing the content of value (special) attributes
567 of the nodes of the given tree, separated by separator string, sorted by
568 value of the (special) attribute sentord or (if sentord does not exist) by
569 (special) attribute order.
570  
571 =cut
572  
573 0 ▶sub make_sentence {
Subroutine make_sentence: not called CC=5 SCAR=34.0
Pod documented
574 0 my ($self,$root,$separator)=@_;
575 0 ▶ return unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
576 0 ▶ $separator=' ' unless defined($separator);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $separator 0 0 0
577 0 my @nodes=();
578 0 ▶ my $sentord = $self->sentord || $self->order;
Condition0%0/3
$self->sentord || $self->order
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->sentord || $self->order
$self->sentord $self->order
Truth table0%0/3
A || B
A= $self->sentordB= $self->order
ABresult
1X1
011
000
579 0 my $value = $self->value;
580 0 my $node=$root;
581 0 ▶ while ($node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 0 0 0
582 0 push @nodes,$node;
583 0 $node=$node->following($root);
584 }
585 return join ($separator,
586 0 map { $_->getAttribute($value) }
587 0 sort { $a->getAttribute($sentord) <=> $b->getAttribute($sentord) } @nodes);
588 }
589  
590  
591 =pod
592  
593 =item $format->clone_node
594  
595 Create a copy of the given node.
596  
597 =cut
598  
599 0 ▶sub clone_node {
Subroutine clone_node: not called CC=5 SCAR=34.0
Pod documented
600 0 my ($self,$node)=@_;
601 0 my $new = ref($node)->new();
602 0 ▶ if ($node->type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->type) { } 0 0 0
603 0 foreach my $atr ($node->type->get_normal_fields,'#name') {
604 0 ▶ if (ref($node->{$atr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{$atr}) { } 0 0 0
605 0 $new->{$atr} = Treex::PML::CloneValue($node->{$atr});
606 } else {
607 0 $new->{$atr} = $node->{$atr};
608 }
609 }
610 0 $new->set_type($node->type);
611 } else {
612 0 foreach (@{$self->list}) {
613 0 $new->{$_}=$node->{$_};
614 }
615 }
616 0 return $new;
617 }
618  
619 =item $format->clone_subtree
620  
621 Create a deep copy of the given subtree.
622  
623 =cut
624  
625 0 ▶sub clone_subtree {
Subroutine clone_subtree: not called CC=4 SCAR=30.0
Pod documented
626 0 my ($self,$node)=@_;
627 0 my $nc;
628 0 ▶ return 0 unless $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
629 0 my $prev_nc=0;
630 0 my $nd=$self->clone_node($node);
631 0 foreach ($node->children()) {
632 0 $nc=$self->clone_subtree($_);
633 0 $nc->set_parent($nd);
634 0 ▶ if ($prev_nc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev_nc) { } 0 0 0
635 0 $nc->set_lbrother($prev_nc);
636 0 $prev_nc->set_rbrother($nc);
637 } else {
638 0 $nd->set_firstson($nc);
639 }
640 0 $prev_nc=$nc;
641 }
642 0 return $nd;
643 }
644  
645  
646 =pod
647  
648 =back
649  
650 =cut
651  
652 =head1 SEE ALSO
653  
654 L<Treex::PML>, L<Treex::PML::Factory>, L<Treex::PML::Document>, L<Treex::PML::Schema>
655  
656 =head1 COPYRIGHT AND LICENSE
657  
658 Copyright (C) 2006-2010 by Petr Pajas, 2010-2024 Jan Stepanek
659  
660 This library is free software; you can redistribute it and/or modify
661 it under the same terms as Perl itself, either Perl version 5.8.2 or,
662 at your option, any later version of Perl 5 you may have available.
663  
664 =cut
665  
666  
667 9 1;
« PML/Schema/XMLNode.pm ↑ Summary PML/Backend/FS.pm »