| 1 | package Treex::Block::Read::BaseReader; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 31 | $Treex::Block::Read::BaseReader::VERSION = '0.08633_1'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 31 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 31 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 31 | ▶ | use File::Slurp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 31 | with 'Treex::Core::DocumentReader'; | |||||||||||||||||||||||||||
| 9 | 31 | ▶ | use Treex::Core::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | sub next_document { | ||||||||||||||||||||||||||||
| 12 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_document: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 1 | return log_fatal "method next_document must be overridden in " . ref($self); | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 31 | has selector => ( isa => 'Treex::Type::Selector', is => 'ro', default => q{} ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Note that "language" is intentionally not required here, | ||||||||||||||||||||||||||||
| 19 | # because some derived classes (e.g. Read::Treex) do not need it. | ||||||||||||||||||||||||||||
| 20 | # However, if a derived class reads a format where the language is not specified, | ||||||||||||||||||||||||||||
| 21 | # it must add the parameter language using: | ||||||||||||||||||||||||||||
| 22 | #has language => ( isa => 'Treex::Type::LangCode', is => 'ro', required=>1 ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 31 | has from => ( | |||||||||||||||||||||||||||
| 26 | isa => 'Treex::Core::Files', | ||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||
| 28 | coerce => 1, | ||||||||||||||||||||||||||||
| 29 | required => 1, | ||||||||||||||||||||||||||||
| 30 | handles => [qw(current_filename file_number _set_file_number)], | ||||||||||||||||||||||||||||
| 31 | documentation => 'arrayref of filenames to be loaded, ' | ||||||||||||||||||||||||||||
| 32 | . 'coerced from a space or comma separated list of filenames, ' | ||||||||||||||||||||||||||||
| 33 | . 'see POD for details', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 31 | has file_stem => ( | |||||||||||||||||||||||||||
| 37 | isa => 'Str', | ||||||||||||||||||||||||||||
| 38 | is => 'ro', | ||||||||||||||||||||||||||||
| 39 | documentation => 'how to name the loaded documents', | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 31 | has is_one_doc_per_file => ( | |||||||||||||||||||||||||||
| 43 | is => 'rw', | ||||||||||||||||||||||||||||
| 44 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 45 | default => 1, | ||||||||||||||||||||||||||||
| 46 | ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 32 | ▶ | has _file_numbers => ( is => 'rw', default => sub { {} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub is_next_document_for_this_job { | ||||||||||||||||||||||||||||
| 51 | 56 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_next_document_for_this_job: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 56 | ▶ | return 1 if !$self->jobindex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | return $self->doc_number % $self->jobs == ( $self->jobindex - 1 ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub next_filename { | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 56 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_filename: called CC=4 SCAR=15.5
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # In parallel processing and one_doc_per_file setting, | ||||||||||||||||||||||||||||
| 61 | # we can skip files that are not supposed to be loaded by this job/reader, | ||||||||||||||||||||||||||||
| 62 | # in order to make loading faster. | ||||||||||||||||||||||||||||
| 63 | 56 | ▶ | while ( $self->is_one_doc_per_file && !$self->is_next_document_for_this_job ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->is_one_doc_per_file and not $self->is_next_document_for_this_job
MC/DC0%0/2
$self->is_one_doc_per_file and not $self->is_next_document_for_this_job
$self->is_one_doc_per_file
not $self->is_next_document_for_this_job
Truth table33%1/3
A and not(B)
A= $self->is_one_doc_per_fileB= not $self->is_next_document_for_this_job
| |||||||||||||||||||||||||||||
| 64 | 0 | $self->_set_file_number( $self->file_number + 1 ); | |||||||||||||||||||||||||||
| 65 | 0 | $self->_set_doc_number( $self->doc_number + 1 ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | # return undef, but do not move further if we are at the end of document list (we might need the current file name) | ||||||||||||||||||||||||||||
| 68 | 56 | ▶ | return if ( $self->file_number >= $self->from->number_of_files ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 30 | $self->_set_file_number( $self->file_number + 1 ); | |||||||||||||||||||||||||||
| 71 | 30 | return $self->current_filename(); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 31 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub new_document { | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 29 | ▶ | my ( $self, $load_from ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_document: called CC=11 SCAR=27.6
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 29 | my $path = $self->current_filename(); | |||||||||||||||||||||||||||
| 80 | 29 | ▶ | log_fatal "next_filename() must be called before new_document()" if !defined $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 29 | my ( $volume, $dirs, $file ) = File::Spec->splitpath($path); | |||||||||||||||||||||||||||
| 82 | 29 | my ( $stem, $extension ) = $file =~ /([^.]+)(\..+)?/; | |||||||||||||||||||||||||||
| 83 | 29 | $stem =~ s/^-$/noname/; | |||||||||||||||||||||||||||
| 84 | 29 | my %args = ( file_stem => $stem, loaded_from => $path ); | |||||||||||||||||||||||||||
| 85 | 29 | ▶ | if ( defined $dirs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 29 | $args{path} = $volume . $dirs; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 29 | ▶ | if ( $self->file_stem ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 0 | $args{file_stem} = $self->file_stem; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 29 | ▶ | 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
| |||||||||||||||||||||||||||||
| 94 | 29 | $args{file_number} = q{}; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 0 | my $num = $self->_file_numbers->{$stem}; | |||||||||||||||||||||||||||
| 98 | 0 | $self->_file_numbers->{$stem} = ++$num; | |||||||||||||||||||||||||||
| 99 | 0 | $args{file_number} = sprintf "%03d", $num; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 29 | ▶ | if ( defined $load_from ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 14 | $args{filename} = $load_from; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 29 | $self->_set_doc_number( $self->doc_number + 1 ); | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 29 | my $document; | |||||||||||||||||||||||||||
| 109 | 29 | ▶ | if ( defined $load_from and $load_from =~ /\.streex$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/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 table66%2/3
A and B
A= defined $load_fromB= $load_from =~ /\.streex$/
| |||||||||||||||||||||||||||||
| 110 | 0 | $document = Treex::Core::Document->retrieve_storable($load_from); | |||||||||||||||||||||||||||
| 111 | 0 | $document->set_storable(1); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | else { | ||||||||||||||||||||||||||||
| 114 | 29 | $document = Treex::Core::Document->new( \%args ); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 29 | ▶ | if ( defined $load_from && $load_from =~ /\.gz$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/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 table66%2/3
A and B
A= defined $load_fromB= $load_from =~ /\.gz$/
| |||||||||||||||||||||||||||||
| 118 | 0 | $document->set_compress(1); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 29 | return $document; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | sub number_of_documents { | ||||||||||||||||||||||||||||
| 125 | 27 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
number_of_documents: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 126 | 27 | ▶ | return $self->is_one_doc_per_file ? $self->from->number_of_files : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | after 'restart' => sub { | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 131 | 0 | $self->_set_file_number(0); | |||||||||||||||||||||||||||
| 132 | 31 | }; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 31 | 1; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =head1 NAME | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | Treex::Block::Read::BaseReader - abstract ancestor for document readers | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 VERSION | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | version 0.08633_1 | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | This class serves as an common ancestor for document readers, | ||||||||||||||||||||||||||||
| 149 | that have a parameter C<from> with a space or comma separated list of filenames | ||||||||||||||||||||||||||||
| 150 | to be loaded. | ||||||||||||||||||||||||||||
| 151 | It is designed to implement the L<Treex::Core::DocumentReader> interface. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | In derived classes you need to define the C<next_document> method, | ||||||||||||||||||||||||||||
| 154 | and you can use C<next_filename> and C<new_document> methods. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =over | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =item from (required) | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | space or comma separated list of filenames, or C<-> for STDIN | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | An '@' directly in front of a file name causes this file to be interpreted as a file | ||||||||||||||||||||||||||||
| 165 | list, with one file name per line, e.g. '@filelist.txt' causes the reader to open | ||||||||||||||||||||||||||||
| 166 | 'filelist.txt' and read a list of files from it. File lists may be arbitrarily | ||||||||||||||||||||||||||||
| 167 | mixed with regular files in the parameter. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | (If you use this method via API you can specify a string array reference or a | ||||||||||||||||||||||||||||
| 170 | L<Treex::Core::Files> object.) | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item file_stem (optional) | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | How to name the loaded documents. | ||||||||||||||||||||||||||||
| 175 | This attribute will be saved to the same-named | ||||||||||||||||||||||||||||
| 176 | attribute in documents and it will be used in document writers | ||||||||||||||||||||||||||||
| 177 | to decide where to save the files. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =back | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =head1 METHODS | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =over | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item next_document | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | This method must be overridden in derived classes. | ||||||||||||||||||||||||||||
| 188 | (The implementation in this class just issues fatal error.) | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =item next_filename | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | returns the next filename (full path) to be loaded | ||||||||||||||||||||||||||||
| 193 | (from the list specified in the attribute C<from>) | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =item new_document($load_from?) | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Returns a new empty document with pre-filled attributes | ||||||||||||||||||||||||||||
| 198 | C<loaded_from>, C<file_stem>, C<file_number> and C<path> | ||||||||||||||||||||||||||||
| 199 | which are guessed based on C<current_filename>. | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =item current_filename | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | returns the last filename returned by C<next_filename> | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =item is_next_document_for_this_job | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Is the document that will be returned by C<next_document> | ||||||||||||||||||||||||||||
| 208 | supposed to be processed by this job? | ||||||||||||||||||||||||||||
| 209 | This is relevant only in parallel processing, | ||||||||||||||||||||||||||||
| 210 | where each job has a different C<$jobnumber> assigned. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =item number_of_documents | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | Returns the number of documents that will be read by this reader. | ||||||||||||||||||||||||||||
| 215 | If C<is_one_doc_per_file> returns C<true>, then the number of documents | ||||||||||||||||||||||||||||
| 216 | equals the number of files given in C<from>. | ||||||||||||||||||||||||||||
| 217 | Otherwise, this method returns C<undef>. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =back | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =head1 SEE | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | L<Treex::Block::Read::BaseTextReader> | ||||||||||||||||||||||||||||
| 224 | L<Treex::Block::Read::Text> | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||