| 1 | package Treex::Core::DocumentReader; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 33 | $Treex::Core::DocumentReader::VERSION = '0.08633_1'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # with Moose >= 2.00, this must be present also in roles | ||||||||||||||||||||||||||||
| 8 | 33 | ▶ | use MooseX::SemiAffordanceAccessor 0.09; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 33 | requires 'next_document'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 33 | requires 'number_of_documents'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # attrs for distributed processing | ||||||||||||||||||||||||||||
| 15 | # TODO: check jobs >= jobindex > 0 | ||||||||||||||||||||||||||||
| 16 | 33 | has jobs => ( | |||||||||||||||||||||||||||
| 17 | is => 'rw', | ||||||||||||||||||||||||||||
| 18 | isa => 'Int', | ||||||||||||||||||||||||||||
| 19 | documentation => 'number of jobs for parallel processing', | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 33 | has jobindex => ( | |||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | isa => 'Int', | ||||||||||||||||||||||||||||
| 25 | documentation => 'ordinal number of the current job in parallel processing', | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # TODO: this should not be needed in future | ||||||||||||||||||||||||||||
| 29 | 33 | has outdir => ( | |||||||||||||||||||||||||||
| 30 | is => 'rw', | ||||||||||||||||||||||||||||
| 31 | isa => 'Str', | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 33 | has doc_number => ( | |||||||||||||||||||||||||||
| 35 | isa => 'Int', | ||||||||||||||||||||||||||||
| 36 | is => 'ro', | ||||||||||||||||||||||||||||
| 37 | writer => '_set_doc_number', | ||||||||||||||||||||||||||||
| 38 | default => 0, | ||||||||||||||||||||||||||||
| 39 | init_arg => undef, | ||||||||||||||||||||||||||||
| 40 | documentation => 'Number of documents loaded so far, i.e.' | ||||||||||||||||||||||||||||
| 41 | . ' the ordinal number of the current (most recently loaded) document.', | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub is_current_document_for_this_job { | ||||||||||||||||||||||||||||
| 45 | 29 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
is_current_document_for_this_job: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 29 | ▶ | return 1 if !$self->jobindex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | return ( $self->doc_number - 1 ) % $self->jobs == ( $self->jobindex - 1 ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub next_document_for_this_job { | ||||||||||||||||||||||||||||
| 51 | 55 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_document_for_this_job: called CC=5 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 52 | 55 | my $doc = $self->next_document(); | |||||||||||||||||||||||||||
| 53 | 55 | ▶ | while ( $doc && !$self->is_current_document_for_this_job ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$doc and not $self->is_current_document_for_this_job
MC/DC0%0/2
$doc and not $self->is_current_document_for_this_job
$doc
not $self->is_current_document_for_this_job
Truth table66%2/3
A and not(B)
A= $docB= not $self->is_current_document_for_this_job
| |||||||||||||||||||||||||||||
| 54 | 0 | $doc = $self->next_document(); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # TODO this is not very elegant | ||||||||||||||||||||||||||||
| 58 | # and it is also wrong, because if next_document issues some warnings, | ||||||||||||||||||||||||||||
| 59 | # these are printed into a wrong file. | ||||||||||||||||||||||||||||
| 60 | # However, I don't know how to get the correct doc_number before executing next_document. | ||||||||||||||||||||||||||||
| 61 | # Regarding perlcritic ProtectPrivateSubs: | ||||||||||||||||||||||||||||
| 62 | # I consider _redirect_output as internal for Treex::Core modules. | ||||||||||||||||||||||||||||
| 63 | 55 | ▶ | 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
| |||||||||||||||||||||||||||||
| 64 | 0 | Treex::Core::Run::_redirect_output( $self->outdir, $self->doc_number, $self->jobindex ); ## no critic (ProtectPrivateSubs) | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 55 | return $doc; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub number_of_documents_per_this_job { | ||||||||||||||||||||||||||||
| 71 | 26 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
number_of_documents_per_this_job: called CC=4 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 26 | ▶ | my $total = $self->number_of_documents() or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 26 | ▶ | return $total if !$self->jobs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | my $rest = $total % $self->jobs; | |||||||||||||||||||||||||||
| 75 | 0 | my $div = ( $total - $rest ) / $self->jobs; | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return $div + ( $rest >= $self->jobindex ? 1 : 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub restart { | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
restart: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 0 | $self->_set_doc_number(0); | |||||||||||||||||||||||||||
| 82 | 0 | return; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 33 | 1; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | __END__ | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =head1 NAME | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Treex::Core::DocumentReader - interface for all document readers | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head1 VERSION | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | version 0.08633_1 | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Document readers are a Treex concept how to load documents to be processed by Treex. | ||||||||||||||||||||||||||||
| 102 | The documents can be stored in files (in various formats) or read from C<STDIN> | ||||||||||||||||||||||||||||
| 103 | or retrieved from a socket etc. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 METHODS | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =head2 To be implemented | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | These methods must be implemented in classes that consume this role. | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | =over | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =item next_document | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | Return next document (L<Treex::Core::Document>). | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =item number_of_documents | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Total number of documents that will be produced by this reader. | ||||||||||||||||||||||||||||
| 120 | If the number is unknown in advance, C<undef> should be returned. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =back | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head2 Already implemented | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =over | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =item is_current_document_for_this_job | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Is the document that was most recently returned by C<$self->next_document()> | ||||||||||||||||||||||||||||
| 131 | supposed to be processed by this job? | ||||||||||||||||||||||||||||
| 132 | Job indices and document numbers are 1-based, so e.g. for | ||||||||||||||||||||||||||||
| 133 | C<jobs = 5, jobindex = 3> we want to load documents with numbers 3,8,13,18,... | ||||||||||||||||||||||||||||
| 134 | C<jobs = 5, jobindex = 5> we want to load documents with numbers 5,10,15,20,... | ||||||||||||||||||||||||||||
| 135 | i.e. those documents where C<(doc_number-1) % jobs == (jobindex-1)>. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =item next_document_for_this_job | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Returns a next document which should be processed by this job. | ||||||||||||||||||||||||||||
| 140 | If C<jobindex> is set, returns "modulo number of jobs". | ||||||||||||||||||||||||||||
| 141 | See C<is_current_document_for_this_job>. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =item number_of_documents_per_this_job | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | Total number of documents that will be produced by this reader for this job. | ||||||||||||||||||||||||||||
| 146 | It's computed based on C<number_of_documents>, C<jobindex> and C<jobs>. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item restart | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Start reading again from the first document. | ||||||||||||||||||||||||||||
| 151 | This implementation just sets the attribute C<doc_number> to zero. | ||||||||||||||||||||||||||||
| 152 | You can add additional behavior using the Moose C<after 'restart'> construct. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =back | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | L<Treex::Block::Read::Sentences> | ||||||||||||||||||||||||||||
| 159 | L<Treex::Block::Read::Text> | ||||||||||||||||||||||||||||
| 160 | L<Treex::Block::Read::Treex> | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||