lib/Treex/Core/Document.pm

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