| 1 | package Treex::Block::Read::BaseTextReader; | ||||||||||
| 2 | { | ||||||||||
| 3 | 19 | $Treex::Block::Read::BaseTextReader::VERSION = '0.08633_1'; | |||||||||
| 4 | } | ||||||||||
| 5 | 19 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 19 | ▶ | use Treex::Core::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 19 | extends 'Treex::Block::Read::BaseReader'; | |||||||||
| 8 | #use File::Slurp 9999; | ||||||||||
| 9 | 19 | ▶ | use PerlIO::via::gzip; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # By default read from STDIN | ||||||||||
| 12 | 19 | has '+from' => ( default => '-' ); | |||||||||
| 13 | |||||||||||
| 14 | 19 | has language => ( isa => 'Treex::Type::LangCode', is => 'ro', required => 1 ); | |||||||||
| 15 | 19 | has lines_per_doc => ( isa => 'Int', is => 'ro', default => 0 ); | |||||||||
| 16 | 19 | has merge_files => ( isa => 'Bool', is => 'ro', default => 0 ); | |||||||||
| 17 | 19 | has encoding => ( isa => 'Str', is => 'ro', default => 'utf8' ); | |||||||||
| 18 | |||||||||||
| 19 | 19 | has _current_fh => ( is => 'rw' ); | |||||||||
| 20 | |||||||||||
| 21 | sub BUILD { | ||||||||||
| 22 | 20 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||
| 23 | 20 | ▶ | if ( $self->lines_per_doc ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 0 | $self->set_is_one_doc_per_file(0); | |||||||||
| 25 | } | ||||||||||
| 26 | 20 | return; | |||||||||
| 27 | } | ||||||||||
| 28 | |||||||||||
| 29 | sub next_filehandle { | ||||||||||
| 30 | |||||||||||
| 31 | 30 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
next_filehandle: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 32 | 30 | my $filename = $self->next_filename(); | |||||||||
| 33 | 30 | ▶ | return if !defined $filename; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | 15 | ▶ | if ( $filename eq '-' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 14 | binmode STDIN, $self->encoding; | |||||||||
| 36 | 14 | return \*STDIN; | |||||||||
| 37 | } | ||||||||||
| 38 | 1 | ▶ | my $mode = $filename =~ /[.]gz$/ ? '<:via(gzip):' : '<:'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 1 | $mode .= $self->encoding; | |||||||||
| 40 | 1 | ▶ | open my $FH, $mode, $filename or log_fatal "Can't open $filename: $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 1 | return $FH; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | sub next_document_text { | ||||||||||
| 45 | |||||||||||
| 46 | 30 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
next_document_text: called CC=9 SCAR=33.4
Pod
documented
| |||||||||||
| 47 | 30 | my $FH = $self->_current_fh; | |||||||||
| 48 | 30 | ▶ | if ( !$FH ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | 30 | ▶ | $FH = $self->next_filehandle() or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 15 | $self->_set_current_fh($FH); | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 15 | ▶ | if ( $self->is_one_doc_per_file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 15 | $self->_set_current_fh(undef); | |||||||||
| 55 | 15 | local $/ = undef; | |||||||||
| 56 | 15 | return <$FH>; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 0 | my $text = ''; | |||||||||
| 60 | LINE: | ||||||||||
| 61 | 0 | for my $line ( 1 .. $self->lines_per_doc ) { | |||||||||
| 62 | 0 | ▶ | while ( eof($FH) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | $FH = $self->next_filehandle(); | |||||||||
| 64 | 0 | ▶ | if ( !$FH ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | ▶ | return if $text eq ''; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 0 | return $text; | |||||||||
| 67 | } | ||||||||||
| 68 | 0 | $self->_set_current_fh($FH); | |||||||||
| 69 | 0 | ▶ | last LINE if !$self->merge_files; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 0 | $text .= <$FH>; | |||||||||
| 73 | } | ||||||||||
| 74 | 0 | return $text; | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 19 | 1; | |||||||||
| 78 | |||||||||||
| 79 | __END__ | ||||||||||
| 80 | |||||||||||
| 81 | =for Pod::Coverage BUILD | ||||||||||
| 82 | |||||||||||
| 83 | =head1 NAME | ||||||||||
| 84 | |||||||||||
| 85 | Treex::Block::Read::BaseTextReader - abstract ancestor for document readers | ||||||||||
| 86 | |||||||||||
| 87 | =head1 VERSION | ||||||||||
| 88 | |||||||||||
| 89 | version 0.08633_1 | ||||||||||
| 90 | |||||||||||
| 91 | =head1 DESCRIPTION | ||||||||||
| 92 | |||||||||||
| 93 | This class serves as an common ancestor for document readers, | ||||||||||
| 94 | that have parameter C<from> with a space or comma separated list of filenames | ||||||||||
| 95 | to be loaded and load the documents from plain text files. | ||||||||||
| 96 | It is designed to implement the L<Treex::Core::DocumentReader> interface. | ||||||||||
| 97 | |||||||||||
| 98 | In derived classes you need to define the C<next_document> method, | ||||||||||
| 99 | and you can use C<next_document_text> and C<new_document> methods. | ||||||||||
| 100 | |||||||||||
| 101 | =head1 ATTRIBUTES | ||||||||||
| 102 | |||||||||||
| 103 | =over | ||||||||||
| 104 | |||||||||||
| 105 | =item language (required) | ||||||||||
| 106 | |||||||||||
| 107 | =item lines_per_doc | ||||||||||
| 108 | |||||||||||
| 109 | If you want to split one file to more documents. | ||||||||||
| 110 | The default is 0 which means, don't split. | ||||||||||
| 111 | |||||||||||
| 112 | =item merge_files | ||||||||||
| 113 | |||||||||||
| 114 | Merge the content of all files (specified in C<from> attribute) into one stream. | ||||||||||
| 115 | Useful in combination with C<lines_per_doc> to get equally-sized documents | ||||||||||
| 116 | even from non-equally-sized files. | ||||||||||
| 117 | |||||||||||
| 118 | =item encoding | ||||||||||
| 119 | |||||||||||
| 120 | What is the encoding of the input files. E.g. C<utf8> (the default), C<cp1250> etc. | ||||||||||
| 121 | |||||||||||
| 122 | =back | ||||||||||
| 123 | |||||||||||
| 124 | =head1 METHODS | ||||||||||
| 125 | |||||||||||
| 126 | =over | ||||||||||
| 127 | |||||||||||
| 128 | =item next_document_text | ||||||||||
| 129 | |||||||||||
| 130 | Returns a content of each file (specified in C<from> attribute) as a text string. | ||||||||||
| 131 | |||||||||||
| 132 | =item next_filehandle | ||||||||||
| 133 | |||||||||||
| 134 | Helper method - you can use this instead of C<next_document_text> | ||||||||||
| 135 | if you don't want to load the whole text into memory | ||||||||||
| 136 | (but do e.g. SAX-like parsing). | ||||||||||
| 137 | |||||||||||
| 138 | =back | ||||||||||
| 139 | |||||||||||
| 140 | =head1 SEE | ||||||||||
| 141 | |||||||||||
| 142 | L<Treex::Block::Read::BaseReader> | ||||||||||
| 143 | L<Treex::Block::Read::Text> | ||||||||||
| 144 | |||||||||||
| 145 | =head1 AUTHOR | ||||||||||
| 146 | |||||||||||
| 147 | Martin Popel | ||||||||||
| 148 | |||||||||||
| 149 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 150 | |||||||||||
| 151 | Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||
| 152 | |||||||||||
| 153 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||