File Coverage

blib/lib/Treex/Core/Phrase/NTerm.pm
Criterion Covered Total %
statement 12 42 28.5
branch 0 12 0.0
condition n/a
subroutine 4 8 50.0
pod n/a
total 16 62 25.8


line stmt bran cond sub pod time code
1             package Treex::Core::Phrase::NTerm;
2             $Treex::Core::Phrase::NTerm::VERSION = '2.20150928';
3 1     1   3105 use utf8;
  1         2  
  1         8  
4 1     1   32 use namespace::autoclean;
  1         2  
  1         7  
5              
6 1     1   65 use Moose;
  1         2  
  1         8  
7 1     1   6264 use Treex::Core::Log;
  1         3  
  1         516  
8              
9             extends 'Treex::Core::Phrase::BaseNTerm';
10              
11              
12              
13             has 'head' =>
14             (
15             is => 'rw',
16             isa => 'Treex::Core::Phrase',
17             required => 1,
18             writer => '_set_head',
19             reader => 'head'
20             );
21              
22              
23              
24             #------------------------------------------------------------------------------
25             # After the object is constructed, this block makes sure that the head refers
26             # back to it as its parent.
27             #------------------------------------------------------------------------------
28             sub BUILD
29             {
30 0     0     my $self = shift;
31 0 0         if(defined($self->head()->parent()))
32             {
33 0           log_fatal("The head already has another parent");
34             }
35 0           $self->head()->_set_parent($self);
36             }
37              
38              
39              
40             #------------------------------------------------------------------------------
41             # Sets a new head child for this phrase. The new head must be already a child
42             # of this phrase. The old head will become an ordinary non-head child.
43             #------------------------------------------------------------------------------
44             sub set_head
45             {
46 0     0     my $self = shift;
47 0           my $new_head = shift; # Treex::Core::Phrase
48 0 0         log_fatal('Dead') if($self->dead());
49 0           my $old_head = $self->head();
50 0 0         return if ($new_head == $old_head);
51             # Remove the new head from the list of non-head children.
52             # (The method will also verify that it is defined and is my child.)
53 0           $self->_remove_child($new_head);
54             # Add the old head to the list of non-head children.
55 0           $self->_add_child($old_head);
56             # Finally, set the new head, using the private bare setter.
57 0           $self->_set_head($new_head);
58             }
59              
60              
61              
62             #------------------------------------------------------------------------------
63             # Replaces the head by another phrase. This is used when we want to transform
64             # the head to a different class of phrases. The replacement must not have a
65             # parent yet.
66             #------------------------------------------------------------------------------
67             sub replace_core_child
68             {
69 0     0     my $self = shift;
70 0           my $old_child = shift; # Treex::Core::Phrase
71 0           my $new_child = shift; # Treex::Core::Phrase
72 0 0         log_fatal('Dead') if($self->dead());
73 0           $self->_check_old_new_child($old_child, $new_child);
74             # We have not checked yet whether the old child is the head.
75 0 0         if($old_child != $self->head())
76             {
77 0           log_fatal("The replacement child is not my head");
78             }
79 0           $old_child->_set_parent(undef);
80 0           $new_child->_set_parent($self);
81 0           $self->_set_head($new_child);
82             }
83              
84              
85              
86             #------------------------------------------------------------------------------
87             # Returns a textual representation of the phrase and all subphrases. Useful for
88             # debugging.
89             #------------------------------------------------------------------------------
90             sub as_string
91             {
92 0     0     my $self = shift;
93 0           my $head = 'HEAD '.$self->head()->as_string();
94 0           my @dependents = $self->dependents('ordered' => 1);
95 0           my $deps = join(', ', map {$_->as_string()} (@dependents));
  0            
96 0 0         $deps = 'DEPS '.$deps if($deps);
97 0           my $subtree = join(' ', ($head, $deps));
98 0           return "(NT $subtree)";
99             }
100              
101              
102              
103             __PACKAGE__->meta->make_immutable();
104              
105             1;
106              
107              
108              
109             =for Pod::Coverage BUILD
110              
111             =encoding utf-8
112              
113             =head1 NAME
114              
115             Treex::Core::Phrase::NTerm
116              
117             =head1 VERSION
118              
119             version 2.20150928
120              
121             =head1 SYNOPSIS
122              
123             use Treex::Core::Document;
124             use Treex::Core::Phrase::Term;
125             use Treex::Core::Phrase::NTerm;
126              
127             my $document = new Treex::Core::Document;
128             my $bundle = $document->create_bundle();
129             my $zone = $bundle->create_zone('en');
130             my $root = $zone->create_atree();
131             my $node = $root->create_child();
132             my $tphrase = new Treex::Core::Phrase::Term ('node' => $node);
133             my $ntphrase = new Treex::Core::Phrase::NTerm ('head' => $tphrase);
134              
135             =head1 DESCRIPTION
136              
137             C<NTerm> is a nonterminal C<Phrase>. It contains (refers to) one or more child
138             C<Phrase>s.
139             See L<Treex::Core::Phrase> for more details.
140              
141             =head1 ATTRIBUTES
142              
143             =over
144              
145             =item head
146              
147             A sub-C<Phrase> of this phrase that is at the moment considered the head phrase (in the sense of dependency syntax).
148             Head is a special case of child (sub-) phrase. The (one) head must always exist; other children are optional.
149              
150             =back
151              
152             =head1 METHODS
153              
154             =over
155              
156             =item set_head ($child_phrase);
157              
158             Sets a new head child for this phrase. The new head must be already a child
159             of this phrase. The old head will become an ordinary non-head child.
160              
161             =item replace_core_child ($old_head, $new_head);
162              
163             Replaces the head by another phrase. This is used when we want to transform
164             the head to a different class of phrases. The replacement must not have a
165             parent yet.
166              
167             =item as_string
168              
169             Returns a textual representation of the phrase and all subphrases. Useful for
170             debugging.
171              
172             =back
173              
174             =head1 AUTHORS
175              
176             Daniel Zeman <zeman@ufal.mff.cuni.cz>
177              
178             =head1 COPYRIGHT AND LICENSE
179              
180             Copyright © 2013, 2015 by Institute of Formal and Applied Linguistics, Charles University in Prague
181             This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.