| 1 | package Treex::Core::Node::EffectiveRelations; | ||||||||||||||||||||||||||||
| 2 | 38 | $Treex::Core::Node::EffectiveRelations::VERSION = '2.20151210'; | |||||||||||||||||||||||||||
| 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 | sub get_echildren { | ||||||||||||||||||||||||||||
| 40 | 12 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_echildren: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 12 | ▶ | if ( !defined $arg_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | $arg_ref = {}; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 12 | ▶ | log_fatal('Incorrect number of arguments') if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 12 | my @echildren; | |||||||||||||||||||||||||||
| 47 | 12 | ▶ | if ( $self->_can_apply_eff($arg_ref) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub get_eparents { | ||||||||||||||||||||||||||||
| 70 | 10 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_eparents: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 10 | ▶ | if ( !defined $arg_ref ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | $arg_ref = {}; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 10 | ▶ | log_fatal('Incorrect number of arguments') if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Get effective parents | ||||||||||||||||||||||||||||
| 77 | 10 | ▶ | my @eparents = $self->_can_apply_eff($arg_ref) ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub _get_eparents { | ||||||||||||||||||||||||||||
| 88 | 10 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_eparents: called CC=8 SCAR=25.7
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # 2) Get the parent | ||||||||||||||||||||||||||||
| 106 | 10 | ▶ | $node = $node->get_parent() or return $self->_fallback_parent($arg_ref); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $node = $node->get_parent() or return $self->_fallback_parent($arg_ref); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | return $self->_fallback_parent($arg_ref); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # --- Utility methods for get_echildren and get_eparents | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub _is_auxCP { | ||||||||||||||||||||||||||||
| 129 | 58 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_is_auxCP: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 130 | 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
| |||||||||||||||||||||||||||||
| 131 | 58 | return $afun =~ /^Aux[CP]$/; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub _get_direct_coap_root { | ||||||||||||||||||||||||||||
| 135 | 37 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_direct_coap_root: called CC=8 SCAR=23.7
| |||||||||||||||||||||||||||||
| 136 | 37 | ▶ | my $parent = $self->get_parent() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 37 | ▶ | return $parent if $self->get_attr('is_member'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 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)
| |||||||||||||||||||||||||||||
| 139 | 13 | ▶ | while ( $arg_ref->{dive}->($parent) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 2 | ▶ | return $parent->get_parent() if $parent->get_attr('is_member'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $parent = $parent->get_parent() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 11 | return; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | sub _get_transitive_coap_root { | ||||||||||||||||||||||||||||
| 147 | 10 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_transitive_coap_root: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 148 | 10 | ▶ | my $root = $self->_get_direct_coap_root($arg_ref) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 8 | ▶ | while ( $root->get_attr('is_member') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 6 | ▶ | $root = $root->_get_direct_coap_root($arg_ref) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | 8 | return $root; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub _can_apply_eff { | ||||||||||||||||||||||||||||
| 156 | 22 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_can_apply_eff: called CC=7 SCAR=20.5
| |||||||||||||||||||||||||||||
| 157 | 22 | ▶ | if ( !$arg_ref->{dive} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 167 | 3 | ▶ | return 0 if $arg_ref->{or_topological}; #TODO: document | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | sub _fallback_parent { | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fallback_parent: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 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
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"}
| |||||||||||||||||||||||||||||
| 181 | 0 | return $self->get_parent(); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # Get my own effective children (i.e. I am their only eff. parent). | ||||||||||||||||||||||||||||
| 185 | sub _get_my_own_echildren { | ||||||||||||||||||||||||||||
| 186 | 9 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_my_own_echildren: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 187 | 9 | my @members = (); | |||||||||||||||||||||||||||
| 188 | 9 | my @queue = $self->get_children(); | |||||||||||||||||||||||||||
| 189 | 9 | ▶ | while (@queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 190 | 6 | my $node = shift @queue; | |||||||||||||||||||||||||||
| 191 | 6 | ▶ | if ( $arg_ref->{dive}->($node) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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 | sub _get_shared_echildren { | ||||||||||||||||||||||||||||
| 209 | 9 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_get_shared_echildren: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | sub get_coap_members { | ||||||||||||||||||||||||||||
| 232 | 19 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_coap_members: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 233 | 19 | ▶ | log_fatal('Incorrect number of arguments') if @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 19 | ▶ | return $self if !$self->is_coap_root(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 {
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;
}
| |||||||||||||||||||||||||||||
| 237 | 13 | ▶ | if ( $dive eq 'AuxCP' ) { $dive = \&_is_auxCP; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 238 | 13 | my @members = (); | |||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 13 | my @queue = grep { $_->is_member } $self->get_children(); | |||||||||||||||||||||||||||
| 241 | 13 | ▶ | while (@queue) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | 52 | my $node = shift @queue; | |||||||||||||||||||||||||||
| 243 | 52 | ▶ | if ( $dive->($node) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 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 | sub is_echild_of { | ||||||||||||||||||||||||||||
| 258 | 0 | ▶ | my ($potential_echild, $eparent, $arg_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_echild_of: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 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 | 1; | |||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | __END__ | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head1 NAME | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Treex::Core::Node::EffectiveRelations | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head1 VERSION | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | version 2.20151210 | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Moose role for nodes with a notion of so called | ||||||||||||||||||||||||||||
| 281 | I<effective> parents and I<effective> children. | ||||||||||||||||||||||||||||
| 282 | This notion is used both | ||||||||||||||||||||||||||||
| 283 | on the a-layer (L<Treex::Core::Node::A>) and | ||||||||||||||||||||||||||||
| 284 | on the t-layer (L<Treex::Core::Node::T>). | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | TODO: explain it, some examples, reference to PDT manual | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | Eg. in the sentence "Martin and Rudolph came", the tree structure is | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | came(and(Martin,Rudolph)) | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | where "and" is a head of a coordination with members "Martin" and "Rudolph". | ||||||||||||||||||||||||||||
| 293 | See what the methods C<get_children>, C<get_echildren>, C<get_parent> | ||||||||||||||||||||||||||||
| 294 | and C<get_eparents> return when called on various nodes | ||||||||||||||||||||||||||||
| 295 | (some pseudocode used for better readability): | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # CHILDREN AND EFFECTIVE CHILDREN | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | "came"->get_children() | ||||||||||||||||||||||||||||
| 301 | # returns "and" | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | "came"->get_echildren() | ||||||||||||||||||||||||||||
| 304 | # returns ("Martin", "Rudolph") | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | "and"->get_children() | ||||||||||||||||||||||||||||
| 307 | # returns ("Martin", "Rudolph") | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | "and"->get_echildren() | ||||||||||||||||||||||||||||
| 310 | # returns ("Martin", "Rudolph") and issues a warning: | ||||||||||||||||||||||||||||
| 311 | # get_echildren called on coap root ([id_of_and]). Fallback to topological one. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | "and"->get_echildren({or_topological => 1}) | ||||||||||||||||||||||||||||
| 314 | # returns ("Martin", "Rudolph") with no warning | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | # PARENTS AND EFFECTIVE PARENTS | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | "Rudolph"->get_parent() | ||||||||||||||||||||||||||||
| 320 | # returns "and" | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | "Rudolph"->get_eparents() | ||||||||||||||||||||||||||||
| 323 | # returns "came" | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | "and"->get_parent() | ||||||||||||||||||||||||||||
| 326 | # returns "came" | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | "and"->get_eparents() | ||||||||||||||||||||||||||||
| 329 | # returns "came" and issues a warning: | ||||||||||||||||||||||||||||
| 330 | # get_eparents called on coap root ([id_of_and]). Fallback to topological one. | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | "and"->get_eparents({or_topological => 1}) | ||||||||||||||||||||||||||||
| 333 | # returns "came" with no warning | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | Note that to skip prepositions and subordinating conjunctions on the a-layer, | ||||||||||||||||||||||||||||
| 337 | you must use option C<dive>, e.g.: | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | my $eff_children = $node->get_echildren({dive=>'AuxCP'}); | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | Methods C<get_eparents> and C<get_echildren> produce a warning | ||||||||||||||||||||||||||||
| 342 | "C<called on coap root ($id). Fallback to topological one.>" | ||||||||||||||||||||||||||||
| 343 | when called on a root of coordination or apposition, | ||||||||||||||||||||||||||||
| 344 | because effective children/parents are not properly defined in this case. | ||||||||||||||||||||||||||||
| 345 | This warning can be supressed by option C<or_topological>. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | =head1 METHODS | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =over | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | =item my @effective_children = $node->get_echildren($arg_ref?) | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | Returns a list of effective children of the C<$node>. It means that | ||||||||||||||||||||||||||||
| 354 | a) instead of coordination/aposition heads, their members are returned | ||||||||||||||||||||||||||||
| 355 | b) shared modifiers of a coord/apos (technically hanged on the head of coord/apos) | ||||||||||||||||||||||||||||
| 356 | count as effective children of the members. | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | OPTIONS: | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | =over | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | =item dive=>$sub_ref | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | Using C<dive>, you can define nodes to be skipped (or I<dived>). | ||||||||||||||||||||||||||||
| 365 | C<dive> is a reference to a subroutine that decides | ||||||||||||||||||||||||||||
| 366 | whether the given node should be skipped or not. | ||||||||||||||||||||||||||||
| 367 | Typically this is used for prepositions and subord. conjunctions on a-layer. | ||||||||||||||||||||||||||||
| 368 | You can set C<dive> to the string C<AuxCP> which is a shortcut | ||||||||||||||||||||||||||||
| 369 | for C<sub {my $self=shift;return $self->afun =~ /^Aux[CP]$/;}>. | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | =item or_topological | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | If the notion of effective child is not defined | ||||||||||||||||||||||||||||
| 374 | (if C<$node> is a head of coordination), | ||||||||||||||||||||||||||||
| 375 | return the topological children without warnings. | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =item ordered, add_self, following_only, preceding_only, first_only, last_only | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | You can specify the same options as in | ||||||||||||||||||||||||||||
| 380 | L<Treex::Core::Node::get_children()|Treex::Core::Node/get_children>. | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =back | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | =item my @effective_parents = $node->get_eparents($arg_ref?) | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | Returns a list of effective parents of the C<$node>. | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | OPTIONS | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =over | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | =item dive | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | see L<get_echildren> | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | =item or_topological | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | If the notion of effective parent is not defined | ||||||||||||||||||||||||||||
| 400 | (if C<$node> is a head of coordination), | ||||||||||||||||||||||||||||
| 401 | return the topological parent without warnings. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =item ignore_incorrect_tree_structure | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | If there is an error in the tree structure, | ||||||||||||||||||||||||||||
| 406 | eg. there is a coordination with no members or a non-root node with no parent, | ||||||||||||||||||||||||||||
| 407 | a warning is issued | ||||||||||||||||||||||||||||
| 408 | (C<The node [node_id] has no effective parent, using the topological one.>). | ||||||||||||||||||||||||||||
| 409 | This option supresses the warning. | ||||||||||||||||||||||||||||
| 410 | Use thoughtfully (preferably do not use at all). | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | =item ordered, add_self, following_only, preceding_only, first_only, last_only | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | You can specify the same options as in | ||||||||||||||||||||||||||||
| 415 | L<Treex::Core::Node::get_children()|Treex::Core::Node/get_children>. | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =back | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =item $node->get_coap_members($arg_ref?) | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | If the node is a coordination/apposition head (see | ||||||||||||||||||||||||||||
| 424 | L<Node::A::is_coap_root()|Node::A/is_coap_root> and | ||||||||||||||||||||||||||||
| 425 | L<Node::T::is_coap_root()|Node::T/is_coap_root>) a list of all coordinated | ||||||||||||||||||||||||||||
| 426 | members is returned. Otherwise, the node itself is returned. | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | OPTIONS | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =over | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =item direct_only | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | In case of nested coordinations return only "first-level" members. | ||||||||||||||||||||||||||||
| 435 | The default is to return I<transitive> members. | ||||||||||||||||||||||||||||
| 436 | For example "(A and B) or C": | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | $or->get_coap_members(); # returns A,B,C | ||||||||||||||||||||||||||||
| 439 | $or->get_coap_members({direct_only=>1}); # returns and,C | ||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | =item dive | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | see L<get_echildren> | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | =back | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =back | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||