File Coverage

blib/lib/Treex/Core/Phrase/PP.pm
Criterion Covered Total %
statement 12 54 22.2
branch 0 32 0.0
condition 0 3 0.0
subroutine 4 12 33.3
pod n/a
total 16 101 15.8


line stmt bran cond sub pod time code
1             package Treex::Core::Phrase::PP;
2             $Treex::Core::Phrase::PP::VERSION = '2.20150928';
3 1     1   1778 use utf8;
  1         3  
  1         7  
4 1     1   31 use namespace::autoclean;
  1         2  
  1         7  
5              
6 1     1   62 use Moose;
  1         2  
  1         6  
7 1     1   5991 use Treex::Core::Log;
  1         2  
  1         796  
8              
9             extends 'Treex::Core::Phrase::BaseNTerm';
10              
11              
12              
13             has 'fun' =>
14             (
15             is => 'rw',
16             isa => 'Treex::Core::Phrase',
17             required => 1,
18             writer => '_set_fun',
19             reader => 'fun'
20             );
21              
22             has 'arg' =>
23             (
24             is => 'rw',
25             isa => 'Treex::Core::Phrase',
26             required => 1,
27             writer => '_set_arg',
28             reader => 'arg'
29             );
30              
31             has 'fun_is_head' =>
32             (
33             is => 'rw',
34             isa => 'Bool',
35             required => 1
36             );
37              
38             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 0     0     my $self = shift;
58 0 0 0       if(defined($self->fun()->parent()) || defined($self->arg()->parent()))
59             {
60 0           log_fatal("The core child already has another parent");
61             }
62 0           $self->fun()->_set_parent($self);
63 0           $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 0     0     my $self = shift;
75 0 0         log_fatal('Dead') if($self->dead());
76 0 0         return $self->fun_is_head() ? $self->fun() : $self->arg();
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 0     0     my $self = shift;
88 0 0         log_fatal('Dead') if($self->dead());
89 0 0         my @children = (($self->fun_is_head() ? $self->arg() : $self->fun()), $self->dependents());
90 0 0         return $self->_order_required(@_) ? $self->order_phrases(@children) : @children;
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 0     0     my $self = shift;
102 0 0         log_fatal('Dead') if($self->dead());
103 0           my @children = ($self->fun(), $self->arg());
104 0 0         return $self->_order_required(@_) ? $self->order_phrases(@children) : @children;
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     0     my $self = shift;
121 0 0         log_fatal('Dead') if($self->dead());
122 0 0         return $self->deprel_at_fun() ? $self->fun()->deprel() : $self->arg()->deprel();
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     0     my $self = shift;
138 0 0         log_fatal('Dead') if($self->dead());
139 0 0         $self->deprel_at_fun() ? $self->fun()->set_deprel(@_) : $self->arg()->set_deprel(@_);
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     0     my $self = shift;
152 0           my $old_child = shift; # Treex::Core::Phrase
153 0           my $new_child = shift; # Treex::Core::Phrase
154 0 0         log_fatal('Dead') if($self->dead());
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 0         if($old_child == $self->fun())
    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     0     my $self = shift;
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));
  0            
185 0 0         $deps = 'DEPS '.$deps if($deps);
186 0           my $subtree = join(' ', ($fun, $arg, $deps));
187 0           return "(PP $subtree)";
188             }
189              
190              
191              
192             __PACKAGE__->meta->make_immutable();
193              
194             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.20150928
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.