lib/Treex/Core/Block.pm

filter: Statementstmt 43.5% 34 / 78 Branchbran 4.1% 1 / 24 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 66.6% 10 / 15 Podpod 81.8% 9 / 11 totaltotal 37.1% 55 / 148 CC 20 SCAR 29.0
CC
20
Cov
32%
_try_process_layer
42.0
process_document
30.0
zone_label
20.0
CRAP 18.2
« lib/Treex/Core/Node/Ordered.pm ↑ Summary lib/Treex/Block/Read/BaseTextReader.pm »
1 package Treex::Core::Block;
2 {
3 34 $Treex::Core::Block::VERSION = '0.08633_1';
4 }
5 34 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 34 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use Treex::Core::Resource;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 34 has selector => ( is => 'ro', isa => 'Treex::Type::Selector', default => '', );
10 34 has language => ( is => 'ro', isa => 'Maybe[Treex::Type::LangCode]', builder => 'build_language' );
11 34 has scenario => (
12 is => 'ro',
13 isa => 'Treex::Core::Scenario',
14 writer => '_set_scenario',
15 weak_ref => 1,
16 );
17  
18 34 has grep_bundle => (
19 is => 'ro',
20 isa => 'Int', # or regex in future?
21 default => 0,
22 documentation => 'apply process_bundle only on the n-th bundle,'
23 . ' 0 (default) means apply to all bundles. Useful for debugging.',
24 );
25  
26 # If the block name contains language (e.g. W2A::EN::Tokenize contains "en")
27 # or target-language (e.g. T2T::CS2EN::FixNegation contains "en"),
28 # it is returned as a default value of the attribute $self->language
29 # so it is not necessary to write the line
30 # has '+language' => ( default => 'en' );
31 # in all *::EN::* blocks and all *::??2EN::* blocks.
32 sub build_language {
33 9 ▶ my $self = shift;
Subroutine build_language: called CC=3 SCAR=12.7
Pod undocumented
34 9 my ($lang) = $self->get_block_name() =~ /::(?:[A-Z][A-Z]2)?([A-Z][A-Z])::/;
35 9 ▶ if ( $lang && Treex::Core::Types::is_lang_code( lc $lang ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang and Treex::Core::Types::is_lang_code(lc $lang)) { } 0 9 9
Condition33%1/3
$lang and Treex::Core::Types::is_lang_code(lc $lang)
!ll&&!rl&&r
900
MC/DC0%0/2
$lang and Treex::Core::Types::is_lang_code(lc $lang)
$lang Treex::Core::Types::is_lang_code(lc $lang)
Truth table33%1/3
A and B
A= $langB= Treex::Core::Types::is_lang_code(lc $lang)
ABresult
0X0
100
111
36 0 return lc $lang;
37 }
38 else {
39 9 return;
40 }
41 }
42  
43 sub zone_label {
44 0 ▶ my ($self) = @_;
Subroutine zone_label: not called CC=4 SCAR=30.0
Pod documented
45 0 ▶ my $label = $self->language or return;
Branch: 1 branch
BranchTrueFalseTotal
my $label = $self->language or return 0 0 0
46 0 ▶ if ( defined $self->selector && $self->selector ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->selector and $self->selector ne "") 0 0 0
Condition0%0/3
defined $self->selector and $self->selector ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->selector and $self->selector ne ""
defined $self->selector $self->selector ne ""
Truth table0%0/3
A and B
A= defined $self->selectorB= $self->selector ne ""
ABresult
0X0
100
111
47 0 $label .= '_' . $self->selector;
48 }
49 0 return $label;
50 }
51  
52 # TODO
53 # has robust => ( is=> 'ro', isa=>'Bool', default=>0,
54 # documentation=>'no fatal errors in robust mode');
55  
56 sub BUILD {
57 45 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
58  
59 45 return;
60 }
61  
62 sub require_files_from_share {
63 39 ▶ my ( $self, @rel_paths ) = @_;
Subroutine require_files_from_share: called CC=1 SCAR=0.6
Pod documented
64 39 my $my_name = 'the block ' . $self->get_block_name();
65 return map {
66 39 log_info $self->get_block_name() . " requires file " . $_;
67 0 Treex::Core::Resource::require_file_from_share( $_, $my_name )
68 } @rel_paths;
69 }
70  
71 sub get_required_share_files {
72 39 ▶ my ($self) = @_;
Subroutine get_required_share_files: called CC=1 SCAR=0.0
Pod documented
73  
74 # By default there are no required share files.
75 # The purpose of this method is to be overriden if needed.
76 39 return ();
77 }
78  
79 sub process_document {
80 0 ▶ my $self = shift;
Subroutine process_document: not called CC=5 SCAR=34.0
Pod documented
81 0 my ($document) = pos_validated_list(
82 \@_,
83 { isa => 'Treex::Core::Document' },
84 );
85  
86 0 ▶ if ( !$document->get_bundles() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($document->get_bundles) 0 0 0
87 0 log_fatal "There are no bundles in the document and block " . $self->get_block_name() .
88 " doesn't override the method process_document";
89 }
90  
91 0 my $bundleNo = 1;
92 0 foreach my $bundle ( $document->get_bundles() ) {
93 0 ▶ if ( !$self->grep_bundle || $self->grep_bundle == $bundleNo ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->grep_bundle or $self->grep_bundle == $bundleNo) 0 0 0
Condition0%0/3
not $self->grep_bundle or $self->grep_bundle == $bundleNo
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->grep_bundle or $self->grep_bundle == $bundleNo
not $self->grep_bundle $self->grep_bundle == $bundleNo
Truth table0%0/3
not(A) or B
A= not $self->grep_bundleB= $self->grep_bundle == $bundleNo
ABresult
1X1
011
000
94 0 $self->process_bundle( $bundle, $bundleNo );
95 }
96 0 $bundleNo++;
97 }
98 0 return 1;
99 }
100  
101 sub process_bundle {
102 0 ▶ my ( $self, $bundle, $bundleNo ) = @_;
Subroutine process_bundle: not called CC=3 SCAR=24.8
Pod documented
103  
104 0 ▶ log_fatal "Parameter language was not set and block " . $self->get_block_name()
Branch: 1 branch
BranchTrueFalseTotal
unless $self->language 0 0 0
105 . " doesn't override the method process_bundle" if !$self->language;
106 0 my $zone = $bundle->get_zone( $self->language, $self->selector );
107 0 ▶ log_fatal(
Branch: 1 branch
BranchTrueFalseTotal
unless $zone 0 0 0
108 "Zone (lang="
109 . $self->language
110 . ", selector="
111 . $self->selector
112 . ") was not found in a bundle and block " . $self->get_block_name()
113 . " doesn't override the method process_bundle"
114 )
115 if !$zone;
116 0 return $self->process_zone( $zone, $bundleNo );
117 }
118  
119 sub _try_process_layer {
120 0 ▶ my $self = shift;
Subroutine _try_process_layer: not called CC=6 SCAR=37.4
121 0 my ( $zone, $layer, $bundleNo ) = @_;
122  
123 0 ▶ return 0 if !$zone->has_tree($layer);
Branch: 1 branch
BranchTrueFalseTotal
unless $zone->has_tree($layer) 0 0 0
124 0 my $tree = $zone->get_tree($layer);
125 0 my $meta = $self->meta;
126  
127 0 ▶ if ( my $m = $meta->find_method_by_name("process_${layer}tree") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $meta->find_method_by_name("process_${layer}tree")) 0 0 0
128 ##$self->process_atree($tree);
129 0 $m->execute( $self, $tree, $bundleNo );
130 0 return 1;
131 }
132  
133 0 ▶ if ( my $m = $meta->find_method_by_name("process_${layer}node") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $m = $meta->find_method_by_name("process_${layer}node")) 0 0 0
134 ## process_ptree should be executed also on the root node (usually the S phrase)
135 0 ▶ my @opts = $layer eq 'p' ? ({add_self => 1}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$layer eq 'p' ? : 0 0 0
136 0 foreach my $node ( $tree->get_descendants(@opts) ) {
137 ##$self->process_anode($node);
138 0 $m->execute( $self, $node, $bundleNo );
139 }
140 0 return 1;
141 }
142  
143 0 return 0;
144 }
145  
146 sub process_zone {
147 0 ▶ my ( $self, $zone, $bundleNo ) = @_;
Subroutine process_zone: not called CC=4 SCAR=30.0
Pod documented
148  
149 0 my $overriden;
150  
151 0 for my $layer (qw(a t n p)) {
152 0 ▶ $overriden ||= $self->_try_process_layer( $zone, $layer, $bundleNo );
Condition0%0/3
$overriden ||= $self->_try_process_layer($zone, $layer, $bundleNo)
l!l&&r!l&&!r
000
MC/DC0%0/2
$overriden ||= $self->_try_process_layer($zone, $layer, $bundleNo)
$overriden $self->_try_process_layer($zone, $layer, $bundleNo)
Truth table0%0/3
A ||= B
A= $overridenB= $self->_try_process_layer($zone, $layer, $bundleNo)
ABresult
1X1
011
000
153 }
154 log_fatal "One of the methods /process_(document|bundle|zone|[atnp](tree|node))/ "
155 . "must be overriden and the corresponding [atnp] trees must be present in bundles.\n"
156 . "The zone '" . $zone->get_label() . "' contains trees ( "
157 0 ▶ . ( join ',', map { $_->get_layer() } $zone->get_all_trees() ) . ")."
Branch: 1 branch
BranchTrueFalseTotal
unless $overriden 0 0 0
158 if !$overriden;
159 0 return;
160 }
161  
162 sub process_start {
163 39 ▶ my ($self) = @_;
Subroutine process_start: called CC=1 SCAR=0.0
Pod documented
164  
165 39 $self->require_files_from_share( $self->get_required_share_files() );
166  
167 39 return;
168 }
169  
170 sub process_end {
171 39 ▶ my ($self) = @_;
Subroutine process_end: called CC=1 SCAR=0.0
Pod documented
172  
173 # default implementation is empty, but can be overriden
174 39 return;
175 }
176  
177 sub get_block_name {
178 48 ▶ my $self = shift;
Subroutine get_block_name: called CC=1 SCAR=0.0
Pod documented
179 48 return ref($self);
180 }
181  
182 34 1;
183  
184 __END__
185  
186 =for Pod::Coverage BUILD build_language
187  
188 =encoding utf-8
189  
190 =head1 NAME
191  
192 Treex::Core::Block - the basic data-processing unit in the Treex framework
193  
194 =head1 VERSION
195  
196 version 0.08633_1
197  
198 =head1 SYNOPSIS
199  
200 package Treex::Block::My::Block;
201 use Moose;
202 use Treex::Core::Common;
203 extends 'Treex::Core::Block';
204
205 sub process_bundle {
206 my ( $self, $bundle) = @_;
207
208 # bundle processing
209
210 }
211  
212 =head1 DESCRIPTION
213  
214 C<Treex::Core::Block> is a base class serving as a common ancestor of
215 all Treex blocks.
216 C<Treex::Core::Block> can't be used directly in any scenario.
217 Use it's descendants which implement one of the methods
218 C<process_document()>, C<process_bundle()>, C<process_zone()>,
219 C<process_[atnp]tree()> or C<process_[atnp]node()>.
220  
221  
222 =head1 CONSTRUCTOR
223  
224 =over 4
225  
226 =item my $block = Treex::Block::My::Block->new();
227  
228 Instance of a block derived from C<Treex::Core::Block> can be created
229 by the constructor (optionally, a reference to a hash of block parameters
230 can be specified as the constructor's argument, see L</BLOCK PARAMETRIZATION>).
231 However, it is not likely to appear in your code since block initialization
232 is usually invoked automatically when initializing a scenario.
233  
234 =back
235  
236 =head1 METHODS FOR BLOCK EXECUTION
237  
238 You must override one of the following methods:
239  
240 =over 4
241  
242 =item $block->process_document($document);
243  
244 Applies the block instance on the given instance of
245 L<Treex::Core::Document>. The default implementation
246 iterates over all bundles in a document and calls C<process_bundle()>. So in
247 most cases you don't need to override this method.
248  
249 =item $block->process_bundle($bundle);
250  
251 Applies the block instance on the given bundle
252 (L<Treex::Core::Bundle>).
253  
254 =item $block->process_zone($zone);
255  
256 Applies the block instance on the given bundle zone
257 (L<Treex::Core::BundleZone>). Unlike
258 C<process_document> and C<process_bundle>, C<process_zone> requires block
259 attribute C<language> (and possibly also C<selector>) to be specified.
260  
261 =item $block->process_I<X>tree($tree);
262  
263 Here I<X> stands for a,t,n or p.
264 This method is executed on the root node of a tree on a given layer (a,t,n,p).
265  
266 =item $block->process_I<X>node($node);
267  
268 Here I<X> stands for a,t,n or p.
269 This method is executed on the every node of a tree on a given layer (a,t,n,p).
270 Note that for layers a, t, and n, this method is not executed on the root node
271 (because the root node is just a "technical" root without the attributes of regular nodes).
272 However, C<process_pnode> is executed also on the root node
273 (because its a regular non-terminal node with a phrase attribute, usually C<S>).
274  
275 =back
276  
277 =head2 $block->process_start();
278  
279 This method is called before all documents are processed.
280 This method is responsible for loading required models.
281  
282 =head2 $block->process_end();
283  
284 This method is called after all documents are processed.
285 The default implementation is empty, but derived classes can override it
286 to e.g. print some final summaries, statistics etc.
287 Overriding this method is preferable to both
288 standard Perl END blocks (where you cannot access C<$self> and instance attributes),
289 and DEMOLISH (which is not called in some cases, e.g. C<treex --watch>).
290  
291  
292  
293 =head1 BLOCK PARAMETRIZATION
294  
295 =over 4
296  
297 =item my $block = BlockGroup::My_Block->new({$name1=>$value1,$name2=>$value2...});
298  
299 Block instances can be parametrized by a hash containing parameter name/value
300 pairs.
301  
302 =item my $param_value = $block->get_parameter($param_name);
303  
304 Parameter values used in block construction can
305 be revealed by C<get_parameter> method (but cannot be changed).
306  
307 =back
308  
309 =head1 MISCEL
310  
311 =over 4
312  
313 =item my $langcode_selector = $block->zone_label();
314  
315 =item my $block_name = $block->get_block_name();
316  
317 It returns the name of the block module.
318  
319 =item my @needed_files = $block->get_required_share_files();
320  
321 If a block requires some files to be present in the shared part of Treex,
322 their list (with relative paths starting in
323 L<Treex::Core::Config->share_dir|Treex::Core::Config/share_dir>) can be
324 specified by redefining by this method. By default, an empty list is returned.
325 Presence of the files is automatically checked in the block constructor. If
326 some of the required file is missing, the constructor tries to download it
327 from L<http://ufallab.ms.mff.cuni.cz>.
328  
329 This method should be used especially for downloading statistical models,
330 but not for installed tools or libraries.
331  
332 sub get_required_share_files {
333 my $self = shift;
334 return (
335 'data/models/mytool/'.$self->language.'/features.gz',
336 'data/models/mytool/'.$self->language.'/weights.tsv',
337 );
338 }
339  
340 =item require_files_from_share()
341  
342 This method checks existence of files given as parameters, it tries to download them if they are not present
343  
344 =back
345  
346 =head1 SEE ALSO
347  
348 L<Treex::Core::Node>,
349 L<Treex::Core::Bundle>,
350 L<Treex::Core::Document>,
351 L<Treex::Core::Scenario>,
352  
353 =head1 AUTHOR
354  
355 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
356  
357 Martin Popel <popel@ufal.mff.cuni.cz>
358  
359 =head1 COPYRIGHT AND LICENSE
360  
361 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
362  
363 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Core/Node/Ordered.pm ↑ Summary lib/Treex/Block/Read/BaseTextReader.pm »