| 1 | package Treex::Block::Read::BaseReader; | ||||||||||||||||||||||||||||
| 2 | 8 | $Treex::Block::Read::BaseReader::VERSION = '2.20210102'; | |||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub next_document { | ||||||||||||||||||||||||||
|
Subroutine
next_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 10 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 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 | 9 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=5 SCAR=29.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 65 | 9 | my ( $self, $args ) = @_; | |||||||||||||||||||||||||||
| 66 | 9 | ▶ | if (my $regex = $self->skip_finished){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if (! -s $filename){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub next_filename { | ||||||||||||||||||||||||||
|
Subroutine
next_filename: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub new_document { | ||||||||||||||||||||||||||
|
Subroutine
new_document: called CC=11 SCAR=30.3
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | 1 | my ( $self, $load_from ) = @_; | |||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->is_one_doc_per_file and not $self->file_stem
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $load_from and $load_from =~ /\.streex$/
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$/
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $load_from and $load_from =~ /\.gz$/
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$/
| |||||||||||||||||||||||||||||
| 153 | 0 | $document->set_compress(1); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | return $document; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | ▶ | sub number_of_documents { | ||||||||||||||||||||||||||
|
Subroutine
number_of_documents: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 161 | 2 | ▶ | return $self->is_one_doc_per_file ? $self->from->number_of_files : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | after 'restart' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 165 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 166 | 0 | $self->_set_file_number(0); | |||||||||||||||||||||||||||
| 167 | 8 | }; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 8 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||