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