Block/W2A/SegmentOnNewlines.pm

filter: Statementstmt 97.9% 47 / 48 Branchbran 71.4% 10 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 75.0% 3 / 4 totaltotal 91.8% 68 / 74 CC 10 SCAR 17.0
CC
10
Cov
92%
process_doc_zone
9.2
process_document
2.0
BEGIN
1.0
CRAP 5.5
« Block/Read/AlignedSentences.pm ↑ Summary Block/W2A/TokenizeOnWhitespace.pm »
1 package Treex::Block::W2A::SegmentOnNewlines;
2 3 $Treex::Block::W2A::SegmentOnNewlines::VERSION = '2.20151102';
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 extends 'Treex::Core::Block';
8  
9 3 has [qw(allow_empty_sentences delete_empty_sentences )] => (
10 is => 'ro',
11 isa => 'Bool',
12 default => 0,
13 );
14  
15 4 ▶sub process_document {
Subroutine process_document: called CC=2 SCAR=6.9
Pod documented
16 4 my ( $self, $document ) = @_;
17 4 foreach my $doczone ($self->get_selected_zones($document->get_all_zones())){
18 4 $self->process_doc_zone($doczone, $document);
19 }
20 3 return;
21 }
22  
23 4 ▶sub process_doc_zone {
Subroutine process_doc_zone: called CC=9 SCAR=22.2
Pod undocumented
24 4 my ( $self, $doczone, $document ) = @_;
25  
26 4 my $text = $doczone->text;
27 4 ▶ log_fatal 'DocZone ' . $doczone->get_label() . ' contains no "text" attribute' if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 4 4
28  
29 4 my @sentences;
30 4 foreach my $sentence ( map { $self->normalize_sentence($_) } $self->get_segments($text) ) {
31 13 ▶ if ( $sentence eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sentence eq '') { } 3 10 13
32 3 ▶ if ( $self->delete_empty_sentences ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->delete_empty_sentences) { } 1 2 3
elsif (not $self->allow_empty_sentences) { } 1 1 2
33 }
34 elsif ( !$self->allow_empty_sentences ) {
35 1 log_fatal 'text in DocZone ' . $doczone->get_label()
36 . ' contains empty sentences. Consider using'
37 . ' the (allow|delete)_empty_sentences option.';
38 }
39 else {
40 1 push @sentences, '';
41 }
42 }
43 else {
44 10 push @sentences, $sentence;
45 }
46 }
47  
48 # Segmentation blocks should work when the document contains
49 # a) no bundles (typical case)
50 # b) same number of bundles as the @sentences
51 # The bundles were perhaps created by segmentation of another language.
52 3 my @bundles = $document->get_bundles();
53 3 ▶ if ( !@bundles ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @bundles) { } 3 0 3
elsif (@bundles != @sentences) { } 0 0 0
54 3 @bundles = map { $document->create_bundle() } @sentences;
55 }
56 elsif ( @bundles != @sentences ) {
57 0 log_fatal 'The document already contained bundles, but the number'
58 . ' of bundles is different than the number of sentences segmented'
59 . ' from the DocZone ' . $doczone->get_label;
60 }
61 3 ▶ while (@bundles) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bundles) 9 3 12
62 9 my $bundle = shift @bundles;
63 9 my $zone = $bundle->create_zone( $doczone->language, $doczone->selector );
64 9 $zone->set_sentence( shift @sentences );
65 }
66 3 return 1;
67 }
68  
69 4 ▶sub get_segments {
Subroutine get_segments: called CC=1 SCAR=0.0
Pod documented
70 4 my ( $self, $text ) = @_;
71 4 return split /\n/, $text;
72 }
73  
74 14 ▶sub normalize_sentence {
Subroutine normalize_sentence: called CC=1 SCAR=0.0
Pod documented
75 14 my ( $self, $sentence ) = @_;
76  
77 # This method can be overriden.
78 # However, since this class serves as an ancestor for other segmentation blocks,
79 # it is questionable what should be the default behavior.
80 # Should we preserve the original sentence
81 # and handle the normalization within tokenization blocks?
82 # Or do we want to have also the sentences tidy?
83 14 $sentence =~ s/[\s\xa0]/ /g; # get rid of tabs or nbsp
84 14 $sentence =~ s/ +/ /g;
85 14 $sentence =~ s/^ +| +$//g; # trim, including nbsp
86 14 return $sentence;
87 }
88  
89 3 1;
90  
91 __END__
« Block/Read/AlignedSentences.pm ↑ Summary Block/W2A/TokenizeOnWhitespace.pm »