blib/lib/Lingua/Treebank/Const.pm

filter: Statementstmt 55.0% 348 / 632 Branchbran 47.9% 137 / 286 Conditioncond 40.0% 16 / 40 MC/DCmcdc 28.0% 7 / 25 Subroutinesub 60.5% 43 / 71 Podpod 80.3% 49 / 61 totaltotal 53.8% 600 / 1115 CC 175 SCAR 28.2
CC
175
Cov
52%
edges_data
110.0
shared_edges
69.3
from_cnf_string
42.0
CRAP 16.7
« blib/lib/Lingua/Treebank.pm ↑ Summary Const/t/50walking.t »
1 package Lingua::Treebank::Const;
2  
3 10 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use Lingua::Treebank;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 our $VERSION = '0.16'; # stay in sync automatically
10 10 our $VERBOSE = $Lingua::Treebank::VERBOSE;
11 10 our $BF_TRAVERSAL;
12 ##################################################################
13 10 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 10 TAG => 1,
15 ANNOT => 2,
16 WORD => 3,
17 PARENT => 4,
18 CHILDREN => 5,
19 NUM => 6,
20 HEADCHILD => 7, # only used after Headfinder
21 10 };
22 use overload
23 10 ▶ '""' => \&stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
24 '0+' => \&numerify, # find location in memory
25 fallback => 1, # numeric tests measure memory location
26 10 ;
27 ##################################################################
28 10 our $INDENT_CHAR = ' ' x 4;
29 10 our $CHILD_PROLOG = "\n";
30 10 our $CHILD_EPILOG = "";
31 10 our $STRINGIFY = 'as_penn_text';
32 ##################################################################
33 121 ▶sub numerify {
Subroutine numerify: called CC=2 SCAR=7.0
Pod documented
34 121 my $self = shift;
35 121 my $num = $self->[NUM];
36 121 ▶ confess "no numeric value!?" unless defined $num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num 0 121 121
37 121 return $num;
38 }
39 ## the approach below is not portable. assign a new number from
40 ## $__NUMID for every constituent at new() instead.
41 # sub numerify {
42 # my $self = shift;
43 # if (not defined $self->[NUM]) {
44 # # fetch out the number indicating the location in memory
45 # my $refstr= overload::StrVal( $self );
46 # if ($refstr =~ m{\( 0x ([0-9a-fA-F]+) \) $}x) { #
47 # # }
48 # # cache it for later to save the regex
49 # $self->[NUM] = hex $1;
50 # }
51 # else {
52 # confess "numerify wasn't able to extract a numeric ref";
53 # }
54 # }
55 # return $self->[NUM];
56 #}
57 10 our $__NUMID = 100; # never be small, just to be sure
58 4743 ▶sub _next_numid {
Subroutine _next_numid: called CC=1 SCAR=0.0
59 # for assigning unique numeric values to each new constituent;
60 # invoked from the ->new() method
61 4743 my $class = shift;
62 4743 ++$__NUMID;
63 4743 return $__NUMID;
64 }
65 ##################################################################
66 26 ▶sub stringify {
Subroutine stringify: called CC=4 SCAR=22.9
Pod documented
67 26 my $self = shift;
68 26 ▶ if ($STRINGIFY eq 'as_penn_text') {
Branch: 3 branches
BranchTrueFalseTotal
if ($Lingua::Treebank::Const::STRINGIFY eq 'as_penn_text') { } 26 0 26
elsif ($Lingua::Treebank::Const::STRINGIFY eq 'words') { } 0 0 0
elsif ($Lingua::Treebank::Const::STRINGIFY eq 'preterm_tags') { } 0 0 0
69 26 return $self->as_penn_text();
70 }
71 elsif ($STRINGIFY eq 'words') {
72 0 return join ' ', map { $_->word() } $self->get_all_terminals();
73 }
74 elsif ($STRINGIFY eq 'preterm_tags') {
75 0 return join ' ', map { $_->tag() } $self->get_all_terminals();
76 }
77 else {
78 0 carp "don't recognize \$",
79 __PACKAGE__, "::STRINGIFY value of $STRINGIFY";
80 }
81 }
82  
83 ##################################################################
84 0 ▶sub edges {
Subroutine edges: not called CC=1 SCAR=6.9
Pod documented
85 0 my $self = shift;
86 return
87 0 map { join ',', @{$_} }
88 $self->edges_data(@_);
89 }
90 0 ▶sub edges_data {
Subroutine edges_data: not called CC=10 SCAR=47.0
Pod undocumented
91 0 my $self = shift;
92 0 my (%args) = @_;
93 0 my %ignore;
94 my $do_terminal =
95 0 ▶ (defined $args{'keepterminal'} ? $args{'keepterminal'} : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'keepterminal'} ? : 0 0 0
96 0 ▶ if (defined $args{ignore}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ignore"}) 0 0 0
97 0 %ignore = map {$_ => 1} @{$args{ignore}};
98 }
99 0 my %coindex;
100 0 ▶ %coindex = %{$args{coindex}} if defined $args{coindex};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"coindex"} 0 0 0
101  
102 0 my (@edges) = $self->_edges_driver(0);
103 0 my (@returns);
104 0 for (@edges) {
105 0 ▶ next if $ignore{$_->[0]};
Branch: 1 branch
BranchTrueFalseTotal
if $ignore{$_->[0]} 0 0 0
106  
107 0 ▶ next if (not $do_terminal and $_->[0] =~ /::/);
Branch: 1 branch
BranchTrueFalseTotal
if not $do_terminal and $_->[0] =~ /::/ 0 0 0
Condition0%0/3
not $do_terminal and $_->[0] =~ /::/
!ll&&!rl&&r
000
MC/DC0%0/2
not $do_terminal and $_->[0] =~ /::/
not $do_terminal $_->[0] =~ /::/
Truth table0%0/3
not(A) and B
A= not $do_terminalB= $_->[0] =~ /::/
ABresult
0X0
100
111
108  
109 0 ▶ $_->[1] = $coindex{$_->[1]} if defined $coindex{$_->[1]};
Branch: 1 branch
BranchTrueFalseTotal
if defined $coindex{$_->[1]} 0 0 0
110 0 ▶ $_->[2] = $coindex{$_->[2]} if defined $coindex{$_->[2]};
Branch: 1 branch
BranchTrueFalseTotal
if defined $coindex{$_->[2]} 0 0 0
111 0 push @returns, $_;
112 }
113 0 return @returns;
114 }
115 0 ▶sub _edges_driver {
Subroutine _edges_driver: not called CC=3 SCAR=24.8
116 0 my $self = shift;
117 0 my $start_index = shift;
118  
119 0 ▶ if ($self->is_terminal()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) 0 0 0
120 0 return ([$self->tag() . '::' . $self->word(),
121 $start_index,
122 $start_index + 1]);
123 }
124 0 my @edges;
125 0 my $l_idx = $start_index;
126 0 for (@{$self->children()}) {
127 0 push @edges, $_->_edges_driver($l_idx);
128 0 $l_idx = $edges[-1][-1];
129 }
130  
131 # don't forget the edge for myself
132 0 push @edges, [$self->tag(), $start_index, $l_idx];
133 0 return @edges;
134 }
135 ##################################################################
136 0 ▶sub shared_edges {
Subroutine shared_edges: not called CC=9 SCAR=42.4
Pod documented
137 0 my $self = shift;
138 0 my $other = shift;
139 0 my %args = @_;
140  
141 0 my %is_phantom = map {$_ => 1} @{$args{phantom}};
142  
143 0 my %coindex; # those trees that need coindexation
144 0 ▶ if (scalar keys %is_phantom) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %is_phantom) 0 0 0
145 0 for ($self->edges_data(%args)) {
146 0 ▶ if ($is_phantom{$_->[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_phantom{$_->[0]}) 0 0 0
147 0 $coindex{$_->[1]} = $_->[2];
148 }
149 }
150 }
151  
152 0 my %edges;
153 0 for ($self->edges(%args, coindex => \%coindex)) {
154 0 $edges{$_}++;
155 }
156 0 my %other_edges;
157 0 for ($other->edges(%args, coindex => \%coindex)) {
158 0 $other_edges{$_}++;
159 }
160 10 ▶ use List::Util 'min';
Subroutine BEGIN: called CC=1 SCAR=0.0
161 0 my @to_return;
162 0 for (keys %edges) {
163 0 ▶ push @to_return, ($_) x min ($edges{$_} || 0, $other_edges{$_} || 0);
Condition0%0/4
$edges{$_} || 0
l!l
00
$other_edges{$_} || 0
l!l
00
MC/DC0%0/1
$edges{$_} || 0
$edges{$_}
MC/DC0%0/1
$other_edges{$_} || 0
$other_edges{$_}
Truth table0%0/2
A
A= $edges{$_}
Aresult
11
00
Truth table0%0/2
A
A= $other_edges{$_}
Aresult
11
00
164 }
165 0 return @to_return;
166 }
167 ##################################################################
168 0 ▶sub list_constituents {
Subroutine list_constituents: not called CC=3 SCAR=24.8
Pod documented
169 0 my $self = shift;
170 0 ▶ if ($self->is_terminal()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) { } 0 0 0
171 0 return ($self);
172 }
173 else {
174 0 my @list;
175 0 for (@{$self->children()}) {
176 0 push @list, $_->list_constituents();
177 }
178 0 return $self, @list;
179 }
180 }
181 ##################################################################
182 # High-power generic function for crawling the tree. Most of the other
183 # functions could probably be implemented in terms of this one.
184 4 ▶sub walk {
Subroutine walk: called CC=14 SCAR=29.2
Pod documented
185 4 my ($self, $action, $stop_criterion, $state, $bf_traversal) = @_;
186  
187 4 ▶ croak "walk()'s first non-self arg not defined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 0 4 4
188 unless (defined $action);
189 4 ▶ if (ref $action eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $action eq '') { } 0 4 4
elsif (not ref $action eq 'CODE') { } 0 4 4
190 0 $action = $self->can($action);
191 0 ▶ if (not defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $action) 0 0 0
192 0 croak "couldn't find method $action to call from within walk"
193 }
194 }
195 elsif (not ref $action eq 'CODE') {
196 0 croak "walk()'s first non-self arg not CODE-ref"
197 }
198  
199 4 ▶ if (defined $stop_criterion) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stop_criterion) 2 2 4
200 2 ▶ if (ref $stop_criterion eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $stop_criterion eq '') { } 0 2 2
elsif (not ref $stop_criterion eq 'CODE') { } 0 2 2
201 0 $stop_criterion = $self->can($stop_criterion);
202 0 ▶ croak "couldn't find method $stop_criterion ",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stop_criterion 0 0 0
203 " to call from within walk()"
204 if (not defined $stop_criterion);
205 }
206 elsif (not ref $stop_criterion eq 'CODE') {
207 0 croak "walk()'s stop criterion arg not a CODE-ref";
208 }
209 }
210  
211 4 my @children = ();
212 4 my @stack = ($self);
213  
214 4 ▶ if (not defined $bf_traversal) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bf_traversal) 2 2 4
215 2 $bf_traversal = $BF_TRAVERSAL;
216 }
217  
218 NODE:
219 4 while (1) {
220 # take one off the front of the line
221 90 my $node = shift @stack;
222 90 ▶ return if not defined $node;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 4 86 90
223  
224 86 &{$action}($node, $state);
225  
226 86 ▶ if ( defined $stop_criterion
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stop_criterion and &{$stop_criterion;}($node, $state)) 2 84 86
Condition100%3/3
defined $stop_criterion and &{$stop_criterion;}($node, $state)
!ll&&!rl&&r
50342
MC/DC100%2/2
defined $stop_criterion and &{$stop_criterion;}($node, $state)
defined $stop_criterion &{$stop_criterion;}($node, $state)
Truth table100%3/3
A and B
A= defined $stop_criterionB= &{$stop_criterion;}($node, $state)
ABresult
0X0
100
111
227 36 and &{$stop_criterion}($node, $state) ) {
228 # don't put the children on the agenda
229 2 next NODE;
230 }
231  
232 # else include node's children
233 84 ▶ if ($bf_traversal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bf_traversal) { } 42 42 84
234 # children go in the back of the line
235 42 push @stack, @{$node->children()};
236 }
237 else { # depth-first traversal
238 # children go in the front of the line
239 42 unshift @stack, @{$node->children()};
240 }
241 }
242 }
243 ##################################################################
244 3 ▶sub find_common_ancestor {
Subroutine find_common_ancestor: called CC=7 SCAR=19.8
Pod documented
245  
246 # returns lowest common ancestor, or undef if there is none.
247  
248 3 my __PACKAGE__ $self = shift;
249 3 my __PACKAGE__ $cousin = shift;
250  
251 # error checking
252 3 ▶ croak "cousin arg not defined" if not defined $cousin;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cousin 0 3 3
253 3 ▶ croak "cousin not a " . __PACKAGE__
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($cousin, __PACKAGE__) 0 3 3
254 unless UNIVERSAL::isa($cousin, __PACKAGE__);
255  
256 3 my __PACKAGE__ $matriarch = $self->root();
257 3 ▶ if ( $cousin->root() != $matriarch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cousin->root != $matriarch) 0 3 3
258 0 return; # no common ancestor
259 }
260  
261 3 my @self_lineage = $self->path_up_to( $matriarch );
262 3 my @cousin_lineage = $cousin->path_up_to( $matriarch );
263  
264 3 ▶ while (@self_lineage and @cousin_lineage) {
Branch: 1 branch
BranchTrueFalseTotal
if (@self_lineage and @cousin_lineage) 6 1 7
Condition66%2/3
@self_lineage and @cousin_lineage
!ll&&!rl&&r
106
MC/DC50%1/2
@self_lineage and @cousin_lineage
@self_lineage @cousin_lineage
Truth table66%2/3
A and B
A= @self_lineageB= @cousin_lineage
ABresult
0X0
100
111
265 6 my __PACKAGE__ $self_gramma = pop @self_lineage;
266 6 my __PACKAGE__ $cousin_gramma = pop @cousin_lineage;
267 6 ▶ if ($self_gramma == $cousin_gramma) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self_gramma == $cousin_gramma) { } 4 2 6
268 4 $matriarch = $self_gramma;
269 }
270 else {
271 # stop looking -- once unshared, thereafter its a waste to
272 # keep looking. No incestuous trees here, one would hope.
273 2 last;
274 }
275 }
276  
277 3 return $matriarch;
278 }
279 ##################################################################
280 8 ▶sub equiv_to {
Subroutine equiv_to: called CC=9 SCAR=23.5
Pod documented
281 8 my __PACKAGE__ $self = shift;
282 8 my __PACKAGE__ $other = shift;
283  
284 8 ▶ if ($self->is_terminal()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) { } 4 4 8
285 4 ▶ return 0 unless $other->is_terminal();
Branch: 1 branch
BranchTrueFalseTotal
unless $other->is_terminal 0 4 4
286  
287 4 ▶ if ($self->tag() ne $other->tag()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tag ne $other->tag) 0 4 4
288 0 return 0;
289 }
290 4 ▶ if ($self->word() ne $other->word()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->word ne $other->word) 0 4 4
291 0 return 0;
292 }
293  
294 # otherwise it all passes:
295 4 return 1;
296 }
297 else {
298 # self non-terminal
299 4 ▶ return 0 if $other->is_terminal();
Branch: 1 branch
BranchTrueFalseTotal
if $other->is_terminal 0 4 4
300  
301 # different number of children
302 4 ▶ return 0 if (@{ $self->children() } != @{ $other->children() });
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->children;} != @{$other->children;} 0 4 4
303  
304 4 foreach my $idx ( 0 .. $#{ $self->children() } ) {
305 7 my __PACKAGE__ $lchild = $self->children($idx);
306 7 my __PACKAGE__ $rchild = $other->children($idx);
307 7 ▶ if (not $lchild->equiv_to($rchild)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($lchild->equiv_to($rchild)) 0 7 7
308 0 return 0;
309 }
310 }
311 # otherwise it all passes
312 4 return 1;
313 }
314 }
315 ##################################################################
316 0 ▶sub equiv_tags {
Subroutine equiv_tags: not called CC=1 SCAR=6.9
Pod documented
317 0 croak "not implemented\n";
318 }
319 0 ▶sub equiv_words {
Subroutine equiv_words: not called CC=1 SCAR=6.9
Pod documented
320 0 croak "not implemented\n";
321 }
322 ##################################################################
323 # height/depth functions
324 ##################################################################
325 0 ▶sub depth_from {
Subroutine depth_from: not called CC=3 SCAR=24.8
Pod documented
326 # return depth from given target. returns undef if $target is not
327 # the ancestor of $self
328  
329 0 my __PACKAGE__ $self = shift;
330 0 my __PACKAGE__ $target = shift;
331  
332 0 ▶ if ($self == $target) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self == $target) { } 0 0 0
elsif ($self->is_root) { } 0 0 0
333 0 return 0;
334 }
335 elsif ($self->is_root()) {
336 0 carp "depth_from argument not an ancestor of instance";
337 # we could check this explicitly, but users may already know
338 # this isn't going to happen, so let's not waste cycles
339 0 return (); # not defined
340 }
341 else {
342 0 return $self->parent->depth_from($target) + 1;
343 }
344 }
345 ##################################################################
346 0 ▶sub depth {
Subroutine depth: not called CC=1 SCAR=6.9
Pod documented
347  
348 # returns how many steps from self up to root
349  
350 0 my __PACKAGE__ $self = shift;
351  
352 # implemented using more general function -- but it does require
353 # two traversals of the tree... other implementations may be easier
354 0 return $self->depth_from( $self->root() );
355  
356 # if benchmarking turns up a problem here, use one of these below
357 # instead (probably the second, since it involves the fewest stack ops
358 # and so is probably the fastest).
359  
360 # simple recursive implementation
361  
362 ## if ( $self->is_root() ) {
363 ## return 0;
364 ## }
365 ## else {
366 ## return $self->parent->depth() + 1;
367 ## }
368  
369 # non-recursive implementation
370 ## my $d = 0;
371 ## my __PACKAGE__ $p = $self->parent;
372 ## until ( $p->is_root() ) {
373 ## $h++;
374 ## $p = $p->parent;
375 ## }
376 ## return $d;
377  
378 }
379 ##################################################################
380 0 ▶sub height {
Subroutine height: not called CC=4 SCAR=30.0
Pod documented
381 # returns longest distance from self down to any leaf
382  
383 # could be re-implemented with get_all_terminals, path_up_to and
384 # array lengths, but that seems unnecessary
385 0 my __PACKAGE__ $self = shift;
386  
387 0 ▶ if ($self->is_terminal()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) { } 0 0 0
388 0 return 0;
389 }
390 else {
391 0 my ($max) = 0;
392  
393 # choose the largest height among the children, return that
394 # (+1)
395 0 foreach my __PACKAGE__ $d (@{ $self->children() }) {
396 0 my $this_height = $d->height();
397 0 ▶ if ($max < $this_height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max < $this_height) 0 0 0
398 0 $max = $this_height;
399 }
400 }
401 0 return $max + 1;
402 }
403 }
404 ##################################################################
405 11 ▶sub get_index {
Subroutine get_index: called CC=5 SCAR=18.9
Pod documented
406 11 my __PACKAGE__ $self = shift;
407 11 my __PACKAGE__ $daughter = shift;
408  
409 11 ▶ if ($self->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) 0 11 11
410 0 carp "get_index called on terminal node, can't get_index";
411 0 return;
412 }
413  
414 11 ▶ if (not $self == $daughter->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self == $daughter->parent) 0 11 11
415 0 carp "argument not daughter of instance, can't get index";
416 0 return ;
417 }
418  
419 11 for ( 0 .. $#{$self->children} ) {
420 17 ▶ if ( $self->children($_) == $daughter ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->children($_) == $daughter) 11 6 17
421 11 return $_;
422 }
423 }
424  
425 0 carp "malformed tree:",
426 " daughter identifies instance as parent, but parent does ",
427 "not claim daughter";
428 0 return ;
429 }
430 ##################################################################
431 # node retrieval functions
432 ##################################################################
433 17 ▶sub path_up_to {
Subroutine path_up_to: called CC=4 SCAR=15.0
Pod documented
434 17 my __PACKAGE__ $self = shift;
435 17 my __PACKAGE__ $terminus = shift;
436  
437 # could be done non-recursively, but this is grammatical structure
438 # -- very small heights. Besides, recursivity is cooler, and
439 # easier to think about
440  
441 17 ▶ if ($self == $terminus) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self == $terminus) { } 7 10 17
elsif ($self->is_root) { } 0 10 10
442 7 return ($self);
443 }
444 elsif ( $self->is_root() ) {
445 0 carp "terminus argument not an ancestor of instance!";
446 0 return ;
447 }
448 else {
449 10 my @path = $self->parent->path_up_to( $terminus );
450 10 ▶ if (not @path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @path) { } 0 10 10
451 0 return; # not found
452 }
453 else {
454 10 return ( $self, @path );
455 }
456 }
457 }
458 ##################################################################
459 17 ▶sub root {
Subroutine root: called CC=2 SCAR=6.9
Pod documented
460 # returns the root of a given node
461 17 my __PACKAGE__ $self = shift;
462 17 ▶ if ($self->is_root()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) { } 7 10 17
463 7 return $self;
464 }
465 else {
466 10 return $self->parent->root();
467 }
468 }
469 ##################################################################
470 # Return a list of ancestors of a node matching a criteria given in a
471 # function parameter.
472 # my $path = <<EOTREE;
473 # (NP
474 # (NP
475 # (VP
476 # (N dog))))
477 # EOTREE
478 #
479 # my $node = Lingua::Treebank::TB3Const->new->from_penn_string($text);
480 # my @terms = $node->get_all_terminals();
481 # my $node = shift @terms;
482 # my @ancestors = $node->select_ancestors(sub{$_[0]->tag() eq "NP"});
483 1 ▶sub select_ancestors {
Subroutine select_ancestors: called CC=3 SCAR=11.0
Pod documented
484 1 my __PACKAGE__ $self = shift;
485 1 my $criteria = shift;
486  
487 1 my @ancestors = ();
488  
489 1 $self = $self->parent();
490 PARENT:
491 1 ▶ while (defined $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self) 3 1 4
492 3 ▶ push @ancestors, $self if (&$criteria($self));
Branch: 1 branch
BranchTrueFalseTotal
if &$criteria($self) 2 1 3
493 3 $self = $self->parent();
494 }
495  
496 1 return @ancestors;
497 }
498 ##################################################################
499 # Return a list of children of a node matching a criteria given in a
500 # function parameter. The children are searched breadth-first.
501 1 ▶sub select_children {
Subroutine select_children: called CC=3 SCAR=11.0
Pod documented
502 1 my __PACKAGE__ $self = shift;
503 1 my $criteria = shift;
504  
505 1 my @children = ();
506 1 my @stack = ($self);
507 CHILD:
508 1 while (1) {
509 6 my $node = pop @stack;
510 6 ▶ last CHILD if (not $node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 1 5 6
511  
512 5 ▶ push @children, $node if (&$criteria($node));
Branch: 1 branch
BranchTrueFalseTotal
if &$criteria($node) 2 3 5
513 5 push @stack, @{$node->children()};
514 }
515  
516 1 return @children;
517 }
518 ##################################################################
519 4750 ▶sub get_all_terminals {
Subroutine get_all_terminals: called CC=3 SCAR=11.0
Pod documented
520 # returns all leaves in a left-right traversal
521  
522 4750 my __PACKAGE__ $self = shift;
523  
524 4750 my @terminals;
525  
526 4750 ▶ if ( $self->is_terminal() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) { } 2487 2263 4750
527 2487 @terminals = ( $self ); # parens force list return
528 }
529 else {
530 2263 foreach my __PACKAGE__ $d ( @{$self->children} ) {
531 4378 push @terminals, $d->get_all_terminals;
532 }
533 }
534 4750 return @terminals;
535 }
536 ##################################################################
537 6 ▶sub next_sib {
Subroutine next_sib: called CC=3 SCAR=11.0
Pod documented
538 6 my __PACKAGE__ $self = shift;
539  
540 6 ▶ return if $self->is_root; # no sib, return undef
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root 1 5 6
541  
542 5 my __PACKAGE__ $parent = $self->parent;
543  
544 5 my $index = $parent->get_index($self);
545  
546 5 ▶ if ($index == $#{$parent->children}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == $#{$parent->children;}) 3 2 5
547 # this is the rightmost of the group of siblings
548 3 return; # no right sib
549 }
550 2 return $parent->children($index + 1);
551 }
552 ##################################################################
553 8 ▶sub prev_sib {
Subroutine prev_sib: called CC=3 SCAR=11.0
Pod documented
554 8 my __PACKAGE__ $self = shift;
555  
556 8 ▶ return if $self->is_root; # no sib, return undef
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root 2 6 8
557  
558 6 my __PACKAGE__ $parent = $self->parent;
559  
560 6 my $index = $parent->get_index($self);
561  
562 6 ▶ if ($index == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index == 0) 4 2 6
563 # this is the leftmost of the group of siblings
564 4 return; # no left sib
565 }
566 2 return $parent->children($index - 1);
567 }
568 ##################################################################
569 5 ▶sub right_leaf {
Subroutine right_leaf: called CC=2 SCAR=6.9
Pod documented
570 5 my __PACKAGE__ $self = shift;
571 # returns rightmost leaf of current node
572  
573 5 ▶ if ($self->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) { } 3 2 5
574 3 return $self;
575 }
576 else {
577 2 my __PACKAGE__ $right_daughter = $self->children(-1);
578 2 return $right_daughter->right_leaf();
579 }
580 }
581 ##################################################################
582 9 ▶sub left_leaf {
Subroutine left_leaf: called CC=2 SCAR=6.9
Pod documented
583 9 my __PACKAGE__ $self = shift;
584 # returns leftmost leaf of current node
585  
586 9 ▶ if ($self->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) { } 4 5 9
587 4 return $self;
588 }
589 else {
590 5 my __PACKAGE__ $left_daughter = $self->children(0);
591 5 return $left_daughter->left_leaf();
592 }
593 }
594 ##################################################################
595 5 ▶sub prev_leaf {
Subroutine prev_leaf: called CC=3 SCAR=11.0
Pod documented
596 # return the next leaf to the left (back in time), not dominated
597 # by the current node
598  
599 # should behave correctly even when called on a non-terminal --
600 # returns the first leaf to the left not-dominated by the current
601 5 my __PACKAGE__ $self = shift;
602  
603 5 my __PACKAGE__ $left_sib = $self->prev_sib;
604  
605 5 ▶ if (defined $left_sib) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $left_sib) { } 1 4 5
606 1 return $left_sib->right_leaf();
607 }
608 else {
609 # no immediate left sib, go up the tree
610  
611 4 ▶ if ( $self->is_root() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) { } 1 3 4
612 1 return; # no previous leaves
613 }
614 else {
615 3 return $self->parent->prev_leaf();
616 }
617 }
618 }
619 ##################################################################
620 2 ▶sub next_leaf {
Subroutine next_leaf: called CC=3 SCAR=11.2
Pod documented
621 # return the next leaf to the right (forward in time)
622  
623 # should behave correctly even when called on a non-terminal --
624 # returns the first leaf to the right not-dominated by the current
625 2 my __PACKAGE__ $self = shift;
626  
627 2 my __PACKAGE__ $right_sib = $self->next_sib;
628  
629 2 ▶ if (defined $right_sib) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $right_sib) { } 1 1 2
630 1 return $right_sib->left_leaf();
631 }
632 else {
633 # no immediate right sib, go up the tree
634  
635 1 ▶ if ( $self->is_root() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) { } 0 1 1
636 0 return; # no previous leaves
637 }
638 else {
639 1 return $self->parent->next_leaf();
640 }
641 }
642 }
643 ##################################################################
644 # boolean requests (one additional argument)
645 ##################################################################
646 0 ▶sub is_descendant_of {
Subroutine is_descendant_of: not called CC=3 SCAR=24.8
Pod documented
647 0 my __PACKAGE__ $self = shift;
648 0 my __PACKAGE__ $grandma = shift;
649  
650 0 ▶ if ($self == $grandma) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self == $grandma) 0 0 0
651 0 return 1; # yes, you are your own descendant. :p
652 }
653 0 ▶ if ($self->is_root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_root) { } 0 0 0
654 0 return 0; # root is descendant of nobody, grandma or otherwise
655 }
656 else {
657 0 return $self->parent->is_descendant_of($grandma);
658 }
659 }
660 ##################################################################
661 0 ▶sub is_ancestor_of {
Subroutine is_ancestor_of: not called CC=1 SCAR=6.9
Pod documented
662 0 my __PACKAGE__ $self = shift;
663 0 my __PACKAGE__ $candidate = shift;
664 0 return $candidate->is_descendant_of($self);
665 }
666 ##################################################################
667 # Are the two nodes siblings?
668 #
669 # my $sibling = <<EOTREE;
670 # (S
671 # (NP
672 # (D the)
673 # (N boy))
674 # (VP
675 # ran))
676 # EOTREE
677 #
678 # my $node = Lingua::Treebank::TB3Const->new()->from_penn_string($sibling);
679 # my @child = @{$node->children()};
680 # my $np = $child[0];
681 # my $vp = $child[1];
682 # print "This is true." if ($np->is_sibling($vp));
683 3 ▶sub is_sibling {
Subroutine is_sibling: called CC=5 SCAR=17.5
Pod documented
684 3 my __PACKAGE__ $self = shift;
685 3 my __PACKAGE__ $other = shift;
686  
687 3 ▶ return 0 if ($self->is_root() or $other->is_root());
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root or $other->is_root 0 3 3
Condition33%1/3
$self->is_root or $other->is_root
l!l&&r!l&&!r
003
MC/DC0%0/2
$self->is_root or $other->is_root
$self->is_root $other->is_root
Truth table33%1/3
A or B
A= $self->is_rootB= $other->is_root
ABresult
1X1
011
000
688  
689 3 my __PACKAGE__ $parent = $self->find_common_ancestor($other);
690 3 ▶ return 0 if (not defined $parent);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 0 3 3
691  
692 3 ▶ return ($parent == $self->parent() and $parent == $other->parent());
Condition66%2/3
$parent == $self->parent && $parent == $other->parent
!ll&&!rl&&r
201
MC/DC50%1/2
$parent == $self->parent && $parent == $other->parent
$parent == $self->parent $parent == $other->parent
Truth table66%2/3
A && B
A= $parent == $self->parentB= $parent == $other->parent
ABresult
0X0
100
111
693 }
694 ##################################################################
695 # I/O methods (to/from text)
696 ##################################################################
697 147 ▶sub as_penn_text {
Subroutine as_penn_text: called CC=12 SCAR=26.0
Pod documented
698 147 my __PACKAGE__ $self = shift;
699 147 my $step = shift;
700 147 my $indentChar = shift;
701 147 my $child_prolog = shift;
702 147 my $child_epilog = shift;
703 147 my $am_head = shift;
704  
705 # set defaults (in case called without full specification)
706 147 ▶ $step = 0 if not defined $step;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $step 28 119 147
707 147 ▶ $indentChar = $INDENT_CHAR if not defined $indentChar;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indentChar 28 119 147
708 147 ▶ $child_prolog = $CHILD_PROLOG if not defined $child_prolog;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_prolog 28 119 147
709 147 ▶ $child_epilog = $CHILD_EPILOG if not defined $child_epilog;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $child_epilog 28 119 147
710  
711 # begin composition of text
712 147 my $label = $self->tag();
713 147 ▶ if (defined $am_head) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $am_head) 0 147 147
714 0 ▶ if ($am_head) {
Branch: 1 branch
BranchTrueFalseTotal
if ($am_head) 0 0 0
715 0 $label = '*'.$label.'*';
716 }
717 }
718 # don't touch if $am_head undef
719  
720 147 ▶ if (defined $self->annot()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->annot) 0 147 147
721 0 $label .= '-' . $self->annot();
722 }
723  
724 147 my $text = '(' . $label . ' ';
725  
726 147 ▶ if ($self->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) { } 78 69 147
727 78 $text .= $self->word();
728 }
729 else {
730 # non-terminal
731 69 my $head = $self->headchild();
732
733 69 foreach my __PACKAGE__ $d ( @{$self->children} ) {
734 116 $text .= $child_prolog;
735 116 $text .= ($indentChar x ($step + 1));
736 116 my $child_is_head;
737 116 ▶ if (defined $head) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $head) 0 116 116
738 0 ▶ $child_is_head = ($head == $d ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$head == $d ? : 0 0 0
739 }
740 116 $text .= $d->as_penn_text($step + 1, $indentChar, $child_prolog, $child_epilog, $child_is_head);
741 116 $text .= $child_epilog;
742 }
743 }
744  
745 147 $text .= ')';
746  
747 147 return $text;
748 }
749 ##################################################################
750 0 ▶sub from_cnf_string {
Subroutine from_cnf_string: not called CC=6 SCAR=37.4
Pod undocumented
751 0 my __PACKAGE__ $self = shift;
752 0 my $class = ref $self;
753 0 local $_ = shift;
754  
755 # Strip leading and trailing whitespace.
756 0 s/^\s+//;
757 0 s/\s+$//;
758 # Remove outermost parenthesis pair.
759 0 ▶ if (s/^ \( \s* (.*) \s* \) $/$1/x) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^ \( \s* (.*) \s* \) $/$1/x) { } 0 0 0
elsif (/^([^_]+)_(\S+)$/) { } 0 0 0
760 # This is a non-terminal node.
761 # Extract the non-terminal tag.
762 0 s/^(\S+)\s*//;
763 0 my $tag = $1;
764 0 $self->tag($tag);
765 # Enumerate all the children of this node.
766 0 ▶ while (length $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 0 0 0
767 0 my $childtext;
768 0 ▶ if ( /^\(/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\(/) { } 0 0 0
769 # The child is a non-terminal node.
770 0 $childtext = $class->find_brackets($_);
771 0 substr ($_, 0, length $childtext) = '';
772 # BUGBUG check for errors here?
773 }
774 else {
775 # The child is a terminal node.
776 0 ▶ s/^(\S+)\s*// or carp "couldn't find text in $_\n";
Branch: 1 branch
BranchTrueFalseTotal
s/^(\S+)\s*// or carp("couldn't find text in $_\n") 0 0 0
777 0 $childtext = $1;
778 }
779 # Create a child node structure.
780 0 my __PACKAGE__ $child = $class->new();
781 0 $child->from_cnf_string($childtext);
782 0 $self->append($child);
783 # Skip whitespace delimiting children.
784 0 s/^\s+//;
785 }
786 }
787 elsif (/^([^_]+)_(\S+)$/) {
788 # This is a terminal node.
789 0 my ($word, $tag) = ($1, $2);
790 0 $self->word($word);
791 0 $self->tag($tag);
792 }
793 else {
794 0 croak "can't parse '$_'";
795 }
796 0 return $self;
797 }
798 ##################################################################
799 4739 ▶sub from_penn_string {
Subroutine from_penn_string: called CC=13 SCAR=26.4
Pod documented
800 4739 my __PACKAGE__ $self = shift;
801 4739 my $class = ref $self;
802 4739 my $text = shift;
803 # pass it a complete constituent in text form.
804  
805 # records the tag plus a list of its subconstituents. If
806 # subconstituents themselves have structure, then they will be
807 # arrayrefs
808  
809 # JGK: why @tags? can't remember...
810 # my (@tags) = shift;
811  
812  
813 # strip off front and back parens and whitespace
814 4739 $text =~ s/^ \s* \( \s* //x;
815 4739 $text =~ s/ \s* \) \s* $//x;
816  
817 # handle perverse cases where the brackets are the text, like
818 # (NP (-LRB- () (NNP Joe) (-RRB- )))
819 4739 $text =~ s/\(-LRB- \(\)/__LPRN__/g;
820 4739 $text =~ s/\(-RRB- \)\)/__RPRN__/g;
821  
822 # tag is everything up to the first whitespace or
823 # parenthesis. Children are everything else.
824 4739 my ($tag, $childrentext) =
825 ($text =~ /^ ([^\s\(]*) \s* (.*) $/sx);
826  
827 4739 ▶ if (not defined $tag or not defined $childrentext) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $tag and defined $childrentext) 0 4739 4739
Condition33%1/3
defined $tag and defined $childrentext
!ll&&!rl&&r
004739
MC/DC0%0/2
defined $tag and defined $childrentext
defined $tag defined $childrentext
Truth table33%1/3
A and B
A= defined $tagB= defined $childrentext
ABresult
0X0
100
111
828 0 croak "couldn't find a constituent in '$text'";
829 0 return; # undef
830 }
831  
832 4739 ▶ if ($tag =~ m/ ^ ( [^-]+? ) ([-=]) ( .* ) $/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ / ^ ( [^-]+? ) ([-=]) ( .* ) $/x) { } 490 4249 4739
833 490 my $short_tag = $1;
834 490 ▶ if ($2 ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 ne "-") 0 490 490
835 0 ▶ warn "found '$2' (should be '-') separating annotation ".
Branch: 1 branch
BranchTrueFalseTotal
if $Lingua::Treebank::Const::VERBOSE 0 0 0
836 "in tag $tag\n" if $VERBOSE;
837 }
838 490 $self->annot( $3 );
839 490 $self->tag( $short_tag );
840 }
841 else {
842 4249 $self->tag($tag);
843 }
844 4739 ▶ while (length $childrentext) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $childrentext) 6855 4739 11594
845 # handle perverse cases where the brackets are the text, like
846 # (NP (-LRB- () (NNP Joe) (-RRB- )))
847 6855 ▶ if ($childrentext =~ s/^\s*__LPRN__\s*//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($childrentext =~ s/^\s*__LPRN__\s*//) { } 3 6852 6855
elsif ($childrentext =~ s/^\s*__RPRN__\s*//) { } 1 6851 6852
848 3 my __PACKAGE__ $child = $class->new();
849 3 $child->tag('-LRB-');
850 3 $child->word('(');
851 3 $self->append($child);
852 3 next;
853 }
854 elsif ($childrentext =~ s/^\s*__RPRN__\s*//) {
855 1 my __PACKAGE__ $child = $class->new();
856 1 $child->tag('-RRB-');
857 1 $child->word(')');
858 1 $self->append($child);
859 1 next;
860 }
861  
862 6851 my $childtext = $class->find_brackets($childrentext);
863 6851 ▶ if (defined $childtext) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $childtext) { } 4366 2485 6851
864 # child is itself a constituent
865 4366 my __PACKAGE__ $child = $class->new();
866 4366 $child->from_penn_string($childtext);
867  
868 4366 $self->append($child);
869  
870 # $child->parent($self);
871 # push @{$self->children}, $child;
872  
873 # chop out the childrentext
874 4366 substr ($childrentext, 0, length $childtext) = "";
875 4366 $childrentext =~ s/^\s+//;
876  
877 4366 ▶ warn "trouble -- child constituent found " .
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->word 0 4366 4366
878 "in token that already had word\n"
879 if defined $self->word;
880 }
881 else {
882 2485 ▶ if ($childrentext =~ tr {()} {()} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($childrentext =~ tr/()//) 0 2485 2485
883 0 carp "found a parenthesis in word '$childrentext'; ",
884 " this suggests that the data had unbalanced parens";
885 }
886  
887 # this is a word; we're done
888 2485 $self->word($childrentext);
889  
890 # eliminate text so that we can exit the while loop
891 2485 $childrentext = '';
892  
893 warn "trouble -- word found in token that "
894 . "already had child constituents\n"
895 2485 ▶ if @{$self->children};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->children;} 0 2485 2485
896 }
897 }
898  
899 4739 return $self;
900 }
901 10 my $bracket_error;
902 7200 ▶sub find_brackets {
Subroutine find_brackets: called CC=9 SCAR=22.3
Pod undocumented
903 7200 my $class = shift;
904 7200 my $text = shift;
905 7200 my $count_l = 1;
906  
907 7200 my $posn = -1;
908  
909 7200 my $nextL = index $text, '(', $posn+1;
910 7200 my $nextR = index $text, ')', $posn+1;
911  
912 7200 ▶ croak ("I found a right bracket before a left-bracket. ",
Branch: 1 branch
BranchTrueFalseTotal
if $nextR < $nextL 0 7200 7200
913 "Brackets mis-nested. Are you using .psd files instead of .mrg?")
914 if $nextR < $nextL;
915  
916 7200 ▶ return if ($nextL==-1 and $nextR==-1);
Branch: 1 branch
BranchTrueFalseTotal
if $nextL == -1 and $nextR == -1 2485 4715 7200
Condition66%2/3
$nextL == -1 and $nextR == -1
!ll&&!rl&&r
471502485
MC/DC50%1/2
$nextL == -1 and $nextR == -1
$nextL == -1 $nextR == -1
Truth table66%2/3
A and B
A= $nextL == -1B= $nextR == -1
ABresult
0X0
100
111
917  
918 4715 $posn=$nextL;
919  
920 4715 ▶ if ($posn == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posn == -1) 0 4715 4715
921 # undefined
922 0 return;
923 }
924  
925 4715 ▶ while ($count_l > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count_l > 0) 41295 4715 46010
926 41295 $nextL=index $text, '(', $posn+1;
927 41295 $nextR=index $text, ')', $posn+1;
928 41295 ▶ if ($nextR == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextR == -1) 0 41295 41295
929 0 croak "missing close parens in $text";
930 }
931 41295 ▶ if ($nextL == -1 or $nextR < $nextL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nextL == -1 or $nextR < $nextL) { } 23005 18290 41295
Condition100%3/3
$nextL == -1 or $nextR < $nextL
l!l&&r!l&&!r
46561834918290
MC/DC100%2/2
$nextL == -1 or $nextR < $nextL
$nextL == -1 $nextR < $nextL
Truth table100%3/3
A or B
A= $nextL == -1B= $nextR < $nextL
ABresult
1X1
011
000
932 23005 $count_l--;
933 23005 $posn=$nextR;
934 }
935 else { # ($nextL < $nextR)
936 18290 $count_l++;
937 18290 $posn = $nextL;
938 }
939 }
940 4715 return substr $text, 0, $posn+1;
941 }
942 ##################################################################
943 # Tree modification methods
944 ##################################################################
945 0 ▶sub flatten {
Subroutine flatten: not called CC=4 SCAR=30.0
Pod documented
946 # pull up all terminals to be children of the instance here,
947 # regardless of how deep they are
948  
949 # see POD below for better details
950  
951 0 my __PACKAGE__ $self = shift;
952  
953 0 ▶ if ($self->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) 0 0 0
954 0 carp "flatten called on terminal node";
955 0 return;
956 }
957  
958 0 foreach my __PACKAGE__ $daughter (@{$self->children}) {
959  
960 0 ▶ next if $daughter->is_terminal; # this child's done
Branch: 1 branch
BranchTrueFalseTotal
if $daughter->is_terminal 0 0 0
961  
962 # pull up all descendants of non-terminal daughter to depend
963 # directly on the daughter
964 0 $daughter->flatten();
965  
966 # now reparent all the grandchildren to self, by retracting
967 # the daughter
968 0 $self->retract($daughter);
969 }
970  
971 0 return $self;
972  
973 # could probably be reimplemented by "get_all_terminals" and
974 # judicious use of insert, but this recursive strategy is more
975 # elegant and takes advantage of brains of retract() method
976 }
977 ##################################################################
978 0 ▶sub retract {
Subroutine retract: not called CC=3 SCAR=24.8
Pod documented
979 # pulls in and removes one layer of non-terminal nodes, attaching
980 # their children directly to the current node, retaining what
981 # surface order they originally had.
982  
983 # see POD for more details
984  
985 0 my __PACKAGE__ $self = shift;
986 0 my __PACKAGE__ $daughter = shift;
987  
988 0 ▶ if ( $daughter->parent() != $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daughter->parent != $self) 0 0 0
989 0 carp "argument daughter does not claim instance as mother,",
990 " can't retract!";
991 0 return;
992 }
993  
994 0 ▶ if ( $daughter->is_terminal() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($daughter->is_terminal) 0 0 0
995 0 carp "daughter is a terminal node, can't retract!";
996 0 return;
997 }
998  
999 0 $self->replace( $daughter, @{$daughter->children} );
1000  
1001 0 return $self;
1002  
1003 }
1004 ##################################################################
1005 0 ▶sub replace {
Subroutine replace: not called CC=2 SCAR=17.9
Pod documented
1006 # replace target arg with replacement list
1007 0 my __PACKAGE__ $self = shift;
1008 0 my __PACKAGE__ $target = shift;
1009 0 my @replacements = @_;
1010  
1011 0 ▶ carp "argument not a child of instance, can't replace!"
Branch: 1 branch
BranchTrueFalseTotal
unless $target->parent == $self 0 0 0
1012 unless ($target->parent == $self);
1013  
1014 0 my $index = $self->get_index($target);
1015  
1016 0 $self->detach_at($index);
1017  
1018 0 $self->insert_at($index, @replacements);
1019 }
1020 ##################################################################
1021 0 ▶sub wither {
Subroutine wither: not called CC=3 SCAR=24.8
Pod documented
1022 0 my __PACKAGE__ $self = shift;
1023 0 ▶ return if $self->is_root();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_root 0 0 0
1024  
1025 0 my __PACKAGE__ $parent = $self->parent();
1026  
1027 0 my $num_sibs = $parent->num_children();
1028  
1029 0 $parent->detach($self);
1030 0 ▶ if ($num_sibs == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_sibs == 1) 0 0 0
1031 # unary parent, wither it too
1032 0 $parent->wither();
1033 }
1034 }
1035 ##################################################################
1036 0 ▶sub detach {
Subroutine detach: not called CC=1 SCAR=6.9
Pod documented
1037 # removes an entire subtree.
1038 0 my __PACKAGE__ $self = shift;
1039 0 my __PACKAGE__ $daughter = shift;
1040  
1041 # actually do the detachment
1042 0 my $index = $self->get_index($daughter);
1043  
1044 0 $self->detach_at($index);
1045  
1046 }
1047 ##################################################################
1048 0 ▶sub detach_at {
Subroutine detach_at: not called CC=5 SCAR=34.0
Pod documented
1049 # remove one daughter node at index
1050 0 my __PACKAGE__ $self = shift;
1051 0 my $index = shift;
1052  
1053 0 ▶ if (not defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 0 0 0
1054 0 croak "no index provided to detach_at method";
1055 }
1056  
1057 0 my __PACKAGE__ $d = $self->children($index);
1058  
1059 0 ▶ if (not defined $d) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $d) 0 0 0
1060 0 carp "no daughter at index $index";
1061 0 return;
1062 }
1063  
1064 # remove links
1065 0 $d->clear_parent();
1066  
1067 0 ▶ if (defined $self->headchild() and $self->headchild() == $d) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->headchild and $self->headchild == $d) 0 0 0
Condition0%0/3
defined $self->headchild and $self->headchild == $d
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->headchild and $self->headchild == $d
defined $self->headchild $self->headchild == $d
Truth table0%0/3
A and B
A= defined $self->headchildB= $self->headchild == $d
ABresult
0X0
100
111
1068 0 $self->clear_headchild();
1069 }
1070  
1071 0 splice @{$self->children}, $index, 1, (); # replace with empty list
1072 }
1073 ##################################################################
1074 0 ▶sub prepend {
Subroutine prepend: not called CC=1 SCAR=6.9
Pod documented
1075 0 my __PACKAGE__ $self = shift;
1076 0 my @daughters = @_;
1077 0 $self->insert_at(0, @daughters);
1078 }
1079 ##################################################################
1080 4370 ▶sub append {
Subroutine append: called CC=1 SCAR=0.0
Pod documented
1081 4370 my __PACKAGE__ $self = shift;
1082 4370 my @daughters = @_;
1083 4370 $self->insert_at(scalar @{$self->children}, @daughters);
1084 }
1085 ##################################################################
1086 0 ▶sub insert_before {
Subroutine insert_before: not called CC=1 SCAR=6.9
Pod undocumented
1087 0 my __PACKAGE__ $self = shift;
1088  
1089 0 my $parent = $self->parent();
1090 0 my $position = $parent->get_index($self);
1091  
1092 0 my @sibs = @_;
1093 0 $parent->insert_at($position, @sibs);
1094  
1095 0 return $self;
1096 }
1097 ##################################################################
1098 0 ▶sub insert_after {
Subroutine insert_after: not called CC=1 SCAR=6.9
Pod undocumented
1099 0 my __PACKAGE__ $self = shift;
1100  
1101 0 my $parent = $self->parent();
1102 0 my $position = $parent->get_index($self);
1103  
1104 0 my @sibs = @_;
1105 0 $parent->insert_at($position + 1, @sibs);
1106  
1107 0 return $self;
1108 }
1109 ##################################################################
1110 4370 ▶sub insert_at {
Subroutine insert_at: called CC=2 SCAR=6.9
Pod documented
1111 4370 my __PACKAGE__ $self = shift;
1112 4370 my $position = shift;
1113 4370 my @daughters = @_;
1114  
1115 4370 foreach my __PACKAGE__ $d (@daughters) {
1116 4370 $d->parent($self);
1117 }
1118  
1119 4370 splice @{$self->children}, $position, 0, @daughters;
1120 4370 return $self;
1121 }
1122 ##################################################################
1123 # FEATURES OF THE CURRENT NODE
1124 ##################################################################
1125 55 ▶sub is_root {
Subroutine is_root: called CC=1 SCAR=0.0
Pod documented
1126 55 my __PACKAGE__ $self = shift;
1127 55 return ( not defined $self->[PARENT] );
1128 }
1129 ##################################################################
1130 # Is this an empty root node?
1131 #
1132 # my $text = <<EOTREE;
1133 # (
1134 # (INTJ
1135 # (UH Okay)
1136 # (. .)
1137 # (-DFL- E_S)))
1138 # EOTREE
1139 #
1140 # my $node = Lingua::Treebank::TB3Const->new->from_penn_string($text)
1141 # print "This is true." if ($node->is_empty_root());
1142 #
1143 1 ▶sub is_empty_root {
Subroutine is_empty_root: called CC=3 SCAR=13.3
Pod documented
1144 1 my __PACKAGE__ $self = shift;
1145  
1146 return ($self->is_root() and
1147 not $self->tag() and
1148 1 ▶ scalar(@{$self->children()}) == 1 )
Condition33%2/6
$self->is_root && !$self->tag && scalar @{$self->children;} == 1
!ll&&!rl&&r
001
$self->is_root and not $self->tag
!ll&&!rl&&r
001
MC/DC0%0/3
$self->is_root && !$self->tag && scalar @{$self->children;} == 1
$self->is_root not $self->tag scalar @{$self->children;} == 1
Truth table25%1/4
A and not(B) && C
A= $self->is_rootB= not $self->tagC= scalar @{$self->children;} == 1
ABCresult
0XX0
10X0
1100
1111
1149 }
1150 #################################################################
1151 4944 ▶sub is_terminal {
Subroutine is_terminal: called CC=4 SCAR=14.8
Pod documented
1152 4944 my __PACKAGE__ $self = shift;
1153 4944 ▶ if (defined $self->[WORD]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[3]) { } 2585 2359 4944
1154 2585 ▶ if ( @{$self->children()} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->children;}) 0 2585 2585
1155 0 carp "how did I get children AND a word?";
1156 }
1157 2585 return 1;
1158 }
1159 else {
1160 2359 ▶ if ( not @{ $self->children() } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->children;}) 0 2359 2359
1161 0 croak "how did I get neither a word NOR children?";
1162 0 return 1; # might as well terminate
1163 }
1164 2359 return 0;
1165 }
1166 }
1167 ##################################################################
1168 18682 ▶sub children {
Subroutine children: called CC=8 SCAR=29.4
Pod documented
1169 18682 my $self = shift;
1170 18682 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 0 18682 18682
1171 0 croak "children() called with >2 args";
1172 }
1173 18682 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) 0 18682 18682
1174 # e.g. $d->children(1, $foo_child);
1175 0 ▶ croak "wrong package type: ", ref($_[1]),
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($_[1], __PACKAGE__) 0 0 0
1176 " . Expecting ", __PACKAGE__
1177 unless UNIVERSAL::isa($_[1], __PACKAGE__);
1178  
1179 0 return $self->[ CHILDREN ][ $_[0] ] = $_[1];
1180  
1181 }
1182 18682 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 42 18640 18682
1183 42 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 0 42 42
1184 # reset entire array,
1185 # e.g. $d->children([ $foo, $bar ])
1186 0 foreach (@{$_[1]}) {
1187 0 ▶ if (not UNIVERSAL::isa($_, __PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($_, __PACKAGE__)) 0 0 0
1188 0 croak "ref ", ref $_, " in arrayref not a ",
1189 __PACKAGE__;
1190 }
1191 }
1192 0 $self->[ CHILDREN ] = $_[1];
1193 }
1194 else {
1195 # getting single element
1196 # e.g. $d->children(2);
1197 42 return $self->[ CHILDREN ][ $_[0] ];
1198 }
1199 }
1200 # else no args
1201 18640 return $self->[ CHILDREN ];
1202 }
1203 ##################################################################
1204 0 ▶sub num_children {
Subroutine num_children: not called CC=1 SCAR=6.9
Pod undocumented
1205 0 my $self = shift;
1206 0 return scalar @{$self->[ CHILDREN ]};
1207 }
1208 ##################################################################
1209 # Functions for headed trees
1210 ##################################################################
1211 0 ▶sub capitalize_headed {
Subroutine capitalize_headed: not called CC=4 SCAR=30.0
Pod undocumented
1212 0 my __PACKAGE__ $self = shift;
1213 0 ▶ if ($self->is_terminal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) 0 0 0
1214 0 return;
1215 }
1216 0 my $head = $self->headchild();
1217 0 for my $kid (@{$self->children}) {
1218 0 ▶ if ($kid == $head) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kid == $head) { } 0 0 0
1219 0 $kid->tag(uc $kid->tag());
1220 }
1221 else {
1222 0 $kid->tag(lc $kid->tag());
1223 }
1224 0 $kid->capitalize_headed();
1225 }
1226 }
1227  
1228 0 ▶sub maximal_projection {
Subroutine maximal_projection: not called CC=3 SCAR=24.8
Pod undocumented
1229 # given a node (usually a leaf!) climb the tree until I'm not the
1230 # headword any more
1231 0 my __PACKAGE__ $self = shift;
1232 0 my $maximal = $self;
1233  
1234 CLIMB:
1235 0 while (1) {
1236 0 my $parent = $maximal->parent();
1237 0 ▶ if (not defined $parent or $parent->headterminal() != $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $parent or $parent->headterminal != $self) 0 0 0
Condition0%0/3
not defined $parent or $parent->headterminal != $self
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $parent or $parent->headterminal != $self
not defined $parent $parent->headterminal != $self
Truth table0%0/3
not(A) or B
A= not defined $parentB= $parent->headterminal != $self
ABresult
1X1
011
000
1238 0 last CLIMB; # done! $maximal is it
1239 }
1240 0 $maximal = $parent;
1241 }
1242 0 return $maximal;
1243 }
1244 ##################################################################
1245 0 ▶sub clear_headchild {
Subroutine clear_headchild: not called CC=1 SCAR=6.9
Pod undocumented
1246 0 my __PACKAGE__ $self = shift;
1247 0 $self->[HEADCHILD] = undef;
1248 }
1249 ##################################################################
1250 0 ▶sub headterminal {
Subroutine headterminal: not called CC=3 SCAR=24.8
Pod undocumented
1251 0 my __PACKAGE__ $self = shift;
1252 0 ▶ if ($self->is_terminal()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_terminal) 0 0 0
1253 0 return $self;
1254 }
1255 0 my $headchild = $self->headchild();
1256  
1257 0 ▶ return undef if not defined $headchild;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headchild 0 0 0
1258  
1259 0 return $headchild->headterminal();
1260 }
1261 ##################################################################
1262 69 ▶sub headchild {
Subroutine headchild: called CC=5 SCAR=28.8
Pod undocumented
1263 69 my __PACKAGE__ $self = shift;
1264 69 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 69 69
1265 # setting
1266 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
1267 0 croak "->headchild() called with >1 argument";
1268 }
1269 0 my $val = $_[0];
1270 0 ▶ croak "->headchild() argument wrong class"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($val, __PACKAGE__) 0 0 0
1271 if ( not UNIVERSAL::isa($val, __PACKAGE__) );
1272  
1273 0 ▶ if (not grep { $val == $_ } @{$self->[ CHILDREN ]}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$val == $_;} @{$$self[5];}) 0 0 0
1274 0 croak "->headchild() setting used value that wasn't ",
1275 "one of its kids";
1276 }
1277 0 $self->[HEADCHILD] = $val;
1278 }
1279 else {
1280 # getting
1281 69 return $self->[HEADCHILD];
1282 }
1283 }
1284 ##################################################################
1285 4433 ▶sub parent {
Subroutine parent: called CC=4 SCAR=14.2
Pod documented
1286 4433 my __PACKAGE__ $self = shift;
1287 4433 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4370 63 4433
1288 # setting
1289 4370 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 4370 4370
1290 0 croak "parent called with >1 argument";
1291 }
1292 4370 my $val = $_[0];
1293 4370 ▶ croak "parent argument wrong class"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($val, __PACKAGE__) 0 4370 4370
1294 if ( not UNIVERSAL::isa($val, __PACKAGE__) );
1295 4370 $self->[PARENT] = $val;
1296 }
1297 else {
1298 # getting
1299 63 return $self->[PARENT];
1300 }
1301 }
1302 ##################################################################
1303 0 ▶sub clear_parent {
Subroutine clear_parent: not called CC=1 SCAR=6.9
Pod undocumented
1304 0 my $self = shift;
1305 0 $self->[PARENT] = undef;
1306 }
1307 ##################################################################
1308 5095 ▶sub tag {
Subroutine tag: called CC=4 SCAR=14.3
Pod documented
1309 5095 my __PACKAGE__ $self = shift;
1310 5095 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4743 352 5095
1311 # setting
1312 4743 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 4743 4743
1313 0 croak "tag() called with >1 argument";
1314 }
1315 4743 ▶ carp "tag() passed a reference!" if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 4743 4743
1316 4743 $self->[TAG] = $_[0];
1317 }
1318 else {
1319 # getting
1320 352 return $self->[TAG];
1321 }
1322 }
1323 ##################################################################
1324 637 ▶sub annot {
Subroutine annot: called CC=4 SCAR=14.3
Pod documented
1325 637 my __PACKAGE__ $self = shift;
1326 637 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 490 147 637
1327 # setting
1328 490 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 490 490
1329 0 croak "annot() called with >1 argument";
1330 }
1331 490 ▶ carp "annot() passed a reference!" if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 490 490
1332 490 $self->[ANNOT] = $_[0];
1333 }
1334 else {
1335 # getting
1336 147 return $self->[ANNOT];
1337 }
1338 }
1339 ##################################################################
1340 6995 ▶sub word {
Subroutine word: called CC=5 SCAR=17.6
Pod documented
1341 6995 my __PACKAGE__ $self = shift;
1342 6995 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 2489 4506 6995
1343 # setting
1344 2489 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 2489 2489
1345 0 croak "word() called with >1 argument";
1346 }
1347  
1348 2489 ▶ if (@{$self->[CHILDREN]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[5];}) 0 2489 2489
1349 0 croak "can't assign a word when children exist, failing!";
1350 0 return;
1351 }
1352  
1353 2489 ▶ carp "word() passed a reference!" if ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 2489 2489
1354 2489 $self->[WORD] = $_[0];
1355 }
1356 else {
1357 # getting
1358 4506 return $self->[WORD];
1359 }
1360 }
1361 ##################################################################
1362 10 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
1363 10 my __PACKAGE__ $self = shift;
1364 return join(" ",
1365 45 map {$_->word()}
1366 10 grep {$_->tag ne '-NONE-'}
1367 $self->get_all_terminals());
1368 }
1369 ##################################################################
1370 4743 ▶sub new {
Subroutine new: called CC=3 SCAR=13.0
Pod documented
1371 4743 my $class = shift;
1372 4743 my %args = @_;
1373 4743 my $self = bless [], $class;
1374 4743 $self->[CHILDREN] = [];
1375 4743 $self->[NUM] = $class->_next_numid();
1376 4743 foreach (keys %args) {
1377 0 ▶ if ($self->can($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($_)) { } 0 0 0
1378 0 $self->$_($args{$_});
1379 }
1380 else {
1381 0 carp "unknown argument $_";
1382 }
1383 }
1384 4743 return $self;
1385 }
1386 ##################################################################
1387  
1388 10 1;
1389  
1390 __END__
« blib/lib/Lingua/Treebank.pm ↑ Summary Const/t/50walking.t »