Block/W2A/BaseChunkParser.pm

filter: Statementstmt 28.1% 20 / 71 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 55.5% 5 / 9 Podpod 25.0% 1 / 4 totaltotal 20.1% 26 / 129 CC 21 SCAR 53.7
CC
21
Cov
19%
process_atree
342.0
split_long_chunks
13.3
label_parenthesis_token
2.0
CRAP 214.4
↑ Summary Block/Write/CoNLLX.pm »
1 package Treex::Block::W2A::BaseChunkParser;
2 2 $Treex::Block::W2A::BaseChunkParser::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 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 extends 'Treex::Core::Block';
8  
9 2 has 'reparse' => ( is => 'rw', isa => 'Bool', default => 0 );
10  
11 2 has max_chunk_size => (
12 is => 'ro',
13 isa => 'Int',
14 default => 150,
15 documentation => 'If a chunk contains more tokens, it is split and each into shorter chunks (each except for the last has max_chunk_size tokens), '
16 . 'so these chunks are parsed separately. '
17 . 'This parameter serves as a safety check for extremely long sentences and parsers that may fail on such sentences. 0 means do not split.',
18 );
19  
20 0 ▶sub split_long_chunks {
Subroutine split_long_chunks: not called CC=4 SCAR=25.8
Pod undocumented
21 0 my ($self, $chunk) = @_;
22 0 my $max_size = $self->max_chunk_size;
23 0 ▶ return $chunk if !$max_size;
Branch: 1 branch
BranchTrueFalseTotal
unless $max_size 0 0 0
24 0 ▶ return $chunk if @$chunk < $max_size;
Branch: 1 branch
BranchTrueFalseTotal
if @$chunk < $max_size 0 0 0
25  
26 2 ▶ use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 0 my $iterator = natatime($max_size, @$chunk);
28 0 my @result;
29 0 ▶ while (my @words = $iterator->()){
Branch: 1 branch
BranchTrueFalseTotal
if (my(@words) = &$iterator()) 0 0 0
30 0 push @result, \@words;
31 }
32 0 return @result;
33 }
34  
35 0 ▶sub process_atree {
Subroutine process_atree: not called CC=18 SCAR=58.3
Pod undocumented
36 0 my ( $self, $a_root ) = @_;
37 0 my @a_nodes = $a_root->get_descendants( { ordered => 1 } );
38  
39 # Skip the sentence if this block is used in "reparse" mode and no reparsing is needed
40 0 ▶ return 1 if $self->reparse && !$a_root->get_attr('reparse');
Branch: 1 branch
BranchTrueFalseTotal
if $self->reparse and not $a_root->get_attr("reparse") 0 0 0
Condition0%0/3
$self->reparse and not $a_root->get_attr("reparse")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->reparse and not $a_root->get_attr("reparse")
$self->reparse not $a_root->get_attr("reparse")
Truth table0%0/3
A and not(B)
A= $self->reparseB= not $a_root->get_attr("reparse")
ABresult
0X0
100
111
41  
42 # Delete old topology (so no cycles will be introduced during the parsing)
43 0 foreach my $a_node (@a_nodes) {
44 0 $a_node->set_parent($a_root);
45 }
46  
47 # Get chunks
48 0 my %chunks;
49 0 foreach my $a_node (@a_nodes) {
50 0 ▶ my $anode_chunks_ref = $a_node->get_attr('chunks') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $anode_chunks_ref = $a_node->get_attr("chunks") or next 0 0 0
51 0 foreach my $name (@$anode_chunks_ref) {
52 0 push @{ $chunks{$name} }, $a_node;
53 }
54 }
55  
56 # Sort the chunks from the shortest to the longest one,
57 # (delete possible full-sentence chunks if they were marked),
58 # split too long chunks.
59 # and add the full sentence as the last chunk (or more chunks if too long).
60 my @sorted_chunks =
61 0 sort { @$a <=> @$b }
62 0 map {$self->split_long_chunks($_)}
63 0 grep { @$_ < @a_nodes }
64 values %chunks;
65 0 push @sorted_chunks, $self->split_long_chunks(\@a_nodes);
66  
67 # Parse each chunk independently (plus the whole sentence)
68 CHUNK:
69 0 foreach my $chunk (@sorted_chunks) {
70  
71 # There can be a nested chunk inside $chunk,
72 # which is shorter and therefore already parsed.
73 # We skip all the nodes of the nested chunk.
74 # This is not true anymore (except for its root which was left attached to the $a_root.)
75 0 my @ch_nodes = grep { $_->parent == $a_root } @$chunk;
76  
77 # If this is a "parenthesis chunk" (enclosed in round brackets),
78 # leave the brackets aside to be hanged on the root of the chunk later.
79 # (Parsers would mostly guess this right, but not always.)
80 0 my ( $lrb, $rrb ) = @ch_nodes[ 0, -1 ];
81 0 ▶ if ( $lrb && $rrb && $lrb->form eq '(' && $rrb->form eq ')' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lrb and $rrb and $lrb->form eq '(' and $rrb->form eq ')') { } 0 0 0
Condition0%0/9
$lrb and $rrb and $lrb->form eq "(" and $rrb->form eq ")"
!ll&&!rl&&r
000
$lrb and $rrb and $lrb->form eq "("
!ll&&!rl&&r
000
$lrb and $rrb
!ll&&!rl&&r
000
MC/DC0%0/4
$lrb and $rrb and $lrb->form eq "(" and $rrb->form eq ")"
$lrb $rrb $lrb->form eq "(" $rrb->form eq ")"
Truth table0%0/5
A and B and C and D
A= $lrbB= $rrbC= $lrb->form eq "("D= $rrb->form eq ")"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
82 0 shift @ch_nodes;
83 0 pop @ch_nodes;
84 }
85 else {
86 0 $lrb = undef;
87 }
88  
89 # Check special cases like "Hello ((bug))."
90 0 ▶ next CHUNK if !@ch_nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @ch_nodes 0 0 0
91  
92 # Here comes the very parsing.
93 0 my ( $ch_root, @other_ch_roots ) = $self->parse_chunk(@ch_nodes);
94  
95 # We should not force one root, because e.g. the whole sentence in PDT
96 # has (typically) two roots: the main verb and the final punctuation
97 # and both should be atttached to the technical root.
98 # foreach my $a_root (@other_ch_roots) {$a_root->set_parent($ch_root);}
99  
100 # If this is "parenthesis chunk" (enclosed in round brackets)
101 0 ▶ if ($lrb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lrb) 0 0 0
102  
103 # Hang both of the brackets on the root of the chunk.
104 0 foreach my $bracket ( $lrb, $rrb ) {
105 0 $bracket->set_parent($ch_root);
106 0 $self->label_parenthesis_token($bracket);
107 }
108  
109 # We can guess the parent of this chunk (usually the previous word)
110 0 ▶ my $ch_parent = $lrb->get_prev_node || $rrb->get_next_node || $a_root;
Condition0%0/6
$lrb->get_prev_node || $rrb->get_next_node || $a_root
l!l&&r!l&&!r
000
$lrb->get_prev_node or $rrb->get_next_node
l!l&&r!l&&!r
000
MC/DC0%0/3
$lrb->get_prev_node || $rrb->get_next_node || $a_root
$lrb->get_prev_node $rrb->get_next_node $a_root
Truth table0%0/4
A or B || C
A= $lrb->get_prev_nodeB= $rrb->get_next_nodeC= $a_root
ABCresult
1XX1
01X1
0011
0000
111  
112 # Prevent cycles in cases like "(Hello) (there)."
113 0 ▶ if ( $ch_parent->is_descendant_of($ch_root) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch_parent->is_descendant_of($ch_root)) 0 0 0
114 0 $ch_parent = $a_root;
115 }
116 0 $ch_root->set_parent($ch_parent);
117 }
118 }
119 0 return;
120 }
121  
122 0 ▶sub label_parenthesis_token {
Subroutine label_parenthesis_token: not called CC=1 SCAR=6.9
Pod documented
123 0 my ( $self, $anode ) = @_;
124 0 $anode->set_conll_deprel('P');
125 0 $anode->set_afun('AuxG');
126 0 return;
127 }
128  
129 0 ▶sub parse_chunk {
Subroutine parse_chunk: not called CC=1 SCAR=6.9
Pod undocumented
130 0 log_fatal 'parse_chunk must be implemented in derived clases';
131 0 my ( $self, @a_nodes ) = @_;
132 0 return;
133 }
134  
135 2 1;
136  
137 __END__
↑ Summary Block/Write/CoNLLX.pm »