Block/W2A/EN/TagLinguaEn.pm

filter: Statementstmt 96.7% 60 / 62 Branchbran 69.2% 18 / 26 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 87.5% 91 / 104 CC 16 SCAR 17.5
CC
16
Cov
88%
process_zone
8.3
_correct_lingua_tag
8.3
_revert_form
2.0
CRAP 5.7
« Tool/EnglishMorpho/exceptions/verbs_ending_with_e.pl ↑ Summary Tool/EnglishMorpho/exceptions/nouns_plural_s.pl »
1 package Treex::Block::W2A::EN::TagLinguaEn;
2 2 $Treex::Block::W2A::EN::TagLinguaEn::VERSION = '2.20151102';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 extends 'Treex::Core::Block';
8 2 ▶use Lingua::EN::Tagger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 has _tagger => (
10 is => 'ro',
11 isa => 'Lingua::EN::Tagger',
12 builder => '_build_tagger',
13 lazy => 1,
14 init_arg => undef,
15 predicate => '_tagger_built',
16 documentation => q{Tagger object},
17 );
18  
19 has _form_corrections => (
20 is => 'ro',
21 isa => 'HashRef[Str]',
22 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 {
24 1 q(``) => q("),
25 q('') => q("),
26 }
27 },
28 2 documentation => q{Possible changes in forms done by tagger},
29 );
30  
31 1 ▶sub _build_tagger {
Subroutine _build_tagger: called CC=1 SCAR=0.0
32 1 my $self = shift;
33 1 my $tagger = Lingua::EN::Tagger->new();
34 1 return $tagger;
35 }
36  
37 48 ▶sub _revert_form { #because Lingua::EN::Tagger changes some forms to another, we need to restore original
Subroutine _revert_form: called CC=2 SCAR=7.0
38 48 my $self = shift;
39 48 my %args = @_;
40 48 my $new = $args{new};
41 48 ▶ return $self->_form_corrections->{$new} // $new;
Condition66%2/3
$self->_form_corrections->{$new} // $new
l!l&&r!l&&!r
4440
MC/DC0%0/2
$self->_form_corrections->{$new} // $new
$self->_form_corrections->{$new} $new
Truth table66%2/3
A // B
A= $self->_form_corrections->{$new}B= $new
ABresult
1X1
011
000
42 }
43  
44  
45 # Substitutions according to http://search.cpan.org/src/ACOBURN/Lingua-EN-Tagger-0.13/README
46 # The PennTB tagset is described e.g. at http://www.computing.dcu.ie/~acahill/tagset.html
47 48 ▶sub _correct_lingua_tag {
Subroutine _correct_lingua_tag: called CC=8 SCAR=21.1
48 48 my ( $self, $linguatag, $wordform ) = @_;
49 48 $linguatag = uc $linguatag; # newer versions of Lingua::EN::Tagger use lowercased tags
50 48 ▶ return 'DT' if $linguatag eq 'DET';
Branch: 1 branch
BranchTrueFalseTotal
if $linguatag eq "DET" 4 44 48
51 44 ▶ return 'PRP$' if $linguatag eq 'PRPS';
Branch: 1 branch
BranchTrueFalseTotal
if $linguatag eq "PRPS" 0 44 44
52 44 ▶ return "-$linguatag-" if $linguatag =~ /^[LR]RB$/;
Branch: 1 branch
BranchTrueFalseTotal
if $linguatag =~ /^[LR]RB$/ 0 44 44
53 44 ▶ if ($linguatag =~ /^PP/) { # allowed "tags" of punctuation mark in PennTB: # $ '' ( ) , . : ``
Branch: 1 branch
BranchTrueFalseTotal
if ($linguatag =~ /^PP/) 9 35 44
54 9 ▶ return $wordform if $wordform =~ /^(?:#|$|''|,|\.|:|``)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $wordform =~ /^(?:#|$|''|,|\.|:|``)$/ 4 5 9
55 5 ▶ return '-LRB-' if $wordform =~ /[\(\[\{]/;
Branch: 1 branch
BranchTrueFalseTotal
if $wordform =~ /[\(\[\{]/ 0 5 5
56 5 ▶ return '-RRB-' if $wordform =~ /[\)\]\}]/;
Branch: 1 branch
BranchTrueFalseTotal
if $wordform =~ /[\)\]\}]/ 0 5 5
57 5 return '.';
58 }
59 35 return $linguatag;
60 }
61  
62 2 ▶sub process_zone {
Subroutine process_zone: called CC=8 SCAR=21.2
Pod documented
63 2 my ( $self, $zone ) = @_;
64  
65 # get the source sentence
66 2 my $sentence = $zone->sentence;
67 2 ▶ log_fatal("No sentence in zone") if !defined $sentence;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sentence 0 2 2
68 2 ▶ log_fatal(qq{There's already atree in zone}) if $zone->has_atree();
Branch: 1 branch
BranchTrueFalseTotal
if $zone->has_atree 0 2 2
69 2 log_debug("Processing sentence: $sentence");
70  
71 #split on whitespace, tags nor tokens doesn't contain spaces
72 2 my @tagged = split /\s+/, $self->_tagger->add_tags($sentence);
73  
74 # create a-tree
75 2 my $a_root = $zone->create_atree();
76 2 my $tag_regex = qr{
77 <(\w+)> #<tag>
78 ([^<]+) #form
79 </\1> #</tag>
80 }x;
81 2 my $space_start = qr{^\s+};
82 2 my $ord = 1;
83 2 foreach my $tag_pair (@tagged) {
84 48 ▶ if ( $tag_pair =~ $tag_regex ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag_pair =~ /$tag_regex/) { } 48 0 48
85 48 my $form = $self->_revert_form( new => $2 );
86 48 my $tag = $self->_correct_lingua_tag( $1, $form );
87 48 ▶ if ( $sentence =~ s/^\Q$form\E// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sentence =~ s/^\Q$form\E//) { } 48 0 48
88  
89 #check if there is space after word
90 48 ▶ my $no_space_after = $sentence =~ m/$space_start/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$sentence =~ /$space_start/ ? : 38 10 48
91 48 ▶ if ( $sentence eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sentence eq "") 1 47 48
92 1 $no_space_after = 0;
93 }
94  
95 #delete it
96 48 $sentence =~ s{$space_start}{};
97  
98 # and create node under root
99 48 $a_root->create_child(
100 form => $form,
101 tag => $tag,
102 no_space_after => $no_space_after,
103 ord => $ord++,
104 );
105 }
106 else {
107 0 log_fatal("Mismatch between tagged word and original sentence: Tagged: $form. Original: $sentence.");
108 }
109 }
110 else {
111 0 log_fatal("Incorrect output format from Lingua::EN::Tagger: $tag_pair");
112 }
113  
114 }
115 2 return 1;
116 }
117  
118 2 1;
119  
120 __END__
« Tool/EnglishMorpho/exceptions/verbs_ending_with_e.pl ↑ Summary Tool/EnglishMorpho/exceptions/nouns_plural_s.pl »