Block/W2A/EN/FixTagsAfterParse.pm

filter: Statementstmt 22.4% 20 / 89 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 33.3% 5 / 15 Podpod 25.0% 2 / 8 totaltotal 14.3% 27 / 188 CC 29 SCAR 48.5
CC
29
Cov
13%
get_fixed_tag
306.0
process_start
20.0
get_pseudo_eparent
20.0
CRAP 127.5
« Block/W2A/EN/FixTags.pm ↑ Summary Block/W2A/EN/FixTokenization.pm »
1 package Treex::Block::W2A::EN::FixTagsAfterParse;
2 1 $Treex::Block::W2A::EN::FixTagsAfterParse::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::Resource qw(require_file_from_share);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 extends 'Treex::Core::Block';
9  
10 1 Readonly my $TAGS_FILE => 'data/models/morpho_analysis/en/forms_with_more_tags.tsv';
11  
12 0 ▶sub get_required_share_files { return $TAGS_FILE; }
Subroutine get_required_share_files: not called CC=1 SCAR=6.9
Pod documented
13  
14 # $CAN_BE{$tag}{$form} == 1 means that $form can have tag $tag
15 # Only forms with more possible tags are stored in this hash.
16 1 my %CAN_BE;
17  
18 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
19 0 my $self = shift;
20  
21 0 return;
22 }
23  
24 0 ▶sub process_start {
Subroutine process_start: not called CC=4 SCAR=30.0
Pod documented
25 0 my $self = shift;
26 0 my $file_path = require_file_from_share($TAGS_FILE);
27 0 ▶ open my $IN, '<:encoding(utf8)', $file_path or log_fatal $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $IN, "<:encoding(utf8)", $file_path or log_fatal($!) 0 0 0
28 0 ▶ while (<$IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $IN)) 0 0 0
29 0 chomp;
30 0 my ( $form, @tags ) = split /\t/, $_;
31 0 foreach my $tag (@tags) {
32 0 $CAN_BE{$tag}{$form} = 1;
33 }
34 }
35 0 close $IN;
36  
37 0 $self->SUPER::process_start();
38  
39 0 return;
40 }
41  
42 # TODO: Should we change m-node's tag?
43 # In TectoMT there is no "knitting", so a-node's tag is only a copy of m-node's tag.
44 # There is a difference between:
45 # $a_node->set_tag($new_tag);
46 # $m_node->set_tag($new_tag);
47  
48 0 ▶sub process_atree {
Subroutine process_atree: not called CC=3 SCAR=24.8
Pod undocumented
49 0 my ( $self, $a_root ) = @_;
50  
51 0 foreach my $a_node ( $a_root->get_descendants() ) {
52 0 my ( $new_tag, $reparse ) = get_fixed_tag($a_node);
53 0 ▶ if ($new_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_tag) 0 0 0
54 0 $a_node->set_tag($new_tag);
55 0 $a_root->set_attr( 'reparse', 1 );
56 }
57 }
58  
59 0 return 1;
60 }
61  
62 0 ▶sub get_fixed_tag {
Subroutine get_fixed_tag: not called CC=17 SCAR=57.2
Pod undocumented
63 0 my ($node) = @_;
64 0 my $tag = $node->tag;
65 0 my $form = lc $node->form;
66 0 my $parent = $node->get_parent();
67 0 my $eparent = get_pseudo_eparent($node);
68 0 my $ord = $node->ord;
69 0 my $follows_parent = $parent->ord + 1 == $ord;
70  
71 # "minus" as a noun
72 # Mathematical operators (plus, minus, times, less, over) can be tagged
73 # as CC (in PTB), but only if there is a real coordination.
74 # So e.g. there is no coordination in "It falls to minus."
75 0 ▶ if ( $form =~ /^(plus|minus|times)$/ && $tag eq 'CC' && !$node->get_children() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form =~ /^(plus|minus|times)$/ and $tag eq "CC" and not $node->get_children) 0 0 0
Condition0%0/6
$form =~ /^(plus|minus|times)$/ and $tag eq "CC" and not $node->get_children
!ll&&!rl&&r
000
$form =~ /^(plus|minus|times)$/ and $tag eq "CC"
!ll&&!rl&&r
000
MC/DC0%0/3
$form =~ /^(plus|minus|times)$/ and $tag eq "CC" and not $node->get_children
$form =~ /^(plus|minus|times)$/ $tag eq "CC" not $node->get_children
Truth table0%0/4
A and B and not(C)
A= $form =~ /^(plus|minus|times)$/B= $tag eq "CC"C= not $node->get_children
ABCresult
0XX0
10X0
1100
1111
76 0 ▶ return $form eq 'times' ? 'NNS' : 'NN';
Branch: 1 branch
BranchTrueFalseTotal
$form eq 'times' ? : 0 0 0
77 }
78  
79 # Some phrasal verb particles (RP) are incorrectly tagged as RB
80 0 ▶ if ( $form eq 'up' && $parent->lemma eq 'shoot' && $follows_parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($form eq "up" and $parent->lemma eq "shoot" and $follows_parent) 0 0 0
Condition0%0/6
$form eq "up" and $parent->lemma eq "shoot" and $follows_parent
!ll&&!rl&&r
000
$form eq "up" and $parent->lemma eq "shoot"
!ll&&!rl&&r
000
MC/DC0%0/3
$form eq "up" and $parent->lemma eq "shoot" and $follows_parent
$form eq "up" $parent->lemma eq "shoot" $follows_parent
Truth table0%0/4
A and B and C
A= $form eq "up"B= $parent->lemma eq "shoot"C= $follows_parent
ABCresult
0XX0
10X0
1100
1111
81 0 return 'RP';
82 }
83  
84 # Clause heads are more likely to be verbs than nouns.
85 # (It holds for all clauses, but here we can recognize only main clauses.)
86 # E.g. word "cost" can be NN, VB, VBP, VBN or VBD, but if it was VB, VBP or VBN,
87 # we hope the tagger would guess it correctly. So let's say it's VBD.
88 0 ▶ if ( $eparent->is_root() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eparent->is_root) 0 0 0
89 0 ▶ return 'VBD' if $tag eq 'NN' && $CAN_BE{VBD}{$form};
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "NN" and $CAN_BE{"VBD"}{$form} 0 0 0
Condition0%0/3
$tag eq "NN" and $CAN_BE{"VBD"}{$form}
!ll&&!rl&&r
000
MC/DC0%0/2
$tag eq "NN" and $CAN_BE{"VBD"}{$form}
$tag eq "NN" $CAN_BE{"VBD"}{$form}
Truth table0%0/3
A and B
A= $tag eq "NN"B= $CAN_BE{"VBD"}{$form}
ABresult
0X0
100
111
90 0 ▶ return 'VBZ' if $tag eq 'NNS' && $CAN_BE{VBZ}{$form};
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "NNS" and $CAN_BE{"VBZ"}{$form} 0 0 0
Condition0%0/3
$tag eq "NNS" and $CAN_BE{"VBZ"}{$form}
!ll&&!rl&&r
000
MC/DC0%0/2
$tag eq "NNS" and $CAN_BE{"VBZ"}{$form}
$tag eq "NNS" $CAN_BE{"VBZ"}{$form}
Truth table0%0/3
A and B
A= $tag eq "NNS"B= $CAN_BE{"VBZ"}{$form}
ABresult
0X0
100
111
91 0 return;
92 }
93  
94 0 my $ep_tag = $eparent->tag;
95 0 my $ep_lemma = $eparent->lemma;
96  
97 # Every modal verb should govern its main verb.
98 # Sometimes is the main verb wrongly tagged as NN.
99 0 ▶ if ( $tag =~ /^NN/ && $ep_tag eq 'MD' && $CAN_BE{VB}{$form} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /^NN/ and $ep_tag eq "MD" and $CAN_BE{"VB"}{$form}) 0 0 0
Condition0%0/6
$tag =~ /^NN/ and $ep_tag eq "MD" and $CAN_BE{"VB"}{$form}
!ll&&!rl&&r
000
$tag =~ /^NN/ and $ep_tag eq "MD"
!ll&&!rl&&r
000
MC/DC0%0/3
$tag =~ /^NN/ and $ep_tag eq "MD" and $CAN_BE{"VB"}{$form}
$tag =~ /^NN/ $ep_tag eq "MD" $CAN_BE{"VB"}{$form}
Truth table0%0/4
A and B and C
A= $tag =~ /^NN/B= $ep_tag eq "MD"C= $CAN_BE{"VB"}{$form}
ABCresult
0XX0
10X0
1100
1111
100 0 my @siblings = get_pseudo_echildren($eparent);
101 0 ▶ return 'VB' if !any { $_->tag =~ /^V/ } @siblings;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &any(sub { $_->tag =~ /^V/; } , @siblings) 0 0 0
102 }
103  
104 #return if can_be_child_and_parent($node, $eparent);
105 #return 'NN' if $tag eq 'JJ' && $POSSIBLE_TAG_FORM{NN}{$form};
106  
107 0 return;
108 }
109  
110 0 ▶sub can_be_child_and_parent {
Subroutine can_be_child_and_parent: not called CC=3 SCAR=24.8
Pod undocumented
111 0 my ( $child, $eparent ) = @_;
112 0 my $c_tag = $child->tag;
113 0 my $p_tag = $eparent->tag;
114 0 my $p_lemma = $eparent->lemma;
115  
116 # Every modal verb should govern its main verb.
117 # Sometimes is the main verb wrongly tagged as NN.
118 0 ▶ if ( $c_tag =~ /^NN/ && $p_tag eq 'MD' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c_tag =~ /^NN/ and $p_tag eq "MD") 0 0 0
Condition0%0/3
$c_tag =~ /^NN/ and $p_tag eq "MD"
!ll&&!rl&&r
000
MC/DC0%0/2
$c_tag =~ /^NN/ and $p_tag eq "MD"
$c_tag =~ /^NN/ $p_tag eq "MD"
Truth table0%0/3
A and B
A= $c_tag =~ /^NN/B= $p_tag eq "MD"
ABresult
0X0
100
111
119 0 ▶ return any { $_->tag =~ /^V/ } get_pseudo_echildren($eparent);
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 }
121 0 return 1;
122 =item
123 if ( $c_tag eq 'JJ' ) {
124 return 1 if $p_tag =~ /^(N|CD|IN)/;
125 return 1 if $p_lemma eq 'be' && $p_tag =~ /^V/;
126 return 0;
127 }
128  
129 #return 0 if $c_tag eq 'NN' && $p_tag eq 'MD';
130 return 1;
131 =cut
132 }
133  
134 # Next 2 methods need no afun filled (unlike $node->get_echildren())
135 0 ▶sub get_pseudo_eparent {
Subroutine get_pseudo_eparent: not called CC=4 SCAR=30.0
Pod undocumented
136 0 my ($node) = @_;
137 0 my $parent = $node->get_parent();
138 0 ▶ return $parent if $parent->is_root();
Branch: 1 branch
BranchTrueFalseTotal
if $parent->is_root 0 0 0
139 0 ▶ while ( $parent->tag eq 'CC' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->tag eq "CC") 0 0 0
140 0 $parent = $parent->get_parent();
141 0 ▶ return $parent if $parent->is_root();
Branch: 1 branch
BranchTrueFalseTotal
if $parent->is_root 0 0 0
142 }
143 0 return $parent;
144 }
145  
146 0 ▶sub get_pseudo_echildren {
Subroutine get_pseudo_echildren: not called CC=3 SCAR=24.8
Pod undocumented
147 0 my ($node) = @_;
148 0 my @children = $node->get_children();
149 0 my @eff_children = ();
150 0 ▶ while (@children) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children) 0 0 0
151 0 my $child = shift @children;
152 0 my @grandchildren = grep { $_->is_member } $child->get_children();
153 0 ▶ if (@grandchildren) { push @children, @grandchildren; }
Branch: 1 branch
BranchTrueFalseTotal
if (@grandchildren) { } 0 0 0
154 0 else { push @eff_children, $child; }
155 }
156 0 return @eff_children;
157 }
158  
159 1 1;
160  
161 =over
162  
163 =item Treex::Block::W2A::EN::FixTagsAfterParse
164  
165 Some errors made by taggers can be detected after the parsing is done.
166  
167 =back
168  
169 =cut
170  
171 # Copyright 2009 Martin Popel
172 # This file is distributed under the GNU General Public License v2. See $TMT_ROOT/README.
« Block/W2A/EN/FixTags.pm ↑ Summary Block/W2A/EN/FixTokenization.pm »