Block/W2A/AnalysisWithAlignedTrees.pm

filter: Statementstmt 20.0% 24 / 120 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 40.0% 6 / 15 Podpod 0.0% 0 / 3 totaltotal 15.2% 30 / 197 CC 29 SCAR 36.5
CC
29
Cov
14%
_get_field_value
110.0
_get_alignment_hash
30.0
compute_tree_distance_aligned
30.0
CRAP 38.4
« Tool/Lexicon/CS.pm ↑ Summary Block/Read/BaseCoNLLReader.pm »
1 package Treex::Block::W2A::AnalysisWithAlignedTrees;
2 2 $Treex::Block::W2A::AnalysisWithAlignedTrees::VERSION = '2.20151102';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # TODO: rename once the role of this block is absolutely clear
7 # (it is something like things that both LabelMIRA and ParseMSTperl need
8 # to work with parallel attributes)
9 # And now it is even worse as it also ensures truncating Czech lemmas...
10 # it is something like MSTperlAttributeHelper
11  
12 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Treex::Tool::Lexicon::CS;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Treex::Tool::Parser::MSTperl::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use features from aligned tree
18 2 has 'use_aligned_tree' => ( isa => 'Bool', is => 'ro', default => '0' );
19  
20 # the language of the tree which is already parsed and is accessed via the
21 # 'aligned_' prefix, eg. en
22 2 has 'alignment_language' => ( isa => 'Str', is => 'ro', default => 'en' );
23  
24 # alignment type to use, eg. 'int' or 'int.gdfa'
25 # (see Align::A::InsertAlignmentFromFile/symmetrizations)
26 2 has 'alignment_type' => ( isa => 'Str', is => 'ro', default => 'int.gdfa' );
27  
28 # use alignment info from the other tree
29 2 has 'alignment_is_backwards' => ( isa => 'Bool', is => 'ro', default => '1' );
30  
31 # get alignment mapping:
32 # CURRENT VERSION:
33 # $alignment_hash->{node_id} = aligned_node
34 # (if there are multiple aligned nodes, the first one is used)
35 # PREVIOUS VERSION:
36 # $alignment_hash->{node_id} = [aligned_node1, aligned_node2, ...]
37 0 ▶sub _get_alignment_hash {
Subroutine _get_alignment_hash: not called CC=5 SCAR=34.0
38 0 my ( $self, $bundle ) = @_;
39  
40 0 my $alignment_hash;
41 0 ▶ if ( $self->use_aligned_tree && $self->alignment_is_backwards ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->use_aligned_tree and $self->alignment_is_backwards) { } 0 0 0
Condition0%0/3
$self->use_aligned_tree and $self->alignment_is_backwards
!ll&&!rl&&r
000
MC/DC0%0/2
$self->use_aligned_tree and $self->alignment_is_backwards
$self->use_aligned_tree $self->alignment_is_backwards
Truth table0%0/3
A and B
A= $self->use_aligned_treeB= $self->alignment_is_backwards
ABresult
0X0
100
111
42  
43 # we need to provide the other direction of the relation
44 0 $alignment_hash = {};
45  
46 # gets root of aligned Analytical tree
47 0 my $aligned_root =
48 $bundle->get_tree( $self->alignment_language, 'A' );
49  
50 0 my @aligned_nodes = $aligned_root->get_descendants;
51  
52 # foreach node in the aligned-language tree
53 0 foreach my $aligned_node ( @aligned_nodes ) {
54 0 my $node = $self->_get_aligned_node($aligned_node);
55 0 ▶ if ( defined $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node) 0 0 0
56  
57 # if there is a node aligned to $aligned_node,
58 # store this information into $alignment_hash
59 0 $alignment_hash->{ $node->get_attr('id') } = $aligned_node;
60 }
61 }
62 } else {
63  
64 # Node->get_aligned_nodes() will be used directly
65 0 $alignment_hash = undef;
66 }
67  
68 0 return $alignment_hash;
69 }
70  
71 # get the first node aligned to $node
72 # with alignment of type set in $self->alignment_type
73 # directly using $node->get_aligned_nodes()
74 # (or return undef)
75 0 ▶sub _get_aligned_node {
Subroutine _get_aligned_node: not called CC=4 SCAR=30.0
76 0 my ( $self, $node ) = @_;
77  
78 0 my $aligned_node = undef;
79  
80 0 my ( $aligned_nodes, $types ) = $node->get_aligned_nodes();
81 0 ▶ if ($aligned_nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($aligned_nodes) 0 0 0
82  
83 # try to find an aligned node with the right type of alignment
84 0 ▶ for ( my $i = 0; $i < @{$aligned_nodes}; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$aligned_nodes;}) 0 0 0
85 0 my $current_aligned_node = $aligned_nodes->[$i];
86 0 my $current_type = $types->[$i];
87  
88 # alignment is of the desired type
89 0 ▶ if ( $self->alignment_type eq $current_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->alignment_type eq $current_type) 0 0 0
90  
91 # this is the node we were looking for
92 0 $aligned_node = $current_aligned_node;
93  
94 # we want to get the first of such nodes
95 0 last;
96 }
97 }
98  
99 # now $aligned_node either has been set to a node of the right kind
100 # or is still undef because there is no such node
101 } # else: there are no aligned nodes, undef will be returned
102  
103 0 return $aligned_node;
104 }
105  
106 0 ▶sub _get_field_value {
Subroutine _get_field_value: not called CC=10 SCAR=47.0
107 0 my ( $self, $node, $field_name, $alignment_hash ) = @_;
108  
109 0 my $field_value = '';
110  
111 0 my ( $field_name_head, $field_name_tail ) = split( /_/, $field_name, 2 );
112  
113 # combined field (contains '_')
114 0 ▶ if ($field_name_tail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field_name_tail) { } 0 0 0
115  
116 # field on aligned node(s)
117 # (current version: take one aligned node at maximum)
118 0 ▶ if ( $field_name_head eq 'aligned' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($field_name_head eq 'aligned') { } 0 0 0
elsif ($field_name_head eq 'dummy') { } 0 0 0
119  
120 # get aligned node
121 0 my $aligned_node = undef;
122 0 ▶ if ( defined $alignment_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alignment_hash) { } 0 0 0
123  
124 # get alignment from the alignment_hash
125 0 $aligned_node = $alignment_hash->{ $node->get_attr('id') };
126 } else {
127  
128 # get alignment directly from the node
129 0 $aligned_node = $self->_get_aligned_node($node);
130 }
131  
132 # get field value on the aligned node
133 0 ▶ if ( defined $aligned_node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aligned_node) { } 0 0 0
134  
135 # if there is an aligned node, call _get_field_value on it
136 0 $field_value =
137 $self->_get_field_value( $aligned_node, $field_name_tail );
138 } else {
139  
140 # if there isn't one, return ''
141 0 $field_value = '';
142 }
143  
144 # dummy or ignored field
145 } elsif ( $field_name_head eq 'dummy' ) {
146 0 $field_value = '';
147  
148 # special field
149 } else {
150  
151 # ord of the parent node
152 0 ▶ if ( $field_name eq 'parent_ord' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($field_name eq 'parent_ord') { } 0 0 0
elsif ($field_name eq 'trunc_lemma') { } 0 0 0
elsif ($field_name eq 'coarse_tag') { } 0 0 0
elsif ($field_name eq 'tree_distance_aligned') { } 0 0 0
153  
154 # this field should be ignored in typical parsing
155 # but can be used eg. in two-stage parsing
156 # and is also needed for 'aligned_parent_ord'
157 0 my $parent = $node->get_parent();
158 0 $field_value = $parent->get_attr('ord');
159  
160 } elsif ( $field_name eq 'trunc_lemma' ) {
161 # lemma without details - works only for Czech
162 0 $field_value =
163 Treex::Tool::Lexicon::CS::truncate_lemma (
164 $node->get_attr('lemma'), 1 );
165  
166 } elsif ( $field_name eq 'coarse_tag' ) {
167 # language-specific coarse grained tag
168 0 $field_value =
169 $self->get_coarse_grained_tag( $node->get_attr('tag') );
170  
171 } elsif ( $field_name eq 'tree_distance_aligned' ) {
172 0 $field_value =
173 $self->compute_tree_distance_aligned($node, $alignment_hash);
174 } else {
175 0 die "Incorrect field $field_name!";
176 }
177 }
178  
179 # ordinary field (does not contain '_')
180 } else {
181 0 $field_value = $node->get_attr($field_name);
182 }
183  
184 0 return $field_value;
185 }
186  
187 0 ▶sub get_coarse_grained_tag {
Subroutine get_coarse_grained_tag: not called CC=1 SCAR=6.9
Pod undocumented
188 0 log_warn 'get_coarse_grained_tag should be implemented in derived classes';
189 0 my ( $self, $tag ) = @_;
190  
191 0 return substr( $tag, 0, 1 );
192 }
193  
194 # TODO: tree_distance computing basically copied from
195 # Treex::Block::Write::LayerAttributes::TreeDistance
196 # which is not a good practice -> find one best place where to have it
197 # and only call it from here
198  
199 0 ▶sub compute_tree_distance_aligned {
Subroutine compute_tree_distance_aligned: not called CC=5 SCAR=34.0
Pod undocumented
200 0 my ( $self, $node, $alignment_hash ) = @_;
201  
202 # array of tree distances
203 # of the node aligned to this node as the child node
204 # and nodes aligned to all other nodes as parent nodes
205 0 my @tree_distances;
206 0 my @all_nodes =
207 ( $node->get_root() )->get_descendants( { ordered => 1 } );
208  
209 # get aligned node
210 0 my $aligned_node = undef;
211 0 ▶ if ( defined $alignment_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alignment_hash) { } 0 0 0
212  
213 # get alignment from the alignment_hash
214 0 $aligned_node = $alignment_hash->{ $node->get_attr('id') };
215 } else {
216  
217 # get alignment directly from the node
218 0 $aligned_node = $self->_get_aligned_node($node);
219 }
220  
221 # get field value on the aligned node
222 0 ▶ if ( defined $aligned_node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aligned_node) { } 0 0 0
223  
224 # compute the distances
225 # distance to root node
226 0 push @tree_distances, $self->_compute_tree_distance_to_root($aligned_node);
227 # distances to potential parents
228 0 foreach my $parent (@all_nodes) {
229 0 push @tree_distances,
230 $self->_compute_tree_distance_aligned(
231 $parent, $aligned_node, $alignment_hash
232 );
233 }
234 } else {
235 # fill with zeroes
236 # distance to root node
237 0 push @tree_distances, 0;
238 # distances to potential parents
239 0 foreach my $parent (@all_nodes) {
240 0 push @tree_distances, 0;
241 }
242 }
243  
244 0 return join ' ', @tree_distances;
245 }
246  
247 0 ▶sub _compute_tree_distance_aligned {
Subroutine _compute_tree_distance_aligned: not called CC=3 SCAR=24.8
248  
249 # TODO: not very effective -> make more effective
250 # (at least quickly identify zeroes - they occur very often;
251 # maybe it'd be better to compute the whole matrix at once...)
252 0 my ( $self, $parent, $aligned_child, $alignment_hash ) = @_;
253  
254 # get aligned parent
255 0 my $aligned_parent = undef;
256 0 ▶ if ( defined $alignment_hash ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alignment_hash) { } 0 0 0
257 # get alignment from the alignment_hash
258 0 $aligned_parent = $alignment_hash->{ $parent->get_attr('id') };
259 } else {
260 # get alignment directly from the node
261 0 $aligned_parent = $self->_get_aligned_node($parent);
262 }
263  
264 # call compute_tree_distance
265 0 my $distance = 0;
266 0 ▶ if ( defined $aligned_parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $aligned_parent) 0 0 0
267 0 $distance =
268 $self->compute_tree_distance( $aligned_parent, $aligned_child );
269 }
270  
271 # else: keep $distance = 0
272  
273 0 return $distance;
274 }
275  
276 0 ▶sub compute_tree_distance {
Subroutine compute_tree_distance: not called CC=2 SCAR=17.9
Pod undocumented
277  
278 0 my ( $self, $ancestor, $descendent ) = @_;
279  
280 0 my $distance = 0;
281  
282 # try standard distance
283 0 $distance =
284 $self->_compute_tree_distance_1_direction( $ancestor, $descendent );
285 0 ▶ if ( $distance == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($distance == 0) 0 0 0
286  
287 # try inversed distance
288 0 $distance = -(
289 $self->_compute_tree_distance_1_direction(
290 $descendent, $ancestor
291 )
292 );
293 }
294  
295 0 return $distance;
296 }
297  
298 0 ▶sub _compute_tree_distance_1_direction {
Subroutine _compute_tree_distance_1_direction: not called CC=4 SCAR=30.0
299  
300 0 my ( $self, $ancestor, $descendent ) = @_;
301  
302 0 my $ancestor_id = $ancestor->get_attr('id');
303  
304 0 my $current_node = $descendent;
305 0 my $distance = 0;
306 0 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (not $current_node->is_root and $current_node->get_attr("id") ne $ancestor_id) 0 0 0
Condition0%0/3
not $current_node->is_root and $current_node->get_attr("id") ne $ancestor_id
!ll&&!rl&&r
000
MC/DC0%0/2
not $current_node->is_root and $current_node->get_attr("id") ne $ancestor_id
not $current_node->is_root $current_node->get_attr("id") ne $ancestor_id
Truth table0%0/3
not(A) and B
A= not $current_node->is_rootB= $current_node->get_attr("id") ne $ancestor_id
ABresult
0X0
100
111
307 !$current_node->is_root()
308 && $current_node->get_attr('id') ne $ancestor_id
309 )
310 {
311  
312 # TODO: apply 'effective' parameter
313 0 $current_node = $current_node->get_parent();
314 0 $distance++;
315 }
316  
317 0 ▶ if ( $current_node->get_attr('id') ne $ancestor_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_node->get_attr("id") ne $ancestor_id) 0 0 0
318  
319 # the $ancestor node was not found as an ancestor of $descendent node
320 # i.e. the cycle stopped because $current_node->is_root()
321 0 $distance = 0;
322 }
323  
324 0 return $distance;
325 }
326  
327 0 ▶sub _compute_tree_distance_to_root {
Subroutine _compute_tree_distance_to_root: not called CC=3 SCAR=24.8
328  
329 0 my ( $self, $descendent ) = @_;
330  
331 0 my $current_node = $descendent;
332 0 my $distance = 0;
333 0 ▶ while ( defined $current_node && !$current_node->is_root() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_node and not $current_node->is_root) 0 0 0
Condition0%0/3
defined $current_node and not $current_node->is_root
!ll&&!rl&&r
000
MC/DC0%0/2
defined $current_node and not $current_node->is_root
defined $current_node not $current_node->is_root
Truth table0%0/3
A and not(B)
A= defined $current_nodeB= not $current_node->is_root
ABresult
0X0
100
111
334 # TODO: apply 'effective' parameter
335 0 $current_node = $current_node->get_parent();
336 0 $distance++;
337 }
338  
339 0 return $distance;
340 }
341  
342 2 1;
343  
344 __END__
« Tool/Lexicon/CS.pm ↑ Summary Block/Read/BaseCoNLLReader.pm »