Treex/Core/Phrase/NTerm.pm

filter: Statementstmt 59.1% 29 / 49 Branchbran 21.4% 3 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 75.0% 3 / 4 totaltotal 54.6% 41 / 75 CC 8 SCAR 17.9
CC
8
Cov
51%
replace_core_child
12.0
as_string
12.0
set_head
3.0
CRAP 6.0
« Treex/Core/TredView/LineStyles.pm ↑ Summary Treex/Block/Write/Treex.pm »
1 package Treex::Core::Phrase::NTerm;
2 6 $Treex::Core::Phrase::NTerm::VERSION = '2.20210102';
3 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 extends 'Treex::Core::Phrase::BaseNTerm';
10  
11  
12  
13 6 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 1 ▶{
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
30 1 my $self = shift;
31 1 ▶ if(defined($self->head()->parent()))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->head->parent) 0 1 1
32 {
33 0 log_fatal("The head already has another parent");
34 }
35 1 $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 1 ▶{
Subroutine set_head: called CC=3 SCAR=11.1
Pod documented
49 1 my $self = shift;
50 1 my $new_head = shift; # Treex::Core::Phrase
51 1 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 1 1
52 1 my $old_head = $self->head();
53 1 ▶ return $self if ($new_head == $old_head);
Branch: 1 branch
BranchTrueFalseTotal
if $new_head == $old_head 0 1 1
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 1 $self->_remove_child($new_head);
57 # Add the old head to the list of non-head children.
58 1 $self->_add_child($old_head);
59 # Finally, set the new head, using the private bare setter.
60 1 $self->_set_head($new_head);
61 # Return reference to self because we have not encapsulated ourselves in a new phrase.
62 1 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 0 ▶{
Subroutine replace_core_child: not called CC=3 SCAR=24.8
Pod documented
74 0 my $self = shift;
75 0 my $old_child = shift; # Treex::Core::Phrase
76 0 my $new_child = shift; # Treex::Core::Phrase
77 0 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 0 0
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 ▶ if($old_child != $self->head())
Branch: 1 branch
BranchTrueFalseTotal
if ($old_child != $self->head) 0 0 0
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 0 ▶{
Subroutine as_string: not called CC=3 SCAR=24.8
Pod documented
97 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));
101 0 ▶ $deps = 'DEPS '.$deps if($deps);
Branch: 1 branch
BranchTrueFalseTotal
if $deps 0 0 0
102 0 my $subtree = join(' ', ($head, $deps));
103 0 ▶ $subtree .= ' _M' if($self->is_member());
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_member 0 0 0
104 0 return "(NT $subtree)";
105 }
106  
107  
108  
109 6 __PACKAGE__->meta->make_immutable();
110  
111 6 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.20210102
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.
« Treex/Core/TredView/LineStyles.pm ↑ Summary Treex/Block/Write/Treex.pm »