Block/W2A/SegmentOnNewlines.pm

filter: Statementstmt 25.5% 11 / 43 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.8% 3 / 7 Podpod 100% 3 / 3 totaltotal 23.9% 17 / 71 CC 11 SCAR 41.9
CC
11
Cov
18%
process_document
110.0
_doczone_name
6.0
get_segments
2.0
CRAP 65.8
« Block/Read/BaseAlignedReader.pm ↑ Summary Block/Read/AlignedSentences.pm »
1 package Treex::Block::W2A::SegmentOnNewlines;
2 BEGIN {
3 5 ▶ $Treex::Block::W2A::SegmentOnNewlines::VERSION = '0.06903_1';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 extends 'Treex::Core::Block';
8  
9 5 has '+language' => ( required => 1 );
10  
11 5 has [qw(allow_empty_sentences delete_empty_sentences )] => (
12 is => 'ro',
13 isa => 'Bool',
14 default => 0,
15 );
16  
17 sub process_document {
18 0 ▶ my ( $self, $document ) = @_;
Subroutine process_document: not called CC=10 SCAR=47.0
Pod documented
19 0 my $doczone = $document->get_zone( $self->language, $self->selector );
20 0 ▶ log_fatal 'The document does not contain a ' . $self->_doczone_name if !$doczone;
Branch: 1 branch
BranchTrueFalseTotal
unless $doczone 0 0 0
21 0 my $text = $doczone->text;
22 0 ▶ log_fatal $self->_doczone_name . ' contains no "text" attribute' if !defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
23  
24 0 my @sentences;
25 0 foreach my $sentence ( map { $self->normalize_sentence($_) } $self->get_segments($text) ) {
26 0 ▶ if ( $sentence eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sentence eq '') { } 0 0 0
27 0 ▶ if ( $self->delete_empty_sentences ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->delete_empty_sentences) { } 0 0 0
elsif (not $self->allow_empty_sentences) { } 0 0 0
28 }
29 elsif ( !$self->allow_empty_sentences ) {
30 0 log_fatal 'text in ' . $self->_doczone_name
31 . ' contains empty sentences. Consider using'
32 . ' the (allow|delete)_empty_sentences option.';
33 }
34 else {
35 0 push @sentences, '';
36 }
37 }
38 else {
39 0 push @sentences, $sentence;
40 }
41 }
42  
43 # Segmentation blocks should work when the document contains
44 # a) no bundles (typical case)
45 # b) same number of bundles as the @sentences
46 # The bundles were perhaps created by segmentation of another language.
47 0 my @bundles = $document->get_bundles();
48 0 ▶ if ( !@bundles ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @bundles) { } 0 0 0
elsif (@bundles != @sentences) { } 0 0 0
49 0 @bundles = map { $document->create_bundle() } @sentences;
50 }
51 elsif ( @bundles != @sentences ) {
52 0 log_fatal 'The document already contained bundles, but the number'
53 . ' of bundles is different than the number of sentences segmented'
54 . ' from the' . $self->_doczone_name;
55 }
56 0 ▶ while (@bundles) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bundles) 0 0 0
57 0 my $bundle = shift @bundles;
58 0 my $zone = $bundle->create_zone( $self->language, $self->selector );
59 0 $zone->set_sentence( shift @sentences );
60 }
61 0 return 1;
62 }
63  
64 sub _doczone_name {
65 0 ▶ my ($self) = @_;
Subroutine _doczone_name: not called CC=2 SCAR=17.9
66 0 ▶ return 'DocZone for language='
Branch: 1 branch
BranchTrueFalseTotal
$self->selector ne '' ? : 0 0 0
67 . $self->language
68 . ( $self->selector ne '' ? ' selector=' . $self->selector : '' );
69 }
70  
71 sub get_segments {
72 0 ▶ my ( $self, $text ) = @_;
Subroutine get_segments: not called CC=1 SCAR=6.9
Pod documented
73 0 return split /\n/, $text;
74 }
75  
76 sub normalize_sentence {
77 0 ▶ my ( $self, $sentence ) = @_;
Subroutine normalize_sentence: not called CC=1 SCAR=6.9
Pod documented
78  
79 # This method can be overriden.
80 # However, since this class serves as an ancestor for other segmentation blocks,
81 # it is questionable what should be the default behavior.
82 # Should we preserve the original sentence
83 # and handle the normalization within tokenization blocks?
84 # Or do we want to have also the sentences tidy?
85 0 $sentence =~ s/[\s\xa0]/ /g; # get rid of tabs or nbsp
86 0 $sentence =~ s/ +/ /g;
87 0 $sentence =~ s/^ +| +$//g; # trim, including nbsp
88 0 return $sentence;
89 }
90  
91 5 1;
92  
93 __END__
94  
95 TODO: has to be reviewed
96  
97 =encoding utf-8
98  
99 =head1 NAME
100  
101 Treex::Block::W2A::SegmentOnNewlines - segment text on new lines
102  
103 =head1 VERSION
104  
105 version 0.06903_1
106  
107 =head1 DESCRIPTION
108  
109 The source text is segmented into sentences which are stored in document bundles.
110 If the document contained no bundles, the bundles are created.
111 Otherwise, the document must contain the same number of bundles as the number of
112 sentences (segmented by this blocks).
113 This means that this block (or its derivatives) can be used in this way:
114  
115 treex Read::Text language=en from=en.txt \
116 Read::Text language=de from=de.txt \
117 W2A::SegmentOnNewlines language=en \
118 W2A::SegmentOnNewlines language=de \
119 ...
120  
121 This class detects sentences based on the newlines in the source text,
122 but it can be used as an ancestor for more apropriate segmentations
123 by overriding the method C<segment_text>.
124  
125 =head1 ATTRIBUTES
126  
127 =over 4
128  
129 =item allow_empty_sentences
130  
131 If set, empty sentences can be produced.
132  
133 =item delete_empty_sentences
134  
135 If set, empty sentences are automatically deleted.
136  
137 =back
138  
139 If none of the previous attributes is set and empty sentence found, fatal is raised.
140  
141 =head1 METHODS
142  
143 =over 4
144  
145 =item get_segments()
146  
147 This method produces segments from text given as parameter, can be overriden.
148  
149 =item normalize_sentence()
150  
151 This method does sentence normalization e.g. trims initial and terminal whitespaces.
152  
153 =back
154  
155 =head1 AUTHOR
156  
157 Martin Popel <popel@ufal.mff.cuni.cz>
158  
159 =head1 COPYRIGHT AND LICENSE
160  
161 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
162  
163 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Block/Read/BaseAlignedReader.pm ↑ Summary Block/Read/AlignedSentences.pm »