| 1 | package Treex::Block::Util::Eval; | ||||||||||||||||||||||||||||
| 2 | 3 | $Treex::Block::Util::Eval::VERSION = '2.20151210'; | |||||||||||||||||||||||||||
| 3 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Treex::Core::Common; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | extends 'Treex::Core::Block'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | has [ | |||||||||||||||||||||||||||
| 8 | qw( document bundle zone | ||||||||||||||||||||||||||||
| 9 | atree ttree ntree ptree | ||||||||||||||||||||||||||||
| 10 | anode tnode nnode pnode | ||||||||||||||||||||||||||||
| 11 | _args) | ||||||||||||||||||||||||||||
| 12 | ] | ||||||||||||||||||||||||||||
| 13 | => ( is => 'rw' ); | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | has expand_code => ( | |||||||||||||||||||||||||||
| 16 | is=> 'ro', | ||||||||||||||||||||||||||||
| 17 | isa => 'Bool', | ||||||||||||||||||||||||||||
| 18 | default => 1, | ||||||||||||||||||||||||||||
| 19 | documentation => 'Should "$." be expanded to "$this->" in all eval codes?' | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub BUILD { | ||||||||||||||||||||||||||||
| 23 | 2 | ▶ | my ( $self, $arg_ref ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | $arg_ref->{_atree} = any { $arg_ref->{$_} } qw(atree anode); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 2 | ▶ | $arg_ref->{_ttree} = any { $arg_ref->{$_} } qw(ttree tnode); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | $arg_ref->{_ntree} = any { $arg_ref->{$_} } qw(ntree nnode); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | $arg_ref->{_ptree} = any { $arg_ref->{$_} } qw(ptree pnode); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | $arg_ref->{_zone} = any { $arg_ref->{$_} } qw(zone _atree _ttree _ntree _ptree); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | $arg_ref->{_bundle} = any { $arg_ref->{$_} } qw(bundle _zone); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | if ( !any { $arg_ref->{$_} } qw(doc document _bundle) ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | log_fatal "At least one of the following parameters must be non-empty:" | |||||||||||||||||||||||||||
| 33 | . " document, bundle, zone, [atnp]tree, [atnp]node."; | ||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | 2 | ▶ | if ($arg_ref->{doc}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | $self->set_document($arg_ref->{doc}); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | $self->_set_args($arg_ref); | |||||||||||||||||||||||||||
| 40 | 2 | return; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub expand_eval_code { | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my ($self, $to_eval) = @_; | ||||||||||||||||||||||||||
|
Subroutine
expand_eval_code: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | return "$to_eval;1;" if !$self->expand_code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | $to_eval =~ s/\$\./\$this->/g; | |||||||||||||||||||||||||||
| 47 | 0 | return "$to_eval;1;"; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ## no critic (ProhibitStringyEval) This block needs string evals | ||||||||||||||||||||||||||||
| 51 | sub process_document { | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my ( $self, $document ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_document: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my $doc = $document; | |||||||||||||||||||||||||||
| 54 | 0 | my $this = $document; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if ( $self->document ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 0 | my $to_eval = $self->expand_eval_code($self->document); | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | eval($to_eval) or log_fatal("While evaluating '$to_eval' got error: $@"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( $self->_args->{_bundle} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | my $bundleNo = 1; | |||||||||||||||||||||||||||
| 62 | 0 | foreach my $bundle ( $document->get_bundles() ) { | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | if ( !$self->select_bundles || $self->_is_bundle_selected->{$bundleNo} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}
MC/DC0%0/2
not $self->select_bundles or $self->_is_bundle_selected->{$bundleNo}
not $self->select_bundles
$self->_is_bundle_selected->{$bundleNo}
Truth table0%0/3
not(A) or B
A= not $self->select_bundlesB= $self->_is_bundle_selected->{$bundleNo}
| |||||||||||||||||||||||||||||
| 64 | 0 | $self->process_bundle($bundle, $bundleNo); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | $bundleNo++; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | 0 | return; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub process_bundle { | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | my ( $self, $bundle, $bundleNo ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_bundle: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($self->report_progress){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | log_info "Processing bundle $bundleNo"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Extract variables $document ($doc), so they can be used in eval code | ||||||||||||||||||||||||||||
| 79 | 0 | my $document = $bundle->get_document(); | |||||||||||||||||||||||||||
| 80 | 0 | my $doc = $document; | |||||||||||||||||||||||||||
| 81 | 0 | my $this = $bundle; | |||||||||||||||||||||||||||
| 82 | 0 | ▶ | if ( $self->bundle ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | if ( !eval $self->expand_eval_code($self->bundle) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | log_fatal "Eval error: $@"; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # quit if no parameters zone|?tree|?node | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return if !$self->_args->{_zone}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | foreach my $zone ( $self->get_selected_zones($bundle->get_all_zones()) ) { | |||||||||||||||||||||||||||
| 92 | 0 | $self->process_zone($zone, $bundleNo); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | return; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | sub process_zone { | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my ( $self, $zone, $bundleNo ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_zone: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Extract variables $bundle, $document ($doc), so they can be used in eval code | ||||||||||||||||||||||||||||
| 101 | 0 | my $bundle = $zone->get_bundle(); | |||||||||||||||||||||||||||
| 102 | 0 | my $document = $bundle->get_document(); | |||||||||||||||||||||||||||
| 103 | 0 | my $doc = $document; | |||||||||||||||||||||||||||
| 104 | 0 | my $this = $zone; | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ( $self->zone ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ( !eval $self->expand_eval_code($self->zone) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | log_fatal "Eval error: $@"; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | foreach my $layer (qw(a t n p)) { | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | next if !$zone->has_tree($layer); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | my $tree = $zone->get_tree($layer); | |||||||||||||||||||||||||||
| 114 | 0 | $this = $tree; | |||||||||||||||||||||||||||
| 115 | 0 | ▶ | if ( my $code = $self->_args->{"${layer}tree"} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( !eval $self->expand_eval_code("my \$${layer}tree = \$tree; $code") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | 0 | log_fatal "Eval error: $@"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ( my $code = $self->_args->{"${layer}node"} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | foreach my $node ( $tree->get_descendants({ordered => ($layer =~ /[at]/ ? 1 : 0)}) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | $this = $node; | |||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ( !eval $self->expand_eval_code("my \$${layer}node = \$node; $code;") ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | log_fatal "Eval error: $@"; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 0 | return; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 3 | 1; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =pod | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head1 NAME | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Treex::Block::Util::Eval - Special block for evaluating code given by parameters. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =head1 VERSION | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | version 2.20151210 | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # on the command line | ||||||||||||||||||||||||||||
| 151 | treex Util::Eval document='print $document->full_filename' -- *.treex | ||||||||||||||||||||||||||||
| 152 | treex Util::Eval language=en anode='print $anode->lemma."\n"' -- *.treex | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # The same two commands even shorter | ||||||||||||||||||||||||||||
| 155 | treex Util::Eval doc='print $.full_filename' -- *.treex | ||||||||||||||||||||||||||||
| 156 | treex -Len Util::Eval anode='say $.lemma' -- *.treex | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # other examples of parameters | ||||||||||||||||||||||||||||
| 159 | language=en,cs zone='say $.language, "\t", $.sentence' | ||||||||||||||||||||||||||||
| 160 | language=all ttree='say $.language, "\t", scalar $.get_children()' | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Evaluate an arbitrary Perl code for each document/bundle/zone/tree/node (according to which parameter | ||||||||||||||||||||||||||||
| 165 | is given). The corresponding object is accessible through a variable of the same name or C<$this>. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | More shortcuts: | ||||||||||||||||||||||||||||
| 168 | You can use doc= instead of document=. | ||||||||||||||||||||||||||||
| 169 | You can use "$." instead of "$this->" where $this is the current doc/bundle/zone/tree/node. | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Martin Popel <popel@ufal.mff.cuni.cz> | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | Copyright © 2011-2012 by Institute of Formal and Applied Linguistics, Charles University in Prague | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||