Block/W2A/Tag.pm

filter: Statementstmt 37.1% 26 / 70 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 50.0% 6 / 12 Podpod 25.0% 1 / 4 totaltotal 26.8% 33 / 123 CC 17 SCAR 47.3
CC
17
Cov
26%
process_atree
207.9
_build_tagger
12.0
BUILD
2.0
CRAP 113.2
« Block/W2A/ResegmentSentences.pm ↑ Summary Block/Read/CoNLLX.pm »
1 package Treex::Block::W2A::Tag;
2 1 $Treex::Block::W2A::Tag::VERSION = '2.20151102';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Treex::Core::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 extends 'Treex::Core::Block';
10  
11 1 has lemmatize => (
12 is => 'ro',
13 isa => 'Bool',
14 default => 0,
15 );
16  
17 1 has module => (
18 is => 'ro',
19 isa => 'Str',
20 lazy_build => 1,
21 );
22  
23 1 has tagger => (
24 is => 'rw',
25 does => 'Treex::Tool::Tagger::Role',
26 lazy_build => 1,
27 );
28  
29 1 has max_sentence_size => (
30 is => 'ro',
31 isa => 'Int',
32 default => 1000,
33 documentation => 'If a sentence contains more tokens, it is split and each chunk of max_sentence_size tokens is sent separately to the tagger. '
34 . 'This parameter serves as a safety check for extremely long sentences and taggers that may fail on such sentences. 0 means do not split.',
35 );
36  
37  
38 1 has _args => (is => 'rw');
39  
40 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
41 0 my ($self, $arg_ref) = @_;
42  
43 # We need to store parameters of this block and pass them to the tagger's constructor
44 0 $self->_set_args($arg_ref);
45 0 return;
46 }
47  
48 0 ▶sub _build_tagger {
Subroutine _build_tagger: not called CC=3 SCAR=24.8
49 0 my ($self) = @_;
50 0 my $module = $self->module;
51  
52 0 ▶ eval "use $module;1" or log_fatal "Can't use $module\n$@";
Branch: 1 branch
BranchTrueFalseTotal
eval "use $module;1" or log_fatal("Can't use $module\n$@") 0 0 0
53 0 ▶ my $tagger = eval "$module->new(\$self->_args);" or log_fatal "Can't load $module\n$@";
Branch: 1 branch
BranchTrueFalseTotal
my $tagger = eval "$module->new(\$self->_args);" or log_fatal("Can't load $module\n$@") 0 0 0
54 0 return $tagger;
55 }
56  
57 0 ▶sub _build_module {
Subroutine _build_module: not called CC=1 SCAR=6.9
58 0 log_fatal 'Parameter "module" is required when using W2A::Tag directly in scenario. E.g. W2A::Tag module=Treex::Tool::Tagger::Featurama::EN';
59 }
60  
61 0 ▶sub process_start {
Subroutine process_start: not called CC=1 SCAR=6.9
Pod documented
62 0 my ($self) = @_;
63  
64 # tagger is build lazily, but it can involve loading huge models,
65 # so it should be done now, instead of when processing the first document.
66 0 $self->tagger;
67 0 return;
68 }
69  
70 0 ▶sub process_atree {
Subroutine process_atree: not called CC=15 SCAR=53.4
Pod undocumented
71 0 my ( $self, $atree ) = @_;
72 0 my @nodes = $atree->get_descendants({ordered=>1});
73  
74 # It is legal to have a tree with no nodes (e.g. for non 1-1 aligned sentences),
75 # so just skip such sentences (and do not log_fatal if no lemmas are found).
76 0 ▶ return if !@nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @nodes 0 0 0
77  
78 0 my $forms_rf = [map { $_->form } @nodes];
79 0 $self->normalize($forms_rf);
80  
81 # Run the tagger with a safety check for extremely long sentences.
82 0 my ( $tags_rf, $lemmas_rf ) = ([], []);
83 0 ▶ if ( $self->max_sentence_size && @nodes > $self->max_sentence_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->max_sentence_size and @nodes > $self->max_sentence_size) { } 0 0 0
Condition0%0/3
$self->max_sentence_size and @nodes > $self->max_sentence_size
!ll&&!rl&&r
000
MC/DC0%0/2
$self->max_sentence_size and @nodes > $self->max_sentence_size
$self->max_sentence_size @nodes > $self->max_sentence_size
Truth table0%0/3
A and B
A= $self->max_sentence_sizeB= @nodes > $self->max_sentence_size
ABresult
0X0
100
111
84 0 my $sentence_size = @nodes;
85 0 log_info("Sentence contains $sentence_size tokens, applying the tagger per partes.");
86 0 my (@tags, @lemmas);
87 1 ▶ use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
88 0 my $iterator = natatime($self->max_sentence_size, @$forms_rf);
89 0 ▶ while (my @forms_part = $iterator->()){
Branch: 1 branch
BranchTrueFalseTotal
if (my(@forms_part) = &$iterator()) 0 0 0
90 0 my ( $tags_part_rf, $lemmas_part_rf ) = $self->tagger->tag_sentence(\@forms_part);
91 0 push @$tags_rf, @$tags_part_rf;
92 0 ▶ push @$lemmas_rf, @$lemmas_part_rf if $lemmas_part_rf;
Branch: 1 branch
BranchTrueFalseTotal
if $lemmas_part_rf 0 0 0
93 }
94 }
95 else {
96 0 ( $tags_rf, $lemmas_rf ) = $self->tagger->tag_sentence($forms_rf);
97 }
98  
99 # Check and fill tags
100 0 ▶ log_fatal "Different number of tokens and tags. TOKENS: @$forms_rf, TAGS: @$tags_rf" if @$tags_rf != @nodes;
Branch: 1 branch
BranchTrueFalseTotal
if @$tags_rf != @nodes 0 0 0
101 0 foreach my $a_node (@nodes) {
102 0 $a_node->set_tag( shift @$tags_rf );
103 }
104  
105 # If the tagger was supposed to lemmatize, check and fill lemmas
106 0 ▶ if ($self->lemmatize){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->lemmatize) 0 0 0
107 0 ▶ log_fatal "Tagger did not return any lemmas" if !$lemmas_rf || !@$lemmas_rf;
Branch: 1 branch
BranchTrueFalseTotal
unless $lemmas_rf and @$lemmas_rf 0 0 0
Condition0%0/3
$lemmas_rf and @$lemmas_rf
!ll&&!rl&&r
000
MC/DC0%0/2
$lemmas_rf and @$lemmas_rf
$lemmas_rf @$lemmas_rf
Truth table0%0/3
A and B
A= $lemmas_rfB= @$lemmas_rf
ABresult
0X0
100
111
108 0 ▶ log_fatal "Different number of tokens and lemmas. TOKENS: @$forms_rf, LEMMAS: @$lemmas_rf" if @$lemmas_rf != @nodes;
Branch: 1 branch
BranchTrueFalseTotal
if @$lemmas_rf != @nodes 0 0 0
109 0 foreach my $a_node (@nodes) {
110 0 my $lemma = shift @$lemmas_rf;
111 0 ▶ if (!defined $lemma || $lemma eq ''){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lemma or $lemma eq "") 0 0 0
Condition0%0/3
not defined $lemma or $lemma eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $lemma or $lemma eq ""
not defined $lemma $lemma eq ""
Truth table0%0/3
not(A) or B
A= not defined $lemmaB= $lemma eq ""
ABresult
1X1
011
000
112 0 log_warn sprintf 'Tagger %s produced an empty lemma for form "%s". Using lc form. %s', ref $self, $a_node->form, $a_node->get_address;
113 0 $lemma = lc $a_node->form;
114 }
115 0 $a_node->set_lemma($lemma);
116 }
117 }
118  
119 0 return;
120 }
121  
122 0 ▶sub normalize {
Subroutine normalize: not called CC=1 SCAR=6.9
Pod undocumented
123 0 my ($self, $forms_rf) = @_;
124  
125 # Derived classes may override this method and do some normalization.
126 0 return;
127 }
128  
129 1 1;
130  
131 __END__
« Block/W2A/ResegmentSentences.pm ↑ Summary Block/Read/CoNLLX.pm »