MSTperl/ModelBase.pm

filter: Statementstmt 75.9% 63 / 83 Branchbran 53.5% 15 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 68.7% 11 / 16 Podpod 100% 10 / 10 totaltotal 72.2% 99 / 137 CC 15 SCAR 13.6
CC
15
Cov
70%
load
7.1
load_tsv
5.4
store
4.7
CRAP 3.9
« MSTperl/Node.pm ↑ Summary MSTperl/Writer.pm »
1 package Treex::Tool::Parser::MSTperl::ModelBase;
2 {
3 9 $Treex::Tool::Parser::MSTperl::ModelBase::VERSION = '0.11949';
4 }
5  
6 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 require File::Temp;
12 9 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use File::Temp qw/ :seekable /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 9 has 'config' => (
16 isa => 'Treex::Tool::Parser::MSTperl::Config',
17 is => 'ro',
18 required => '1',
19 );
20  
21 9 has 'featuresControl' => (
22 isa => 'Treex::Tool::Parser::MSTperl::FeaturesControl',
23 is => 'rw',
24 );
25  
26 # called after preprocessing training data, before entering the MIRA phase
27 1 ▶sub prepare_for_mira {
Subroutine prepare_for_mira: called CC=1 SCAR=0.0
Pod documented
28  
29 # my ( $self, $trainer ) = @_;
30  
31 # nothing in the base, to be overridden in extending packages
32  
33 1 return;
34 }
35  
36 # returns number of features in the model (where a "feature" can stand for
37 # various things depending on the algorithm used)
38 0 ▶sub get_feature_count {
Subroutine get_feature_count: not called CC=1 SCAR=6.9
Pod documented
39  
40 # my ($self) = @_;
41  
42 # nothing in the base, to be overridden in extending packages
43  
44 0 return 0;
45 }
46  
47 # LOADING AND STORING
48  
49 2 ▶sub store {
Subroutine store: called CC=4 SCAR=15.5
Pod documented
50  
51 # (Str $filename)
52 2 my ( $self, $filename ) = @_;
53  
54 2 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 2 2
55 0 print "Saving model to '$filename'... ";
56 }
57  
58 2 open my $file, ">:encoding(utf8)", $filename;
59 2 print $file Dumper $self->get_data_to_store();
60 2 close $file;
61  
62 2 ▶ if ( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) { } 2 0 2
63 2 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 2 2
64 0 print "Model saved.\n";
65 }
66 2 return 1;
67 } else {
68 0 croak "MSTperl parser error:"
69 . "unable to create the model file '$filename'!";
70 }
71 }
72  
73 0 ▶sub get_data_to_store {
Subroutine get_data_to_store: not called CC=1 SCAR=6.9
Pod documented
74  
75 # my ($self) = @_;
76  
77 0 croak 'abstract method get_data_to_store to be overridden' .
78 ' and called on extending packages!';
79 }
80  
81 1 ▶sub store_tsv {
Subroutine store_tsv: called CC=4 SCAR=15.2
Pod documented
82  
83 # (Str $filename)
84 1 my ( $self, $filename ) = @_;
85  
86 1 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 1 1
87 0 print "Saving model to '$filename'... ";
88 }
89  
90 1 open my $file, ">:encoding(utf8)", $filename;
91 1 print $file join "\n", @{ $self->get_data_to_store_tsv() };
92 1 close $file;
93  
94 1 ▶ if ( -e $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $filename) { } 1 0 1
95 1 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 1 1
96 0 print "Model saved.\n";
97 }
98 1 return 1;
99 } else {
100 0 croak "MSTperl parser error:"
101 . "unable to create the model file '$filename'!";
102 }
103 }
104  
105 0 ▶sub get_data_to_store_tsv {
Subroutine get_data_to_store_tsv: not called CC=1 SCAR=6.9
Pod documented
106  
107 # my ($self) = @_;
108  
109 0 croak 'abstract method get_tsv_data_to_store to be overridden' .
110 ' and called on extending packages!';
111 }
112  
113 4 ▶sub load {
Subroutine load: called CC=5 SCAR=19.5
Pod documented
114  
115 # (Str $filename)
116 4 my ( $self, $filename ) = @_;
117  
118 4 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 4 4
119 0 print "Loading model from '$filename'...\n";
120 }
121  
122 4 my $tmpfile;
123 4 ▶ if ( $filename =~ /\.gz$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.gz$/) 0 4 4
124 0 $tmpfile = File::Temp->new( UNLINK => 1 );
125 0 system "gunzip -c $filename > $tmpfile";
126 0 $filename = $tmpfile->filename;
127 }
128  
129 4 my $data = do $filename;
130 4 my $result = $self->load_data($data);
131  
132 4 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 4 0 4
133 4 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 4 4
134 0 print "Model loaded.\n";
135 }
136 4 return 1;
137 } else {
138 0 croak "MSTperl parser error:"
139 . "model file data error!";
140 }
141 }
142  
143 0 ▶sub load_data {
Subroutine load_data: not called CC=1 SCAR=6.9
Pod documented
144  
145 # my ( $self, $data ) = @_;
146  
147 0 croak 'abstract method load_data to be overridden' .
148 ' and called on extending packages!';
149  
150 }
151  
152 1 ▶sub load_tsv {
Subroutine load_tsv: called CC=5 SCAR=16.9
Pod documented
153  
154 # (Str $filename)
155 1 my ( $self, $filename ) = @_;
156  
157 1 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 1 1
158 0 print "Loading model from '$filename'... ";
159 }
160  
161 1 my @data;
162  
163 #read the file
164 1 open my $file, '<:encoding(utf8)', $filename;
165 1 ▶ while (<$file>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 1444 1 1445
166 1444 chomp;
167 1444 push @data, $_;
168 }
169 1 close $file;
170  
171 1 my $result = $self->load_data_tsv( [@data] );
172  
173 1 ▶ if ($result) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 1 0 1
174 1 ▶ if ( $self->config->DEBUG >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->DEBUG >= 1) 0 1 1
175 0 print "Model loaded.\n";
176 }
177 1 return 1;
178 } else {
179 0 croak "MSTperl parser error:"
180 . "model file data error!";
181 }
182 }
183  
184 0 ▶sub load_data_tsv {
Subroutine load_data_tsv: not called CC=1 SCAR=6.9
Pod documented
185  
186 # my ( $self, $data ) = @_;
187  
188 0 croak 'abstract method load_tsv_data to be overridden' .
189 ' and called on extending packages!';
190  
191 }
192  
193 9 1;
194  
195 __END__
« MSTperl/Node.pm ↑ Summary MSTperl/Writer.pm »