line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Treex::Tool::Parser::MSTperl::ModelBase; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Treex::Tool::Parser::MSTperl::ModelBase::VERSION = '0.11949'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
3917
|
use Data::Dumper; |
|
1
|
|
|
|
|
8736
|
|
|
1
|
|
|
|
|
69
|
|
7
|
1
|
|
|
1
|
|
861
|
use autodie; |
|
1
|
|
|
|
|
53591
|
|
|
1
|
|
|
|
|
6
|
|
8
|
1
|
|
|
1
|
|
7162
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Carp; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
require File::Temp; |
12
|
|
|
|
|
|
|
use File::Temp (); |
13
|
|
|
|
|
|
|
use File::Temp qw/ :seekable /; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has 'config' => ( |
16
|
|
|
|
|
|
|
isa => 'Treex::Tool::Parser::MSTperl::Config', |
17
|
|
|
|
|
|
|
is => 'ro', |
18
|
|
|
|
|
|
|
required => '1', |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub prepare_for_mira { |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# my ( $self, $trainer ) = @_; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# nothing in the base, to be overridden in extending packages |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
sub get_feature_count { |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# my ($self) = @_; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# nothing in the base, to be overridden in extending packages |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
return 0; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# LOADING AND STORING |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub store { |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# (Str $filename) |
52
|
|
|
|
|
|
|
my ( $self, $filename ) = @_; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
55
|
|
|
|
|
|
|
print "Saving model to '$filename'... "; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
open my $file, ">:encoding(utf8)", $filename; |
59
|
|
|
|
|
|
|
print $file Dumper $self->get_data_to_store(); |
60
|
|
|
|
|
|
|
close $file; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
if ( -e $filename ) { |
63
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
64
|
|
|
|
|
|
|
print "Model saved.\n"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
return 1; |
67
|
|
|
|
|
|
|
} else { |
68
|
|
|
|
|
|
|
croak "MSTperl parser error:" |
69
|
|
|
|
|
|
|
. "unable to create the model file '$filename'!"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub get_data_to_store { |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# my ($self) = @_; |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
croak 'abstract method get_data_to_store to be overridden' . |
78
|
|
|
|
|
|
|
' and called on extending packages!'; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub store_tsv { |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# (Str $filename) |
84
|
|
|
|
|
|
|
my ( $self, $filename ) = @_; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
87
|
|
|
|
|
|
|
print "Saving model to '$filename'... "; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
open my $file, ">:encoding(utf8)", $filename; |
91
|
|
|
|
|
|
|
print $file join "\n", @{ $self->get_data_to_store_tsv() }; |
92
|
|
|
|
|
|
|
close $file; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
if ( -e $filename ) { |
95
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
96
|
|
|
|
|
|
|
print "Model saved.\n"; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
return 1; |
99
|
|
|
|
|
|
|
} else { |
100
|
|
|
|
|
|
|
croak "MSTperl parser error:" |
101
|
|
|
|
|
|
|
. "unable to create the model file '$filename'!"; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub get_data_to_store_tsv { |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
# my ($self) = @_; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
croak 'abstract method get_tsv_data_to_store to be overridden' . |
110
|
|
|
|
|
|
|
' and called on extending packages!'; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub load { |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
# (Str $filename) |
116
|
|
|
|
|
|
|
my ( $self, $filename ) = @_; |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
119
|
|
|
|
|
|
|
print "Loading model from '$filename'...\n"; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my $tmpfile; |
123
|
|
|
|
|
|
|
if ( $filename =~ /\.gz$/ ) { |
124
|
|
|
|
|
|
|
$tmpfile = File::Temp->new( UNLINK => 1 ); |
125
|
|
|
|
|
|
|
system "gunzip -c $filename > $tmpfile"; |
126
|
|
|
|
|
|
|
$filename = $tmpfile->filename; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my $data = do $filename; |
130
|
|
|
|
|
|
|
my $result = $self->load_data($data); |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
if ($result) { |
133
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
134
|
|
|
|
|
|
|
print "Model loaded.\n"; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
return 1; |
137
|
|
|
|
|
|
|
} else { |
138
|
|
|
|
|
|
|
croak "MSTperl parser error:" |
139
|
|
|
|
|
|
|
. "model file data error!"; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub load_data { |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
# my ( $self, $data ) = @_; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
croak 'abstract method load_data to be overridden' . |
148
|
|
|
|
|
|
|
' and called on extending packages!'; |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub load_tsv { |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
# (Str $filename) |
155
|
|
|
|
|
|
|
my ( $self, $filename ) = @_; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
158
|
|
|
|
|
|
|
print "Loading model from '$filename'... "; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
my @data; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
#read the file |
164
|
|
|
|
|
|
|
open my $file, '<:encoding(utf8)', $filename; |
165
|
|
|
|
|
|
|
while (<$file>) { |
166
|
|
|
|
|
|
|
chomp; |
167
|
|
|
|
|
|
|
push @data, $_; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
close $file; |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
my $result = $self->load_data_tsv( [@data] ); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
if ($result) { |
174
|
|
|
|
|
|
|
if ( $self->config->DEBUG >= 1 ) { |
175
|
|
|
|
|
|
|
print "Model loaded.\n"; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
return 1; |
178
|
|
|
|
|
|
|
} else { |
179
|
|
|
|
|
|
|
croak "MSTperl parser error:" |
180
|
|
|
|
|
|
|
. "model file data error!"; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
sub load_data_tsv { |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# my ( $self, $data ) = @_; |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
croak 'abstract method load_tsv_data to be overridden' . |
189
|
|
|
|
|
|
|
' and called on extending packages!'; |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
1; |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
__END__ |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=pod |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=for Pod::Coverage BUILD |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=encoding utf-8 |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 NAME |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Treex::Tool::Parser::MSTperl::ModelBase |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 VERSION |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
version 0.11949 |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head1 DESCRIPTION |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
This is a base class for an in-memory represenation of a parsing or labelling |
214
|
|
|
|
|
|
|
model. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=head1 FIELDS |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=over 4 |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=item config |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Instance of L<Treex::Tool::Parser::MSTperl::Config> containing settings to be |
223
|
|
|
|
|
|
|
used for the model. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=item featuresControl |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Provides access to features, especially enabling their computation. |
228
|
|
|
|
|
|
|
Intance of L<Treex::Tool::Parser::MSTperl::FeaturesControl>. |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=back |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 METHODS |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 Loading and storing |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=over 4 |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=item $model->load('modelfile.model'); |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
Loads model from file in L<Data::Dumper> format, eg.: |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
$VAR1 = { |
243
|
|
|
|
|
|
|
'0:být|VB' => '0.0042', |
244
|
|
|
|
|
|
|
'1:pes|N1' => '0.0021', |
245
|
|
|
|
|
|
|
... |
246
|
|
|
|
|
|
|
}; |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
The feature codes are represented by their indexes |
249
|
|
|
|
|
|
|
(see L<Treex::Tool::Parser::MSTperl::FeaturesControl/simple_feature_codes>). |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=item $model->load_tsv('modelfile.tsv'); |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Loads model from file in TSV (tab separated values) format, eg.: |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
L|T:být|VB [tab] 0.0042 |
256
|
|
|
|
|
|
|
l|t:pes|N1 [tab] 0.0021 |
257
|
|
|
|
|
|
|
... |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
The feature codes are written as text. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item $model->store('modelfile.model'); |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
Stores model into file in L<Data::Dumper> format. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item $model->store_tsv('modelfile.tsv'); |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
Stores model into file in TSV format: |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=back |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head3 Method stubs to be overridden in extending packages. |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=over 4 |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=item $data = get_data_to_store(), $data = get_data_to_store_tsv() |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
Returns the data that form the model to be saved to a model file. |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=item load_data($data), load_data_tsv($data) |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
Fills the model with model data acquired from a model file. |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
=back |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head2 Training support |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
=head3 Method stubs to be overridden in extending packages. |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=over 4 |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=item prepare_for_mira |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Called after preprocessing training data, before entering the MIRA phase. |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=item get_feature_count |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
Only to provide information about the model. |
298
|
|
|
|
|
|
|
Returns number of features in the model (where a "feature" can stand for |
299
|
|
|
|
|
|
|
various things depending on the algorithm used). |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
=back |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=head1 AUTHORS |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
Rudolf Rosa <rosa@ufal.mff.cuni.cz> |
306
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
ZdenÄk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz> |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles |
312
|
|
|
|
|
|
|
University in Prague |
313
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or modify it under |
315
|
|
|
|
|
|
|
the same terms as Perl itself. |