Block/W2A/ResegmentSentences.pm

filter: Statementstmt 42.4% 45 / 106 Branchbran 18.1% 4 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 85.7% 12 / 14 Podpod 100% 1 / 1 totaltotal 41.0% 62 / 151 CC 19 SCAR 49.2
CC
19
Cov
37%
process_bundle
272.0
_get_segmenter
4.1
__ANON__
2.0
CRAP 136.9
« Block/Write/CoNLLX.pm ↑ Summary Block/W2A/Tag.pm »
1 package Treex::Block::W2A::ResegmentSentences;
2 2 $Treex::Block::W2A::ResegmentSentences::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 remove => (
10 is => 'ro',
11 isa => enum( [qw(no all diff)] ),
12 default => 'no',
13 documentation => 'remove=no ... Do not delete any bundles (default). '
14 . 'remove=all ... Delete bundles with multiple subsegments. '
15 . 'remove=diff ... Delete bundles with zones with different number of subsegments.',
16 );
17  
18 has 'segmenters' => (
19 is => 'rw',
20 isa => 'HashRef[Treex::Tool::Segment::RuleBased]',
21 1 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 );
23  
24 9 ▶sub _get_segmenter {
Subroutine _get_segmenter: called CC=4 SCAR=14.1
25 9 my $self = shift;
26 9 my $lang = uc shift;
27 9 ▶ if ( exists $self->segmenters->{$lang} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->segmenters->{$lang}) 6 3 9
28 6 return $self->segmenters->{$lang};
29 }
30 3 my $specific = "Treex::Tool::Segment::${lang}::RuleBased";
31 3 my $fallback = "Treex::Tool::Segment::RuleBased";
32 3 foreach my $class ( $specific, $fallback ) {
33 6 my $segmenter;
34 {
35 6 local $SIG{"__DIE__"};
36 0 ▶ $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
37 }
38 6 ▶ if ($segmenter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segmenter) { } 3 3 6
39 3 $self->segmenters->{$lang} = $segmenter;
40 3 return $segmenter;
41 }
42 else {
43 3 log_info("Failed during creating segmenter $class: $@");
44 }
45 }
46 0 log_fatal("Cannot create segmenter for $lang");
47 }
48  
49 0 ▶sub process_bundle {
Subroutine process_bundle: not called CC=16 SCAR=56.1
Pod documented
50 0 my ( $self, $bundle ) = @_;
51  
52 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
53 0 my %sentences;
54 0 my ( $my_segments, $max_segments ) = ( 0, 0 );
55 0 foreach my $zone ( $bundle->get_all_zones() ) {
56 0 my $lang = $zone->language;
57 0 my $label = $zone->get_label();
58 0 my $segmenter = $self->_get_segmenter($lang);
59 0 $sentences{$label} = [ $segmenter->get_segments( $zone->sentence ) ];
60 0 my $segments = @{ $sentences{$label} };
61 0 ▶ if ( $segments > $max_segments ) { $max_segments = $segments; }
Branch: 1 branch
BranchTrueFalseTotal
if ($segments > $max_segments) 0 0 0
62 0 ▶ if ( $label eq $my_label ) { $my_segments = $segments; }
Branch: 1 branch
BranchTrueFalseTotal
if ($label eq $my_label) 0 0 0
63 }
64  
65 # If no language (and selector) were specified for this block
66 # resegment all zones
67 0 ▶ if ( $my_segments == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($my_segments == 0) 0 0 0
68 0 $my_segments = $max_segments;
69 }
70  
71 # We are finished if
72 # the zone to be processed contains just one sentence.
73 0 ▶ return if $my_segments == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $my_segments == 1 0 0 0
74  
75 # So we have more subsegments. Delete the bundle and exit if requested.
76 0 ▶ if ($self->remove eq 'all'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->remove eq "all") 0 0 0
77 0 $bundle->remove();
78 0 return;
79 }
80  
81 # TODO: If a zone contains less subsegments (e.g. just 1) than $segments
82 # we can try to split it to equally long chunks regardless of the real
83 # sentence boundaries. Anyway, all evaluation blocks should join the
84 # segments together again before measuring BLEU etc.
85 0 my $doc = $bundle->get_document;
86 0 my $orig_id = $bundle->id;
87 0 my $last_bundle = $bundle;
88 0 my @labels = keys %sentences;
89  
90 # If any zone has different number of subsegments than $my_segments
91 # and the user requested to delete such bundles, do it and exit.
92 0 ▶ if ($self->remove eq 'diff'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->remove eq "diff") 0 0 0
93 0 ▶ if (any {$_ != $my_segments} map {scalar @{$sentences{$_}}} @labels) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $_ != $my_segments; } , map({scalar @{$sentences{$_};};} @labels))) 0 0 0
94 0 $bundle->remove();
95 0 return;
96 }
97 }
98  
99 # First subsegment will be saved into the original bundle (with renamed id)
100 0 $bundle->set_id("${orig_id}_1of$my_segments");
101 0 foreach my $zone ( $bundle->get_all_zones() ) {
102 0 my $label = $zone->get_label();
103 0 my $sent = shift @{ $sentences{$label} };
104 0 $zone->set_sentence($sent);
105 }
106  
107 # Other subsegments will be saved to new bundles
108 0 for my $i ( 2 .. $my_segments ) {
109 0 my $new_bundle = $doc->create_bundle( { after => $last_bundle } );
110 0 $last_bundle = $new_bundle;
111 0 $new_bundle->set_id("${orig_id}_${i}of$my_segments");
112 0 foreach my $label (@labels) {
113 0 my $sent = shift @{ $sentences{$label} };
114 0 ▶ if ( !defined $sent ) { $sent = ' '; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sent) 0 0 0
115  
116 # If some zone contains more segments than the "current" zone,
117 # the remaining segments will be joined to the last bundle.
118 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
119 0 $sent .= ' ' . join( ' ', @{ $sentences{$label} } );
120 }
121 0 my ( $lang, $selector ) = split /_/, $label;
122 0 my $new_zone = $new_bundle->create_zone( $lang, $selector );
123 0 $new_zone->set_sentence($sent);
124 }
125 }
126  
127 0 return;
128 }
129  
130 2 1;
131  
132 __END__
« Block/Write/CoNLLX.pm ↑ Summary Block/W2A/Tag.pm »