Treex/Core/Phrase/PP.pm

filter: Statementstmt 56.4% 35 / 62 Branchbran 25.0% 8 / 32 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 8 / 12 Podpod 87.5% 7 / 8 totaltotal 49.5% 59 / 119 CC 18 SCAR 20.1
CC
18
Cov
45%
replace_core_child
20.0
deprel
12.0
set_deprel
12.0
CRAP 7.4
« Treex/Block/Read/Treex.pm ↑ Summary Treex/Core/Document.pm »
1 package Treex::Core::Phrase::PP;
2 3 $Treex::Core::Phrase::PP::VERSION = '2.20151210';
3 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 extends 'Treex::Core::Phrase::BaseNTerm';
10  
11  
12  
13 3 has 'fun' =>
14 (
15 is => 'rw',
16 isa => 'Treex::Core::Phrase',
17 required => 1,
18 writer => '_set_fun',
19 reader => 'fun'
20 );
21  
22 3 has 'arg' =>
23 (
24 is => 'rw',
25 isa => 'Treex::Core::Phrase',
26 required => 1,
27 writer => '_set_arg',
28 reader => 'arg'
29 );
30  
31 3 has 'fun_is_head' =>
32 (
33 is => 'rw',
34 isa => 'Bool',
35 required => 1
36 );
37  
38 3 has 'deprel_at_fun' =>
39 (
40 is => 'rw',
41 isa => 'Bool',
42 required => 1,
43 documentation =>
44 'Where (at what core child) is the label of the relation between this '.
45 'phrase and its parent? It is either at the function word or at the '.
46 'argument, regardless which of them is the head.'
47 );
48  
49  
50  
51 #------------------------------------------------------------------------------
52 # After the object is constructed, this block makes sure that the core children
53 # refer back to it as their parent.
54 #------------------------------------------------------------------------------
55 sub BUILD
56 {
57 1 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=12.7
Pod undocumented
58 1 ▶ if(defined($self->fun()->parent()) || defined($self->arg()->parent()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->fun->parent or defined $self->arg->parent) 0 1 1
Condition33%1/3
defined $self->fun->parent or defined $self->arg->parent
l!l&&r!l&&!r
001
MC/DC0%0/2
defined $self->fun->parent or defined $self->arg->parent
defined $self->fun->parent defined $self->arg->parent
Truth table33%1/3
A or B
A= defined $self->fun->parentB= defined $self->arg->parent
ABresult
1X1
011
000
59 {
60 0 log_fatal("The core child already has another parent");
61 }
62 1 $self->fun()->_set_parent($self);
63 1 $self->arg()->_set_parent($self);
64 }
65  
66  
67  
68 #------------------------------------------------------------------------------
69 # Returns the head child of the phrase. Depending on the current preference,
70 # it is either the function word or its argument.
71 #------------------------------------------------------------------------------
72 sub head
73 {
74 3 ▶ my $self = shift;
Subroutine head: called CC=3 SCAR=11.7
Pod documented
75 3 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 3 3
76 3 ▶ return $self->fun_is_head() ? $self->fun() : $self->arg();
Branch: 1 branch
BranchTrueFalseTotal
$self->fun_is_head ? : 0 3 3
77 }
78  
79  
80  
81 #------------------------------------------------------------------------------
82 # Returns the list of non-head children of the phrase, i.e. the dependents plus
83 # either the function word or the argument (whichever is currently not the head).
84 #------------------------------------------------------------------------------
85 sub nonhead_children
86 {
87 1 ▶ my $self = shift;
Subroutine nonhead_children: called CC=4 SCAR=14.9
Pod documented
88 1 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 1 1
89 1 ▶ my @children = (($self->fun_is_head() ? $self->arg() : $self->fun()), $self->dependents());
Branch: 1 branch
BranchTrueFalseTotal
$self->fun_is_head ? : 0 1 1
90 1 ▶ return $self->_order_required(@_) ? $self->order_phrases(@children) : @children;
Branch: 1 branch
BranchTrueFalseTotal
$self->_order_required(@_) ? : 0 1 1
91 }
92  
93  
94  
95 #------------------------------------------------------------------------------
96 # Returns the list of the children of the phrase that are not dependents, i.e.
97 # both the function word and the argument.
98 #------------------------------------------------------------------------------
99 sub core_children
100 {
101 2 ▶ my $self = shift;
Subroutine core_children: called CC=3 SCAR=11.4
Pod documented
102 2 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 2 2
103 2 my @children = ($self->fun(), $self->arg());
104 2 ▶ return $self->_order_required(@_) ? $self->order_phrases(@children) : @children;
Branch: 1 branch
BranchTrueFalseTotal
$self->_order_required(@_) ? : 0 2 2
105 }
106  
107  
108  
109 #------------------------------------------------------------------------------
110 # Returns the type of the dependency relation of the phrase to the governing
111 # phrase. A prepositional phrase has the same deprel as one of its core
112 # children. Depending on the current preference it is either the function word or
113 # the argument. This is not necessarily the same child that is the current
114 # head. For example, in the Prague annotation style, the preposition is head
115 # but its deprel is always 'AuxP' while the real deprel of the whole phrase is
116 # stored at the argument.
117 #------------------------------------------------------------------------------
118 sub deprel
119 {
120 0 ▶ my $self = shift;
Subroutine deprel: not called CC=3 SCAR=24.8
Pod documented
121 0 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 0 0
122 0 ▶ return $self->deprel_at_fun() ? $self->fun()->deprel() : $self->arg()->deprel();
Branch: 1 branch
BranchTrueFalseTotal
$self->deprel_at_fun ? : 0 0 0
123 }
124  
125  
126  
127 #------------------------------------------------------------------------------
128 # Sets a new type of the dependency relation of the phrase to the governing
129 # phrase. For PPs the label is propagated to one of the core children.
130 # Depending on the current preference it is either the function word or the
131 # argument. This is not necessarily the same child that is the current head.
132 # The label is not propagated to the underlying dependency tree
133 # (the project_dependencies() method would have to be called to achieve that).
134 #------------------------------------------------------------------------------
135 sub set_deprel
136 {
137 0 ▶ my $self = shift;
Subroutine set_deprel: not called CC=3 SCAR=24.8
Pod documented
138 0 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 0 0
139 0 ▶ $self->deprel_at_fun() ? $self->fun()->set_deprel(@_) : $self->arg()->set_deprel(@_);
Branch: 1 branch
BranchTrueFalseTotal
$self->deprel_at_fun ? : 0 0 0
140 }
141  
142  
143  
144 #------------------------------------------------------------------------------
145 # Replaces one of the core children (function word or argument) by another
146 # phrase. This is used when we want to transform the child to a different class
147 # of phrase. The replacement must not have a parent yet.
148 #------------------------------------------------------------------------------
149 sub replace_core_child
150 {
151 0 ▶ my $self = shift;
Subroutine replace_core_child: not called CC=4 SCAR=30.0
Pod documented
152 0 my $old_child = shift; # Treex::Core::Phrase
153 0 my $new_child = shift; # Treex::Core::Phrase
154 0 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 0 0
155 0 $self->_check_old_new_child($old_child, $new_child);
156 0 $old_child->_set_parent(undef);
157 0 $new_child->_set_parent($self);
158 0 ▶ if($old_child == $self->fun())
Branch: 2 branches
BranchTrueFalseTotal
if ($old_child == $self->fun) { } 0 0 0
elsif ($old_child == $self->arg) { } 0 0 0
159 {
160 0 $self->_set_fun($new_child);
161 }
162 elsif($old_child == $self->arg())
163 {
164 0 $self->_set_arg($new_child);
165 }
166 else
167 {
168 0 log_fatal("The child to be replaced is not in my core");
169 }
170 }
171  
172  
173  
174 #------------------------------------------------------------------------------
175 # Returns a textual representation of the phrase and all subphrases. Useful for
176 # debugging.
177 #------------------------------------------------------------------------------
178 sub as_string
179 {
180 0 ▶ my $self = shift;
Subroutine as_string: not called CC=2 SCAR=17.9
Pod documented
181 0 my $fun = 'FUN '.$self->fun()->as_string();
182 0 my $arg = 'ARG '.$self->arg()->as_string();
183 0 my @dependents = $self->dependents('ordered' => 1);
184 0 my $deps = join(', ', map {$_->as_string()} (@dependents));
185 0 ▶ $deps = 'DEPS '.$deps if($deps);
Branch: 1 branch
BranchTrueFalseTotal
if $deps 0 0 0
186 0 my $subtree = join(' ', ($fun, $arg, $deps));
187 0 return "(PP $subtree)";
188 }
189  
190  
191  
192 3 __PACKAGE__->meta->make_immutable();
193  
194 3 1;
195  
196  
197  
198 =for Pod::Coverage BUILD
199  
200 =encoding utf-8
201  
202 =head1 NAME
203  
204 Treex::Core::Phrase::PP
205  
206 =head1 VERSION
207  
208 version 2.20151210
209  
210 =head1 SYNOPSIS
211  
212 use Treex::Core::Document;
213 use Treex::Core::Phrase::Term;
214 use Treex::Core::Phrase::PP;
215  
216 my $document = new Treex::Core::Document;
217 my $bundle = $document->create_bundle();
218 my $zone = $bundle->create_zone('en');
219 my $root = $zone->create_atree();
220 my $prep = $root->create_child();
221 my $noun = $prep->create_child();
222 $prep->set_deprel('AuxP');
223 $noun->set_deprel('Adv');
224 my $prepphr = new Treex::Core::Phrase::Term ('node' => $prep);
225 my $argphr = new Treex::Core::Phrase::Term ('node' => $noun);
226 my $pphrase = new Treex::Core::Phrase::PP ('fun' => $prepphr, 'arg' => $argphr, 'fun_is_head' => 1);
227  
228 =head1 DESCRIPTION
229  
230 C<Phrase::PP> (standing for I<prepositional phrase>) is a special case of
231 C<Phrase::NTerm>. The model example is a preposition (possibly compound) and
232 its argument (typically a noun phrase), plus possible dependents of the whole,
233 such as emphasizers or punctuation. However, it can be also used for
234 subordinating conjunctions plus relative clauses, or for any pair of a function
235 word and its (one) argument.
236  
237 While we know the two key children (let's call them the preposition and the
238 argument), we do not take for fixed which one of them is the head (but the head
239 is indeed one of these two, and not any other child). Depending on the
240 preferred annotation style, we can pick the preposition or the argument as the
241 current head.
242  
243 =head1 ATTRIBUTES
244  
245 =over
246  
247 =item fun
248  
249 A sub-C<Phrase> of this phrase that contains the preposition (or another
250 function word if this is not a true prepositional phrase).
251  
252 =item arg
253  
254 A sub-C<Phrase> (typically a noun phrase) of this phrase that contains the
255 argument of the preposition (or of the other function word if this is not
256 a true prepositional phrase).
257  
258 =item fun_is_head
259  
260 Boolean attribute that defines the currently preferred annotation style.
261 C<True> means that the function word is considered the head of the phrase.
262 C<False> means that the argument is the head.
263  
264 =item deprel_at_fun
265  
266 Where (at what core child) is the label of the relation between this phrase and
267 its parent? It is either at the function word or at the argument, regardless
268 which of them is the head.
269  
270 =back
271  
272 =head1 METHODS
273  
274 =over
275  
276 =item head
277  
278 A sub-C<Phrase> of this phrase that is at the moment considered the head phrase
279 (in the sense of dependency syntax).
280 Depending on the current preference, it is either the function word or its
281 argument.
282  
283 =item nonhead_children
284  
285 Returns the list of non-head children of the phrase, i.e. the dependents plus either
286 the function word or the argument (whichever is currently not the head).
287  
288 =item core_children
289  
290 Returns the list of the children of the phrase that are not dependents, i.e. both the
291 function word and the argument.
292  
293 =item deprel
294  
295 Returns the type of the dependency relation of the phrase to the governing
296 phrase. A prepositional phrase has the same deprel as one of its core
297 children. Depending on the current preference it is either the function word or
298 the argument. This is not necessarily the same child that is the current
299 head. For example, in the Prague annotation style, the preposition is head
300 but its deprel is always C<AuxP> while the real deprel of the whole phrase is
301 stored at the argument.
302  
303 =item set_deprel
304  
305 Sets a new type of the dependency relation of the phrase to the governing
306 phrase. For PPs the label is propagated to one of the core children.
307 Depending on the current preference it is either the function word or the
308 argument. This is not necessarily the same child that is the current head.
309 The label is not propagated to the underlying dependency tree
310 (the project_dependencies() method would have to be called to achieve that).
311  
312 =item replace_core_child
313  
314 Replaces one of the core children (function word or argument) by another
315 phrase. This is used when we want to transform the child to a different class
316 of phrase. The replacement must not have a parent yet.
317  
318 =item as_string
319  
320 Returns a textual representation of the phrase and all subphrases. Useful for
321 debugging.
322  
323 =back
324  
325 =head1 AUTHORS
326  
327 Daniel Zeman <zeman@ufal.mff.cuni.cz>
328  
329 =head1 COPYRIGHT AND LICENSE
330  
331 Copyright © 2013, 2015 by Institute of Formal and Applied Linguistics, Charles University in Prague
332 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Block/Read/Treex.pm ↑ Summary Treex/Core/Document.pm »