Treex/Core/Node/EffectiveRelations.pm

filter: Statementstmt 71.6% 106 / 148 Branchbran 65.4% 55 / 84 Conditioncond 58.8% 10 / 17 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 76.1% 16 / 21 Podpod 60.0% 3 / 5 totaltotal 67.4% 193 / 286 CC 52 SCAR 21.8
CC
52
Cov
69%
get_esiblings
30.0
_get_eparents
13.0
_fallback_parent
12.0
CRAP 8.8
« Treex/Core/Node/T.pm ↑ Summary Treex/Core/Files.pm »
1 package Treex::Core::Node::EffectiveRelations;
2 38 $Treex::Core::Node::EffectiveRelations::VERSION = '2.20210102';
3 38 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # with Moose >= 2.00, this must be present also in roles
6 38 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 38 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 38 has is_member => (
10 is => 'rw',
11 isa => 'Bool',
12 documentation => 'Is this node a member of a coordination (i.e. conjunct) or apposition?',
13 );
14  
15 # Shared modifiers of coordinations can be distinguished in PDT style
16 # just based on the fact they are hanged on the conjunction (coord. head).
17 # However, in other styles (e.g. Stanford) this attribute might be useful.
18 38 has is_shared_modifier => (
19 is => 'rw',
20 isa => 'Bool',
21 documentation => 'Is this node a shared modifier of a coordination?',
22 );
23  
24 38 requires 'is_coap_root';
25  
26 # Implementation details:
27 # Members of a coordination can be distinguished from shared modifiers
28 # according to the attribute is_member. The only problem is when some
29 # members are hanged on prepositions(afun=AuxP) or subord. conjunctions(AuxC).
30 # The PDT style is that Aux[CP] nodes can never have is_member=1, resulting in
31 # e.g. "It was in(parent=and) Prague(parent=in,is_member=1)
32 # and(parent=was) in(parent=and) London(parent=in,is_member=1)."
33 # The style adpoted in Treex is that members have always is_member=1
34 # no matter what afun they have. This results in:
35 # "It was in(parent=and,is_member=1) Prague(parent=in)
36 # and(parent=was) in(parent=and,is_member=1) London(parent=in)."
37 # Both annotation styles have their pros and cons.
38  
39 12 ▶sub get_echildren {
Subroutine get_echildren: called CC=4 SCAR=14.0
Pod documented
40 12 my ( $self, $arg_ref ) = @_;
41 12 ▶ if ( !defined $arg_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg_ref) 0 12 12
42 0 $arg_ref = {};
43 }
44 12 ▶ log_fatal('Incorrect number of arguments') if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 12 12
45  
46 12 my @echildren;
47 12 ▶ if ( $self->_can_apply_eff($arg_ref) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_can_apply_eff($arg_ref)) { } 9 3 12
48 # 1) Get my own effective children (i.e. I am their only eff. parent).
49 # These are in my subtree.
50 9 @echildren = $self->_get_my_own_echildren($arg_ref);
51  
52 # 2) Add shared effective children
53 # (i.e. I am their eff. parent, but not the only one).
54 # This can happen only if I am member of a coordination
55 # and these eff. children are shared modifiers of the coordination.
56 9 push @echildren, $self->_get_shared_echildren($arg_ref);
57 } else {
58 3 @echildren = $self->get_children();
59 }
60  
61 # 3) Process eventual switches (ordered=>1, add_self=>1,...)
62 #return @echildren if !$arg_ref; TODO this cannot happen now, see $arg_ref = {} if !defined $arg_ref;
63 12 delete $arg_ref->{dive};
64 12 delete $arg_ref->{or_topological};
65 12 delete $arg_ref->{ignore_incorrect_tree_structure};
66 12 return $self->_process_switches( $arg_ref, @echildren );
67 }
68  
69 10 ▶sub get_eparents {
Subroutine get_eparents: called CC=4 SCAR=14.6
Pod documented
70 10 my ( $self, $arg_ref ) = @_;
71 10 ▶ if ( !defined $arg_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg_ref) 0 10 10
72 0 $arg_ref = {};
73 }
74 10 ▶ log_fatal('Incorrect number of arguments') if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 10 10
75  
76 # Get effective parents
77 10 ▶ my @eparents = $self->_can_apply_eff($arg_ref) ?
Branch: 1 branch
BranchTrueFalseTotal
$self->_can_apply_eff($arg_ref) ? : 10 0 10
78 $self->_get_eparents($arg_ref) : ($self->get_parent());
79  
80 # Process eventual switches (ordered=>1, add_self=>1,...)
81 10 delete $arg_ref->{dive};
82 10 delete $arg_ref->{or_topological};
83 10 delete $arg_ref->{ignore_incorrect_tree_structure};
84 10 return $self->_process_switches( $arg_ref, @eparents );
85 }
86  
87 10 ▶sub _get_eparents {
Subroutine _get_eparents: called CC=8 SCAR=25.7
88 10 my ( $self, $arg_ref ) = @_;
89  
90 # 0) Check if there is a topological parent.
91 # Otherwise, there is no chance getting effective parents.
92 10 ▶ if ( !$self->get_parent() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_parent) 0 10 10
93 0 my $id = $self->id;
94  
95 #TODO: log_fatal if !$robust
96 0 log_warn( "The node $id has no effective nor topological parent, using the root", 1 );
97 0 return $self->get_root();
98 }
99  
100 # 1) If $self is a member of a coordination/aposition,
101 # get the highest node representing $self -- i.e. the coord/apos root.
102 # Otherwise, let $node be $self.
103 10 ▶ my $node = $self->_get_transitive_coap_root($arg_ref) || $self;
Condition66%2/3
$self->_get_transitive_coap_root($arg_ref) || $self
l!l&&r!l&&!r
820
MC/DC0%0/2
$self->_get_transitive_coap_root($arg_ref) || $self
$self->_get_transitive_coap_root($arg_ref) $self
Truth table66%2/3
A || B
A= $self->_get_transitive_coap_root($arg_ref)B= $self
ABresult
1X1
011
000
104  
105 # 2) Get the parent
106 10 ▶ $node = $node->get_parent() or return $self->_fallback_parent($arg_ref);
Branch: 1 branch
BranchTrueFalseTotal
$node = $node->get_parent or return $self->_fallback_parent($arg_ref) 10 0 10
107  
108 # 3) If it is a node to be dived, look above for the first non-dive ancestor.
109 10 ▶ while ( $arg_ref->{dive}->($node) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"dive"}->($node)) 0 10 10
110 0 ▶ $node = $node->get_parent() or return $self->_fallback_parent($arg_ref);
Branch: 1 branch
BranchTrueFalseTotal
$node = $node->get_parent or return $self->_fallback_parent($arg_ref) 0 0 0
111 }
112  
113 # If $node is not a head of a coordination/aposition,
114 # it is the effective parent we are looking for.
115 10 ▶ return $node if !$node->is_coap_root();
Branch: 1 branch
BranchTrueFalseTotal
unless $node->is_coap_root 8 2 10
116  
117 # Otherwise, there can be more than one effective parent.
118 # All effective parents (of $self) are shared modifiers
119 # of the coordination rooted in $node.
120 2 my @eff = $node->get_coap_members($arg_ref);
121 2 ▶ return @eff if @eff;
Branch: 1 branch
BranchTrueFalseTotal
if @eff 2 0 2
122  
123 0 return $self->_fallback_parent($arg_ref);
124 }
125  
126 # --- Utility methods for get_echildren and get_eparents
127  
128 58 ▶sub _is_auxCP {
Subroutine _is_auxCP: called CC=2 SCAR=6.9
129 58 my ($self) = @_;
130 58 ▶ my $afun = $self->afun || '';
Condition100%2/2
$self->afun || ''
l!l
571
MC/DC100%1/1
$self->afun || ''
$self->afun
Truth table100%2/2
A
A= $self->afun
Aresult
11
00
131 58 return $afun =~ /^Aux[CP]$/;
132 }
133  
134 37 ▶sub _get_direct_coap_root {
Subroutine _get_direct_coap_root: called CC=8 SCAR=23.7
135 37 my ( $self, $arg_ref ) = @_;
136 37 ▶ my $parent = $self->get_parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->get_parent or return 37 0 37
137 37 ▶ return $parent if $self->get_attr('is_member');
Branch: 1 branch
BranchTrueFalseTotal
if $self->get_attr("is_member") 24 13 37
138 13 ▶ return if !$arg_ref->{dive} || $arg_ref->{dive}->($self);
Branch: 1 branch
BranchTrueFalseTotal
if not $arg_ref->{"dive"} or $arg_ref->{"dive"}->($self) 0 13 13
Condition33%1/3
not $arg_ref->{"dive"} or $arg_ref->{"dive"}->($self)
l!l&&r!l&&!r
0013
MC/DC0%0/2
not $arg_ref->{"dive"} or $arg_ref->{"dive"}->($self)
not $arg_ref->{"dive"} $arg_ref->{"dive"}->($self)
Truth table33%1/3
not(A) or B
A= not $arg_ref->{"dive"}B= $arg_ref->{"dive"}->($self)
ABresult
1X1
011
000
139 13 ▶ while ( $arg_ref->{dive}->($parent) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref->{"dive"}->($parent)) 2 11 13
140 2 ▶ return $parent->get_parent() if $parent->get_attr('is_member');
Branch: 1 branch
BranchTrueFalseTotal
if $parent->get_attr("is_member") 2 0 2
141 0 ▶ $parent = $parent->get_parent() or return;
Branch: 1 branch
BranchTrueFalseTotal
$parent = $parent->get_parent or return 0 0 0
142 }
143 11 return;
144 }
145  
146 10 ▶sub _get_transitive_coap_root {
Subroutine _get_transitive_coap_root: called CC=4 SCAR=13.9
147 10 my ( $self, $arg_ref ) = @_;
148 10 ▶ my $root = $self->_get_direct_coap_root($arg_ref) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $root = $self->_get_direct_coap_root($arg_ref) or return 8 2 10
149 8 ▶ while ( $root->get_attr('is_member') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->get_attr("is_member")) 6 8 14
150 6 ▶ $root = $root->_get_direct_coap_root($arg_ref) or return;
Branch: 1 branch
BranchTrueFalseTotal
$root = $root->_get_direct_coap_root($arg_ref) or return 6 0 6
151 }
152 8 return $root;
153 }
154  
155 22 ▶sub _can_apply_eff {
Subroutine _can_apply_eff: called CC=7 SCAR=20.5
156 22 my ( $self, $arg_ref ) = @_;
157 22 ▶ if ( !$arg_ref->{dive} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $arg_ref->{'dive'}) { } 11 11 22
elsif ($arg_ref->{'dive'} eq 'AuxCP') { } 11 0 11
158 11 ▶ $arg_ref->{dive} = sub {0};
Subroutine __ANON__: called CC=1 SCAR=0.0
159 }
160 elsif ( $arg_ref->{dive} eq 'AuxCP' ) {
161 11 $arg_ref->{dive} = \&_is_auxCP;
162 }
163 22 ▶ my $error = $arg_ref->{dive}->($self)
Branch: 2 branches
BranchTrueFalseTotal
$arg_ref->{'dive'}->($self) ? : 1 21 22
$self->is_coap_root ? : 2 19 21
164 ? 'a node that is "to be dived"'
165 : $self->is_coap_root() ? 'coap root' : 0;
166 22 ▶ return 1 if !$error;
Branch: 1 branch
BranchTrueFalseTotal
unless $error 19 3 22
167 3 ▶ return 0 if $arg_ref->{or_topological}; #TODO: document
Branch: 1 branch
BranchTrueFalseTotal
if $arg_ref->{"or_topological"} 3 0 3
168 0 my $method_name = ( caller 1 )[3];
169 0 my $id = $self->id;
170 0 log_warn( "$method_name called on $error ($id). Fallback to topological one.", 1 );
171 0 return 0;
172 }
173  
174 # used only if there is an error in the tree structure,
175 # eg. there is a coordination with no members or a non-root node with no parent
176 0 ▶sub _fallback_parent {
Subroutine _fallback_parent: not called CC=3 SCAR=24.8
177 0 my ( $self, $arg_ref ) = @_;
178 0 my $id = $self->get_attr('id');
179 log_warn "The node $id has no effective parent, using the topological one."
180 0 ▶ if ( !$arg_ref || !$arg_ref->{ignore_incorrect_tree_structure} );
Branch: 1 branch
BranchTrueFalseTotal
unless $arg_ref and $arg_ref->{"ignore_incorrect_tree_structure"} 0 0 0
Condition0%0/3
$arg_ref and $arg_ref->{"ignore_incorrect_tree_structure"}
!ll&&!rl&&r
000
MC/DC0%0/2
$arg_ref and $arg_ref->{"ignore_incorrect_tree_structure"}
$arg_ref $arg_ref->{"ignore_incorrect_tree_structure"}
Truth table0%0/3
A and B
A= $arg_refB= $arg_ref->{"ignore_incorrect_tree_structure"}
ABresult
0X0
100
111
181 0 return $self->get_parent();
182 }
183  
184 # Get my own effective children (i.e. I am their only eff. parent).
185 9 ▶sub _get_my_own_echildren {
Subroutine _get_my_own_echildren: called CC=4 SCAR=13.9
186 9 my ( $self, $arg_ref ) = @_;
187 9 my @members = ();
188 9 my @queue = $self->get_children();
189 9 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 6 9 15
190 6 my $node = shift @queue;
191 6 ▶ if ( $arg_ref->{dive}->($node) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg_ref->{'dive'}->($node)) { } 0 6 6
elsif ($node->is_coap_root) { } 3 3 6
192 0 push @queue, $node->get_children();
193 }
194 elsif ( $node->is_coap_root() ) {
195 3 push @members, $node->get_coap_members($arg_ref);
196  
197 #push @queue, grep { $_->get_attr('is_member') } $node->get_children();
198 }
199 else {
200 3 push @members, $node;
201 }
202 }
203 9 return @members;
204 }
205  
206 # Get shared effective children
207 # (i.e. I am their eff. parent but not the only one).
208 9 ▶sub _get_shared_echildren {
Subroutine _get_shared_echildren: called CC=3 SCAR=11.0
209 9 my ( $self, $arg_ref ) = @_;
210  
211 # Only members of coord/apos can have shared eff. children
212 9 ▶ my $coap_root = $self->_get_direct_coap_root($arg_ref) or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $coap_root = $self->_get_direct_coap_root($arg_ref) or return () 6 3 9
213 6 my @shared_echildren = ();
214  
215 # All shared modifiers of $coap_root are eff. children of $self.
216 # We must process all possibly nested coap_roots.
217 # In the first iteration, $self is one of children of $coap_root.
218 # (In case of "diving", it's not $self, but its governing Aux[CP].)
219 # However, it has is_member==1, so it won't get into @shared_echildren.
220 # Similarly for other iterations.
221 6 ▶ while ($coap_root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coap_root) 12 6 18
222 push @shared_echildren,
223 6 map { $_->get_coap_members($arg_ref) }
224 12 grep { !$_->get_attr('is_member') }
225 $coap_root->get_children();
226 12 $coap_root = $coap_root->_get_direct_coap_root($arg_ref);
227 }
228 6 return @shared_echildren;
229 }
230  
231 19 ▶sub get_coap_members {
Subroutine get_coap_members: called CC=9 SCAR=22.0
Pod documented
232 19 my ( $self, $arg_ref ) = @_;
233 19 ▶ log_fatal('Incorrect number of arguments') if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 19 19
234 19 ▶ return $self if !$self->is_coap_root();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_coap_root 6 13 19
235 13 my $direct_only = $arg_ref->{direct_only};
236 13 ▶ my $dive = $arg_ref->{dive} || sub {0};
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$arg_ref->{'dive'} || sub { 0; }
l!l&&r!l&&!r
940
MC/DC0%0/2
$arg_ref->{'dive'} || sub { 0; }
$arg_ref->{'dive'} sub { 0; }
Truth table66%2/3
A || B
A= $arg_ref->{'dive'}B= sub { 0; }
ABresult
1X1
011
000
237 13 ▶ if ( $dive eq 'AuxCP' ) { $dive = \&_is_auxCP; }
Branch: 1 branch
BranchTrueFalseTotal
if ($dive eq "AuxCP") 4 9 13
238 13 my @members = ();
239  
240 13 my @queue = grep { $_->is_member } $self->get_children();
241 13 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 52 13 65
242 52 my $node = shift @queue;
243 52 ▶ if ( $dive->($node) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$dive($node)) { } 5 47 52
elsif (not $direct_only and $node->is_coap_root) { } 5 42 47
244 5 push @queue, $node->get_children();
245 }
246 ▶ elsif ( !$direct_only && $node->is_coap_root() ) {
Condition100%3/3
not $direct_only and $node->is_coap_root
!ll&&!rl&&r
10325
MC/DC100%2/2
not $direct_only and $node->is_coap_root
not $direct_only $node->is_coap_root
Truth table100%3/3
not(A) and B
A= not $direct_onlyB= $node->is_coap_root
ABresult
0X0
100
111
247 5 push @queue, grep { $_->is_member } $node->get_children();
248 }
249 else {
250 42 push @members, $node;
251 }
252 }
253 13 return @members;
254 }
255  
256 38 ▶use List::MoreUtils "any";
Subroutine BEGIN: called CC=1 SCAR=0.0
257 0 ▶sub is_echild_of {
Subroutine is_echild_of: not called CC=1 SCAR=6.9
Pod undocumented
258 0 my ($potential_echild, $eparent, $arg_ref) = @_;
259  
260 0 my @echildren = $eparent->get_echildren( $arg_ref );
261 0 ▶ return any { $_ == $potential_echild } @echildren;
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 }
263  
264 38 my @eff_args = qw(dive or_topological ignore_incorrect_tree_structure);
265 # Remove args relevant only to eff relations from arg_ref and return them
266 0 ▶sub _extract_eff_args {
Subroutine _extract_eff_args: not called CC=3 SCAR=24.8
267 0 my ($self, $arg_ref) = @_;
268  
269 0 my $eff_arg_ref = {};
270 0 foreach my $eff_arg (@eff_args) {
271 0 ▶ if (defined $arg_ref->{$eff_arg}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg_ref->{$eff_arg}) 0 0 0
272 0 $eff_arg_ref->{$eff_arg} = $arg_ref->{$eff_arg};
273 0 delete $arg_ref->{$eff_arg};
274 }
275 }
276  
277 0 return $eff_arg_ref;
278 }
279  
280 0 ▶sub get_esiblings {
Subroutine get_esiblings: not called CC=5 SCAR=34.0
Pod undocumented
281 0 my ( $self, $arg_ref ) = @_;
282 0 ▶ log_fatal('Incorrect number of arguments') if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 0 0
283 0 ▶ if ( !defined $arg_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg_ref) 0 0 0
284 0 $arg_ref = {};
285 }
286  
287 0 my $eff_arg_ref = $self->_extract_eff_args($arg_ref);
288  
289 0 my @eparents = $self->get_eparents($eff_arg_ref);
290
291 0 my %esiblings_hash;
292 0 foreach my $eparent (@eparents) {
293 0 my @esiblings = grep { $_ ne $self } $eparent->get_echildren($eff_arg_ref);
294 0 foreach my $esibling (@esiblings) {
295 0 $esiblings_hash{$esibling->id} = $esibling;
296 }
297 }
298 0 my @esiblings = values %esiblings_hash;
299  
300 0 return $self->_process_switches($arg_ref, @esiblings);
301 }
302  
303 38 1;
304  
305 __END__
« Treex/Core/Node/T.pm ↑ Summary Treex/Core/Files.pm »