| 1 | package Treex::Core::DocumentReader::Base; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 1 | $Treex::Core::DocumentReader::Base::VERSION = '0.08633_1'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | with 'Treex::Core::DocumentReader'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | has encoding => ( isa => 'Str', is => 'ro', default => 'utf8' ); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | has selector => ( isa => 'Treex::Type::Selector', is => 'ro', default => '' ); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | has file_stem => ( | |||||||||||||||||||||||||||
| 14 | isa => 'Str', | ||||||||||||||||||||||||||||
| 15 | is => 'ro', | ||||||||||||||||||||||||||||
| 16 | documentation => 'how to name the loaded documents', | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 1 | has lines_per_doc => ( isa => 'Int', is => 'ro', default => 0 ); | |||||||||||||||||||||||||||
| 20 | 1 | has merge_files => ( isa => 'Bool', is => 'ro', default => 0 ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | has check_same_number_of_files_per_zone => ( | |||||||||||||||||||||||||||
| 23 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 24 | is => 'ro', | ||||||||||||||||||||||||||||
| 25 | default => 1, | ||||||||||||||||||||||||||||
| 26 | documentation => 'exit with fatal error if zones have different number of input files', | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | has save_doc_text => ( | |||||||||||||||||||||||||||
| 30 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 31 | is => 'ro', | ||||||||||||||||||||||||||||
| 32 | default => 0, | ||||||||||||||||||||||||||||
| 33 | documentation => 'save raw document text into (each) doc-zone "text" attribute', | ||||||||||||||||||||||||||||
| 34 | ); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | has _files_per_zone => ( is => 'rw', default => 0 ); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | has zones => ( | ||||||||||||||||||||||||||||
| 39 | isa => 'ArrayRef[Treex::Core::DocumentReader::ZoneReader]', | ||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||
| 41 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 42 | 1 | ); | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 1 | has is_one_doc_per_file => ( | |||||||||||||||||||||||||||
| 45 | is => 'ro', | ||||||||||||||||||||||||||||
| 46 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 47 | default => 1, | ||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub BUILD { | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 0 | foreach my $arg ( keys %{$args} ) { | |||||||||||||||||||||||||||
| 53 | 0 | my ( $lang, $sele ) = ( $arg, '' ); | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( $arg =~ /_/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | ( $lang, $sele ) = split /_/, $arg; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( Treex::Core::Types::is_lang_code($lang) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 58 | 0 | $self->add_zone_files( $lang, $sele, $args->{$arg} ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | elsif ( $arg =~ /selector|language|scenario/ ) { } | ||||||||||||||||||||||||||||
| 61 | else { | ||||||||||||||||||||||||||||
| 62 | 0 | log_warn "$arg is not a zone label (e.g. en_src)"; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | return; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub add_zone_filenames { | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my ( $self, $language, $selector, $files_string ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
add_zone_filenames: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | 0 | $files_string =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 71 | 0 | my @files = split( /[ ,]+/, $files_string ); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( $self->check_same_number_of_files_per_zone ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ( !$self->_files_per_zone ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 75 | 0 | $self->_set_files_per_zone( scalar @files ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | elsif ( @files != $self->_files_per_zone ) { | ||||||||||||||||||||||||||||
| 78 | 0 | log_fatal("All zones must have the same number of files"); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | push @{ $self->zones }, Treex::Core::DocumentReader::ReaderZone->new( | |||||||||||||||||||||||||||
| 83 | language => $language, | ||||||||||||||||||||||||||||
| 84 | selector => $selector, | ||||||||||||||||||||||||||||
| 85 | filenames => \@files, | ||||||||||||||||||||||||||||
| 86 | encoding => $self->encoding, | ||||||||||||||||||||||||||||
| 87 | lines_per_doc => $self->lines_per_doc, | ||||||||||||||||||||||||||||
| 88 | merge_files => $self->merge_files, | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | 0 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub new_document { | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | my ( $self, $load_from ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new_document: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 0 | my ( $stem, $file_number ) = ( '', '' ); | |||||||||||||||||||||||||||
| 96 | 0 | my ( $volume, $dirs, $file ); | |||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $self->file_stem ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 0 | ( $stem, $file_number ) = ( $self->file_stem, undef ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | else { # Magical heuristics how to choose default name for a document loaded from several files | ||||||||||||||||||||||||||||
| 101 | 0 | foreach my $zone ( @{ $self->zones } ) { | |||||||||||||||||||||||||||
| 102 | 0 | my $filename = $zone->current_filename; | |||||||||||||||||||||||||||
| 103 | 0 | ( $volume, $dirs, $file ) = File::Spec->splitpath($filename); | |||||||||||||||||||||||||||
| 104 | 0 | my ( $name, $extension ) = $file =~ /([^.]+)(\..+)?/; | |||||||||||||||||||||||||||
| 105 | 0 | my $zonelabel = $zone->zone_label; | |||||||||||||||||||||||||||
| 106 | 0 | my $lang = $zone->language; | |||||||||||||||||||||||||||
| 107 | 0 | my $sele = $zone->selector; | |||||||||||||||||||||||||||
| 108 | 0 | $name =~ s/[_-]?($lang|$sele|$zonelabel)[_-]?//gi; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ( !$name && !$stem ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name or $stem
MC/DC0%0/2
$name or $stem
$name
$stem
Truth table0%0/3
A or B
A= $nameB= $stem
| |||||||||||||||||||||||||||||
| 110 | 0 | $name = 'noname'; | |||||||||||||||||||||||||||
| 111 | 0 | $file_number = undef; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if ( $stem !~ /$name/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | if ( $stem ne '' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $stem .= '_'; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | $stem .= $name; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | $self->_set_doc_number( $self->doc_number + 1 ); | |||||||||||||||||||||||||||
| 123 | return Treex::Core::Document->new( | ||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | file_stem => $stem, | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | loaded_from => join( ',', map { $_->current_filename } @{ $self->zones } ), | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 127 | defined $file_number ? ( file_number => $file_number ) : (), | ||||||||||||||||||||||||||||
| 128 | defined $dirs ? ( path => $volume . $dirs ) : (), | ||||||||||||||||||||||||||||
| 129 | defined $load_from ? ( filename => $load_from ) : (), | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub number_of_documents { | ||||||||||||||||||||||||||||
| 135 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
number_of_documents: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 136 | 0 | ▶ | return if !$self->is_one_doc_per_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 0 | return $self->_files_per_zone; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | after 'restart' => sub { | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 142 | 0 | foreach my $zone_reader ( values %{ $self->zones } ) { | |||||||||||||||||||||||||||
| 143 | 0 | $zone_reader->reset(); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | return; | |||||||||||||||||||||||||||
| 146 | 1 | }; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub zonelabels { | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
zonelabels: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 150 | 0 | return map { $_->zone_label } @{ $self->zones }; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub next_document { | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
next_document: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 155 | 0 | my ( %texts, %sents, $n_sentences ); | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 0 | foreach my $zone ( @{ $self->zones } ) { | |||||||||||||||||||||||||||
| 158 | 0 | my $text = $zone->next_document_text(); | |||||||||||||||||||||||||||
| 159 | 0 | my $zonelabel = $zone->zonelabel; | |||||||||||||||||||||||||||
| 160 | 0 | $texts{$zonelabel} = $text; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 0 | my $doc = $self->new_document(); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | if ( $self->save_doc_text ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | foreach my $zone ( @{ $self->zones } ) { | |||||||||||||||||||||||||||
| 166 | 0 | my $zonelabel = $zone->zonelabel; | |||||||||||||||||||||||||||
| 167 | 0 | my $language = $zone->language; | |||||||||||||||||||||||||||
| 168 | 0 | my $selector = $zone->selector; | |||||||||||||||||||||||||||
| 169 | 0 | my $doczone = $doc->create_zone( $language, $selector ); | |||||||||||||||||||||||||||
| 170 | 0 | $doczone->set_text( $texts{$zonelabel} ); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | my $same_n_sentences = 1; | |||||||||||||||||||||||||||
| 175 | 0 | foreach my $zonelabel ( $self->zonelabels ) { | |||||||||||||||||||||||||||
| 176 | 0 | my $text = $texts{$zonelabel}; | |||||||||||||||||||||||||||
| 177 | 0 | my @sentences = $self->get_sentences_from_doc_text( $text, $zonelabel ); | |||||||||||||||||||||||||||
| 178 | 0 | $sents{$zonelabel} = \@sentences; | |||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ( !defined $n_sentences ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 180 | 0 | $n_sentences = @sentences; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | elsif ( $n_sentences != @sentences ) { | ||||||||||||||||||||||||||||
| 183 | 0 | $same_n_sentences = 0; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if ( !$same_n_sentences ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | log_fatal 'Different number of sentences for each zone: ' | ||||||||||||||||||||||||||||
| 189 | 0 | . join( ', ', map { "$_=" . scalar( @{ $sents{$_} } ) } $self->zonelabels ); | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | for my $i ( 1 .. $n_sentences ) { | |||||||||||||||||||||||||||
| 193 | 0 | my $bundle = $doc->create_bundle(); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 0 | foreach my $zonelabel ( $self->zonelabels ) { | |||||||||||||||||||||||||||
| 196 | 0 | my ( $language, $selector ) = ( $zonelabel, '' ); | |||||||||||||||||||||||||||
| 197 | 0 | ▶ | if ( $zonelabel =~ /_/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | ( $language, $selector ) = split /_/, $zonelabel; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | 0 | my $zone = $bundle->create_zone( $language, $selector ); | |||||||||||||||||||||||||||
| 201 | 0 | $self->fill_bundle_zone( $zone, $sents{$zonelabel}[ $i - 1 ] ); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 0 | return $doc; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | sub get_sentences_from_doc_text { | ||||||||||||||||||||||||||||
| 209 | 0 | ▶ | my ( $self, $doc_text, $zone_label ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_sentences_from_doc_text: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 210 | 0 | return split /\n/, $doc_text; | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | sub fill_bundle_zone { | ||||||||||||||||||||||||||||
| 214 | 0 | ▶ | my ( $self, $zone, $raw_sentence ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
fill_bundle_zone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 215 | 0 | return log_fatal 'fill_bundle_zone must be overriden'; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 1 | 1; | |||||||||||||||||||||||||||