File Coverage

blib/lib/Treex/Core/Phrase/NTerm.pm
Criterion Covered Total %
statement 12 44 27.2
branch 0 14 0.0
condition n/a
subroutine 4 8 50.0
pod n/a
total 16 66 24.2


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