| 1 | package Treex::Core::DocumentReader; | ||||||||||||||||||||||||||||
| 2 | 10 | $Treex::Core::DocumentReader::VERSION = '2.20210102'; | |||||||||||||||||||||||||||
| 3 | 10 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # with Moose >= 2.00, this must be present also in roles | ||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use MooseX::SemiAffordanceAccessor 0.09; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 10 | requires 'next_document'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | requires 'number_of_documents'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # attrs for distributed processing | ||||||||||||||||||||||||||||
| 13 | # TODO: check jobs >= jobindex > 0 | ||||||||||||||||||||||||||||
| 14 | 10 | has jobs => ( | |||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||
| 16 | isa => 'Int', | ||||||||||||||||||||||||||||
| 17 | documentation => 'number of jobs for parallel processing', | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 10 | has jobindex => ( | |||||||||||||||||||||||||||
| 21 | is => 'rw', | ||||||||||||||||||||||||||||
| 22 | isa => 'Int', | ||||||||||||||||||||||||||||
| 23 | documentation => 'ordinal number of the current job in parallel processing', | ||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # TODO: this should not be needed in future | ||||||||||||||||||||||||||||
| 27 | 10 | has outdir => ( | |||||||||||||||||||||||||||
| 28 | is => 'rw', | ||||||||||||||||||||||||||||
| 29 | isa => 'Str', | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 10 | has doc_number => ( | |||||||||||||||||||||||||||
| 33 | isa => 'Int', | ||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | writer => '_set_doc_number', | ||||||||||||||||||||||||||||
| 36 | default => 0, | ||||||||||||||||||||||||||||
| 37 | init_arg => undef, | ||||||||||||||||||||||||||||
| 38 | documentation => 'Number of documents loaded so far, i.e.' | ||||||||||||||||||||||||||||
| 39 | . ' the ordinal number of the current (most recently loaded) document.', | ||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 10 | has consumer => ( | |||||||||||||||||||||||||||
| 43 | isa => 'Treex::Block::Read::ConsumerReader', | ||||||||||||||||||||||||||||
| 44 | is => 'rw' | ||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | ▶ | sub next_document_for_this_job { | ||||||||||||||||||||||||||
|
Subroutine
next_document_for_this_job: called CC=7 SCAR=31.4
Pod
documented
| |||||||||||||||||||||||||||||
| 48 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # In parallel execution, the file name is sent from the head to the workers via TCP | ||||||||||||||||||||||||||||
| 51 | # and only one doc per file is allowed, so we can override the file list to contain just | ||||||||||||||||||||||||||||
| 52 | # the file to be processed and set the $self->file_number counter to 0 – just before the file | ||||||||||||||||||||||||||||
| 53 | # to be processed (we will get another file name and reset it again next time). | ||||||||||||||||||||||||||||
| 54 | # | ||||||||||||||||||||||||||||
| 55 | # $self->doc_number is set to the number of processed files minus 1 since it will be increased | ||||||||||||||||||||||||||||
| 56 | # in next_document(). | ||||||||||||||||||||||||||||
| 57 | # | ||||||||||||||||||||||||||||
| 58 | # This is an ugly hack (next_filename _set_file_number is defined only in BaseReader and BaseAlignedReader), | ||||||||||||||||||||||||||||
| 59 | # but this code must be specified here in next_document_for_this_job because the method next_filename | ||||||||||||||||||||||||||||
| 60 | # may be overriden or may not be used at all (e.g., BaseTextReader delegates its functionality | ||||||||||||||||||||||||||||
| 61 | # to Treex::Core::Files). | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | if ( $self->consumer ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | my $res = $self->consumer->call("next_filename"); | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($res) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | $self->_set_file_number(0); | |||||||||||||||||||||||||||
| 67 | 0 | $self->_set_doc_number( $res->{file_number} - 1 ); | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # $res->{result} contains the next file name for plain readers, | ||||||||||||||||||||||||||||
| 70 | # a hashref: zone -> file name for aligned readers | ||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (ref($res->{result}) eq 'HASH'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | # here we assume that all zones exist in _filenames | ||||||||||||||||||||||||||||
| 73 | # (they should since all arguments are passed on to jobs) | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | while (my ($zone, $filename) = each %{$res->{result}}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $self->_filenames->{$zone}->_set_filenames( [ $filename ] ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | $self->from->_set_filenames( [ $res->{result} ] ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # Martin Majliš had the following for BaseAlignedReader but I see no reason for it. | ||||||||||||||||||||||||||||
| 84 | # elsif ($self->_files_per_zone){ | ||||||||||||||||||||||||||||
| 85 | # $self->_set_file_number($self->_files_per_zone + 2); | ||||||||||||||||||||||||||||
| 86 | #} | ||||||||||||||||||||||||||||
| 87 | else { | ||||||||||||||||||||||||||||
| 88 | 0 | return; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | my $doc = $self->next_document(); | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # TODO this is not very elegant | ||||||||||||||||||||||||||||
| 95 | # and it is also wrong, because if next_document issues some warnings, | ||||||||||||||||||||||||||||
| 96 | # these are printed into a wrong file. | ||||||||||||||||||||||||||||
| 97 | # However, I don't know how to get the correct doc_number before executing next_document. | ||||||||||||||||||||||||||||
| 98 | # Regarding perlcritic ProtectPrivateSubs: | ||||||||||||||||||||||||||||
| 99 | # I consider _redirect_output as internal for Treex::Core modules. | ||||||||||||||||||||||||||||
| 100 | # print STDERR "DOC: " . $doc . " : " . $self->doc_number . ", JOB: " . $self->jobindex . "\n"; | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | ▶ | if ( $doc && $self->jobindex ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$doc and $self->jobindex
MC/DC0%0/2
$doc and $self->jobindex
$doc
$self->jobindex
Truth table66%2/3
A and B
A= $docB= $self->jobindex
| |||||||||||||||||||||||||||||
| 103 | 0 | Treex::Core::Parallel::Node::_redirect_output( $self->outdir, $self->doc_number, $self->jobindex ); ## no critic (ProtectPrivateSubs) | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | return $doc; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | sub number_of_documents_per_this_job { | ||||||||||||||||||||||||||
|
Subroutine
number_of_documents_per_this_job: called CC=4 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 111 | 1 | ▶ | my $total = $self->number_of_documents() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | return $total if !$self->jobs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | my $rest = $total % $self->jobs; | |||||||||||||||||||||||||||
| 114 | 0 | my $div = ( $total - $rest ) / $self->jobs; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | return $div + ( $rest >= $self->jobindex ? 1 : 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub restart { | ||||||||||||||||||||||||||
|
Subroutine
restart: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 120 | 0 | $self->_set_doc_number(0); | |||||||||||||||||||||||||||
| 121 | 0 | return; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # Readers usually do not need any share files, | ||||||||||||||||||||||||||||
| 125 | # but all blocks should implement this method | ||||||||||||||||||||||||||||
| 126 | # and readers do not extend Treex::Core::Block. | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub get_required_share_files { | ||||||||||||||||||||||||||
|
Subroutine
get_required_share_files: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 128 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||
| 129 | 0 | return (); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 10 | 1; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||