Block/W2A/ResegmentSentences.pm

filter: Statementstmt 40.7% 35 / 86 Branchbran 25.0% 4 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 91.6% 11 / 12 Podpod 100% 1 / 1 totaltotal 41.4% 51 / 123 CC 16 SCAR 44.8
CC
16
Cov
36%
process_bundle
182.0
_get_segmenter
4.1
BEGIN
1.0
CRAP 88.6
↑ Summary Block/Read/BaseAlignedReader.pm »
1 package Treex::Block::W2A::ResegmentSentences;
2 BEGIN {
3 2 ▶ $Treex::Block::W2A::ResegmentSentences::VERSION = '0.06903_1';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
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 has 'segmenters' => (
10 is => 'rw',
11 isa => 'HashRef[Treex::Tool::Segment::RuleBased]',
12 1 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 2 );
14  
15 sub _get_segmenter {
16 9 ▶ my $self = shift;
Subroutine _get_segmenter: called CC=4 SCAR=14.2
17 9 my $lang = uc shift;
18 9 ▶ if ( exists $self->segmenters->{$lang} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->segmenters->{$lang}) 6 3 9
19 6 return $self->segmenters->{$lang};
20 }
21 3 my $specific = "Treex::Tool::Segment::${lang}::RuleBased";
22 3 my $fallback = "Treex::Tool::Segment::RuleBased";
23 3 foreach my $class ( $specific, $fallback ) {
24 6 ▶ my $segmenter = eval "use $class; $class->new()"; ##no critic (BuiltinFunctions::ProhibitStringyEval) We want to use it, it is simpler and we check result
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
Subroutine BEGIN: called CC=1 SCAR=0.3
25 6 ▶ if ($segmenter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segmenter) { } 3 3 6
26 3 $self->segmenters->{$lang} = $segmenter;
27 3 return $segmenter;
28 }
29 else {
30 3 log_info("Failed during creating segmenter $class: $@");
31 }
32 }
33 0 log_fatal("Cannot create segmenter for $lang");
34 }
35  
36 sub process_bundle {
37 0 ▶ my ( $self, $bundle ) = @_;
Subroutine process_bundle: not called CC=13 SCAR=52.0
Pod documented
38  
39 0 ▶ my $my_label = $self->zone_label || '';
Condition0%0/2
$self->zone_label || ''
l!l
00
MC/DC0%0/1
$self->zone_label || ''
$self->zone_label
Truth table0%0/2
A
A= $self->zone_label
Aresult
11
00
40 0 my %sentences;
41 0 my ( $my_segments, $max_segments ) = ( 0, 0 );
42 0 foreach my $zone ( $bundle->get_all_zones() ) {
43 0 my $lang = $zone->language;
44 0 my $label = $zone->get_label();
45 0 my $segmenter = $self->_get_segmenter($lang);
46 0 $sentences{$label} = [ $segmenter->get_segments( $zone->sentence ) ];
47 0 my $segments = @{ $sentences{$label} };
48 0 ▶ if ( $segments > $max_segments ) { $max_segments = $segments; }
Branch: 1 branch
BranchTrueFalseTotal
if ($segments > $max_segments) 0 0 0
49 0 ▶ if ( $label eq $my_label ) { $my_segments = $segments; }
Branch: 1 branch
BranchTrueFalseTotal
if ($label eq $my_label) 0 0 0
50 }
51  
52 # If no language (and selector) were specified for this block
53 # resegment all zones
54 0 ▶ if ( $my_segments == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($my_segments == 0) 0 0 0
55 0 $my_segments = $max_segments;
56 }
57  
58 # We are finished if
59 # the zone to be processed contains just one sentence.
60 0 ▶ return if $my_segments == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $my_segments == 1 0 0 0
61  
62 # TODO: If a zone contains less subsegments (e.g. just 1) than $segments
63 # we can try to split it to equally long chunks regardless of the real
64 # sentence boundaries. Anyway, all evaluation blocks should join the
65 # segments together again before measuring BLEU etc.
66 0 my $doc = $bundle->get_document;
67 0 my $orig_id = $bundle->id;
68 0 $bundle->set_id("${orig_id}_1of$my_segments");
69 0 foreach my $zone ( $bundle->get_all_zones() ) {
70 0 my $label = $zone->get_label();
71 0 my $sent = shift @{ $sentences{$label} };
72 0 $zone->set_sentence($sent);
73 }
74 0 my $last_bundle = $bundle;
75 0 my @labels = keys %sentences;
76  
77 # TODO parameter to set how many bundles should be created: $my_segments or $max_segments?
78 0 for my $i ( 2 .. $my_segments ) {
79 0 my $new_bundle = $doc->create_bundle( { after => $last_bundle } );
80 0 $last_bundle = $new_bundle;
81 0 $new_bundle->set_id("${orig_id}_${i}of$my_segments");
82 0 foreach my $label (@labels) {
83 0 my $sent = shift @{ $sentences{$label} };
84 0 ▶ if ( !defined $sent ) { $sent = ' '; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sent) 0 0 0
85  
86 # If some zone contains more segments than the "current" zone,
87 # the remaining segments will be joined to the last bundle.
88 0 ▶ if ( $i == $my_segments && $max_segments > $my_segments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $my_segments and $max_segments > $my_segments) 0 0 0
Condition0%0/3
$i == $my_segments and $max_segments > $my_segments
!ll&&!rl&&r
000
MC/DC0%0/2
$i == $my_segments and $max_segments > $my_segments
$i == $my_segments $max_segments > $my_segments
Truth table0%0/3
A and B
A= $i == $my_segmentsB= $max_segments > $my_segments
ABresult
0X0
100
111
89 0 $sent .= ' ' . join( ' ', @{ $sentences{$label} } );
90 }
91 0 my ( $lang, $selector ) = split /_/, $label;
92 0 my $new_zone = $new_bundle->create_zone( $lang, $selector );
93 0 $new_zone->set_sentence($sent);
94 }
95 }
96  
97 0 return;
98 }
99  
100 2 1;
101  
102 __END__
103  
104 TODO POD
105  
106 =encoding utf-8
107  
108 =head1 NAME
109  
110 Treex::Block::W2A::ResegmentSentences
111  
112 =head1 VERSION
113  
114 =head1 DESCRIPTION
115  
116 If the sentence segmenter says that the current sentence is
117 actually composed of two or more sentences, then new bundles
118 are inserted after the current bundle, each containing just
119 one piece of the resegmented original sentence.
120  
121 All zones are processed.
122 The number of bundles created is determined by the number of subsegments
123 in the "current" zone (specified by the parameters C<language> and C<selector>).
124 If a zone contains less subsegments than the current one,
125 the remaining bundles will contain empty sentence.
126 If a zone contains more subsegments than the current one,
127 the remaining subsegments will be joined in the last bundle.
128  
129 In other words, it is granted that the current zone,
130 will not contain empty sentences.
131  
132 =head1 AUTHOR
133  
134 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
135  
136 Martin Popel <popel@ufal.mff.cuni.cz>
137  
138 =head1 COPYRIGHT AND LICENSE
139  
140 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
141  
142 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
143  
↑ Summary Block/Read/BaseAlignedReader.pm »