Block/Read/BaseAlignedReader.pm

filter: Statementstmt 74.4% 73 / 98 Branchbran 52.7% 19 / 36 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 84.6% 11 / 13 Podpod 83.3% 5 / 6 totaltotal 67.4% 110 / 163 CC 25 SCAR 28.8
CC
25
Cov
67%
BUILD
38.0
new_document
15.0
current_filenames
3.1
CRAP 17.8
« Block/W2A/ParseMSTperl.pm ↑ Summary Tool/ProcessUtils.pm »
1 package Treex::Block::Read::BaseAlignedReader;
2 6 $Treex::Block::Read::BaseAlignedReader::VERSION = '2.20151102';
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 with 'Treex::Core::DocumentReader';
8 6 ▶use Treex::Core::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub next_document {
Subroutine next_document: called CC=1 SCAR=0.0
Pod documented
11 1 my ($self) = @_;
12 1 return log_fatal "method next_document must be overriden in " . ref($self);
13 }
14  
15 6 has selector => ( isa => 'Treex::Type::Selector', is => 'ro', default => '' );
16  
17 6 has file_stem => (
18 isa => 'Str',
19 is => 'ro',
20 documentation => 'how to name the loaded documents',
21 );
22  
23 # private attributes
24 has _filenames => (
25 isa => 'HashRef[Str]',
26 is => 'rw',
27 init_arg => undef,
28 4 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
29 6 documentation => 'mapping zone_label->filenames to be loaded;'
30 . ' automatically initialized from constructor arguments',
31 );
32  
33 6 has _files_per_zone => ( is => 'rw', default => 0 );
34  
35 6 has _file_number => (
36 isa => 'Int',
37 is => 'rw',
38 default => 0,
39 init_arg => undef,
40 documentation => 'Number of n-tuples of input files loaded so far.',
41 );
42  
43 6 has skip_finished => (
44 isa => 'Str',
45 is => 'ro',
46 documentation => 'Skip input files for which a matching non-empty output file exists '
47 . '(presumably created by a previous unfinished Treex run). '
48 . 'This parameter specifies a regex substitution how to derive the output filename from the input filename. '
49 . 'It is parallel to the parameter substitute={indir}{outdir} in writers. '
50 . 'However, you need to take care of filename extensions too, '
51 . 'e.g. if converting conll to treex, you should use skip_finished={indir/(.+).conll$}{outdir/$1.treex.gz}',
52 );
53  
54 #BUILD is needed for processing generic arguments - now only shortcuts of type langcode_selector
55 4 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=36.4
Pod undocumented
56 4 my ( $self, $args ) = @_;
57 4 foreach my $arg ( keys %{$args} ) {
58 4 my ( $lang, $sele ) = ( $arg, '' );
59 4 ▶ if ( $arg =~ /_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /_/) 2 2 4
60 2 ( $lang, $sele ) = split /_/, $arg;
61 }
62 4 ▶ if ( is_lang_code($lang) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_lang_code($lang)) { } 4 0 4
elsif ($arg =~ /selector|language|scenario/) { } 0 0 0
63 4 my $files = Treex::Core::Files->new({string => $args->{$arg}});
64 4 ▶ if ( !$self->_files_per_zone ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->_files_per_zone) { } 2 2 4
elsif ($files->number_of_files != $self->_files_per_zone) { } 0 2 2
65 2 $self->_set_files_per_zone( $files->number_of_files );
66 }
67 elsif ( $files->number_of_files != $self->_files_per_zone ) {
68 0 log_fatal('All zones must have the same number of files: ' . $files->number_of_files . ' != ' . $self->_files_per_zone);
69 }
70 4 $self->_filenames->{$arg} = $files;
71 }
72 elsif ( $arg =~ /selector|language|scenario/ ) { }
73 0 else { log_warn "$arg is not a zone label (e.g. en_src)"; }
74 }
75 4 ▶ if (my $regex = $self->skip_finished){
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $self->skip_finished) 0 4 4
76 0 foreach my $zone (keys %{$self->_filenames}) {
77 0 my $filenames_ref = $self->_filenames->{$zone}->filenames;
78 0 my @filtered_filenames;
79 0 my $eval_string = '$filename =~ s' . $regex . '; 1;';
80 0 for my $input_filename (@$filenames_ref){
81 0 my $filename = $input_filename;
82 0 ▶ eval $eval_string or log_fatal "Failed to eval $eval_string"; ## no critic qw(BuiltinFunctions::ProhibitStringyEval)
Branch: 1 branch
BranchTrueFalseTotal
eval $eval_string or log_fatal("Failed to eval $eval_string") 0 0 0
83 0 ▶ if (! -s $filename){
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $filename) 0 0 0
84 0 push @filtered_filenames, $input_filename;
85 #say "not finished: $input_filename -> $filename";
86 } #else {say "finished: $input_filename -> $filename";}
87 }
88 0 $self->_filenames->{$zone} = Treex::Core::Files->new({filenames => \@filtered_filenames});
89 0 my $input_number = @$filenames_ref;
90 0 my $filtered_number = @filtered_filenames;
91 0 my $finished_number = $input_number - $filtered_number;
92 0 log_info "$finished_number files out of $input_number were finished, reading only the remaining $filtered_number.";
93 }
94 }
95 4 return;
96 }
97  
98 3 ▶sub current_filenames {
Subroutine current_filenames: called CC=3 SCAR=11.4
Pod documented
99 3 my ($self) = @_;
100 3 my $n = $self->_file_number;
101 3 ▶ return if $n == 0 || $n > $self->_files_per_zone;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 or $n > $self->_files_per_zone 0 3 3
Condition33%1/3
$n == 0 or $n > $self->_files_per_zone
l!l&&r!l&&!r
003
MC/DC0%0/2
$n == 0 or $n > $self->_files_per_zone
$n == 0 $n > $self->_files_per_zone
Truth table33%1/3
A or B
A= $n == 0B= $n > $self->_files_per_zone
ABresult
1X1
011
000
102 3 my %result = map { $_ => $self->_filenames->{$_}->filenames->[ $n - 1 ] } keys %{ $self->_filenames };
103 3 return \%result;
104 }
105  
106 2 ▶sub next_filenames {
Subroutine next_filenames: called CC=1 SCAR=0.0
Pod documented
107 2 my ($self) = @_;
108 2 $self->_set_file_number( $self->_file_number + 1 );
109 2 return $self->current_filenames;
110 }
111  
112 1 ▶sub new_document {
Subroutine new_document: called CC=12 SCAR=27.1
Pod documented
113 1 my ( $self, $load_from ) = @_;
114 1 my %filenames = %{$self->current_filenames()};
115 1 ▶ log_fatal "next_filenames() must be called before new_document()" if !%filenames;
Branch: 1 branch
BranchTrueFalseTotal
unless %filenames 0 1 1
116  
117 1 my ( $stem, $file_number ) = ( '', '' );
118 1 my ( $volume, $dirs, $file );
119 1 ▶ if ( $self->file_stem ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file_stem) { } 0 1 1
120 0 ( $stem, $file_number ) = ( $self->file_stem, undef );
121 }
122 else { # Magical heuristics how to choose default name for a document loaded from several files
123 1 foreach my $zone_label ( keys %filenames ) {
124 2 my $filename = $filenames{$zone_label};
125 2 ( $volume, $dirs, $file ) = File::Spec->splitpath($filename);
126  
127 # Delete file extension, e.g.
128 # file.01.conll -> file.01
129 # cs42.treex.gz -> cs42
130 2 $file =~ s/\.[^.]+(\.gz)?$//;
131  
132 # Substitute standard input for noname.
133 2 $file =~ s/^-$/noname/;
134  
135 # Heuristically delete indication of language&selector from the filename.
136 2 my ( $lang, $sele ) = ( $zone_label, '' );
137 2 ▶ if ( $zone_label =~ /_/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zone_label =~ /_/) 1 1 2
138 1 ( $lang, $sele ) = split /_/, $zone_label;
139 }
140 2 $file =~ s/[_-]?($lang|$sele|$zone_label)[_-]?//gi;
141 2 ▶ if ( !$file && !$stem ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file or $stem) 0 2 2
Condition33%1/3
$file or $stem
l!l&&r!l&&!r
200
MC/DC0%0/2
$file or $stem
$file $stem
Truth table33%1/3
A or B
A= $fileB= $stem
ABresult
1X1
011
000
142 0 $file = 'noname';
143 0 $file_number = undef;
144 }
145 2 ▶ if ( $stem !~ /$file/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stem =~ /$file/) 1 1 2
146 1 ▶ if ( $stem ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stem ne "") 0 1 1
147 0 $stem .= '_';
148 }
149 1 $stem .= $file;
150 }
151 }
152 }
153  
154 1 $self->_set_doc_number( $self->doc_number + 1 );
155 1 ▶ return Treex::Core::Document->new(
Branch: 3 branches
BranchTrueFalseTotal
defined $file_number ? : 1 0 1
defined $dirs ? : 1 0 1
defined $load_from ? : 0 1 1
156 {
157 file_stem => $stem,
158 loaded_from => join( ',', values %filenames ),
159 defined $file_number ? ( file_number => $file_number ) : (),
160 defined $dirs ? ( path => $volume . $dirs ) : (),
161 defined $load_from ? ( filename => $load_from ) : (),
162 }
163 );
164 }
165  
166 0 ▶sub number_of_documents {
Subroutine number_of_documents: not called CC=1 SCAR=6.9
Pod documented
167 0 my $self = shift;
168 0 return $self->_files_per_zone;
169 }
170  
171 0 ▶after 'restart' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 my $self = shift;
173 0 $self->_set_file_number(0);
174 6 };
175  
176 6 1;
177  
178 __END__
« Block/W2A/ParseMSTperl.pm ↑ Summary Tool/ProcessUtils.pm »