Treex/Core/Document.pm

filter: Statementstmt 89.0% 269 / 302 Branchbran 67.3% 62 / 92 Conditioncond 65.0% 13 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 86.0% 37 / 43 Podpod 65.2% 15 / 23 totaltotal 81.5% 401 / 492 CC 75 SCAR 19.6
CC
75
Cov
83%
BUILD
17.9
retrieve_storable
13.5
_rebless_and_index
9.0
CRAP 7.1
« Treex/Core/Phrase/PP.pm ↑ Summary Treex/Core/Node/T.pm »
1 package Treex::Core::Document;
2 33 $Treex::Core::Document::VERSION = '2.20151210';
3 33 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Treex::Core::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use Treex::Core::DocZone;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Treex::Core::Bundle;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 33 ▶use Treex::PML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 33 Treex::PML::UseBackends('PMLBackend');
11 33 Treex::PML::AddResourcePath( Treex::Core::Config->pml_schema_dir() );
12  
13 33 with 'Treex::Core::WildAttr';
14  
15 33 ▶use Scalar::Util qw( weaken reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 33 ▶use PerlIO::via::gzip;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 33 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 33 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 33 ▶use Lingua::Interset::FeatureStructure;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 33 has loaded_from => ( is => 'rw', isa => 'Str', default => '' );
23 33 has path => ( is => 'rw', isa => 'Str' );
24 33 has file_stem => ( is => 'rw', isa => 'Str', default => 'noname' );
25 33 has file_number => ( is => 'rw', isa => 'Str', builder => 'build_file_number' );
26 33 has compress => ( is => 'rw', isa => 'Bool', default => undef, documentation => 'compression to .gz' );
27 33 has storable => (
28 is => 'rw',
29 isa => 'Bool',
30 default => undef,
31 documentation => 'using Storable with gz compression instead of Treex::PML'
32 );
33  
34 33 has _hash => ( is => 'rw', isa => 'Str' );
35  
36 sub get_hash {
37 0 ▶ my $self = shift;
Subroutine get_hash: not called CC=2 SCAR=17.9
Pod undocumented
38 0 ▶ if ( ! defined($self->_hash) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_hash) 0 0 0
39 0 $Storable::canonical = 1;
40 0 $self->_set_hash(md5_hex(Storable::nfreeze($self)));
41 0 $Storable::canonical = 0;
42 }
43 0 return $self->_hash;
44 }
45  
46 sub set_hash {
47 0 ▶ my ($self, $hash) = @_;
Subroutine set_hash: not called CC=1 SCAR=6.9
Pod undocumented
48  
49 0 $self->_set_hash($hash);
50  
51 0 return;
52 }
53  
54 has _pmldoc => (
55 isa => 'Treex::PML::Document',
56 is => 'rw',
57 init_arg => 'pml_doc',
58 writer => '_set_pmldoc',
59 handles => {
60 set_filename => 'changeFilename',
61 33 map { $_ => $_ }
62 qw( clone writeFile writeTo filename URL
63 changeFilename changeURL fileFormat changeFileFormat
64 backend changeBackend encoding changeEncoding userData
65 changeUserData metaData changeMetaData listMetaData
66 appData changeAppData listAppData
67  
68 documentRootData
69  
70 FS changeFS
71  
72 hint changeHint pattern_count pattern patterns
73 changePatterns tail changeTail
74  
75 trees changeTrees treeList tree delete_tree lastTreeNo notSaved
76 currentTreeNo currentNode nodes value_line value_line_list
77 determine_node_type )
78 },
79 builder => '_create_empty_pml_doc',
80 );
81  
82 has _index => (
83 is => 'rw',
84 32 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
85 33 );
86  
87 has _backref => (
88 is => 'rw',
89 32 ▶ default => sub { return {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
90 33 );
91  
92 33 has _latest_node_number => ( # for generating document-unique IDs
93 is => 'rw',
94 default => 0,
95 );
96  
97 33 ▶use Treex::PML::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 33 my $factory = Treex::PML::Factory->new();
99  
100 33 my $highest_file_number = 1;
101  
102 # the description attribute is stored inside the meta structures of pml documents,
103 # that is why it is not realized as a regular Moose attribute
104  
105 sub set_description {
106 2 ▶ my ( $self, $attr_value ) = @_;
Subroutine set_description: called CC=1 SCAR=0.0
Pod undocumented
107  
108 return Treex::PML::Node::set_attr(
109 $self->metaData('pml_root')->{meta},
110 2 'description', $attr_value
111 );
112 }
113  
114 sub description {
115 4 ▶ my $self = shift;
Subroutine description: called CC=1 SCAR=0.0
Pod documented
116 4 return Treex::PML::Node::attr( $self->metaData('pml_root')->{meta}, 'description' );
117 }
118  
119 sub build_file_number {
120 31 ▶ return sprintf "%03d", $highest_file_number++;
Subroutine build_file_number: called CC=1 SCAR=0.0
Pod undocumented
121 }
122  
123 # Full filename without the extension
124 sub full_filename {
125 2 ▶ log_fatal 'Incorrect number of arguments' if @_ != 1;
Subroutine full_filename: called CC=5 SCAR=20.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 2 2
126 2 my $self = shift;
127 2 my $path = '';
128 2 ▶ if (defined $self->path && $self->path ne ''){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->path and $self->path ne "") 0 2 2
Condition33%1/3
defined $self->path and $self->path ne ""
!ll&&!rl&&r
020
MC/DC0%0/2
defined $self->path and $self->path ne ""
defined $self->path $self->path ne ""
Truth table33%1/3
A and B
A= defined $self->pathB= $self->path ne ""
ABresult
0X0
100
111
129 0 $path = $self->path;
130 0 ▶ $path .= '/' if $path !~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[/$] 0 0 0
131 }
132 2 return $path . $self->file_stem . $self->file_number;
133 }
134  
135 sub BUILD {
136 32 ▶ my $self = shift;
Subroutine BUILD: called CC=17 SCAR=28.9
Pod undocumented
137 32 my ($params_rf) = @_;
138 32 my $pmldoc;
139  
140 32 ▶ if ( defined $params_rf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params_rf) 32 0 32
141  
142 # creating Treex::Core::Document from an already existing Treex::PML::Document instance
143 32 ▶ if ( $params_rf->{pmldoc} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params_rf->{'pmldoc'}) { } 0 32 32
elsif ($params_rf->{'filename'}) { } 10 22 32
144 0 $pmldoc = $params_rf->{pmldoc};
145 }
146  
147 # loading Treex::Core::Document from a file
148 elsif ( $params_rf->{filename} ) {
149  
150 10 ▶ if ( $params_rf->{filename} =~ /.streex$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params_rf->{'filename'} =~ /.streex$/) { } 0 10 10
151 0 log_fatal 'Storable (.streex) docs must be retrieved by Treex::Core::Document->retrieve_storable($filename)';
152 }
153  
154 else {
155  
156 # If the file contains invalid PML (e.g. unknown afun value)
157 # Treex::PML fails with die.
158 # TODO: we should rather catch the die message and report it via log_fatal
159 10 $pmldoc = eval {
160 # In r10421, ZŽ added here recover => 1:
161 # $factory->createDocumentFromFile( $params_rf->{filename}, { recover => 1 });
162 # However, if the file contains invalid PML (e.g. unknown afun value), the recover=>1 option
163 # results in returning a $pmldoc which seems to be OK, but it contains no bundles,
164 # so Treex crashes on subsequent blocks which is misleading for users.
165 # If we really want to be fault-tolerant, it seems we would need to set Treex::PML::Instance::Reader::STRICT=0,
166 # but I don't no enough about PML internals and I think it's better to make such errors fatal.
167 # Martin Popel
168 10 $factory->createDocumentFromFile( $params_rf->{filename});
169 };
170 10 ▶ log_fatal "Error while loading " . $params_rf->{filename} . ( $@ ? "\n$@" : '' )
Branch: 2 branches
BranchTrueFalseTotal
unless defined $pmldoc 0 10 10
$@ ? : 0 0 0
171 if !defined $pmldoc;
172 }
173 }
174 }
175  
176 # constructing treex document from an existing file
177 32 ▶ if ($pmldoc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pmldoc) 10 22 32
178 10 $self->_set_pmldoc($pmldoc);
179  
180 # ensuring Treex::Core types (partially copied from the factory)
181 # $doczone hashref will be reused as the blessed instance variable
182 10 for my $doczone ($self->get_all_zones()){
183 3 Treex::Core::DocZone->new($doczone);
184 }
185  
186 10 $self->_rebless_and_index();
187 }
188  
189 32 $self->deserialize_wild;
190 32 foreach my $bundle ( $self->get_bundles ) {
191 12 $bundle->deserialize_wild;
192 12 foreach my $bundlezone ( $bundle->get_all_zones ) {
193 14 foreach my $tree ( $bundlezone->get_all_trees ){
194 32 ▶ my $ordered = $tree->type->get_structure_name =~ /[at]-(root|node)/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$tree->type->get_structure_name =~ /[at]-(root|node)/ ? : 19 13 32
195 32 my $correct_ord = 0;
196 32 ▶ my @nodes = $tree->get_descendants( { add_self => 1, ($ordered ? (ordered => 1) : ()) } );
Branch: 1 branch
BranchTrueFalseTotal
$ordered ? : 19 13 32
197 32 foreach my $node (@nodes){
198 # normalize ord, so there are no gaps
199 42 ▶ if ($ordered){
Branch: 1 branch
BranchTrueFalseTotal
if ($ordered) 28 14 42
200 28 $node->_set_ord($correct_ord);
201 28 $correct_ord++;
202 }
203 42 $node->deserialize_wild;
204 42 ▶ if ( $node->DOES('Treex::Core::Node::Interset') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->DOES("Treex::Core::Node::Interset")) 28 14 42
205 28 $node->deserialize_iset;
206 }
207 }
208 }
209 }
210 }
211  
212 32 return;
213 }
214  
215 sub _rebless_and_index {
216 10 ▶ my $self = shift;
Subroutine _rebless_and_index: called CC=9 SCAR=22.0
217 10 foreach my $bundle ( $self->get_bundles ) {
218 12 bless $bundle, 'Treex::Core::Bundle';
219  
220 12 $bundle->_set_document($self);
221  
222 12 ▶ if ( defined $bundle->{zones} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bundle->{"zones"}) 8 4 12
223 8 foreach my $zone ( map { $_->value() } $bundle->{zones}->elements ) {
224  
225 # $zone hashref will be reused as the blessed instance variable
226 14 Treex::Core::BundleZone->new($zone);
227 14 $zone->_set_bundle($bundle);
228  
229 14 foreach my $tree ( $zone->get_all_trees ) {
230 32 my $layer;
231 32 ▶ if ( $tree->type->get_structure_name =~ /(\S)-(root|node|nonterminal|terminal)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->type->get_structure_name =~ /(\S)-(root|node|nonterminal|terminal)/) { } 32 0 32
232 32 $layer = uc($1);
233 }
234 else {
235 0 log_fatal "Unexpected member in zone structure: " . $tree->type->get_structure_name;
236 }
237 32 foreach my $node ( $tree, $tree->descendants ) { # must still call Treex::PML::Node's API
238 42 bless $node, "Treex::Core::Node::$layer";
239 42 $self->index_node_by_id( $node->get_id, $node );
240 42 ▶ if ($layer eq 'A' && $node->{iset}){
Branch: 1 branch
BranchTrueFalseTotal
if ($layer eq "A" and $node->{"iset"}) 3 39 42
Condition100%3/3
$layer eq "A" and $node->{"iset"}
!ll&&!rl&&r
3093
MC/DC100%2/2
$layer eq "A" and $node->{"iset"}
$layer eq "A" $node->{"iset"}
Truth table100%3/3
A and B
A= $layer eq "A"B= $node->{"iset"}
ABresult
0X0
100
111
241 3 $node->{iset} = Lingua::Interset::FeatureStructure->new(%{$node->{iset}});
242 }
243 }
244 32 $tree->_set_zone($zone);
245 }
246 }
247 }
248 }
249 10 return;
250 }
251  
252 sub _pml_attribute_hash {
253 0 ▶ my $self = shift;
Subroutine _pml_attribute_hash: not called CC=1 SCAR=6.9
254 0 return $self->metaData('pml_root')->{meta};
255 }
256  
257 #my $_treex_schema_file = Treex::PML::ResolvePath( '.', 'treex_schema.xml', 1 );
258 33 my $_treex_schema_file = Treex::Core::Config->pml_schema_dir . "/" . 'treex_schema.xml';
259 33 ▶if ( not -f $_treex_schema_file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $_treex_schema_file) 0 33 33
260 0 log_fatal "Can't find PML schema $_treex_schema_file";
261 }
262  
263 33 my $_treex_schema = Treex::PML::Schema->new( { filename => $_treex_schema_file } );
264  
265 sub _create_empty_pml_doc { ## no critic (ProhibitUnusedPrivateSubroutines)
266 32 ▶ my $fsfile = Treex::PML::Document->create
Subroutine _create_empty_pml_doc: called CC=1 SCAR=0.0
267 (
268 name => "x", #$filename, ???
269 FS => Treex::PML::FSFormat->new(
270 {
271 'deepord' => ' N' # ???
272 }
273 ),
274 trees => [],
275 backend => 'PMLBackend',
276 encoding => "utf-8",
277 );
278  
279 32 $fsfile->changeMetaData( 'schema-url', 'treex_schema.xml' );
280 32 $fsfile->changeMetaData( 'schema', $_treex_schema );
281 32 $fsfile->changeMetaData( 'pml_root', { meta => {}, bundles => undef, } );
282 32 return $fsfile;
283 }
284  
285 # --- INDEXING
286  
287 sub index_node_by_id {
288 412 ▶ my $self = shift;
Subroutine index_node_by_id: called CC=3 SCAR=11.0
Pod documented
289 412 my ( $id, $node ) = pos_validated_list(
290 \@_,
291 { isa => 'Treex::Type::Id' },
292 { isa => 'Maybe[Treex::Core::Node]' }, #jde to takhle?
293 );
294 412 my $index = $self->_index;
295 412 ▶ if ( defined $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node) { } 301 111 412
296 301 $index->{$id} = $node;
297 301 weaken $index->{$id};
298  
299 301 my $refs = $node->_get_referenced_ids;
300 301 foreach my $type ( keys %{$refs} ) {
301 4 $self->index_backref( $type, $id, $refs->{$type} );
302 }
303 }
304 else {
305 111 delete $index->{$id};
306 }
307 412 return;
308 }
309  
310 # Add references to the reversed references list
311 sub index_backref {
312 21 ▶ my ( $self, $type, $source, $targets ) = @_;
Subroutine index_backref: called CC=5 SCAR=16.1
Pod undocumented
313 21 my $backref = $self->_backref;
314  
315 21 foreach my $target ( @{$targets} ) {
316 27 ▶ next if ( !defined($target) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 5 22 27
317 22 ▶ my $target_backrefs = $backref->{$target} // {};
Condition100%2/2
$backref->{$target} // {}
l!l
1210
MC/DC100%1/1
$backref->{$target} // {}
$backref->{$target}
Truth table100%2/2
A
A= $backref->{$target}
Aresult
11
00
318 22 $backref->{$target} = $target_backrefs;
319  
320 22 ▶ $target_backrefs->{$type} = [] if ( !$target_backrefs->{$type} );
Branch: 1 branch
BranchTrueFalseTotal
unless $target_backrefs->{$type} 14 8 22
321 22 push @{ $target_backrefs->{$type} }, $source;
322 }
323 21 return;
324 }
325  
326 # Remove references from the reversed references list
327 sub remove_backref {
328 10 ▶ my ( $self, $type, $source, $targets ) = @_;
Subroutine remove_backref: called CC=4 SCAR=14.0
Pod undocumented
329 10 my $backref = $self->_backref;
330  
331 10 foreach my $target ( @{$targets} ) {
332 16 ▶ next if ( !defined($target) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $target 0 16 16
333 16 my $target_backrefs = $backref->{$target};
334 16 ▶ next if ( !$target_backrefs );
Branch: 1 branch
BranchTrueFalseTotal
unless $target_backrefs 0 16 16
335  
336 16 $target_backrefs->{$type} = [ grep { $_ ne $source } @{ $target_backrefs->{$type} } ];
337 }
338 10 return;
339 }
340  
341 # Return a hash of references ( type->[nodes] ) leading to the node with the given id
342 sub get_references_to_id {
343 12 ▶ my ( $self, $id ) = @_;
Subroutine get_references_to_id: called CC=2 SCAR=7.0
Pod documented
344 12 my $backref = $self->_backref;
345  
346 12 ▶ return if ( !$backref->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $backref->{$id} 0 12 12
347 12 return $backref->{$id}; # TODO clone ?
348 }
349  
350 # Remove all references and backreferences leading to the $node (calls remove_reference() on the source nodes)
351 sub _remove_references_to_node {
352 69 ▶ my ( $self, $node ) = @_;
Subroutine _remove_references_to_node: called CC=5 SCAR=16.1
353 69 my $id = $node->id;
354 69 my $backref = $self->_backref;
355  
356 # First, delete backreferences to the $node
357 69 my $refs = $node->_get_referenced_ids();
358 69 foreach my $type ( keys %{$refs} ) {
359 1 $self->remove_backref( $type, $id, $refs->{$type} );
360 }
361  
362 # Second, delete references to the $node
363 69 ▶ return if ( !$backref->{$id} );
Branch: 1 branch
BranchTrueFalseTotal
unless $backref->{$id} 65 4 69
364 4 my $node_backref = $backref->{$id};
365  
366 4 foreach my $type ( keys %{$node_backref} ) {
367 6 foreach my $source ( @{ $node_backref->{$type} } ) {
368 6 $self->get_node_by_id($source)->remove_reference( $type, $id );
369 }
370 }
371  
372 # Third, delete backreferences from the $node
373 4 delete $backref->{$id};
374 4 return;
375 }
376  
377 sub id_is_indexed {
378 181 ▶ my $self = shift;
Subroutine id_is_indexed: called CC=1 SCAR=0.0
Pod documented
379 181 my ($id) = pos_validated_list(
380 \@_,
381 { isa => 'Treex::Type::Id' },
382 );
383 181 return ( defined $self->_index->{$id} );
384 }
385  
386 sub get_node_by_id {
387 38 ▶ my $self = shift;
Subroutine get_node_by_id: called CC=2 SCAR=7.9
Pod documented
388 38 my ($id) = pos_validated_list(
389 \@_,
390 { isa => 'Treex::Type::Id' },
391 );
392 38 ▶ if ( defined $self->_index->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_index->{$id}) { } 38 0 38
393 38 return $self->_index->{$id};
394 }
395 else {
396 0 log_fatal "ID not indexed: id=\"$id\"";
397  
398 # This is something very fatal. Treex assumes every node ID to
399 # be valid and pointing to a node *in the given document*.
400 # (It is fine to have a node with no a/lex.rf
401 # attribute, but if the attribute is there, the value
402 # has to be an ID within the document.)
403 #
404 # If your data violates the requirement and your IDs point to
405 # a different document, the only hack we suggest is to drop such
406 # references...
407 }
408 0 return;
409 }
410  
411 sub get_all_node_ids {
412 0 ▶ log_fatal('Incorrect number of arguments') if @_ != 1;
Subroutine get_all_node_ids: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
413 0 my $self = shift;
414 0 return ( keys %{ $self->_index } );
415 }
416  
417 # -------------------------------------- ACCESS TO BUNDLES -------------------
418  
419 sub get_bundles {
420 98 ▶ log_fatal('Incorrect number of arguments') if @_ != 1;
Subroutine get_bundles: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 98 98
421 98 my $self = shift;
422 98 return $self->trees;
423 }
424  
425 sub create_bundle {
426 28 ▶ my ( $self, $arg_ref ) = @_;
Subroutine create_bundle: called CC=6 SCAR=17.9
Pod documented
427 28 my $fsfile = $self->_pmldoc();
428 28 my $new_bundle;
429 my $position_of_new;
430  
431 28 ▶ if ( $arg_ref and ( $arg_ref->{after} or $arg_ref->{before} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_ref and $arg_ref->{'after'} || $arg_ref->{'before'}) { } 2 26 28
Condition66%4/6
$arg_ref and $arg_ref->{"after"} || $arg_ref->{"before"}
!ll&&!rl&&r
2602
$arg_ref->{"after"} or $arg_ref->{"before"}
l!l&&r!l&&!r
110
MC/DC33%1/3
$arg_ref and $arg_ref->{"after"} || $arg_ref->{"before"}
$arg_ref $arg_ref->{"after"} $arg_ref->{"before"}
Truth table75%3/4
A and B or C
A= $arg_refB= $arg_ref->{"after"}C= $arg_ref->{"before"}
ABCresult
0XX0
1000
11X1
1011
432 2 ▶ my $reference_bundle = ( $arg_ref->{after} ) ? $arg_ref->{after} : $arg_ref->{before};
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'after'} ? : 1 1 2
433 2 my $position_of_reference = $reference_bundle->get_position;
434 2 ▶ $position_of_new = $position_of_reference + ( $arg_ref->{after} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$arg_ref->{'after'} ? : 1 1 2
435 }
436  
437 else { # default: append at the end of the document
438 26 $position_of_new = scalar( $self->get_bundles() );
439 }
440  
441 28 $new_bundle = $fsfile->new_tree($position_of_new);
442 28 $new_bundle->set_type_by_name( $fsfile->metaData('schema'), 'bundle.type' );
443 28 bless $new_bundle, "Treex::Core::Bundle"; # is this correct/sufficient with Moose ????
444 28 $new_bundle->_set_document($self);
445  
446 28 $new_bundle->set_id( "s" . ( $fsfile->lastTreeNo + 1 ) );
447  
448 28 return $new_bundle;
449 }
450  
451 # ----------------------- ACCESS TO ZONES ------------------------------------
452  
453 sub create_zone {
454 4 ▶ my $self = shift;
Subroutine create_zone: called CC=2 SCAR=6.9
Pod documented
455 4 my ( $language, $selector ) = pos_validated_list(
456 \@_,
457 { isa => 'Treex::Type::LangCode' },
458 { isa => 'Treex::Type::Selector', default => '' },
459 );
460  
461 4 my $new_zone = Treex::Core::DocZone->new(
462 {
463 'language' => $language,
464 'selector' => $selector
465 }
466 );
467  
468 4 my $new_element = Treex::PML::Seq::Element->new( 'zone', $new_zone );
469  
470 4 my $meta = $self->metaData('pml_root')->{meta};
471 4 ▶ if ( defined $meta->{zones} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $meta->{'zones'}) { } 2 2 4
472 2 $meta->{zones}->unshift_element_obj($new_element);
473 }
474 else {
475 2 $meta->{zones} = Treex::PML::Seq->new( [$new_element] );
476 }
477  
478 4 return $new_zone;
479 }
480  
481 sub get_all_zones {
482 15 ▶ my $self = shift;
Subroutine get_all_zones: called CC=2 SCAR=6.9
Pod undocumented
483 15 my $meta = $self->metaData('pml_root')->{meta};
484 15 ▶ return if !$meta->{zones};
Branch: 1 branch
BranchTrueFalseTotal
unless $meta->{"zones"} 9 6 15
485
486 # Each element is a pair [$name, $value]. We need just the values.
487 6 return map {$_->[1]} $meta->{zones}->elements;
488 }
489  
490 sub get_zone {
491 4 ▶ my $self = shift;
Subroutine get_zone: called CC=4 SCAR=14.2
Pod documented
492 4 my ( $language, $selector ) = pos_validated_list(
493 \@_,
494 { isa => 'Treex::Type::LangCode' },
495 { isa => 'Treex::Type::Selector', default => '' },
496 );
497  
498 4 foreach my $zone ($self->get_all_zones()) {
499 12 ▶ return $zone if $zone->language eq $language && $zone->selector eq $selector;
Branch: 1 branch
BranchTrueFalseTotal
if $zone->language eq $language and $zone->selector eq $selector 4 8 12
Condition66%2/3
$zone->language eq $language and $zone->selector eq $selector
!ll&&!rl&&r
084
MC/DC50%1/2
$zone->language eq $language and $zone->selector eq $selector
$zone->language eq $language $zone->selector eq $selector
Truth table66%2/3
A and B
A= $zone->language eq $languageB= $zone->selector eq $selector
ABresult
0X0
100
111
500 }
501 0 return;
502 }
503  
504 sub get_or_create_zone {
505 0 ▶ my $self = shift;
Subroutine get_or_create_zone: not called CC=2 SCAR=17.9
Pod documented
506 0 my ( $language, $selector ) = pos_validated_list(
507 \@_,
508 { isa => 'Treex::Type::LangCode' },
509 { isa => 'Treex::Type::Selector', default => '' },
510 );
511  
512 0 my $fs_zone = $self->get_zone( $language, $selector );
513 0 ▶ if ( not defined $fs_zone ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fs_zone) 0 0 0
514 0 $fs_zone = $self->create_zone( $language, $selector );
515 }
516 0 return $fs_zone;
517 }
518  
519 # -------------------- LOADING AND SAVING ------------------------------------
520  
521 sub load {
522 0 ▶ my $self = shift;
Subroutine load: not called CC=1 SCAR=6.9
Pod documented
523 0 return $self->_pmldoc->load(@_);
524  
525 # TODO: this is unfinished: should be somehow connected with the code in BUILD
526 }
527  
528 sub save {
529 11 ▶ my $self = shift;
Subroutine save: called CC=3 SCAR=11.0
Pod documented
530 11 my ($filename) = @_;
531  
532 11 ▶ if ( $filename =~ /\.streex$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /\.streex$/) { } 1 10 11
533 1 ▶ open( my $F, ">:via(gzip)", $filename ) or log_fatal $!;
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open my $F, ">:via(gzip)", $filename or log_fatal($!) 0 1 1
534 1 print $F Storable::nfreeze($self);
535 1 close $F;
536  
537 # using Storable::nstore_fd($self,*$F) emits 'Inappropriate ioctl for device'
538 }
539  
540 else {
541 10 $self->_serialize_all_wild();
542 10 return $self->_pmldoc->save(@_);
543 }
544  
545 1 return;
546 }
547  
548 sub _serialize_all_wild {
549 10 ▶ my ($self) = @_;
Subroutine _serialize_all_wild: called CC=5 SCAR=16.1
550 10 $self->serialize_wild;
551 10 foreach my $bundle ( $self->get_bundles ) {
552 12 $bundle->serialize_wild;
553 12 foreach my $bundlezone ( $bundle->get_all_zones ) {
554 14 foreach my $node ( map { $_->get_descendants( { add_self => 1 } ) } $bundlezone->get_all_trees ) {
555 42 $node->serialize_wild;
556 42 ▶ if ( $node->DOES('Treex::Core::Node::Interset') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->DOES("Treex::Core::Node::Interset")) 28 14 42
557 28 $node->serialize_iset;
558 }
559 }
560 }
561 }
562 10 return;
563 }
564  
565 sub retrieve_storable {
566 1 ▶ my ( $class, $file ) = @_; # $file stands for a file name, but it can be also file handle (needed by the TrEd backend for .streex)
Subroutine retrieve_storable: called CC=12 SCAR=26.0
Pod documented
567  
568 1 my $FILEHANDLE;
569 1 my $opened = 0;
570  
571 1 ▶ if ( ref($file) and reftype($file) eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file and builtin::reftype($file) eq 'GLOB') { } 0 1 1
Condition33%1/3
ref $file and builtin::reftype($file) eq "GLOB"
!ll&&!rl&&r
100
MC/DC0%0/2
ref $file and builtin::reftype($file) eq "GLOB"
ref $file builtin::reftype($file) eq "GLOB"
Truth table33%1/3
A and B
A= ref $fileB= builtin::reftype($file) eq "GLOB"
ABresult
0X0
100
111
572 0 $FILEHANDLE = $file;
573 }
574 else {
575 1 ▶ log_fatal "filename=$file, but Treex::Core::Document->retrieve_storable(\$filename) can be used only for .streex files"
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\.streex$/ 0 1 1
576 unless $file =~ /\.streex$/;
577 1 ▶ open $FILEHANDLE, "<:via(gzip)", $file or log_fatal($!);
Branch: 1 branch
BranchTrueFalseTotal
open $FILEHANDLE, "<:via(gzip)", $file or log_fatal($!) 0 1 1
578 1 $opened = 1;
579 }
580  
581 1 my $serialized;
582  
583 # reading it this way is silly, but both slurping the file or
584 # using Storable::retrieve_fd lead to errors when used with via(gzip)
585 1 ▶ while (<$FILEHANDLE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $FILEHANDLE)) 4083 1 4084
586 4083 $serialized .= $_;
587 }
588  
589 1 ▶ if ( $opened ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opened) 1 0 1
590 1 close($FILEHANDLE);
591 }
592  
593 # my $retrieved_doc = Storable::retrieve_fd(*$FILEHANDLE) or log_fatal($!);
594 1 ▶ my $retrieved_doc = Storable::thaw($serialized) or log_fatal $!;
Branch: 1 branch
BranchTrueFalseTotal
my $retrieved_doc = Storable::thaw($serialized) or log_fatal($!) 0 1 1
595  
596 1 ▶ if ( not ref($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 1 0 1
597 1 $retrieved_doc->set_loaded_from($file);
598 1 my ( $volume, $dirs, $file_name ) = File::Spec->splitpath($file);
599 1 $retrieved_doc->set_path( $volume . $dirs );
600  
601 # $retrieved_doc->changeFilename($file); # why this doesn't affect the name displayed in TrEd?
602 }
603  
604 # *.streex files saved before r8789 (2012-05-29) have no PML types with nodes, let's fix it
605 # TODO: delete this hack as soon as no such old streex files are needed.
606 1 foreach my $bundle ( $retrieved_doc->get_bundles() ) {
607 2 foreach my $bundlezone ( $bundle->get_all_zones() ) {
608 1 foreach my $node ( map { $_->get_descendants() } $bundlezone->get_all_trees() ) {
609  
610 # skip this hack if we are dealing with a new streex file
611 #return $retrieved_doc if $node->type;
612 # This shortcut does not work since old files have only *some* nodes without types
613 1 $node->fix_pml_type();
614 }
615 }
616 }
617  
618 1 return $retrieved_doc;
619 }
620  
621 33 __PACKAGE__->meta->make_immutable;
622  
623 33 1;
624  
625 __END__
626  
627  
628  
629 =for Pod::Coverage BUILD build_file_number description set_description
630  
631 =encoding utf-8
632  
633 =head1 NAME
634  
635 Treex::Core::Document - representation of a text and its linguistic analyses in the Treex framework
636  
637 =head1 VERSION
638  
639 version 2.20151210
640  
641 =head1 DESCRIPTION
642  
643 A document consists of a sequence of bundles, mirroring a sequence
644 of natural language sentences (typically, but not necessarily,
645 originating from the same text). Attributes (attribute-value pairs)
646 can be attached to a document as a whole.
647  
648 Note that the references from the bundles to the containing document are weak,
649 so make sure you always keep a reference to the document in scope to prevent
650 the contents of the document from being garbage-collected.
651  
652 =head1 ATTRIBUTES
653  
654 C<Treex::Core::Document>'s instances have the following attributes:
655  
656 =over 4
657  
658 =item description
659  
660 Textual description of the file's content that is stored in the file.
661  
662 =item loaded_from
663  
664 =item path
665  
666 =item file_stem
667  
668 =item file_number
669  
670 =back
671  
672 The attributes can be accessed using semi-affordance accessors:
673 getters have the same names as attributes, while setters start with
674 C<set_>. For example, the attribute C<path> has a getter C<path()> and a setter C<set_path($path)>
675  
676  
677  
678 =head1 METHODS
679  
680 =head2 Constructor
681  
682 =over 4
683  
684 =item my $new_document = Treex::Core::Document->new;
685  
686 creates a new empty document object.
687  
688 =item my $new_document = Treex::Core::Document->new( { pmldoc => $pmldoc } );
689  
690 creates a C<Treex::Core::Document> instance from an already existing L<Treex::PML::Document> instance
691  
692 =item my $new_document = Treex::Core::Document->new( { filename => $filename } );
693  
694 loads a C<Treex::Core::Document> instance from a .treex file
695  
696 =back
697  
698  
699 =head2 Access to zones
700  
701 Document zones are instances of L<Treex::Core::DocZone>, parametrized
702 by language code and possibly also by another free label
703 called selector, whose purpose is to distinguish zones for the same language
704 but from a different source.
705  
706 =over 4
707  
708 =item my $zone = $doc->create_zone( $langcode, ?$selector );
709  
710 =item my $zone = $doc->get_zone( $langcode, ?$selector );
711  
712 =item my $zone = $doc->get_or_create_zone( $langcode, ?$selector );
713  
714 =back
715  
716  
717 =head2 Access to bundles
718  
719 =over 4
720  
721 =item my @bundles = $document->get_bundles();
722  
723 Returns the array of bundles contained in the document.
724  
725  
726 =item my $new_bundle = $document->create_bundle();
727  
728 Creates a new empty bundle and appends it
729 at the end of the document.
730  
731 =item my $new_bundle = $document->new_bundle_before( $existing_bundle );
732  
733 Creates a new empty bundle and inserts it
734 in front of the existing bundle.
735  
736 =item my $new_bundle = $document->new_bundle_after( $existing_bundle );
737  
738 Creates a new empty bundle and inserts it
739 after the existing bundle.
740  
741 =back
742  
743  
744 =head2 Node indexing
745  
746 =over 4
747  
748 =item $document->index_node_by_id( $id, $node );
749  
750 The node is added to the document's indexing table C<id2node> (it is done
751 automatically in L<Treex::Core::Node::set_attr()|Treex::Core::Node/set_attr>
752 if the attribute name is 'C<id>'). When using C<undef> in the place of the
753 second argument, the entry for the given id is deleted from the hash.
754  
755  
756 =item my $node = $document->get_node_by_id( $id );
757  
758 Return the node which has the value C<$id> in its 'C<id>' attribute,
759 no matter to which tree and to which bundle in the given document
760 the node belongs to.
761  
762 It is prohibited in Treex for IDs to point outside of the current document.
763 In rare cases where your data has such links, we recommend you to split the
764 documents differently or hack it by dropping the problematic links.
765  
766 =item $document->id_is_indexed( $id );
767  
768 Return C<true> if the given C<id> is already present in the indexing table.
769  
770 =item $document->get_all_node_ids();
771  
772 Return the array of all node identifiers indexed in the document.
773  
774 =item $document->get_references_to_id( $id );
775  
776 Return all references leading to the given node id in a hash (keys are reference types, e.g. 'alignment',
777 'a/lex.rf' etc., values are arrays of nodes referencing this node).
778  
779 =item $document->remove_refences_to_id( $id );
780  
781 Remove all references to the given node id (calls remove_reference() on each referencing node).
782  
783 =back
784  
785 =head2 Serializing
786  
787 =over 4
788  
789 =item my $document = load($filename, \%opts)
790  
791 Loads document from C<$filename> given C<%opts> using L<Treex::PML::Document::load()>
792  
793 =item $document->save($filename)
794  
795 Saves document to C<$filename> using L<Treex::PML::Document::save()>,
796 or by the Storable module if the file's extension is .streex.gz.
797  
798 =item Treex::Core::Document->retrieve_storable($filename)
799  
800 Loading a document from the .streex (Storable) format.
801  
802 =back
803  
804 =head2 Other
805  
806 =over 4
807  
808 =item my $filename = $doc->full_filename;
809  
810 full filename without the extension
811  
812 =back
813  
814  
815 =head1 AUTHOR
816  
817 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
818  
819 Martin Popel <popel@ufal.mff.cuni.cz>
820  
821 Ondřej Dušek <odusek@ufal.mff.cuni.cz>
822  
823 =head1 COPYRIGHT AND LICENSE
824  
825 Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
826  
827 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Core/Phrase/PP.pm ↑ Summary Treex/Core/Node/T.pm »