Treex/Core/Node/P.pm

filter: Statementstmt 68.0% 98 / 144 Branchbran 47.6% 20 / 42 Conditioncond 57.1% 12 / 21 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 73.3% 11 / 15 Podpod 100% 9 / 9 totaltotal 62.6% 151 / 241 CC 34 SCAR 24.3
CC
34
Cov
63%
stringify_as_mrg
42.0
copy_ptree
20.0
stringify_as_text
12.0
CRAP 11.4
« Treex/Block/Read/BaseReader.pm ↑ Summary Treex/Core/Phrase/Term.pm »
1 package Treex::Core::Node::P;
2 36 $Treex::Core::Node::P::VERSION = '2.20210102';
3 36 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 36 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 36 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 36 extends 'Treex::Core::Node';
8  
9 # dirty: merging terminal and nonterminal nodes' attributes
10  
11 # common:
12 36 has [qw(is_head index coindex edgelabel)] => ( is => 'rw' );
13  
14 # terminal specific
15 36 has [qw(form lemma tag)] => ( is => 'rw' );
16  
17 # non-terminal specific
18 36 has [qw( phrase functions )] => ( is => 'rw' );
19  
20 136 ▶sub get_pml_type_name {
Subroutine get_pml_type_name: called CC=8 SCAR=22.3
Pod documented
21 136 my ($self) = @_;
22  
23 136 ▶ if ( $self->is_root() or $self->phrase or ($self->{'#name'} || '') eq 'nonterminal') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_root or $self->phrase or ($self->{'#name'} || '') eq 'nonterminal') { } 100 36 136
elsif ($self->tag or ($self->{'#name'} || '') eq 'terminal') { } 36 0 36
Condition87%7/8
$self->is_root or $self->phrase or ($self->{"#name"} || "") eq "nonterminal"
l!l&&r!l&&!r
693136
$self->is_root or $self->phrase
l!l&&r!l&&!r
244567
$self->{'#name'} || ''
l!l
670
MC/DC0%0/3
$self->is_root or $self->phrase or ($self->{"#name"} || "") eq "nonterminal"
$self->is_root $self->phrase $self->{'#name'}
Truth table75%3/4
A or B or C
A= $self->is_rootB= $self->phraseC= $self->{'#name'}
ABCresult
1XX1
01X1
0011
0000
24 100 return 'p-nonterminal.type';
25 }
26 ▶ elsif ( $self->tag or ($self->{'#name'} || '') eq 'terminal') {
Condition40%2/5
$self->tag or ($self->{"#name"} || "") eq "terminal"
l!l&&r!l&&!r
0360
$self->{'#name'} || ''
l!l
360
MC/DC0%0/2
$self->tag or ($self->{"#name"} || "") eq "terminal"
$self->tag $self->{'#name'}
Truth table33%1/3
A or B
A= $self->tagB= $self->{'#name'}
ABresult
1X1
011
000
27 36 return 'p-terminal.type';
28 }
29 else {
30 0 return;
31 }
32 }
33  
34 61 ▶sub is_terminal {
Subroutine is_terminal: called CC=2 SCAR=7.2
Pod documented
35 61 my $self = shift @_;
36 61 ▶ return $self->get_pml_type_name eq 'p-terminal.type' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_pml_type_name eq 'p-terminal.type' ? : 0 61 61
37 }
38  
39 0 ▶sub create_child {
Subroutine create_child: not called CC=1 SCAR=6.9
Pod documented
40 0 my $self = shift;
41 0 log_warn 'With Treex::Core::Node::P you should you either create_terminal_child() or create_nonterminal_child() instead of create_child()';
42 0 return $self->SUPER::create_child(@_);
43 }
44  
45 25 ▶sub create_nonterminal_child {
Subroutine create_nonterminal_child: called CC=5 SCAR=16.4
Pod documented
46 25 my $self = shift @_;
47 25 ▶ log_warn 'Adding a child to a terminal p-node ' . $self->id if $self->is_terminal();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 0 25 25
48 25 my $arg_ref;
49 25 ▶ if ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 1 24 25
elsif (@_ % 2) { } 0 24 24
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2401
MC/DC50%1/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
50 1 $arg_ref = $_[0];
51 }
52 elsif ( @_ % 2 ) {
53 0 log_fatal "Odd number of elements for create_nonterminal_child";
54 }
55 else {
56 24 $arg_ref = {@_};
57 }
58 25 $arg_ref->{'#name'} = 'nonterminal';
59 25 my $fs_file = $self->get_bundle->get_document()->_pmldoc;
60 25 my $child = $self->SUPER::create_child($arg_ref);
61 25 $child->set_type_by_name( $fs_file->metaData('schema'), 'p-nonterminal.type' );
62 25 return $child;
63 }
64  
65 36 ▶sub create_terminal_child {
Subroutine create_terminal_child: called CC=5 SCAR=17.8
Pod documented
66 36 my $self = shift @_;
67 36 ▶ log_warn 'Adding a child to a terminal p-node ' . $self->id if $self->is_terminal();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_terminal 0 36 36
68 36 my $arg_ref;
69 36 ▶ if ( scalar @_ == 1 && ref $_[0] eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @_ == 1 and ref $_[0] eq 'HASH') { } 0 36 36
elsif (@_ % 2) { } 0 36 36
Condition33%1/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
3600
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
70 0 $arg_ref = $_[0];
71 }
72 elsif ( @_ % 2 ) {
73 0 log_fatal "Odd number of elements for create_terminal_child";
74 }
75 else {
76 36 $arg_ref = {@_};
77 }
78 36 $arg_ref->{'#name'} = 'terminal';
79 36 my $fs_file = $self->get_bundle->get_document()->_pmldoc;
80 36 my $child = $self->SUPER::create_child($arg_ref);
81 36 $child->set_type_by_name( $fs_file->metaData('schema'), 'p-terminal.type' );
82 36 return $child;
83 }
84  
85 3 ▶sub create_from_mrg {
Subroutine create_from_mrg: called CC=3 SCAR=11.0
Pod documented
86 3 my ( $self, $mrg_string ) = @_;
87  
88 # normalize spaces
89 3 $mrg_string =~ s/([()])/ $1 /g;
90 3 $mrg_string =~ s/\s+/ /g;
91 3 $mrg_string =~ s/^ //g;
92 3 $mrg_string =~ s/ $//g;
93  
94 # remove back brackets (except for round)
95 3 $mrg_string =~ s/-LSB-/\[/g;
96 3 $mrg_string =~ s/-RSB-/\]/g;
97 3 $mrg_string =~ s/-LCB-/\{/g;
98 3 $mrg_string =~ s/-RCB-/\}/g;
99  
100 # mrg string should always start with "( ROOT (" as in the Stanford parser.
101 # The ROOT non-terminal has usually just one child "S",
102 # but sometimes (in parsing or in Brown treebank) it has more children,
103 # so the artificial root non-terminal "ROOT" makes sure it is formally a tree (not a forest).
104 # Charniak parser output starts with "( S1 (".
105 # PennTB trees start with "( (".
106 3 $mrg_string =~ s/^\( (S1 )?\(/( ROOT (/;
107
108 3 my @tokens = split / /, $mrg_string;
109  
110 3 $self->_parse_mrg_nonterminal( \@tokens );
111
112 # If there is just one child, we can remove the extra ROOT non-terminal.
113 3 ▶ if ($self->get_children == 1){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_children == 1) 2 1 3
114 2 my ($child) = $self->get_children();
115 2 foreach my $grandchild ($child->get_children) {
116 6 $grandchild->set_parent($self);
117 }
118 2 $self->set_phrase($child->phrase);
119 2 $self->set_functions($child->functions);
120 2 $child->remove();
121 }
122  
123 3 return;
124 }
125  
126 106 ▶sub _reduce {
Subroutine _reduce: called CC=2 SCAR=7.4
127 106 my ( $self, $tokens_rf, $expected_token ) = @_;
128 106 ▶ if ( $tokens_rf->[0] eq $expected_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens_rf->[0] eq $expected_token) { } 106 0 106
129 106 return shift @{$tokens_rf};
130 }
131 else {
132 0 log_fatal "Unparsable mrg remainder: '$expected_token' is not at the beginning of: "
133 . join( " ", @$tokens_rf );
134 }
135 }
136  
137 23 ▶sub _parse_mrg_nonterminal {
Subroutine _parse_mrg_nonterminal: called CC=5 SCAR=16.1
138 23 my ( $self, $tokens_rf ) = @_;
139 23 $self->_reduce( $tokens_rf, "(" );
140  
141 # phrase type and (optionally) a list of grammatical functions
142 23 my $label = shift @{$tokens_rf};
143 23 my @label_components = split /-/, $label;
144 23 $self->set_phrase( shift @label_components );
145  
146 # TODO: handle traces correctly
147 # Delete trace indices (e.g. NP-SBJ-10 ... -NONE- *T*-10)
148 # Delete suffixes in Brown data, e.g. :"SBJ=1" -> "SBJ", "LOC=2" -> "LOC"
149 23 @label_components = grep { !/^\d+$/ } @label_components;
150 23 foreach my $comp (@label_components) {
151 7 $comp =~ s/=\d+$//;
152 }
153  
154 23 ▶ if (@label_components) {
Branch: 1 branch
BranchTrueFalseTotal
if (@label_components) 7 16 23
155 7 $self->set_functions( \@label_components );
156 }
157  
158 23 ▶ while ( $tokens_rf->[0] eq "(" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens_rf->[0] eq "(") 50 23 73
159 50 ▶ if ( $tokens_rf->[2] eq "(" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens_rf->[2] eq '(') { } 20 30 50
160 20 my $new_nonterminal = $self->create_nonterminal_child();
161 20 $new_nonterminal->_parse_mrg_nonterminal($tokens_rf);
162 }
163 else {
164 30 my $new_terminal_child = $self->create_terminal_child();
165 30 $new_terminal_child->_parse_mrg_terminal($tokens_rf);
166 }
167 }
168  
169 23 $self->_reduce( $tokens_rf, ")" );
170 23 return;
171 }
172  
173 30 ▶sub _parse_mrg_terminal {
Subroutine _parse_mrg_terminal: called CC=1 SCAR=0.0
174 30 my ( $self, $tokens_rf ) = @_;
175 30 $self->_reduce( $tokens_rf, "(" );
176  
177 30 my $tag = shift @{$tokens_rf};
178 30 my $form = shift @{$tokens_rf};
179 30 $form =~ s/-LRB-/\(/g;
180 30 $form =~ s/-RRB-/\)/g;
181 30 $self->set_form($form);
182 30 $self->set_tag($tag);
183  
184 30 $self->_reduce( $tokens_rf, ")" );
185 30 return;
186 }
187  
188 0 ▶sub stringify_as_mrg {
Subroutine stringify_as_mrg: not called CC=6 SCAR=37.4
Pod documented
189 0 my ($self) = @_;
190 0 my $string;
191 0 ▶ if ( $self->phrase ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->phrase) { } 0 0 0
192 0 ▶ my @functions = $self->functions ? @{ $self->functions } : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->functions ? : 0 0 0
193 0 $string = join '-', $self->phrase, @functions;
194 0 $string .= ' ';
195 }
196 else {
197 0 ▶ my $tag = defined $self->tag ? $self->tag : '?';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->tag ? : 0 0 0
198 0 ▶ my $form = defined $self->form ? $self->form : '?';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->form ? : 0 0 0
199 0 $form =~ s/ /_/g;
200 0 $string = "$tag $form";
201 0 $string =~ s/\(/-LRB-/g;
202 0 $string =~ s/\)/-RRB-/g;
203 }
204 0 ▶ if ( $self->children ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->children) 0 0 0
205 0 $string .= join ' ', map { $_->stringify_as_mrg() } $self->children;
206 }
207 0 return "($string)";
208 }
209  
210 0 ▶sub stringify_as_text {
Subroutine stringify_as_text: not called CC=3 SCAR=24.8
Pod documented
211 0 my ($self) = @_;
212 0 my @children = $self->get_children();
213 0 ▶ return $self->form // '<?>' if !@children;
Branch: 1 branch
BranchTrueFalseTotal
unless @children 0 0 0
Condition0%0/2
$self->form // "<?>"
l!l
00
MC/DC0%0/1
$self->form // "<?>"
$self->form
Truth table0%0/2
A
A= $self->form
Aresult
11
00
214 0 return join ' ', map { $_->stringify_as_text() } @children;
215 }
216  
217 #------------------------------------------------------------------------------
218 # Recursively copy children from myself to another node.
219 # This function is specific to the P layer because it contains the list of
220 # attributes. If we could figure out the list automatically, the function would
221 # become general enough to reside directly in Node.pm.
222 #------------------------------------------------------------------------------
223 sub copy_ptree
224 0 ▶{
Subroutine copy_ptree: not called CC=4 SCAR=30.0
Pod documented
225 0 my $self = shift;
226 0 my $target = shift;
227  
228 # TODO probably we should do deepcopy
229 0 my %copy_of_wild = %{$self->wild};
230 0 $target->set_wild(\%copy_of_wild);
231  
232 0 my @children0 = $self->get_children();
233 0 foreach my $child0 (@children0)
234 {
235  
236 # Create a copy of the child node.
237 0 ▶ my $child1 = $child0->is_leaf() ? $target->create_terminal_child() : $target->create_nonterminal_child();
Branch: 1 branch
BranchTrueFalseTotal
$child0->is_leaf ? : 0 0 0
238  
239 # We should copy all attributes that the node has but it is not easy to figure out which these are.
240 # TODO: As a workaround, we list the attributes here directly.
241 0 foreach my $attribute (
242 'form', 'lemma', 'tag', # terminal
243 'phrase', 'functions', # nonterminal
244 'edgelabel', 'is_head', 'index', 'coindex' # common
245 )
246 {
247 0 my $value = $child0->get_attr($attribute);
248 0 $child1->set_attr( $attribute, $value );
249 }
250  
251 # TODO probably we should do deepcopy
252 0 %copy_of_wild = %{$child0->wild};
253 0 $child1->set_wild(\%copy_of_wild);
254  
255 # Call recursively on the subtrees of the children.
256 0 $child0->copy_ptree($child1);
257 }
258  
259 0 return;
260 }
261  
262 36 1;
263  
264 __END__
« Treex/Block/Read/BaseReader.pm ↑ Summary Treex/Core/Phrase/Term.pm »