| 1 | package Treex::Core::Node::Ordered; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 62 | $Treex::Core::Node::Ordered::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 | 62 | ▶ | use List::Util qw(first); # TODO: this wouldn't be needed if there was Treex::Core::Common for roles | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 62 | ▶ | use Treex::Core::Types; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 62 | has ord => ( | |||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||
| 15 | isa => 'Treex::Type::NonNegativeInt', | ||||||||||||||||||||||||||||
| 16 | writer => '_set_ord', | ||||||||||||||||||||||||||||
| 17 | reader => 'ord', | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | sub precedes { | ||||||||||||||||||||||||||||
| 21 | 2 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 2; | ||||||||||||||||||||||||||
|
Subroutine
precedes: called CC=2 SCAR=7.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 2 | my ( $self, $another_node ) = @_; | |||||||||||||||||||||||||||
| 23 | 2 | return $self->ord() < $another_node->ord(); | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Methods get_next_node and get_prev_node are implemented | ||||||||||||||||||||||||||||
| 27 | # so they can handle deprecated fractional ords. | ||||||||||||||||||||||||||||
| 28 | # When no "fract-ords" will be used in the whole TectoMT nor Treex | ||||||||||||||||||||||||||||
| 29 | # this could be reimplemented a bit more effectively. | ||||||||||||||||||||||||||||
| 30 | # TODO | ||||||||||||||||||||||||||||
| 31 | sub get_next_node { | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 1; | ||||||||||||||||||||||||||
|
Subroutine
get_next_node: not called CC=7 SCAR=40.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 34 | 0 | my $my_ord = $self->ord(); | |||||||||||||||||||||||||||
| 35 | 0 | ▶ | log_fatal('Undefined ordering value') if !defined $my_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Find closest higher ord | ||||||||||||||||||||||||||||
| 38 | 0 | my ( $next_node, $next_ord ) = ( undef, undef ); | |||||||||||||||||||||||||||
| 39 | 0 | foreach my $node ( $self->get_root()->get_descendants() ) { | |||||||||||||||||||||||||||
| 40 | 0 | my $ord = $node->ord(); | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | next if $ord <= $my_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | next if defined $next_ord && $ord > $next_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $next_ord and $ord > $next_ord
MC/DC0%0/2
defined $next_ord and $ord > $next_ord
defined $next_ord
$ord > $next_ord
Truth table0%0/3
A and B
A= defined $next_ordB= $ord > $next_ord
| |||||||||||||||||||||||||||||
| 43 | 0 | ( $next_node, $next_ord ) = ( $node, $ord ); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 0 | return $next_node; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub get_prev_node { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 1; | ||||||||||||||||||||||||||
|
Subroutine
get_prev_node: not called CC=7 SCAR=40.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 51 | 0 | my $my_ord = $self->ord(); | |||||||||||||||||||||||||||
| 52 | 0 | ▶ | log_fatal('Undefined ordering value') if !defined $my_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Find closest lower ord | ||||||||||||||||||||||||||||
| 55 | 0 | my ( $prev_node, $prev_ord ) = ( undef, undef ); | |||||||||||||||||||||||||||
| 56 | 0 | foreach my $node ( $self->get_root()->get_descendants() ) { | |||||||||||||||||||||||||||
| 57 | 0 | my $ord = $node->ord(); | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | next if $ord >= $my_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | next if defined $prev_ord && $ord < $prev_ord; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $prev_ord and $ord < $prev_ord
MC/DC0%0/2
defined $prev_ord and $ord < $prev_ord
defined $prev_ord
$ord < $prev_ord
Truth table0%0/3
A and B
A= defined $prev_ordB= $ord < $prev_ord
| |||||||||||||||||||||||||||||
| 60 | 0 | ( $prev_node, $prev_ord ) = ( $node, $ord ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | return $prev_node; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub _normalize_node_ordering { | ||||||||||||||||||||||||||||
| 66 | 10 | ▶ | log_fatal 'Incorrect number of arguments' if @_ != 1; | ||||||||||||||||||||||||||
|
Subroutine
_normalize_node_ordering: called CC=4 SCAR=14.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 10 | ▶ | log_fatal('Ordering normalization can be applied only on root nodes!') if $self->get_parent(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 10 | my $new_ord = 0; | |||||||||||||||||||||||||||
| 70 | 10 | foreach my $node ( $self->get_descendants( { ordered => 1, add_self => 1 } ) ) { | |||||||||||||||||||||||||||
| 71 | 48 | $node->_set_ord($new_ord); | |||||||||||||||||||||||||||
| 72 | 48 | $new_ord++ | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 10 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub _check_shifting_method_args { | ||||||||||||||||||||||||||||
| 78 | 22 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_check_shifting_method_args: called CC=10 SCAR=26.7
| |||||||||||||||||||||||||||||
| 79 | 22 | my @c = caller 1; | |||||||||||||||||||||||||||
| 80 | 22 | my $stack = "$c[3] called from $c[1], line $c[2]"; | |||||||||||||||||||||||||||
| 81 | 22 | ▶ | log_fatal( 'Incorrect number of arguments for ' . $stack ) if @_ < 2 || @_ > 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@_ < 2 or @_ > 3
MC/DC0%0/2
@_ < 2 or @_ > 3
@_ < 2
@_ > 3
Truth table33%1/3
A or B
A= @_ < 2B= @_ > 3
| |||||||||||||||||||||||||||||
| 82 | 22 | ▶ | log_fatal( 'Undefined reference node for ' . $stack ) if !$reference_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 22 | ▶ | log_fatal( 'Reference node must be from the same tree. In ' . $stack ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | if $reference_node->get_root() != $self->get_root(); | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | log_fatal '$reference_node is a descendant of $self.' | ||||||||||||||||||||||||||||
| 87 | . ' Maybe you have forgotten {without_children=>1}. ' . "\n" . $stack | ||||||||||||||||||||||||||||
| 88 | 22 | ▶ | if !$arg_ref->{without_children} && $reference_node->is_descendant_of($self); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $arg_ref->{"without_children"} and $reference_node->is_descendant_of($self)
MC/DC0%0/2
not $arg_ref->{"without_children"} and $reference_node->is_descendant_of($self)
not $arg_ref->{"without_children"}
$reference_node->is_descendant_of($self)
Truth table33%1/3
not(A) and B
A= not $arg_ref->{"without_children"}B= $reference_node->is_descendant_of($self)
| |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 22 | ▶ | return if !defined $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 22 | ▶ | log_fatal( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 'Second argument for shifting methods can be only options hash reference. In ' . $stack | ||||||||||||||||||||||||||||
| 94 | ) if ref $arg_ref ne 'HASH'; | ||||||||||||||||||||||||||||
| 95 | 22 | ▶ | my $unknown = first { $_ ne 'without_children' } keys %{$arg_ref}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 96 | 22 | ▶ | log_warn("Unknown switch '$unknown' for $stack") if defined $unknown; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 22 | return; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub shift_after_node { | ||||||||||||||||||||||||||||
| 101 | 16 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_after_node: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 16 | ▶ | return if $self == $reference_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 16 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 104 | 16 | ▶ | return $self->_shift_to_node( $reference_node, 1, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 16 | return $self->_shift_to_node( $reference_node, 1, 0 ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub shift_before_node { | ||||||||||||||||||||||||||||
| 109 | 6 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_before_node: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 6 | ▶ | return if $self == $reference_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 6 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 112 | 6 | ▶ | return $self->_shift_to_node( $reference_node, 0, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 6 | return $self->_shift_to_node( $reference_node, 0, 0 ); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub shift_after_subtree { | ||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_after_subtree: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 0 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | my $last_node; | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ( $arg_ref->{without_children} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | ($last_node) = reverse grep { $_ != $self } $reference_node->get_descendants( { ordered => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | else { | ||||||||||||||||||||||||||||
| 125 | 0 | $last_node = $reference_node->get_descendants( { except => $self, last_only => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | return if !defined $last_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | return $self->_shift_to_node( $last_node, 1, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | return $self->_shift_to_node( $last_node, 1, 0 ); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub shift_before_subtree { | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my ( $self, $reference_node, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
shift_before_subtree: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 0 | _check_shifting_method_args(@_); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $first_node; | |||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ( $arg_ref->{without_children} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 0 | ($first_node) = grep { $_ != $self } $reference_node->get_descendants( { ordered => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | else { | ||||||||||||||||||||||||||||
| 141 | 0 | $first_node = $reference_node->get_descendants( { except => $self, first_only => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | return if !defined $first_node; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 0 | ▶ | return $self->_shift_to_node( $first_node, 0, $arg_ref->{without_children} ) if $arg_ref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | return $self->_shift_to_node( $first_node, 0, 0 ); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # This method does the real work for all shift_* methods. | ||||||||||||||||||||||||||||
| 149 | # However, due to unfriendly name and arguments it's not public. | ||||||||||||||||||||||||||||
| 150 | sub _shift_to_node { | ||||||||||||||||||||||||||||
| 151 | 22 | ▶ | my ( $self, $reference_node, $after, $without_children ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_shift_to_node: called CC=12 SCAR=24.9
| |||||||||||||||||||||||||||||
| 152 | 22 | my @all_nodes = $self->get_root()->get_descendants(); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # Make sure that ord of all nodes is defined | ||||||||||||||||||||||||||||
| 155 | #my $maximal_ord = @all_nodes; -this does not work, since there may be gaps in ordering | ||||||||||||||||||||||||||||
| 156 | 22 | my $maximal_ord = 10000; | |||||||||||||||||||||||||||
| 157 | 22 | foreach my $d (@all_nodes) { | |||||||||||||||||||||||||||
| 158 | 146 | ▶ | if ( !defined $d->ord() ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 22 | $d->_set_ord( $maximal_ord++ ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # Which nodes are to be moved? | ||||||||||||||||||||||||||||
| 164 | # $self only (the {without_children=>1} switch) | ||||||||||||||||||||||||||||
| 165 | # or $self and all its descendants (the default)? | ||||||||||||||||||||||||||||
| 166 | 22 | my @nodes_to_move; | |||||||||||||||||||||||||||
| 167 | 22 | ▶ | if ($without_children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | @nodes_to_move = ($self); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | else { | ||||||||||||||||||||||||||||
| 171 | 22 | @nodes_to_move = $self->get_descendants( { ordered => 1, add_self => 1 } ); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # Let's make a hash, so we can easily recognize which nodes are to be moved. | ||||||||||||||||||||||||||||
| 175 | 22 | my %is_moving = map { $_ => 1 } @nodes_to_move; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | # Recompute ord of all nodes. | ||||||||||||||||||||||||||||
| 178 | # The technical root has ord=0 and the first node will have ord=1. | ||||||||||||||||||||||||||||
| 179 | 22 | my $counter = 1; | |||||||||||||||||||||||||||
| 180 | 22 | my $nodes_moved = 0; | |||||||||||||||||||||||||||
| 181 | 22 | @all_nodes = sort { $a->ord() <=> $b->ord() } @all_nodes; | |||||||||||||||||||||||||||
| 182 | 22 | foreach my $node (@all_nodes) { | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | # We skip nodes that are being moved. | ||||||||||||||||||||||||||||
| 185 | # Their ord is recomuted elsewhere (look 8 lines down). | ||||||||||||||||||||||||||||
| 186 | 146 | ▶ | next if $is_moving{$node}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # If moving "after" a reference node | ||||||||||||||||||||||||||||
| 189 | # then ord of the $node can be recomputed now | ||||||||||||||||||||||||||||
| 190 | # even if it is actually the $reference_node. | ||||||||||||||||||||||||||||
| 191 | 124 | ▶ | if ($after) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 88 | $node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Now we insert (i.e. recompute ord of) all nodes which are being moved. | ||||||||||||||||||||||||||||
| 196 | # The nodes are inserted in the original order. | ||||||||||||||||||||||||||||
| 197 | 124 | ▶ | if ( $node == $reference_node ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 16 | foreach my $moving_node (@nodes_to_move) { | |||||||||||||||||||||||||||
| 199 | 16 | $moving_node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | 16 | $nodes_moved = 1; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # If moving "before" a node, then now it is the right moment | ||||||||||||||||||||||||||||
| 205 | # for recomputing ord of the $node | ||||||||||||||||||||||||||||
| 206 | 124 | ▶ | if ( !$after ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 36 | $node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # If $is_moving{$reference_node}, e.g. when there is just one node in the tree, | ||||||||||||||||||||||||||||
| 212 | # we need to do the reordering now (otherwise the ord would be still 10000). | ||||||||||||||||||||||||||||
| 213 | 22 | ▶ | if ( !$nodes_moved ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 6 | foreach my $moving_node (@nodes_to_move) { | |||||||||||||||||||||||||||
| 215 | 6 | $moving_node->_set_ord( $counter++ ); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | 22 | return; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | ###!!! DZ: reverting to 7183, 2011-11-03. | ||||||||||||||||||||||||||||
| 222 | # This function by Martin is faster than the my own below but it does not always return correct results. | ||||||||||||||||||||||||||||
| 223 | # The key problem could be that it relies on ord values forming a contiguous sequence which might not always be true. | ||||||||||||||||||||||||||||
| 224 | # Go to devel/normalize_treebanks/nonprojectivity and test that make yields the same numbers for the old and the new method. | ||||||||||||||||||||||||||||
| 225 | sub is_nonprojective_does_not_work { | ||||||||||||||||||||||||||||
| 226 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_nonprojective_does_not_work: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # Root and its children are always projective | ||||||||||||||||||||||||||||
| 229 | 0 | my $parent = $self->get_parent(); | |||||||||||||||||||||||||||
| 230 | 0 | ▶ | return 0 if !$parent || $parent->is_root(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $parent or $parent->is_root
MC/DC0%0/2
not $parent or $parent->is_root
not $parent
$parent->is_root
Truth table0%0/3
not(A) or B
A= not $parentB= $parent->is_root
| |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # Edges between neighbouring nodes are always projective. | ||||||||||||||||||||||||||||
| 233 | # Check it now to make it a bit faster. | ||||||||||||||||||||||||||||
| 234 | 0 | my ( $ordA, $ordB ) = ( $self->ord, $parent->ord ); | |||||||||||||||||||||||||||
| 235 | 0 | ▶ | if ( $ordA > $ordB ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 0 | ( $ordA, $ordB ) = ( $ordB, $ordA ); | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | 0 | my $distance = $ordB - $ordA; | |||||||||||||||||||||||||||
| 239 | 0 | ▶ | return 0 if $distance == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # Get all the descendants of $parent that are in the span of the edge. | ||||||||||||||||||||||||||||
| 242 | 0 | ▶ | my @span = grep { $_->ord > $ordA && $_->ord < $ordB } $parent->get_descendants(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | # For projective edges @span must include all the nodes between $parent and $self. | ||||||||||||||||||||||||||||
| 245 | 0 | return @span != $distance - 1; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 249 | # Tells whether the node is attached to its parent nonprojectively, i.e. there | ||||||||||||||||||||||||||||
| 250 | # is at least one node between this node and its parent that is not dominated | ||||||||||||||||||||||||||||
| 251 | # by the parent. | ||||||||||||||||||||||||||||
| 252 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 253 | sub is_nonprojective | ||||||||||||||||||||||||||||
| 254 | { | ||||||||||||||||||||||||||||
| 255 | 0 | ▶ | log_fatal('Incorrect number of arguments') if(scalar(@_)!=1); | ||||||||||||||||||||||||||
|
Subroutine
is_nonprojective: not called CC=7 SCAR=40.3
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 257 | 0 | my $parent = $self->parent(); | |||||||||||||||||||||||||||
| 258 | # A node that does not have a parent cannot be nonprojective. | ||||||||||||||||||||||||||||
| 259 | 0 | ▶ | return 0 if(!$parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | # Get a hash of all descendants of the parent. | ||||||||||||||||||||||||||||
| 261 | 0 | my @pdesc = $parent->get_descendants({add_self=>1}); | |||||||||||||||||||||||||||
| 262 | 0 | my %pdesc; map {$pdesc{$_}++} (@pdesc); | |||||||||||||||||||||||||||
| 263 | # Figure out whether the node is to the left or to the right from its parent. | ||||||||||||||||||||||||||||
| 264 | 0 | my $nord = $self->ord(); | |||||||||||||||||||||||||||
| 265 | 0 | my $pord = $parent->ord(); | |||||||||||||||||||||||||||
| 266 | 0 | my ($x, $y); | |||||||||||||||||||||||||||
| 267 | 0 | ▶ | if($pord>$nord) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | { | ||||||||||||||||||||||||||||
| 269 | 0 | $x = $self; | |||||||||||||||||||||||||||
| 270 | 0 | $y = $parent; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | else | ||||||||||||||||||||||||||||
| 273 | { | ||||||||||||||||||||||||||||
| 274 | 0 | $x = $parent; | |||||||||||||||||||||||||||
| 275 | 0 | $y = $self; | |||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||
| 277 | # Get the ordered list of all nodes between $x and $y. | ||||||||||||||||||||||||||||
| 278 | 0 | my $xord = $x->ord(); | |||||||||||||||||||||||||||
| 279 | 0 | my $yord = $y->ord(); | |||||||||||||||||||||||||||
| 280 | 0 | ▶ | my @between = grep {$_->ord()>$xord && $_->ord()<$yord} ($parent->root()->get_descendants({ordered=>1})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | # This node is nonprojective if @between contains anything that is not in %pdesc. | ||||||||||||||||||||||||||||
| 282 | 0 | foreach my $b (@between) | |||||||||||||||||||||||||||
| 283 | { | ||||||||||||||||||||||||||||
| 284 | 0 | ▶ | if(!$pdesc{$b}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | { | ||||||||||||||||||||||||||||
| 286 | 0 | return 1; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | 0 | return 0; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 62 | 1; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | __END__ | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | =head1 NAME | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | Treex::Core::Node::Ordered | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | =head1 VERSION | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | version 0.08633_1 | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | Moose role for nodes which can/should be ordered by the attribute C<ord> | ||||||||||||||||||||||||||||
| 309 | (usually following the word order). | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =over | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =item ord | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | The ordering attribute, ordinal number of a node. | ||||||||||||||||||||||||||||
| 318 | The ordering should be without gaps, so | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | print join ' ', map {$_->ord} $root->get_descendants({ordered=>1}); | ||||||||||||||||||||||||||||
| 321 | # should print | ||||||||||||||||||||||||||||
| 322 | # 1 2 3 ... number_of_descendants | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | =back | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =head1 METHODS | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | =head2 Access to nodes ordering | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | =over | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | =item my $boolean = $node->precedes($another_node); | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | Does this node precede C<$another_node>? | ||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =item my $following_node = $node->get_next_node(); | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Return the closest following node (according to the ordering attribute) | ||||||||||||||||||||||||||||
| 339 | or C<undef> if C<$node> is the last one in the tree. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =item my $preceding_node = $node->get_prev_node(); | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | Return the closest preceding node (according to the ordering attribute) | ||||||||||||||||||||||||||||
| 344 | or C<undef> if C<$node> is the first one in the tree. | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | =back | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | =head2 Reordering nodes | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | Next four methods for changing the order of nodes | ||||||||||||||||||||||||||||
| 351 | (the word order defined by the attribute C<ord>) | ||||||||||||||||||||||||||||
| 352 | have an optional argument C<$arg_ref> for specifying switches. | ||||||||||||||||||||||||||||
| 353 | So far there is only one switch - C<without_children> | ||||||||||||||||||||||||||||
| 354 | which is by default set to 0. | ||||||||||||||||||||||||||||
| 355 | It means that the default behavior is to shift the node | ||||||||||||||||||||||||||||
| 356 | with all its descendants. | ||||||||||||||||||||||||||||
| 357 | Only if you want to leave the position of the descendants unchanged | ||||||||||||||||||||||||||||
| 358 | and shift just the node, use e.g. | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | $node->shift_after_node($reference_node, {without_children=>1}); | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | Shifting involves only changing the ordering attribute (C<ord>) of nodes. | ||||||||||||||||||||||||||||
| 363 | There is no rehanging (changing parents). The node which is | ||||||||||||||||||||||||||||
| 364 | going to be shifted must be already added to the tree | ||||||||||||||||||||||||||||
| 365 | and the reference node must be in the same tree. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | For languages with left-to-right script: C<after> means "to the right of" | ||||||||||||||||||||||||||||
| 368 | and C<before> means "to the left of". | ||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | =over | ||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | =item $node->shift_after_node($reference_node); | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | Shifts (changes the C<ord> of) the node just behind the reference node. | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | =item $node->shift_after_subtree($reference_node); | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | Shifts (changes the C<ord> of) the node behind the subtree of the reference node. | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | =item $node->shift_before_node($reference_node); | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | Shifts (changes the C<ord> of) the node just in front of the reference node. | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | =item $node->shift_before_subtree($reference_node); | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | Shifts (changes the C<ord> of) the node in front of the subtree of the reference node. | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =back | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | =head2 Nonprojectivity | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | =over | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | =item my $nonproj = $node->is_nonprojective(); | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | Return 1 if the node is attached to its parent nonprojectively, i.e. there is | ||||||||||||||||||||||||||||
| 397 | at least one node between this node and its parent that is not descendant of | ||||||||||||||||||||||||||||
| 398 | the parent. Return 0 otherwise. | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =back | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||