| 1 | package Treex::View::TreeLayout; | ||
| 2 | our $AUTHORITY = 'cpan:MICHALS'; | ||
| 3 | $Treex::View::TreeLayout::VERSION = '1.0.0'; | ||
| 4 | # ABSTRACT: Helpers for delaing with p-trees and node labels | ||
| 5 | |||
| 6 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Treex::Core::Types; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Treex::View::TreeLayout - Inspired by L<Treex::Core::TredView::TreeLayout> | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Set of helper methods for generating layout for p-trees and labels | ||
| 17 | These methods are heavily inspired by L<Treex::Core::TredView::TreeLayout> | ||
| 18 | |||
| 19 | =head1 NOTE | ||
| 20 | |||
| 21 | TODO: Refactoring needed | ||
| 22 | |||
| 23 | =head1 METHODS | ||
| 24 | |||
| 25 | =cut | ||
| 26 | |||
| 27 | has 'treex_doc' => ( is => 'rw' ); | ||
| 28 | |||
| 29 | my @layers = map {lc} Treex::Core::Types::layers(); | ||
| 30 | |||
| 31 | =head2 get_tree_label | ||
| 32 | |||
| 33 | Tree label based on tree type, current language and selector | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub get_tree_label { | ||
| 38 | my ( $self, $tree ) = @_; | ||
| 39 | my $label = $tree->language . '-' . $tree->get_layer; | ||
| 40 | my $sel = $tree->selector; | ||
| 41 | $label .= '-' . $sel if $sel; | ||
| 42 | return $label; | ||
| 43 | } | ||
| 44 | |||
| 45 | =head2 get_layout_label | ||
| 46 | |||
| 47 | Returns comma separated string of layout labels - used for storing | ||
| 48 | layout configuration. | ||
| 49 | |||
| 50 | C<NOT USED> | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | sub get_layout_label { | ||
| 55 | my ( $self, $bundle ) = @_; | ||
| 56 | |||
| 57 | return unless ref($bundle) eq 'Treex::Core::Bundle'; | ||
| 58 | |||
| 59 | my @label; | ||
| 60 | my @zones = $bundle->get_all_zones(); | ||
| 61 | foreach my $zone ( sort { $a->language cmp $b->language } @zones ) { | ||
| 62 | push @label, map { $self->get_tree_label($_) } sort { $a->get_layer cmp $b->get_layer } $zone->get_all_trees(); | ||
| 63 | } | ||
| 64 | |||
| 65 | return join ',', @label; | ||
| 66 | } | ||
| 67 | |||
| 68 | =head2 get_zone_label | ||
| 69 | |||
| 70 | Zone label based on language and selectors | ||
| 71 | |||
| 72 | =cut | ||
| 73 | |||
| 74 | sub get_zone_label { | ||
| 75 | my ( $self, $zone ) = @_; | ||
| 76 | return $zone->language . ( $zone->selector ? '-' . $zone->selector : '' ); | ||
| 77 | } | ||
| 78 | |||
| 79 | # Copied from Treex::Core::TredView | ||
| 80 | sub _spread_nodes { | ||
| 81 | my ( $self, $node ) = @_; | ||
| 82 | |||
| 83 | my ( $left, $right, $gap, $pos ) = ( -1, 0, 0, 0 ); | ||
| 84 | my ( @buf, @lower ); | ||
| 85 | for my $child ( $node->children ) { | ||
| 86 | ( $pos, @buf ) = $self->_spread_nodes($child); | ||
| 87 | if ( $left < 0 ) { | ||
| 88 | $left = $pos; | ||
| 89 | } | ||
| 90 | $right += $gap; | ||
| 91 | $gap = scalar(@buf); | ||
| 92 | push @lower, @buf; | ||
| 93 | } | ||
| 94 | $right += $pos; | ||
| 95 | return ( 0, $node ) if !@lower; | ||
| 96 | |||
| 97 | my $mid; | ||
| 98 | if ( scalar( $node->children ) == 1 ) { | ||
| 99 | $mid = int( ( $#lower + 1 ) / 2 - 1 ); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $mid = int( ( $left + $right ) / 2 ); | ||
| 103 | } | ||
| 104 | |||
| 105 | return ( $mid + 1 ), @lower[ 0 .. $mid ], $node, @lower[ ( $mid + 1 ) .. $#lower ]; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 get_nodes | ||
| 109 | |||
| 110 | List of all nodes in given tree | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | sub get_nodes { | ||
| 115 | my ( $self, $tree ) = @_; | ||
| 116 | my @nodes; | ||
| 117 | |||
| 118 | if ( $tree->get_layer eq 'p' ) { | ||
| 119 | @nodes = $self->_spread_nodes($tree); | ||
| 120 | shift @nodes; | ||
| 121 | } | ||
| 122 | elsif ( $tree->does('Treex::Core::Node::Ordered') ) { | ||
| 123 | @nodes = $tree->get_descendants( { add_self => 1, ordered => 1 } ); | ||
| 124 | } | ||
| 125 | else { | ||
| 126 | @nodes = $tree->get_descendants( { add_self => 1 } ); | ||
| 127 | } | ||
| 128 | return @nodes; | ||
| 129 | } | ||
| 130 | |||
| 131 | =head2 get_sentence_for_a_zone | ||
| 132 | |||
| 133 | Get sentence - array of array refs, including separate words and | ||
| 134 | pointers to tree nodes | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub get_sentence_for_a_zone { | ||
| 139 | my ( $self, $zone, $alignment ) = @_; | ||
| 140 | return if !$zone->has_atree(); | ||
| 141 | my %refs = (); | ||
| 142 | |||
| 143 | if ( $zone->has_ttree() ) { | ||
| 144 | for my $tnode ( $zone->get_ttree->get_descendants ) { | ||
| 145 | my $id = $tnode->id; | ||
| 146 | for my $aux ( TredMacro::ListV( $tnode->attr('a/aux.rf') ) ) { | ||
| 147 | push @{ $refs{$aux} }, $id; | ||
| 148 | if ( exists $alignment->{$id} ) { | ||
| 149 | push @{ $refs{$aux} }, @{ $alignment->{ $tnode->get_attr('id') } }; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | if ( $tnode->attr('a/lex.rf') ) { | ||
| 153 | push @{ $refs{ $tnode->attr('a/lex.rf') } }, $id; | ||
| 154 | if ( exists $alignment->{ $tnode->get_attr('id') } ) { | ||
| 155 | push @{ $refs{ $tnode->attr('a/lex.rf') } }, @{ $alignment->{ $tnode->get_attr('id') } }; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | my @anodes = $zone->get_atree->get_descendants( { ordered => 1 } ); | ||
| 162 | for my $anode (@anodes) { | ||
| 163 | my $id = $anode->id; | ||
| 164 | push @{ $refs{$id} }, $id; | ||
| 165 | if ( exists $alignment->{$id} ) { | ||
| 166 | push @{ $refs{$id} }, @{ $alignment->{$id} }; | ||
| 167 | } | ||
| 168 | if ( $anode->attr('p_terminal.rf') ) { | ||
| 169 | my $pnode = $self->treex_doc->get_node_by_id( $anode->attr('p_terminal.rf') ); | ||
| 170 | push @{ $refs{$id} }, $pnode->id; | ||
| 171 | while ( $pnode->parent ) { | ||
| 172 | $pnode = $pnode->parent; | ||
| 173 | push @{ $refs{$id} }, $pnode->id; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | my @out; | ||
| 179 | for my $anode (@anodes) { | ||
| 180 | push @out, [ $anode->form, @{ $refs{ $anode->id } || [] } ]; | ||
| 181 | if ( !$anode->no_space_after ) { | ||
| 182 | push @out, [ ' ', 'space' ]; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | push @out, [ "\n", 'newline' ]; | ||
| 187 | return \@out; | ||
| 188 | } | ||
| 189 | |||
| 190 | =head2 value_line | ||
| 191 | |||
| 192 | Returns all sentences in current bundle, creating so called value_line | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | sub value_line { | ||
| 197 | my ( $self, $bundle ) = @_; | ||
| 198 | |||
| 199 | my %alignment = (); | ||
| 200 | foreach my $zone ( $bundle->get_all_zones() ) { | ||
| 201 | foreach my $layer (@layers) { | ||
| 202 | if ( $zone->has_tree($layer) ) { | ||
| 203 | my $root = $zone->get_tree($layer); | ||
| 204 | foreach my $node ( $root, $root->get_descendants ) { | ||
| 205 | if ( exists $node->{'alignment'} ) { | ||
| 206 | foreach my $ref ( @{ $node->get_attr('alignment') } ) { | ||
| 207 | push @{ $alignment{ $node->{id} } }, $ref->{'counterpart.rf'}; | ||
| 208 | push @{ $alignment{ $ref->{'counterpart.rf'} } }, $node->{id}; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | my @out = (); | ||
| 217 | foreach my $zone ( $bundle->get_all_zones() ) { | ||
| 218 | push @out, ( [ '[' . $zone->get_label . ']', 'label' ], [ ' ', 'space' ] ); | ||
| 219 | if ( my $sentence = $self->get_sentence_for_a_zone( $zone, \%alignment ) ) { | ||
| 220 | push @out, @$sentence; | ||
| 221 | } | ||
| 222 | elsif ( defined $zone->sentence ) { | ||
| 223 | push @out, [ $zone->sentence . "\n", 'text' ]; | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | push @out, [ "\n", 'newline' ]; | ||
| 227 | } | ||
| 228 | } | ||
| 229 | return \@out; | ||
| 230 | } | ||
| 231 | |||
| 232 | __PACKAGE__->meta->make_immutable; | ||
| 233 | |||
| 234 | 1; | ||
| 235 | __END__ | ||