lib/Treex/Core/Node/P.pm

filter: Statementstmt 66.3% 81 / 122 Branchbran 39.2% 11 / 28 Conditioncond 40.0% 2 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 71.4% 10 / 14 Podpod 100% 8 / 8 totaltotal 62.7% 113 / 180 CC 19 SCAR 25.8
CC
19
Cov
61%
stringify_as_mrg
42.0
copy_ptree
20.0
stringify_as_text
12.0
CRAP 13.2
« lib/Treex/Block/Util/ChangeDocumentPath.pm ↑ Summary lib/Treex/Block/Write/BaseWriter.pm »
1 package Treex::Core::Node::P;
2 {
3 60 $Treex::Core::Node::P::VERSION = '0.08633_1';
4 }
5  
6 60 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 60 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 ▶use Treex::Core::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 60 extends 'Treex::Core::Node';
11  
12 # dirty: merging terminal and nonterminal nodes' attributes
13  
14 # common:
15 60 has [qw(is_head index coindex edgelabel)] => ( is => 'rw' );
16  
17 # terminal specific
18 60 has [qw(form lemma tag)] => ( is => 'rw' );
19  
20 # non-terminal specific
21 60 has [qw( phrase functions )] => ( is => 'rw' );
22  
23 sub get_pml_type_name {
24 51 ▶ my ($self) = @_;
Subroutine get_pml_type_name: called CC=4 SCAR=13.9
Pod documented
25  
26 51 ▶ if ( $self->is_root() or $self->phrase ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_root or $self->phrase) { } 12 39 51
elsif ($self->tag) { } 1 38 39
Condition66%2/3
$self->is_root or $self->phrase
l!l&&r!l&&!r
12039
MC/DC50%1/2
$self->is_root or $self->phrase
$self->is_root $self->phrase
Truth table66%2/3
A or B
A= $self->is_rootB= $self->phrase
ABresult
1X1
011
000
27 12 return 'p-nonterminal.type';
28 }
29 elsif ( $self->tag ) {
30 1 return 'p-terminal.type';
31 }
32 else {
33 38 return;
34 }
35 }
36  
37 sub is_terminal {
38 0 ▶ my $self = shift @_;
Subroutine is_terminal: not called CC=2 SCAR=17.9
Pod documented
39 0 ▶ 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 0 0
40 }
41  
42 sub create_nonterminal_child {
43 10 ▶ my $self = shift @_;
Subroutine create_nonterminal_child: called CC=1 SCAR=0.0
Pod documented
44 10 my $fs_file = $self->get_bundle->get_document()->_pmldoc;
45 10 my $child = $self->create_child(@_);
46 10 $child->set_type_by_name( $fs_file->metaData('schema'), 'p-nonterminal.type' );
47 10 $child->{'#name'} = 'nonterminal';
48 10 return $child;
49 }
50  
51 sub create_terminal_child {
52 18 ▶ my $self = shift @_;
Subroutine create_terminal_child: called CC=1 SCAR=0.0
Pod documented
53 18 my $fs_file = $self->get_bundle->get_document()->_pmldoc;
54 18 my $child = $self->create_child(@_);
55 18 $child->set_type_by_name( $fs_file->metaData('schema'), 'p-terminal.type' );
56 18 $child->{'#name'} = 'terminal';
57 18 return $child;
58 }
59  
60 sub create_from_mrg {
61 1 ▶ my ( $self, $mrg_string ) = @_;
Subroutine create_from_mrg: called CC=1 SCAR=0.0
Pod documented
62  
63 # normalize spaces
64 1 $mrg_string =~ s/([()])/ $1 /g;
65 1 $mrg_string =~ s/\s+/ /g;
66 1 $mrg_string =~ s/^ //g;
67 1 $mrg_string =~ s/ $//g;
68  
69 # remove back brackets (except for round)
70 1 $mrg_string =~ s/-LSB-/\[/g;
71 1 $mrg_string =~ s/-RSB-/\]/g;
72 1 $mrg_string =~ s/-LCB-/\{/g;
73 1 $mrg_string =~ s/-RCB-/\}/g;
74  
75 # remove extra outer parenthesis
76 1 $mrg_string =~ s/^\( (\(.+) \)$/$1/;
77  
78 # remove one extra non-terminal (ROOT comes from Stanford, S1 comes from Charniak parser)
79 1 $mrg_string =~ s/^\( (ROOT|S1) (.+) \)$/$2/g;
80 1 my @tokens = split / /, $mrg_string;
81  
82 1 $self->_parse_mrg_nonterminal( \@tokens );
83 1 return;
84 }
85  
86 sub _reduce {
87 58 ▶ my ( $self, $tokens_rf, $expected_token ) = @_;
Subroutine _reduce: called CC=2 SCAR=7.4
88 58 ▶ if ( $tokens_rf->[0] eq $expected_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens_rf->[0] eq $expected_token) { } 58 0 58
89 58 return shift @{$tokens_rf};
90 }
91 else {
92 0 log_fatal "Unparsable mrg remainder: '$expected_token' is not at the beginning of: "
93 . join( " ", @$tokens_rf );
94 }
95 }
96  
97 sub _parse_mrg_nonterminal {
98 11 ▶ my ( $self, $tokens_rf ) = @_;
Subroutine _parse_mrg_nonterminal: called CC=4 SCAR=13.9
99 11 $self->_reduce( $tokens_rf, "(" );
100  
101 # phrase type and (optionally) a list of grammatical functions
102 11 my $label = shift @{$tokens_rf};
103 11 my @label_components = split /-/, $label;
104 11 $self->set_phrase( shift @label_components );
105  
106 # TODO: handle traces correctly
107 # Delete trace indices (e.g. NP-SBJ-10 ... -NONE- *T*-10)
108 11 @label_components = grep { !/^\d+$/ } @label_components;
109  
110 11 ▶ if (@label_components) {
Branch: 1 branch
BranchTrueFalseTotal
if (@label_components) 3 8 11
111 3 $self->set_functions( \@label_components );
112 }
113  
114 11 ▶ while ( $tokens_rf->[0] eq "(" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens_rf->[0] eq "(") 28 11 39
115 28 ▶ if ( $tokens_rf->[2] eq "(" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tokens_rf->[2] eq '(') { } 10 18 28
116 10 my $new_nonterminal = $self->create_nonterminal_child();
117 10 $new_nonterminal->_parse_mrg_nonterminal($tokens_rf);
118 }
119 else {
120 18 my $new_terminal_child = $self->create_terminal_child();
121 18 $new_terminal_child->_parse_mrg_terminal($tokens_rf);
122 }
123 }
124  
125 11 $self->_reduce( $tokens_rf, ")" );
126 11 return;
127 }
128  
129 sub _parse_mrg_terminal {
130 18 ▶ my ( $self, $tokens_rf ) = @_;
Subroutine _parse_mrg_terminal: called CC=1 SCAR=0.0
131 18 $self->_reduce( $tokens_rf, "(" );
132  
133 18 my $tag = shift @{$tokens_rf};
134 18 my $form = shift @{$tokens_rf};
135 18 $form =~ s/-LRB-/\(/g;
136 18 $form =~ s/-RRB-/\)/g;
137 18 $self->set_form($form);
138 18 $self->set_tag($tag);
139  
140 18 $self->_reduce( $tokens_rf, ")" );
141 18 return;
142 }
143  
144 sub stringify_as_mrg {
145 0 ▶ my ($self) = @_;
Subroutine stringify_as_mrg: not called CC=6 SCAR=37.4
Pod documented
146 0 my $string;
147 0 ▶ if ( $self->phrase ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->phrase) { } 0 0 0
148 0 ▶ my @functions = $self->functions ? @{ $self->functions } : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->functions ? : 0 0 0
149 0 $string = join '-', $self->phrase, @functions;
150 0 $string .= ' ';
151 }
152 else {
153 0 ▶ my $tag = defined $self->tag ? $self->tag : '?';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->tag ? : 0 0 0
154 0 ▶ my $form = defined $self->form ? $self->form : '?';
Branch: 1 branch
BranchTrueFalseTotal
defined $self->form ? : 0 0 0
155 0 $form =~ s/ /_/g;
156 0 $string = "$tag $form";
157 0 $string =~ s/\(/-LRB-/g;
158 0 $string =~ s/\)/-RRB-/g;
159 }
160 0 ▶ if ( $self->children ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->children) 0 0 0
161 0 $string .= join ' ', map { $_->stringify_as_mrg() } $self->children;
162 }
163 0 return "($string)";
164 }
165  
166 sub stringify_as_text {
167 0 ▶ my ($self) = @_;
Subroutine stringify_as_text: not called CC=3 SCAR=24.8
Pod documented
168 0 my @children = $self->get_children();
169 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
170 0 return join ' ', map { $_->stringify_as_text() } @children;
171 }
172  
173 #------------------------------------------------------------------------------
174 # Recursively copy children from myself to another node.
175 # This function is specific to the P layer because it contains the list of
176 # attributes. If we could figure out the list automatically, the function would
177 # become general enough to reside directly in Node.pm.
178 #------------------------------------------------------------------------------
179 sub copy_ptree
180 {
181 0 ▶ my $self = shift;
Subroutine copy_ptree: not called CC=4 SCAR=30.0
Pod documented
182 0 my $target = shift;
183  
184 # TODO probably we should do deepcopy
185 0 my %copy_of_wild = %{$self->wild};
186 0 $target->set_wild(\%copy_of_wild);
187  
188 0 my @children0 = $self->get_children();
189 0 foreach my $child0 (@children0)
190 {
191  
192 # Create a copy of the child node.
193 0 ▶ my $child1 = $child0->is_leaf() ? $target->create_terminal_child() : $target->create_nonterminal_child();
Branch: 1 branch
BranchTrueFalseTotal
$child0->is_leaf ? : 0 0 0
194  
195 # We should copy all attributes that the node has but it is not easy to figure out which these are.
196 # TODO: As a workaround, we list the attributes here directly.
197 0 foreach my $attribute (
198 'form', 'lemma', 'tag', # terminal
199 'phrase', 'functions', # nonterminal
200 'edgelabel', 'is_head', 'index', 'coindex' # common
201 )
202 {
203 0 my $value = $child0->get_attr($attribute);
204 0 $child1->set_attr( $attribute, $value );
205 }
206  
207 # TODO probably we should do deepcopy
208 0 %copy_of_wild = %{$child0->wild};
209 0 $child1->set_wild(\%copy_of_wild);
210  
211 # Call recursively on the subtrees of the children.
212 0 $child0->copy_ptree($child1);
213 }
214  
215 0 return;
216 }
217  
218 60 1;
219  
220 __END__
221  
222 =encoding utf-8
223  
224 =head1 NAME
225  
226 Treex::Core::Node::P
227  
228 =head1 VERSION
229  
230 version 0.08633_1
231  
232 =head1 DESCRIPTION
233  
234 Representation of nodes of phrase structure (constituency) trees.
235  
236 =head1 METHODS
237  
238 =head2 $node->is_terminal()
239  
240 Is C<$node> a terminal node?
241 Does its C<get_pml_type_name eq 'p-terminal.type'>?
242  
243 =head2 my $child_phrase = $node->create_nonterminal_child()
244  
245 Create a new non-terminal child node,
246 i.e. a node representing a constituent (phrase).
247  
248 =head2 my $child_terminal = $node->create_terminal_child()
249  
250 Create a new terminal child node,
251 i.e. a node representing a token.
252  
253 =head2 my $node->create_from_mrg($mrg_string)
254  
255 Fill C<$node>'s attributes and create its subtree
256 from the serialized string in the PennTB C<mrg> format.
257 E.g.: I<(NP (DT a) (JJ nonexecutive) (NN director))>.
258  
259 =head2 my $mrg_string = $node->stringify_as_mrg()
260  
261 Serialize the tree structure of C<$node> and its subtree
262 as a string in the PennTB C<mrg> format.
263 E.g.: I<(NP (DT a) (JJ nonexecutive) (NN director))>.
264  
265 =head2 my $tokenized_text = $node->stringify_as_text()
266  
267 Get the text representing C<$node>'s subtree.
268 The text is tokenized, i.e. all tokens are separated by a space.
269  
270 =head2 $node->copy_ptree($target_node)
271  
272 Recursively copy children from C<$node> to C<$target_node>.
273 This method is specific to the P layer because it contains the list of
274 attributes. If we could figure out the list automatically, the method would
275 become general enough to reside directly in Node.pm.
276  
277 =head1 AUTHOR
278  
279 Martin Popel <popel@ufal.mff.cuni.cz>
280 Zdeněk Žabokrtský <zabokrtsky@ufal.mff.cuni.cz>
281 Daniel Zeman <zeman@ufal.mff.cuni.cz>
282  
283 =head1 COPYRIGHT AND LICENSE
284  
285 Copyright © 2011 by Institute of Formal and Applied Linguistics, Charles University in Prague
286  
287 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« lib/Treex/Block/Util/ChangeDocumentPath.pm ↑ Summary lib/Treex/Block/Write/BaseWriter.pm »