lib/Treex/Core/Node/A.pm

filter: Statementstmt 20.4% 29 / 142 Branchbran 6.2% 3 / 48 Conditioncond 22.2% 2 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 28.0% 7 / 25 Podpod 52.3% 11 / 21 totaltotal 21.1% 53 / 251 CC 33 SCAR 40.1
CC
33
Cov
17%
set_real_afun
156.0
get_real_afun
110.0
get_nonterminal_pnodes
12.0
CRAP 55.1
« lib/Treex/Core/Node/InClause.pm ↑ Summary lib/Treex/Block/Util/Eval.pm »
1 package Treex::Core::Node::A;
2 {
3 60 $Treex::Core::Node::A::VERSION = '0.08633_1';
4 }
5  
6 60 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 60 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 extends 'Treex::Core::Node';
10 60 with 'Treex::Core::Node::Ordered';
11 60 with 'Treex::Core::Node::InClause';
12 60 with 'Treex::Core::Node::EffectiveRelations';
13 #with 'Treex::Core::Node::Interset';
14  
15 # Original w-layer and m-layer attributes
16 60 has [qw(form lemma tag no_space_after)] => ( is => 'rw' );
17  
18 # Original a-layer attributes
19 60 has [
20 qw(afun is_parenthesis_root edge_to_collapse is_auxiliary)
21 ] => ( is => 'rw' );
22  
23 sub get_pml_type_name {
24 43 ▶ my ($self) = @_;
Subroutine get_pml_type_name: called CC=2 SCAR=6.9
Pod documented
25 43 ▶ return $self->is_root() ? 'a-root.type' : 'a-node.type';
Branch: 1 branch
BranchTrueFalseTotal
$self->is_root ? : 16 27 43
26 }
27  
28 # the node is a root of a coordination/apposition construction
29 sub is_coap_root {
30 51 ▶ my ($self) = @_;
Subroutine is_coap_root: called CC=3 SCAR=11.4
Pod documented
31 51 ▶ log_fatal('Incorrect number of arguments') if @_ != 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 51 51
32 51 ▶ return defined $self->afun && $self->afun =~ /^(Coord|Apos)$/;
Condition66%2/3
defined $self->afun && $self->afun =~ /^(Coord|Apos)$/
!ll&&!rl&&r
03714
MC/DC50%1/2
defined $self->afun && $self->afun =~ /^(Coord|Apos)$/
defined $self->afun $self->afun =~ /^(Coord|Apos)$/
Truth table66%2/3
A && B
A= defined $self->afunB= $self->afun =~ /^(Coord|Apos)$/
ABresult
0X0
100
111
33 }
34  
35 sub n_node {
36 7 ▶ my ($self) = @_;
Subroutine n_node: called CC=1 SCAR=0.0
Pod undocumented
37 7 my ($first_n_node) = $self->get_referencing_nodes('a.rf');
38 7 return $first_n_node;
39 }
40  
41 #------------------------------------------------------------------------------
42 # Figures out the real function of the subtree. If its own afun is AuxP or
43 # AuxC, finds the first descendant with a real afun and returns it. If this is
44 # a coordination or apposition root, finds the first member and returns its
45 # afun (but note that members of the same coordination can differ in afuns if
46 # some of them have 'ExD').
47 #------------------------------------------------------------------------------
48 sub get_real_afun
49 {
50 0 ▶ my $self = shift;
Subroutine get_real_afun: not called CC=10 SCAR=47.0
Pod documented
51 0 my $warnings = shift;
52 0 my $afun = $self->afun();
53 0 ▶ if ( not defined($afun) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $afun) 0 0 0
54 0 $afun = '';
55 }
56 0 ▶ if ( $afun =~ m/^Aux[PC]$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($afun =~ /^Aux[PC]$/) { } 0 0 0
elsif ($self->is_coap_root) { } 0 0 0
57 {
58 0 my @children = $self->children();
59 0 my $n = scalar(@children);
60 0 ▶ if ( $n < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) { } 0 0 0
61 {
62 0 ▶ if ($warnings)
Branch: 1 branch
BranchTrueFalseTotal
if ($warnings) 0 0 0
63 {
64 0 my $i_sentence = $self->get_bundle()->get_position() + 1; # tred numbers from 1
65 0 my $form = $self->form();
66 0 log_warn("$afun node does not have children (sentence $i_sentence, '$form')");
67 }
68 }
69 else
70 {
71 0 ▶ if ( $n > 1 && $warnings )
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 1 and $warnings) 0 0 0
Condition0%0/3
$n > 1 and $warnings
!ll&&!rl&&r
000
MC/DC0%0/2
$n > 1 and $warnings
$n > 1 $warnings
Truth table0%0/3
A and B
A= $n > 1B= $warnings
ABresult
0X0
100
111
72 {
73 0 my $i_sentence = $self->get_bundle()->get_position() + 1; # tred numbers from 1
74 0 my $form = $self->form();
75 0 log_warn("$afun node has $n children so it is not clear which one bears the real afun (sentence $i_sentence, '$form')");
76 }
77 0 return $children[0]->get_real_afun();
78 }
79 }
80 elsif ( $self->is_coap_root() )
81 {
82 0 my @members = $self->get_coap_members();
83 0 my $n = scalar(@members);
84 0 ▶ if ( $n < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) { } 0 0 0
85 {
86 0 ▶ if ($warnings)
Branch: 1 branch
BranchTrueFalseTotal
if ($warnings) 0 0 0
87 {
88 0 my $i_sentence = $self->get_bundle()->get_position() + 1; # tred numbers from 1
89 0 my $form = $self->form();
90 0 log_warn("$afun does not have members (sentence $i_sentence, '$form')");
91 }
92 }
93 else
94 {
95 0 return $members[0]->get_real_afun();
96 }
97 }
98 0 return $afun;
99 }
100  
101 #------------------------------------------------------------------------------
102 # Sets the real function of the subtree. If its current afun is AuxP or AuxC,
103 # finds the first descendant with a real afun replaces it. If this is
104 # a coordination or apposition root, finds all the members and replaces their
105 # afuns (but note that members of the same coordination can differ in afuns if
106 # some of them have 'ExD'; this method can only set the same afun for all).
107 #------------------------------------------------------------------------------
108 sub set_real_afun
109 {
110 0 ▶ my $self = shift;
Subroutine set_real_afun: not called CC=12 SCAR=50.5
Pod documented
111 0 my $new_afun = shift;
112 0 my $warnings = shift;
113 0 my $afun = $self->afun();
114 0 ▶ if ( not defined($afun) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $afun) 0 0 0
115 0 $afun = '';
116 }
117 0 ▶ if ( $afun =~ m/^Aux[PC]$/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($afun =~ /^Aux[PC]$/) { } 0 0 0
elsif ($self->is_coap_root) { } 0 0 0
118 {
119 0 my @children = $self->children();
120 0 my $n = scalar(@children);
121 0 ▶ if ( $n < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) { } 0 0 0
122 {
123 0 ▶ if ($warnings)
Branch: 1 branch
BranchTrueFalseTotal
if ($warnings) 0 0 0
124 {
125 0 my $i_sentence = $self->get_bundle()->get_position() + 1; # tred numbers from 1
126 0 my $form = $self->form();
127 0 log_warn("$afun node does not have children (sentence $i_sentence, '$form')");
128 }
129 }
130 else
131 {
132 0 ▶ if ( $warnings && $n > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($warnings and $n > 1) 0 0 0
Condition0%0/3
$warnings and $n > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$warnings and $n > 1
$warnings $n > 1
Truth table0%0/3
A and B
A= $warningsB= $n > 1
ABresult
0X0
100
111
133 {
134 0 my $i_sentence = $self->get_bundle()->get_position() + 1; # tred numbers from 1
135 0 my $form = $self->form();
136 0 log_warn("$afun node has $n children so it is not clear which one bears the real afun (sentence $i_sentence, '$form')");
137 }
138 0 foreach my $child (@children)
139 {
140 0 $child->set_real_afun($new_afun);
141 }
142 0 return;
143 }
144 }
145 elsif ( $self->is_coap_root() )
146 {
147 0 my @members = $self->get_coap_members();
148 0 my $n = scalar(@members);
149 0 ▶ if ( $n < 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 1) { } 0 0 0
150 {
151 0 ▶ if ($warnings)
Branch: 1 branch
BranchTrueFalseTotal
if ($warnings) 0 0 0
152 {
153 0 my $i_sentence = $self->get_bundle()->get_position() + 1; # tred numbers from 1
154 0 my $form = $self->form();
155 0 log_warn("$afun does not have members (sentence $i_sentence, '$form')");
156 }
157 }
158 else
159 {
160 0 foreach my $member (@members)
161 {
162 0 $member->set_real_afun($new_afun);
163 }
164 0 return;
165 }
166 }
167 0 $self->set_afun($new_afun);
168 0 return $afun;
169 }
170  
171 #------------------------------------------------------------------------------
172 # Recursively copy children from myself to another node.
173 # This function is specific to the A layer because it contains the list of
174 # attributes. If we could figure out the list automatically, the function would
175 # become general enough to reside directly in Node.pm.
176 #
177 # NOTE: We could possibly make just copy_attributes() layer-dependent and unify
178 # the main copy_atree code.
179 #------------------------------------------------------------------------------
180 sub copy_atree
181 {
182 0 ▶ my $self = shift;
Subroutine copy_atree: not called CC=2 SCAR=17.9
Pod documented
183 0 my $target = shift;
184  
185 # TODO probably we should do deepcopy
186 # Why is this here ? the attributes of the root node are NOT copied, are they ??
187 0 my %copy_of_wild = %{$self->wild};
188 0 $target->set_wild(\%copy_of_wild);
189  
190 0 my @children0 = $self->get_children( { ordered => 1 } );
191 0 foreach my $child0 (@children0)
192 {
193  
194 # Create a copy of the child node.
195 0 my $child1 = $target->create_child();
196  
197 # Copy all attributes of the original node to the new one
198 0 $child0->copy_attributes($child1);
199  
200 # Call recursively on the subtrees of the children.
201 0 $child0->copy_atree($child1);
202 }
203  
204 0 return;
205 }
206  
207  
208 sub copy_attributes {
209 0 ▶ my ($self, $other) = @_;
Subroutine copy_attributes: not called CC=2 SCAR=17.9
Pod undocumented
210  
211 # We should copy all attributes that the node has but it is not easy to figure out which these are.
212 # TODO: As a workaround, we list the attributes here directly.
213 0 foreach my $attribute (
214 'form', 'lemma', 'tag', 'no_space_after', 'ord', 'afun', 'is_member', 'is_parenthesis_root',
215 'conll/deprel', 'conll/cpos', 'conll/pos', 'conll/feat', 'is_shared_modifier', 'morphcat',
216 )
217 {
218 0 my $value = $self->get_attr($attribute);
219 0 $other->set_attr( $attribute, $value );
220 }
221  
222 # TODO probably we should do deepcopy
223 0 my %copy_of_wild = %{$self->wild};
224 0 $other->set_wild(\%copy_of_wild);
225  
226 0 return;
227 }
228  
229 # -- linking to p-layer --
230  
231 sub get_terminal_pnode {
232 0 ▶ my ($self) = @_;
Subroutine get_terminal_pnode: not called CC=2 SCAR=17.9
Pod documented
233 0 ▶ my $p_rf = $self->get_attr('p_terminal.rf') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $p_rf = $self->get_attr("p_terminal.rf") or return 0 0 0
234 0 my $doc = $self->get_document();
235 0 return $doc->get_node_by_id($p_rf);
236 }
237  
238 sub set_terminal_pnode {
239 0 ▶ my ($self, $pnode) = @_;
Subroutine set_terminal_pnode: not called CC=2 SCAR=17.9
Pod documented
240 0 ▶ my $new_id = defined $pnode ? $pnode->id : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $pnode ? : 0 0 0
241 0 $self->set_attr( 'p_terminal.rf', $new_id );
242 0 return;
243 }
244  
245 sub get_nonterminal_pnodes {
246 0 ▶ my ($self) = @_;
Subroutine get_nonterminal_pnodes: not called CC=3 SCAR=24.8
Pod documented
247 0 ▶ my $pnode = $self->get_terminal_pnode() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pnode = $self->get_terminal_pnode or return 0 0 0
248 0 my @nonterminals = ();
249 0 ▶ while ( $pnode->is_head ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pnode->is_head) 0 0 0
250 0 $pnode = $pnode->get_parent();
251 0 push @nonterminals, $pnode;
252 }
253 0 return @nonterminals;
254 }
255  
256 sub get_pnodes {
257 0 ▶ my ($self) = @_;
Subroutine get_pnodes: not called CC=1 SCAR=6.9
Pod documented
258 0 return ( $self->get_terminal_pnode, $self->get_nonterminal_pnodes );
259 }
260  
261 # -- referenced node ids --
262  
263 override '_get_reference_attrs' => sub {
264 61 ▶ my ($self) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
265 61 return ('p_terminal.rf');
266 60 };
267  
268 # -- other --
269  
270 # Used only for Czech, so far.
271 sub reset_morphcat {
272 0 ▶ my ($self) = @_;
Subroutine reset_morphcat: not called CC=3 SCAR=24.8
Pod documented
273 0 foreach my $category (
274 qw(pos subpos gender number case possgender possnumber
275 person tense grade negation voice reserve1 reserve2)
276 )
277 {
278 0 my $old_value = $self->get_attr("morphcat/$category");
279 0 ▶ if ( !defined $old_value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $old_value) 0 0 0
280 0 $self->set_attr( "morphcat/$category", '.' );
281 }
282 }
283 0 return;
284 }
285  
286 # Used only for reading from PCEDT/PDT trees, so far.
287 sub get_subtree_string {
288 0 ▶ my ($self) = @_;
Subroutine get_subtree_string: not called CC=2 SCAR=17.9
Pod documented
289 0 ▶ return join '', map { $_->form . ( $_->no_space_after ? '' : ' ' ) } $self->get_descendants( { ordered => 1 } );
Branch: 1 branch
BranchTrueFalseTotal
$_->no_space_after ? : 0 0 0
290 }
291  
292 #----------- CoNLL attributes -------------
293  
294 0 ▶sub conll_deprel { return $_[0]->get_attr('conll/deprel'); }
Subroutine conll_deprel: not called CC=1 SCAR=6.9
Pod undocumented
295 0 ▶sub conll_cpos { return $_[0]->get_attr('conll/cpos'); }
Subroutine conll_cpos: not called CC=1 SCAR=6.9
Pod undocumented
296 0 ▶sub conll_pos { return $_[0]->get_attr('conll/pos'); }
Subroutine conll_pos: not called CC=1 SCAR=6.9
Pod undocumented
297 0 ▶sub conll_feat { return $_[0]->get_attr('conll/feat'); }
Subroutine conll_feat: not called CC=1 SCAR=6.9
Pod undocumented
298  
299 0 ▶sub set_conll_deprel { return $_[0]->set_attr( 'conll/deprel', $_[1] ); }
Subroutine set_conll_deprel: not called CC=1 SCAR=6.9
Pod undocumented
300 0 ▶sub set_conll_cpos { return $_[0]->set_attr( 'conll/cpos', $_[1] ); }
Subroutine set_conll_cpos: not called CC=1 SCAR=6.9
Pod undocumented
301 0 ▶sub set_conll_pos { return $_[0]->set_attr( 'conll/pos', $_[1] ); }
Subroutine set_conll_pos: not called CC=1 SCAR=6.9
Pod undocumented
302 0 ▶sub set_conll_feat { return $_[0]->set_attr( 'conll/feat', $_[1] ); }
Subroutine set_conll_feat: not called CC=1 SCAR=6.9
Pod undocumented
303  
304 60 1;
305  
306 __END__
307  
308 ######## QUESTIONABLE / DEPRECATED METHODS ###########
309  
310  
311 # For backward compatibility with PDT-style
312 # TODO: This should be handled in format converters/Readers.
313 sub is_coap_member {
314 my ($self) = @_;
315 log_fatal("Incorrect number of arguments") if @_ != 1;
316 return (
317 $self->is_member
318 || ( ( $self->afun || '' ) =~ /^Aux[CP]$/ && grep { $_->is_coap_member } $self->get_children )
319 )
320 ? 1 : undef;
321 }
322  
323 # deprecated, use get_coap_members
324 sub get_transitive_coap_members { # analogy of PML_T::ExpandCoord
325 my ($self) = @_;
326 log_fatal("Incorrect number of arguments") if @_ != 1;
327 if ( $self->is_coap_root ) {
328 return (
329 map { $_->is_coap_root ? $_->get_transitive_coap_members : ($_) }
330 grep { $_->is_coap_member } $self->get_children
331 );
332 }
333 else {
334  
335 #log_warn("The node ".$self->get_attr('id')." is not root of a coordination/apposition construction\n");
336 return ($self);
337 }
338 }
339  
340 # deprecated, get_coap_members({direct_only})
341 sub get_direct_coap_members {
342 my ($self) = @_;
343 log_fatal("Incorrect number of arguments") if @_ != 1;
344 if ( $self->is_coap_root ) {
345 return ( grep { $_->is_coap_member } $self->get_children );
346 }
347 else {
348  
349 #log_warn("The node ".$self->get_attr('id')." is not root of a coordination/apposition construction\n");
350 return ($self);
351 }
352 }
353  
354 # too easy to implement and too rarely used to be a part of API
355 sub get_transitive_coap_root { # analogy of PML_T::GetNearestNonMember
356 my ($self) = @_;
357 log_fatal("Incorrect number of arguments") if @_ != 1;
358 while ( $self->is_coap_member ) {
359 $self = $self->get_parent;
360 }
361 return $self;
362 }
363  
364  
365 =encoding utf-8
366  
367 =head1 NAME
368  
369 Treex::Core::Node::A
370  
371 =head1 VERSION
372  
373 version 0.08633_1
374  
375 =head1 DESCRIPTION
376  
377 a-layer (analytical) node
378  
379 =head1 ATTRIBUTES
380  
381 For each attribute (e.g. C<tag>), there is
382 a getter method (C<< my $tag = $anode->tag(); >>)
383 and a setter method (C<< $anode->set_tag('NN'); >>).
384  
385 =head2 Original w-layer and m-layer attributes
386  
387 =over
388  
389 =item form
390  
391 =item lemma
392  
393 =item tag
394  
395 =item no_space_after
396  
397 =back
398  
399 =head2 Original a-layer attributes
400  
401 =over
402  
403 =item afun
404  
405 =item is_parenthesis_root
406  
407 =item edge_to_collapse
408  
409 =item is_auxiliary
410  
411 =back
412  
413 =head1 METHODS
414  
415 =head2 Links from a-trees to phrase-structure trees
416  
417 =over 4
418  
419 =item $node->get_terminal_pnode
420  
421 Returns a terminal node from the phrase-structure tree
422 that corresponds to the a-node.
423  
424 =item $node->set_terminal_pnode($pnode)
425  
426 Set the given terminal node from the phrase-structure tree
427 as corresponding to the a-node.
428  
429 =item $node->get_nonterminal_pnodes
430  
431 Returns an array of non-terminal nodes from the phrase-structure tree
432 that correspond to the a-node.
433  
434 =item $node->get_pnodes
435  
436 Returns the corresponding terminal node and all non-terminal nodes.
437  
438 =back
439  
440 =head2 Other
441  
442 =over 4
443  
444 =item reset_morphcat
445  
446 =item get_pml_type_name
447  
448 Root and non-root nodes have different PML type in the pml schema
449 (C<a-root.type>, C<a-node.type>)
450  
451 =item is_coap_root
452  
453 Is this node a root (or head) of a coordination/apposition construction?
454 On a-layer this is decided based on C<afun =~ /^(Coord|Apos)$/>.
455  
456 =item get_real_afun()
457  
458 Figures out the real function of the subtree. If its own afun is C<AuxP> or
459 C<AuxC>, finds the first descendant with a real afun and returns it. If this is
460 a coordination or apposition root, finds the first member and returns its
461 afun (but note that members of the same coordination can differ in afuns if
462 some of them have C<ExD>).
463  
464 =item set_real_afun($new_afun)
465  
466 Sets the real function of the subtree. If its current afun is C<AuxP> or C<AuxC>,
467 finds the first descendant with a real afun replaces it. If this is
468 a coordination or apposition root, finds all the members and replaces their
469 afuns (but note that members of the same coordination can differ in afuns if
470 some of them have C<ExD>; this method can only set the same afun for all).
471  
472 =item copy_atree()
473  
474 Recursively copy children from myself to another node.
475 This method is specific to the A layer because it contains the list of
476 attributes. If we could figure out the list automatically, the method would
477 become general enough to reside directly in Node.pm.
478  
479 =item get_n_node()
480  
481 If this a-node is a part of a named entity,
482 this method returns the corresponding n-node (L<Treex::Core::Node::N>).
483 If this node is a part of more than one named entities,
484 only the most nested one is returned.
485 For example: "Bank of China"
486  
487 $n_node_for_china = $a_node_china->get_n_node();
488 print $n_node_for_china->get_attr('normalized_name'); # China
489 $n_node_for_bank_of_china = $n_node_for_china->get_parent();
490 print $n_node_for_bank_of_china->get_attr('normalized_name'); # Bank of China
491  
492 =item $node->get_subtree_string
493  
494 Return the string corresponding to a subtree rooted in C<$node>.
495 It's computed based on attributes C<form> and C<no_space_after>.
496  
497 =back
498  
499  
500 =head1 AUTHOR
501  
502 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
503  
504 Martin Popel <popel@ufal.mff.cuni.cz>
505  
506 =head1 COPYRIGHT AND LICENSE
507  
508 Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
509  
510 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Core/Node/InClause.pm ↑ Summary lib/Treex/Block/Util/Eval.pm »