Treex/Block/Read/BaseReader.pm

filter: Statementstmt 70.7% 58 / 82 Branchbran 41.6% 10 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 91.6% 11 / 12 Podpod 80.0% 4 / 5 totaltotal 62.3% 86 / 138 CC 17 SCAR 24.9
CC
17
Cov
62%
new_document
20.7
BUILD
18.7
number_of_documents
2.1
CRAP 12.1
« Treex/Block/Write/BaseWriter.pm ↑ Summary Treex/Core/Node/P.pm »
1 package Treex::Block::Read::BaseReader;
2 8 $Treex::Block::Read::BaseReader::VERSION = '2.20151210';
3 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 with 'Treex::Core::DocumentReader';
7 8 ▶use Treex::Core::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub next_document {
10 1 ▶ my ($self) = @_;
Subroutine next_document: called CC=1 SCAR=0.0
Pod documented
11 1 return log_fatal "method next_document must be overridden in " . ref($self);
12 }
13  
14 # Default language is und (undetermined/unknown) and selector empty.
15 8 has selector => ( isa => 'Treex::Type::Selector', is => 'ro', default => q{} );
16 8 has language => ( isa => 'Treex::Type::LangCode', is => 'ro', default => 'und' );
17  
18  
19 8 has from => (
20 isa => 'Treex::Core::Files',
21 is => 'rw',
22 coerce => 1,
23 required => 1,
24 handles => [qw(current_filename file_number _set_file_number)],
25 documentation => 'arrayref of filenames to be loaded, '
26 . 'coerced from a space or comma separated list of filenames, '
27 . 'see POD for details',
28 );
29  
30 8 has file_stem => (
31 isa => 'Str',
32 is => 'ro',
33 documentation => 'how to name the loaded documents',
34 );
35  
36 8 has is_one_doc_per_file => (
37 is => 'rw',
38 isa => 'Bool',
39 default => 1,
40 );
41  
42 9 ▶has _file_numbers => ( is => 'rw', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
43  
44 8 has _file_number_width => (
45 is => 'rw',
46 isa => 'Int',
47 default => 3,
48 documentation => 'The number of digits for numbered filenames. '
49 . 'The default (3) will create filenames with three digits as "001.treex.gz".'
50 );
51  
52 8 has skip_finished => (
53 isa => 'Str',
54 is => 'ro',
55 documentation => 'Skip input files for which a matching non-empty output file exists '
56 . '(presumably created by a previous unfinished Treex run). '
57 . 'This parameter specifies a regex substitution how to derive the output filename from the input filename. '
58 . 'It is parallel to the parameter substitute={indir}{outdir} in writers. '
59 . 'However, you need to take care of filename extensions too, '
60 . 'e.g. if converting conll to treex, you should use skip_finished={indir/(.+).conll$}{outdir/$1.treex.gz}',
61 );
62  
63  
64 sub BUILD {
65 9 ▶ my ( $self, $args ) = @_;
Subroutine BUILD: called CC=5 SCAR=29.3
Pod undocumented
66 9 ▶ if (my $regex = $self->skip_finished){
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->skip_finished) 0 9 9
67 0 my $filenames_ref = $self->from->filenames;
68 0 my @filtered_filenames;
69 0 my $eval_string = '$filename =~ s' . $regex . '; 1;';
70  
71 0 for my $input_filename (@$filenames_ref){
72 0 my $filename = $input_filename;
73  
74 # see r14228 for an alternative implementation (without stringy eval) which cannot handle $1 in rexex
75 0 ▶ eval $eval_string or log_fatal "Failed to eval $eval_string"; ## no critic qw(BuiltinFunctions::ProhibitStringyEval)
Branch: 1 branch
BranchTrueFalseTotal
eval $eval_string or log_fatal("Failed to eval $eval_string") 0 0 0
76  
77 0 ▶ if (! -s $filename){
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $filename) 0 0 0
78 0 push @filtered_filenames, $input_filename;
79 #say "not finished: $input_filename -> $filename";
80 } #else {say "finished: $input_filename -> $filename";}
81 }
82 0 $self->from->_set_filenames(\@filtered_filenames);
83 0 my $input_number = @$filenames_ref;
84 0 my $filtered_number = @filtered_filenames;
85 0 my $finished_number = $input_number - $filtered_number;
86 0 log_info "$finished_number files out of $input_number were finished, reading only the remaining $filtered_number.";
87 }
88 9 return;
89 }
90  
91 sub next_filename {
92 1 ▶ my ($self) = @_;
Subroutine next_filename: called CC=2 SCAR=7.0
Pod documented
93  
94 # return undef, but do not move further if we are at the end of document list (we might need the current file name)
95 1 ▶ return if ( $self->file_number >= $self->from->number_of_files );
Branch: 1 branch
BranchTrueFalseTotal
if $self->file_number >= $self->from->number_of_files 0 1 1
96  
97 1 $self->_set_file_number( $self->file_number + 1 );
98 1 return $self->current_filename();
99 }
100  
101 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 sub new_document {
104 1 ▶ my ( $self, $load_from ) = @_;
Subroutine new_document: called CC=11 SCAR=30.3
Pod documented
105 1 my $path = $self->current_filename();
106 1 ▶ log_fatal "next_filename() must be called before new_document()" if !defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 1 1
107 1 my ( $volume, $dirs, $file ) = File::Spec->splitpath($path);
108  
109 # Delete file extension, e.g.
110 # file.01.conll -> file.01
111 # cs42.treex.gz -> cs42
112 1 $file =~ s/\.[^.]+(\.gz)?$//;
113  
114 # Substitute standard input for noname.
115 1 $file =~ s/^-$/noname/;
116  
117 1 my %args = ( file_stem => $file, loaded_from => $path );
118 1 ▶ if ( defined $dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dirs) 1 0 1
119 1 $args{path} = $volume . $dirs;
120 }
121  
122 # Override the naming heuristics above, if file_stem was specified.
123 1 ▶ if ( $self->file_stem ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_stem) 0 1 1
124 0 $args{file_stem} = $self->file_stem;
125 }
126  
127 1 ▶ if ( $self->is_one_doc_per_file && !$self->file_stem ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_one_doc_per_file and not $self->file_stem) { } 1 0 1
Condition33%1/3
$self->is_one_doc_per_file and not $self->file_stem
!ll&&!rl&&r
001
MC/DC0%0/2
$self->is_one_doc_per_file and not $self->file_stem
$self->is_one_doc_per_file not $self->file_stem
Truth table33%1/3
A and not(B)
A= $self->is_one_doc_per_fileB= not $self->file_stem
ABresult
0X0
100
111
128 1 $args{file_number} = q{};
129 }
130 else {
131 0 my $num = $self->_file_numbers->{$file};
132 0 $self->_file_numbers->{$file} = ++$num;
133 0 my $fmt = "%0".$self->_file_number_width."d";
134 0 $args{file_number} = sprintf $fmt, $num;
135 }
136  
137 1 ▶ if ( defined $load_from ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $load_from) 0 1 1
138 0 $args{filename} = $load_from;
139 }
140  
141 1 $self->_set_doc_number( $self->doc_number + 1 );
142  
143 1 my $document;
144 1 ▶ if ( defined $load_from and $load_from =~ /\.streex$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $load_from and $load_from =~ /\.streex$/) { } 0 1 1
Condition33%1/3
defined $load_from and $load_from =~ /\.streex$/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $load_from and $load_from =~ /\.streex$/
defined $load_from $load_from =~ /\.streex$/
Truth table33%1/3
A and B
A= defined $load_fromB= $load_from =~ /\.streex$/
ABresult
0X0
100
111
145 0 $document = Treex::Core::Document->retrieve_storable($load_from);
146 0 $document->set_storable(1);
147 }
148 else {
149 1 $document = Treex::Core::Document->new( \%args );
150 }
151  
152 1 ▶ if ( defined $load_from && $load_from =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $load_from and $load_from =~ /\.gz$/) 0 1 1
Condition33%1/3
defined $load_from and $load_from =~ /\.gz$/
!ll&&!rl&&r
100
MC/DC0%0/2
defined $load_from and $load_from =~ /\.gz$/
defined $load_from $load_from =~ /\.gz$/
Truth table33%1/3
A and B
A= defined $load_fromB= $load_from =~ /\.gz$/
ABresult
0X0
100
111
153 0 $document->set_compress(1);
154 }
155  
156 1 return $document;
157 }
158  
159 sub number_of_documents {
160 2 ▶ my $self = shift;
Subroutine number_of_documents: called CC=2 SCAR=7.2
Pod documented
161 2 ▶ return $self->is_one_doc_per_file ? $self->from->number_of_files : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_one_doc_per_file ? : 2 0 2
162 }
163  
164 after 'restart' => sub {
165 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
166 0 $self->_set_file_number(0);
167 8 };
168  
169 8 1;
170  
171 __END__
172  
173 =pod
174  
175 =encoding utf-8
176  
177 =head1 NAME
178  
179 Treex::Block::Read::BaseReader - abstract ancestor for document readers
180  
181 =head1 VERSION
182  
183 version 2.20151210
184  
185 =head1 DESCRIPTION
186  
187 This class serves as a common ancestor for document readers
188 that have the parameter C<from> with a space or comma separated list of filenames
189 to be loaded.
190 It is designed to implement the L<Treex::Core::DocumentReader> interface.
191  
192 In derived classes you need to define the C<next_document> method,
193 and you can use C<next_filename> and C<new_document> methods.
194  
195 =head1 ATTRIBUTES
196  
197 =over
198  
199 =item from (required)
200  
201 space or comma separated list of filenames, or C<-> for STDIN
202  
203 An '@' directly in front of a file name causes this file to be interpreted as a file
204 list, with one file name per line, e.g. '@filelist.txt' causes the reader to open
205 'filelist.txt' and read a list of files from it. File lists may be arbitrarily
206 mixed with regular files in the parameter.
207  
208 Similarly, you can use I<!> for wildcard expansion, e.g.
209 C<treex -Len Read::Treex from='!dir??/file*.txt'>.
210 The single quotes are needed for two reasons.
211 First, to prevent bash from interpreting the wildcard characters.
212 Second, to prevent bash from interpreting the exclamation mark as history expansion.
213  
214 The I<@filelist> and I<!wildcard> conventions are used in several tools, e.g. 7z or javac.
215  
216 (If you use this method via API you can specify a string array reference or a
217 L<Treex::Core::Files> object.)
218  
219 =item file_stem (optional)
220  
221 How to name the loaded documents.
222 This attribute will be saved to the same-named
223 attribute in documents and it will be used in document writers
224 to decide where to save the files.
225  
226 =back
227  
228 =head1 METHODS
229  
230 =over
231  
232 =item next_document
233  
234 This method must be overridden in derived classes.
235 (The implementation in this class just issues fatal error.)
236  
237 =item next_filename
238  
239 returns the next filename (full path) to be loaded
240 (from the list specified in the attribute C<from>)
241  
242 =item new_document($load_from?)
243  
244 Returns a new empty document with pre-filled attributes
245 C<loaded_from>, C<file_stem>, C<file_number> and C<path>
246 which are guessed based on C<current_filename>.
247  
248 =item current_filename
249  
250 returns the last filename returned by C<next_filename>
251  
252 =item is_next_document_for_this_job
253  
254 Is the document that will be returned by C<next_document>
255 supposed to be processed by this job?
256 This is relevant only in parallel processing,
257 where each job has a different C<$jobnumber> assigned.
258  
259 =item number_of_documents
260  
261 Returns the number of documents that will be read by this reader.
262 If C<is_one_doc_per_file> returns C<true>, then the number of documents
263 equals the number of files given in C<from>.
264 Otherwise, this method returns C<undef>.
265  
266 =back
267  
268 =head1 SEE
269  
270 L<Treex::Block::Read::BaseTextReader>
271 L<Treex::Block::Read::Text>
272  
273 =head1 AUTHOR
274  
275 Martin Popel <popel@ufal.mff.cuni.cz>
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
280  
281 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Block/Write/BaseWriter.pm ↑ Summary Treex/Core/Node/P.pm »