| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Treex::Block::Util::Eval; |
|
2
|
|
|
|
|
|
|
$Treex::Block::Util::Eval::VERSION = '2.20210102'; |
|
3
|
1
|
|
|
1
|
|
8
|
use Moose; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
9
|
|
|
4
|
1
|
|
|
1
|
|
7415
|
use Treex::Core::Common; |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
|
|
extends 'Treex::Core::Block'; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has [ |
|
8
|
|
|
|
|
|
|
qw( document bundle zone |
|
9
|
|
|
|
|
|
|
atree ttree ntree ptree |
|
10
|
|
|
|
|
|
|
anode tnode nnode pnode |
|
11
|
|
|
|
|
|
|
_args) |
|
12
|
|
|
|
|
|
|
] |
|
13
|
|
|
|
|
|
|
=> ( is => 'rw' ); |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
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
|
|
|
2
|
0
|
26
|
my ( $self, $arg_ref ) = @_; |
|
24
|
2
|
|
|
4
|
|
26
|
$arg_ref->{_atree} = any { $arg_ref->{$_} } qw(atree anode); |
|
|
4
|
|
|
|
|
12
|
|
|
25
|
2
|
|
|
4
|
|
15
|
$arg_ref->{_ttree} = any { $arg_ref->{$_} } qw(ttree tnode); |
|
|
4
|
|
|
|
|
12
|
|
|
26
|
2
|
|
|
4
|
|
15
|
$arg_ref->{_ntree} = any { $arg_ref->{$_} } qw(ntree nnode); |
|
|
4
|
|
|
|
|
11
|
|
|
27
|
2
|
|
|
4
|
|
14
|
$arg_ref->{_ptree} = any { $arg_ref->{$_} } qw(ptree pnode); |
|
|
4
|
|
|
|
|
11
|
|
|
28
|
2
|
|
|
10
|
|
13
|
$arg_ref->{_zone} = any { $arg_ref->{$_} } qw(zone _atree _ttree _ntree _ptree); |
|
|
10
|
|
|
|
|
23
|
|
|
29
|
2
|
|
|
4
|
|
10
|
$arg_ref->{_bundle} = any { $arg_ref->{$_} } qw(bundle _zone); |
|
|
4
|
|
|
|
|
13
|
|
|
30
|
|
|
|
|
|
|
|
|
31
|
2
|
50
|
|
4
|
|
15
|
if ( !any { $arg_ref->{$_} } qw(doc document _bundle) ) { |
|
|
4
|
|
|
|
|
11
|
|
|
32
|
0
|
|
|
|
|
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
|
50
|
|
|
|
13
|
if ($arg_ref->{doc}) { |
|
36
|
0
|
|
|
|
|
0
|
$self->set_document($arg_ref->{doc}); |
|
37
|
|
|
|
|
|
|
} |
|
38
|
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
74
|
$self->_set_args($arg_ref); |
|
40
|
2
|
|
|
|
|
6
|
return; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub expand_eval_code { |
|
44
|
0
|
|
|
0
|
0
|
|
my ($self, $to_eval) = @_; |
|
45
|
0
|
0
|
|
|
|
|
return "$to_eval;1;" if !$self->expand_code; |
|
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
|
|
|
0
|
1
|
|
my ( $self, $document ) = @_; |
|
53
|
0
|
|
|
|
|
|
my $doc = $document; |
|
54
|
0
|
|
|
|
|
|
my $this = $document; |
|
55
|
0
|
0
|
|
|
|
|
if ( $self->document ) { |
|
56
|
0
|
|
|
|
|
|
my $to_eval = $self->expand_eval_code($self->document); |
|
57
|
0
|
0
|
|
|
|
|
eval($to_eval) or log_fatal("While evaluating '$to_eval' got error: $@"); |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
0
|
0
|
|
|
|
|
if ( $self->_args->{_bundle} ) { |
|
61
|
0
|
|
|
|
|
|
my $bundleNo = 1; |
|
62
|
0
|
|
|
|
|
|
foreach my $bundle ( $document->get_bundles() ) { |
|
63
|
0
|
0
|
0
|
|
|
|
if ( !$self->select_bundles || $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
|
|
|
0
|
1
|
|
my ( $self, $bundle, $bundleNo ) = @_; |
|
74
|
0
|
0
|
|
|
|
|
if ($self->report_progress){ |
|
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
|
0
|
|
|
|
|
if ( $self->bundle ) { |
|
83
|
0
|
0
|
|
|
|
|
if ( !eval $self->expand_eval_code($self->bundle) ) { |
|
84
|
0
|
|
|
|
|
|
log_fatal "Eval error: $@"; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# quit if no parameters zone|?tree|?node |
|
89
|
0
|
0
|
|
|
|
|
return if !$self->_args->{_zone}; |
|
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
|
|
|
0
|
1
|
|
my ( $self, $zone, $bundleNo ) = @_; |
|
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
|
0
|
|
|
|
|
if ( $self->zone ) { |
|
106
|
0
|
0
|
|
|
|
|
if ( !eval $self->expand_eval_code($self->zone) ) { |
|
107
|
0
|
|
|
|
|
|
log_fatal "Eval error: $@"; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
} |
|
110
|
|
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
foreach my $layer (qw(a t n p)) { |
|
112
|
0
|
0
|
|
|
|
|
next if !$zone->has_tree($layer); |
|
113
|
0
|
|
|
|
|
|
my $tree = $zone->get_tree($layer); |
|
114
|
0
|
|
|
|
|
|
$this = $tree; |
|
115
|
0
|
0
|
|
|
|
|
if ( my $code = $self->_args->{"${layer}tree"} ) { |
|
116
|
0
|
0
|
|
|
|
|
if ( !eval $self->expand_eval_code("my \$${layer}tree = \$tree; $code") ) { |
|
117
|
0
|
|
|
|
|
|
log_fatal "Eval error: $@"; |
|
118
|
|
|
|
|
|
|
} |
|
119
|
|
|
|
|
|
|
} |
|
120
|
0
|
0
|
|
|
|
|
if ( my $code = $self->_args->{"${layer}node"} ) { |
|
121
|
0
|
0
|
|
|
|
|
foreach my $node ( $tree->get_descendants({ordered => ($layer =~ /[at]/ ? 1 : 0)}) ) { |
|
122
|
0
|
|
|
|
|
|
$this = $node; |
|
123
|
0
|
0
|
|
|
|
|
if ( !eval $self->expand_eval_code("my \$${layer}node = \$node; $code;") ) { |
|
124
|
0
|
|
|
|
|
|
log_fatal "Eval error: $@"; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} |
|
129
|
0
|
|
|
|
|
|
return; |
|
130
|
|
|
|
|
|
|
} |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
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.20210102 |
|
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. |