| 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
| |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my $tagger = eval "$module->new(\$self->_args);" or log_fatal "Can't load $module\n$@"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->max_sentence_size and @nodes > $self->max_sentence_size
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | 0 | ▶ | log_fatal "Tagger did not return any lemmas" if !$lemmas_rf || !@$lemmas_rf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$lemmas_rf and @$lemmas_rf
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
| |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | log_fatal "Different number of tokens and lemmas. TOKENS: @$forms_rf, LEMMAS: @$lemmas_rf" if @$lemmas_rf != @nodes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | foreach my $a_node (@nodes) { | |||||||||||||||||||||||||||
| 110 | 0 | my $lemma = shift @$lemmas_rf; | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | if (!defined $lemma || $lemma eq ''){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $lemma or $lemma eq ""
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 ""
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||