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