| 1 | package Treex::Core::Node::EffectiveRelations; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 62 | $Treex::Core::Node::EffectiveRelations::VERSION = '0.08633_1'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 62 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # with Moose >= 2.00, this must be present also in roles | ||||||||||||||||||||||||||||
| 8 | 62 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 62 | ▶ | use Treex::Core::Log; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 62 | has is_member => ( | |||||||||||||||||||||||||||
| 12 | is => 'rw', | ||||||||||||||||||||||||||||
| 13 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 14 | documentation => 'Is this node a member of a coordination (i.e. conjunct) or apposition?', | ||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Shared modifiers of coordinations can be distinguished in PDT style | ||||||||||||||||||||||||||||
| 18 | # just based on the fact they are hanged on the conjunction (coord. head). | ||||||||||||||||||||||||||||
| 19 | # However, in other styles (e.g. Stanford) this attribute might be useful. | ||||||||||||||||||||||||||||
| 20 | 62 | has is_shared_modifier => ( | |||||||||||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||||||||||
| 22 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 23 | documentation => 'Is this node a shared modifier of a coordination?', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 62 | requires 'is_coap_root'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Implementation details: | ||||||||||||||||||||||||||||
| 29 | # Members of a coordination can be distinguished from shared modifiers | ||||||||||||||||||||||||||||
| 30 | # according to the attribute is_member. The only problem is when some | ||||||||||||||||||||||||||||
| 31 | # members are hanged on prepositions(afun=AuxP) or subord. conjunctions(AuxC). | ||||||||||||||||||||||||||||
| 32 | # The PDT style is that Aux[CP] nodes can never have is_member=1, resulting in | ||||||||||||||||||||||||||||
| 33 | # e.g. "It was in(parent=and) Prague(parent=in,is_member=1) | ||||||||||||||||||||||||||||
| 34 | # and(parent=was) in(parent=and) London(parent=in,is_member=1)." | ||||||||||||||||||||||||||||
| 35 | # The style adpoted in Treex is that members have always is_member=1 | ||||||||||||||||||||||||||||
| 36 | # no matter what afun they have. This results in: | ||||||||||||||||||||||||||||
| 37 | # "It was in(parent=and,is_member=1) Prague(parent=in) | ||||||||||||||||||||||||||||
| 38 | # and(parent=was) in(parent=and,is_member=1) London(parent=in)." | ||||||||||||||||||||||||||||
| 39 | # Both annotation styles have their pros and cons. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub get_echildren { | ||||||||||||||||||||||||||||
| 42 | 12 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_echildren: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 12 | ▶ | if ( !defined $arg_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 0 | $arg_ref = {}; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 12 | ▶ | log_fatal('Incorrect number of arguments') if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 12 | ▶ | $self->_can_apply_eff($arg_ref) or return $self->get_children(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # 1) Get my own effective children (i.e. I am their only eff. parent). | ||||||||||||||||||||||||||||
| 50 | # These are in my subtree. | ||||||||||||||||||||||||||||
| 51 | 9 | my @echildren = $self->_get_my_own_echildren($arg_ref); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # 2) Add shared effective children | ||||||||||||||||||||||||||||
| 54 | # (i.e. I am their eff. parent, but not the only one). | ||||||||||||||||||||||||||||
| 55 | # This can happen only if I am member of a coordination | ||||||||||||||||||||||||||||
| 56 | # and these eff. children are shared modifiers of the coordination. | ||||||||||||||||||||||||||||
| 57 | 9 | push @echildren, $self->_get_shared_echildren($arg_ref); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # 3) Process eventual switches (ordered=>1, add_self=>1,...) | ||||||||||||||||||||||||||||
| 60 | #return @echildren if !$arg_ref; TODO this cannot happen now, see $arg_ref = {} if !defined $arg_ref; | ||||||||||||||||||||||||||||
| 61 | 9 | delete $arg_ref->{dive}; | |||||||||||||||||||||||||||
| 62 | 9 | delete $arg_ref->{or_topological}; | |||||||||||||||||||||||||||
| 63 | 9 | return $self->_process_switches( $arg_ref, @echildren ); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub get_eparents { | ||||||||||||||||||||||||||||
| 67 | 10 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_eparents: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 10 | ▶ | if ( !defined $arg_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $arg_ref = {}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 10 | ▶ | log_fatal('Incorrect number of arguments') if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 10 | ▶ | $self->_can_apply_eff($arg_ref) or return $self->get_parent(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Get effective parents | ||||||||||||||||||||||||||||
| 75 | 10 | my @eparents = $self->_get_eparents($arg_ref); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # Process eventual switches (ordered=>1, add_self=>1,...) | ||||||||||||||||||||||||||||
| 78 | 10 | delete $arg_ref->{dive}; | |||||||||||||||||||||||||||
| 79 | 10 | delete $arg_ref->{or_topological}; | |||||||||||||||||||||||||||
| 80 | 10 | delete $arg_ref->{ignore_incorrect_tree_structure}; | |||||||||||||||||||||||||||
| 81 | 10 | return $self->_process_switches( $arg_ref, @eparents ); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub _get_eparents { | ||||||||||||||||||||||||||||
| 85 | 10 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_eparents: called CC=8 SCAR=25.7
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # 0) Check if there is a topological parent. | ||||||||||||||||||||||||||||
| 88 | # Otherwise, there is no chance getting effective parents. | ||||||||||||||||||||||||||||
| 89 | 10 | ▶ | if ( !$self->get_parent() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | my $id = $self->id; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | #TODO: log_fatal if !$robust | ||||||||||||||||||||||||||||
| 93 | 0 | log_warn( "The node $id has no effective nor topological parent, using the root", 1 ); | |||||||||||||||||||||||||||
| 94 | 0 | return $self->get_root(); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # 1) If $self is a member of a coordination/aposition, | ||||||||||||||||||||||||||||
| 98 | # get the highest node representing $self -- i.e. the coord/apos root. | ||||||||||||||||||||||||||||
| 99 | # Otherwise, let $node be $self. | ||||||||||||||||||||||||||||
| 100 | 10 | ▶ | my $node = $self->_get_transitive_coap_root($arg_ref) || $self; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->_get_transitive_coap_root($arg_ref) || $self
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
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | # 2) Get the parent | ||||||||||||||||||||||||||||
| 103 | 10 | ▶ | $node = $node->get_parent() or return $self->_fallback_parent($arg_ref); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # 3) If it is a node to be dived, look above for the first non-dive ancestor. | ||||||||||||||||||||||||||||
| 106 | 10 | ▶ | while ( $arg_ref->{dive}->($node) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | $node = $node->get_parent() or return $self->_fallback_parent($arg_ref); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # If $node is not a head of a coordination/aposition, | ||||||||||||||||||||||||||||
| 111 | # it is the effective parent we are looking for. | ||||||||||||||||||||||||||||
| 112 | 10 | ▶ | return $node if !$node->is_coap_root(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Otherwise, there can be more than one effective parent. | ||||||||||||||||||||||||||||
| 115 | # All effective parents (of $self) are shared modifiers | ||||||||||||||||||||||||||||
| 116 | # of the coordination rooted in $node. | ||||||||||||||||||||||||||||
| 117 | 2 | my @eff = $node->get_coap_members($arg_ref); | |||||||||||||||||||||||||||
| 118 | 2 | ▶ | return @eff if @eff; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | return $self->_fallback_parent($arg_ref); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # --- Utility methods for get_echildren and get_eparents | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | sub _is_auxCP { | ||||||||||||||||||||||||||||
| 126 | 58 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_is_auxCP: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 127 | 58 | ▶ | my $afun = $self->afun || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->afun || ''
MC/DC100%1/1
$self->afun || ''
$self->afun
Truth table100%2/2
A
A= $self->afun
| |||||||||||||||||||||||||||||
| 128 | 58 | return $afun =~ /^Aux[CP]$/; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | sub _get_direct_coap_root { | ||||||||||||||||||||||||||||
| 132 | 37 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_direct_coap_root: called CC=8 SCAR=23.7
| |||||||||||||||||||||||||||||
| 133 | 37 | ▶ | my $parent = $self->get_parent() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 37 | ▶ | return $parent if $self->get_attr('is_member'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 13 | ▶ | return if !$arg_ref->{dive} || $arg_ref->{dive}->($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $arg_ref->{"dive"} or $arg_ref->{"dive"}->($self)
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)
| |||||||||||||||||||||||||||||
| 136 | 13 | ▶ | while ( $arg_ref->{dive}->($parent) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 2 | ▶ | return $parent->get_parent() if $parent->get_attr('is_member'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | $parent = $parent->get_parent() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | 11 | return; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub _get_transitive_coap_root { | ||||||||||||||||||||||||||||
| 144 | 10 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_transitive_coap_root: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 145 | 10 | ▶ | my $root = $self->_get_direct_coap_root($arg_ref) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 8 | ▶ | while ( $root->get_attr('is_member') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 6 | ▶ | $root = $root->_get_direct_coap_root($arg_ref) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 8 | return $root; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub _can_apply_eff { | ||||||||||||||||||||||||||||
| 153 | 22 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_can_apply_eff: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||
| 154 | 22 | ▶ | if ( !$arg_ref->{dive} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 155 | 11 | ▶ | $arg_ref->{dive} = sub {0}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | elsif ( $arg_ref->{dive} eq 'AuxCP' ) { | ||||||||||||||||||||||||||||
| 158 | 11 | $arg_ref->{dive} = \&_is_auxCP; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | 22 | ▶ | my $error = $arg_ref->{dive}->($self) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 161 | ? 'a node that is "to be dived"' | ||||||||||||||||||||||||||||
| 162 | : $self->is_coap_root() ? 'coap root' : 0; | ||||||||||||||||||||||||||||
| 163 | 22 | ▶ | return 1 if !$error; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 3 | ▶ | return 0 if $arg_ref->{or_topological}; #TODO: document | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | my $method_name = ( caller 1 )[3]; | |||||||||||||||||||||||||||
| 166 | 0 | my $id = $self->id; | |||||||||||||||||||||||||||
| 167 | 0 | log_warn( "$method_name called on $error ($id). Fallback to topological one.", 1 ); | |||||||||||||||||||||||||||
| 168 | 0 | return 0; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | # used only if there is an error in the tree structure, | ||||||||||||||||||||||||||||
| 172 | # eg. there is a coordination with no members or a non-root node with no parent | ||||||||||||||||||||||||||||
| 173 | sub _fallback_parent { | ||||||||||||||||||||||||||||
| 174 | 0 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fallback_parent: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 175 | 0 | my $id = $self->get_attr('id'); | |||||||||||||||||||||||||||
| 176 | log_warn "The node $id has no effective parent, using the topological one." | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ( !$arg_ref || !$arg_ref->{ignore_incorrect_tree_structure} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$arg_ref and $arg_ref->{"ignore_incorrect_tree_structure"}
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"}
| |||||||||||||||||||||||||||||
| 178 | 0 | return $self->get_parent(); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # Get my own effective children (i.e. I am their only eff. parent). | ||||||||||||||||||||||||||||
| 182 | sub _get_my_own_echildren { | ||||||||||||||||||||||||||||
| 183 | 9 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_my_own_echildren: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 184 | 9 | my @members = (); | |||||||||||||||||||||||||||
| 185 | 9 | my @queue = $self->get_children(); | |||||||||||||||||||||||||||
| 186 | 9 | ▶ | while (@queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 6 | my $node = shift @queue; | |||||||||||||||||||||||||||
| 188 | 6 | ▶ | if ( $arg_ref->{dive}->($node) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 189 | 0 | push @queue, $node->get_children(); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | elsif ( $node->is_coap_root() ) { | ||||||||||||||||||||||||||||
| 192 | 3 | push @members, $node->get_coap_members($arg_ref); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | #push @queue, grep { $_->get_attr('is_member') } $node->get_children(); | ||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | else { | ||||||||||||||||||||||||||||
| 197 | 3 | push @members, $node; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 9 | return @members; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | # Get shared effective children | ||||||||||||||||||||||||||||
| 204 | # (i.e. I am their eff. parent but not the only one). | ||||||||||||||||||||||||||||
| 205 | sub _get_shared_echildren { | ||||||||||||||||||||||||||||
| 206 | 9 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_shared_echildren: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | # Only members of coord/apos can have shared eff. children | ||||||||||||||||||||||||||||
| 209 | 9 | ▶ | my $coap_root = $self->_get_direct_coap_root($arg_ref) or return (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 6 | my @shared_echildren = (); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | # All shared modifiers of $coap_root are eff. children of $self. | ||||||||||||||||||||||||||||
| 213 | # We must process all possibly nested coap_roots. | ||||||||||||||||||||||||||||
| 214 | # In the first iteration, $self is one of children of $coap_root. | ||||||||||||||||||||||||||||
| 215 | # (In case of "diving", it's not $self, but its governing Aux[CP].) | ||||||||||||||||||||||||||||
| 216 | # However, it has is_member==1, so it won't get into @shared_echildren. | ||||||||||||||||||||||||||||
| 217 | # Similarly for other iterations. | ||||||||||||||||||||||||||||
| 218 | 6 | ▶ | while ($coap_root) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 219 | push @shared_echildren, | ||||||||||||||||||||||||||||
| 220 | 6 | map { $_->get_coap_members($arg_ref) } | |||||||||||||||||||||||||||
| 221 | 12 | grep { !$_->get_attr('is_member') } | |||||||||||||||||||||||||||
| 222 | $coap_root->get_children(); | ||||||||||||||||||||||||||||
| 223 | 12 | $coap_root = $coap_root->_get_direct_coap_root($arg_ref); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | 6 | return @shared_echildren; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | sub get_coap_members { | ||||||||||||||||||||||||||||
| 229 | 19 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_coap_members: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 230 | 19 | ▶ | log_fatal('Incorrect number of arguments') if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | 19 | ▶ | return $self if !$self->is_coap_root(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 232 | 13 | my $direct_only = $arg_ref->{direct_only}; | |||||||||||||||||||||||||||
| 233 | 13 | ▶ | my $dive = $arg_ref->{dive} || sub {0}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$arg_ref->{'dive'} || sub {
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;
}
| |||||||||||||||||||||||||||||
| 234 | 13 | ▶ | if ( $dive eq 'AuxCP' ) { $dive = \&_is_auxCP; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 13 | my @members = (); | |||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 13 | my @queue = grep { $_->is_member } $self->get_children(); | |||||||||||||||||||||||||||
| 238 | 13 | ▶ | while (@queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 52 | my $node = shift @queue; | |||||||||||||||||||||||||||
| 240 | 52 | ▶ | if ( $dive->($node) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 241 | 5 | push @queue, $node->get_children(); | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | ▶ | elsif ( !$direct_only && $node->is_coap_root() ) { | |||||||||||||||||||||||||||
|
Condition100%3/3
not $direct_only and $node->is_coap_root
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
| |||||||||||||||||||||||||||||
| 244 | 5 | push @queue, grep { $_->is_member } $node->get_children(); | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | else { | ||||||||||||||||||||||||||||
| 247 | 42 | push @members, $node; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | 13 | return @members; | |||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 62 | 1; | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | __END__ | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head1 NAME | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Treex::Core::Node::EffectiveRelations | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =head1 VERSION | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | version 0.08633_1 | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | Moose role for nodes with a notion of so called | ||||||||||||||||||||||||||||
| 270 | I<effective> parents and I<effective> children. | ||||||||||||||||||||||||||||
| 271 | This notion is used both | ||||||||||||||||||||||||||||
| 272 | on the a-layer (L<Treex::Core::Node::A>) and | ||||||||||||||||||||||||||||
| 273 | on the t-layer (L<Treex::Core::Node::T>). | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | TODO: explain it, some examples, reference to PDT manual | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | Eg. in the sentence "Martin and Rudolph came", the tree structure is | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | came(and(Martin,Rudolph)) | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | where "and" is a head of a coordination with members "Martin" and "Rudolph". | ||||||||||||||||||||||||||||
| 282 | See what the methods C<get_children>, C<get_echildren>, C<get_parent> | ||||||||||||||||||||||||||||
| 283 | and C<get_eparents> return when called on various nodes | ||||||||||||||||||||||||||||
| 284 | (some pseudocode used for better readability): | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | # CHILDREN AND EFFECTIVE CHILDREN | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | "came"->get_children() | ||||||||||||||||||||||||||||
| 290 | # returns "and" | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | "came"->get_echildren() | ||||||||||||||||||||||||||||
| 293 | # returns ("Martin", "Rudolph") | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | "and"->get_children() | ||||||||||||||||||||||||||||
| 296 | # returns ("Martin", "Rudolph") | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | "and"->get_echildren() | ||||||||||||||||||||||||||||
| 299 | # returns ("Martin", "Rudolph") and issues a warning: | ||||||||||||||||||||||||||||
| 300 | # get_echildren called on coap root ([id_of_and]). Fallback to topological one. | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | "and"->get_echildren({or_topological => 1}) | ||||||||||||||||||||||||||||
| 303 | # returns ("Martin", "Rudolph") with no warning | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | # PARENTS AND EFFECTIVE PARENTS | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | "Rudolph"->get_parent() | ||||||||||||||||||||||||||||
| 309 | # returns "and" | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | "Rudolph"->get_eparents() | ||||||||||||||||||||||||||||
| 312 | # returns "came" | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | "and"->get_parent() | ||||||||||||||||||||||||||||
| 315 | # returns "came" | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | "and"->get_eparents() | ||||||||||||||||||||||||||||
| 318 | # returns "came" and issues a warning: | ||||||||||||||||||||||||||||
| 319 | # get_eparents called on coap root ([id_of_and]). Fallback to topological one. | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | "and"->get_eparents({or_topological => 1}) | ||||||||||||||||||||||||||||
| 322 | # returns "came" with no warning | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | Note that to skip prepositions and subordinating conjunctions on the a-layer, | ||||||||||||||||||||||||||||
| 326 | you must use option C<dive>, e.g.: | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | my $eff_children = $node->get_echildren({dive=>'AuxCP'}); | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | Methods C<get_eparents> and C<get_echildren> produce a warning | ||||||||||||||||||||||||||||
| 331 | "C<called on coap root ($id). Fallback to topological one.>" | ||||||||||||||||||||||||||||
| 332 | when called on a root of coordination or apposition, | ||||||||||||||||||||||||||||
| 333 | because effective children/parents are not properly defined in this case. | ||||||||||||||||||||||||||||
| 334 | This warning can be supressed by option C<or_topological>. | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =head1 METHODS | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =over | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | =item my @effective_children = $node->get_echildren($arg_ref?) | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | Returns a list of effective children of the C<$node>. It means that | ||||||||||||||||||||||||||||
| 343 | a) instead of coordination/aposition heads, their members are returned | ||||||||||||||||||||||||||||
| 344 | b) shared modifiers of a coord/apos (technically hanged on the head of coord/apos) | ||||||||||||||||||||||||||||
| 345 | count as effective children of the members. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | OPTIONS: | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =over | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =item dive=>$sub_ref | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | Using C<dive>, you can define nodes to be skipped (or I<dived>). | ||||||||||||||||||||||||||||
| 354 | C<dive> is a reference to a subroutine that decides | ||||||||||||||||||||||||||||
| 355 | whether the given node should be skipped or not. | ||||||||||||||||||||||||||||
| 356 | Typically this is used for prepositions and subord. conjunctions on a-layer. | ||||||||||||||||||||||||||||
| 357 | You can set C<dive> to the string C<AuxCP> which is a shortcut | ||||||||||||||||||||||||||||
| 358 | for C<sub {my $self=shift;return $self->afun =~ /^Aux[CP]$/;}>. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =item or_topological | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | If the notion of effective child is not defined | ||||||||||||||||||||||||||||
| 363 | (if C<$node> is a head of coordination), | ||||||||||||||||||||||||||||
| 364 | return the topological children without warnings. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =item ordered, add_self, following_only, preceding_only, first_only, last_only | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | You can specify the same options as in | ||||||||||||||||||||||||||||
| 369 | L<Treex::Core::Node::get_children()|Treex::Core::Node/get_children>. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =back | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =item my @effective_parents = $node->get_eparents($arg_ref?) | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | Returns a list of effective parents of the C<$node>. | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | OPTIONS | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =over | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =item dive | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | see L<get_echildren> | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =item or_topological | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | If the notion of effective parent is not defined | ||||||||||||||||||||||||||||
| 389 | (if C<$node> is a head of coordination), | ||||||||||||||||||||||||||||
| 390 | return the topological parent without warnings. | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =item ignore_incorrect_tree_structure | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | If there is an error in the tree structure, | ||||||||||||||||||||||||||||
| 395 | eg. there is a coordination with no members or a non-root node with no parent, | ||||||||||||||||||||||||||||
| 396 | a warning is issued | ||||||||||||||||||||||||||||
| 397 | (C<The node [node_id] has no effective parent, using the topological one.>). | ||||||||||||||||||||||||||||
| 398 | This option supresses the warning. | ||||||||||||||||||||||||||||
| 399 | Use thoughtfully (preferably do not use at all). | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | =item ordered, add_self, following_only, preceding_only, first_only, last_only | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | You can specify the same options as in | ||||||||||||||||||||||||||||
| 404 | L<Treex::Core::Node::get_children()|Treex::Core::Node/get_children>. | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | =back | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =item $node->get_coap_members($arg_ref?) | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | If the node is a coordination/apposition head (see | ||||||||||||||||||||||||||||
| 413 | L<Node::A::is_coap_root()|Node::A/is_coap_root> and | ||||||||||||||||||||||||||||
| 414 | L<Node::T::is_coap_root()|Node::T/is_coap_root>) a list of all coordinated | ||||||||||||||||||||||||||||
| 415 | members is returned. Otherwise, the node itself is returned. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | OPTIONS | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =over | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =item direct_only | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | In case of nested coordinations return only "first-level" members. | ||||||||||||||||||||||||||||
| 424 | The default is to return I<transitive> members. | ||||||||||||||||||||||||||||
| 425 | For example "(A and B) or C": | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | $or->get_coap_members(); # returns A,B,C | ||||||||||||||||||||||||||||
| 428 | $or->get_coap_members({direct_only=>1}); # returns and,C | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =item dive | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | see L<get_echildren> | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | =back | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =back | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||