Treex/Block/Util/Find.pm

filter: Statementstmt 27.9% 12 / 43 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 2 / 4 Podpod 100% 1 / 1 totaltotal 20.0% 15 / 75 CC 15 SCAR 46.9
CC
15
Cov
18%
process_zone
156.0
_check_errors
20.0
BEGIN
1.0
CRAP 108.6
« Treex/Block/Util/FixInvalidIDs.pm ↑ Summary Treex/Core/Cloud.pm »
1 package Treex::Block::Util::Find;
2 1 $Treex::Block::Util::Find::VERSION = '2.20151210';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 extends 'Treex::Block::Util::Eval';
6  
7 1 has on_error => (
8 is => 'ro',
9 isa => enum( [qw(fatal warn ignore)] ),
10 default => 'fatal',
11 documentation => 'what to do on errors in the code for eval',
12 );
13  
14 1 has expand_code => (
15 is=> 'ro',
16 isa => 'Bool',
17 default => 1,
18 documentation => 'Should "$." be expanded to "$this->" in all eval codes?',
19 );
20  
21 1 has max_nodes_per_tree => (
22 is=> 'ro',
23 isa => 'Int',
24 default => 0,
25 documentation => 'Print at most N trees for each tree. Default 0 means unlimited.',
26 );
27  
28  
29 ## no critic (ProhibitStringyEval) This block needs string evals
30 sub process_zone {
31 0 ▶ my ( $self, $zone ) = @_;
Subroutine process_zone: not called CC=12 SCAR=50.5
Pod documented
32  
33 # Extract variables $bundle, $document ($doc), so they can be used in eval code
34 0 my $bundle = $zone->get_bundle();
35 0 my $document = $bundle->get_document();
36 0 my $doc = $document;
37  
38 0 foreach my $layer (qw(a t n p)) {
39 0 ▶ next if !$zone->has_tree($layer);
Branch: 1 branch
BranchTrueFalseTotal
unless $zone->has_tree($layer) 0 0 0
40 0 my $tree = $zone->get_tree($layer);
41 0 my $this = $tree;
42 0 ▶ if ( my $code = $self->_args->{"${layer}tree"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->_args->{"${layer}tree"}) 0 0 0
43 0 ▶ $code =~ s/\$\./\$this->/g if $self->expand_code;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expand_code 0 0 0
44 0 ▶ if ( eval "my \$${layer}tree = \$tree; $code" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "my \$${layer}tree = \$tree; $code") 0 0 0
45 0 say $tree->get_address();
46 }
47 0 $self->_check_errors($code);
48 }
49 0 ▶ if ( my $code = $self->_args->{"${layer}node"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->_args->{"${layer}node"}) 0 0 0
50 0 my $nodes_found = 0;
51 0 ▶ $code =~ s/\$\./\$this->/g if $self->expand_code;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expand_code 0 0 0
52 NODE:
53 0 foreach my $node ( $tree->get_descendants() ) {
54 0 $this = $node;
55 0 ▶ if ( eval "my \$${layer}node = \$node; $code" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval "my \$${layer}node = \$node; $code") 0 0 0
56 0 say $node->get_address();
57 0 ▶ if ($self->max_nodes_per_tree){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_nodes_per_tree) 0 0 0
58 0 $nodes_found++;
59 0 ▶ last NODE if $nodes_found >= $self->max_nodes_per_tree;
Branch: 1 branch
BranchTrueFalseTotal
if $nodes_found >= $self->max_nodes_per_tree 0 0 0
60 }
61 }
62 0 $self->_check_errors($code);
63 }
64 }
65 }
66 0 return;
67 }
68  
69 sub _check_errors {
70 0 ▶ my ($self, $code) = @_;
Subroutine _check_errors: not called CC=4 SCAR=30.0
71 0 ▶ return if !$@ || $self->on_error eq 'ignore';
Branch: 1 branch
BranchTrueFalseTotal
if not $@ or $self->on_error eq "ignore" 0 0 0
Condition0%0/3
not $@ or $self->on_error eq "ignore"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $@ or $self->on_error eq "ignore"
not $@ $self->on_error eq "ignore"
Truth table0%0/3
not(A) or B
A= not $@B= $self->on_error eq "ignore"
ABresult
1X1
011
000
72 0 my $msg = "While evaluating '$code' got error: $@";
73 0 ▶ log_fatal $msg if $self->on_error eq 'fatal';
Branch: 1 branch
BranchTrueFalseTotal
if $self->on_error eq "fatal" 0 0 0
74 0 log_warn $msg;
75 0 return;
76 }
77  
78 1 1;
79  
80 __END__
81  
82 =pod
83  
84 =encoding utf-8
85  
86 =head1 NAME
87  
88 Treex::Block::Util::Find - Finding nodes based on criteria specified by parameters
89  
90 =head1 VERSION
91  
92 version 2.20151210
93  
94 =head1 SYNOPSIS
95  
96 # on the command line
97 treex Util::Find anode='$anode->lemma eq "dog"' -- *.treex.gz
98 treex Util::Find language=en tnode='$tnode->gram_gender eq "fem"' -- *.treex.gz
99  
100 # The same two commands even shorter
101 treex Util::Find anode='$.lemma eq "dog"' -- *.treex.gz
102 treex -Len Util::Find tnode='$.gram_gender eq "fem"' -- *.treex.gz
103  
104 # View a-trees with at least one coordination in ttred
105 treex Util::Find anode='$.is_member' max_nodes_per_tree=1 -- *.treex.gz | ttred -l-
106  
107 =head1 DESCRIPTION
108  
109 The criteria specified in [atnp](node|tree) is an arbitrary Perl code.
110 If the code evaluates to a true value, the address of the node is printed
111 (in a format suitable for piping into C<ttred>).
112  
113 You can use "$." instead of "$this->" where $this is the current tree/node.
114  
115 =head1 AUTHOR
116  
117 Martin Popel <popel@ufal.mff.cuni.cz>
118  
119 =head1 COPYRIGHT AND LICENSE
120  
121 Copyright © 2012 by Institute of Formal and Applied Linguistics, Charles University in Prague
122  
123 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Block/Util/FixInvalidIDs.pm ↑ Summary Treex/Core/Cloud.pm »