Treex/Core/Phrase/NTerm.pm

filter: Statementstmt 59.5% 28 / 47 Branchbran 25.0% 3 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 6 / 8 Podpod 75.0% 3 / 4 totaltotal 56.3% 40 / 71 CC 7 SCAR 15.4
CC
7
Cov
53%
replace_core_child
12.0
as_string
6.0
set_head
3.0
CRAP 4.7
« Treex/Block/Write/Treex.pm ↑ Summary Treex/Core/Zone.pm »
1 package Treex::Core::Phrase::NTerm;
2 3 $Treex::Core::Phrase::NTerm::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 '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 1 ▶ my $self = shift;
Subroutine BUILD: called CC=2 SCAR=7.6
Pod undocumented
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.
43 #------------------------------------------------------------------------------
44 sub set_head
45 {
46 1 ▶ my $self = shift;
Subroutine set_head: called CC=3 SCAR=11.1
Pod documented
47 1 my $new_head = shift; # Treex::Core::Phrase
48 1 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 1 1
49 1 my $old_head = $self->head();
50 1 ▶ return if ($new_head == $old_head);
Branch: 1 branch
BranchTrueFalseTotal
if $new_head == $old_head 0 1 1
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 1 $self->_remove_child($new_head);
54 # Add the old head to the list of non-head children.
55 1 $self->_add_child($old_head);
56 # Finally, set the new head, using the private bare setter.
57 1 $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 ▶ my $self = shift;
Subroutine replace_core_child: not called CC=3 SCAR=24.8
Pod documented
70 0 my $old_child = shift; # Treex::Core::Phrase
71 0 my $new_child = shift; # Treex::Core::Phrase
72 0 ▶ log_fatal('Dead') if($self->dead());
Branch: 1 branch
BranchTrueFalseTotal
if $self->dead 0 0 0
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 ▶ if($old_child != $self->head())
Branch: 1 branch
BranchTrueFalseTotal
if ($old_child != $self->head) 0 0 0
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 ▶ my $self = shift;
Subroutine as_string: not called CC=2 SCAR=17.9
Pod documented
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));
96 0 ▶ $deps = 'DEPS '.$deps if($deps);
Branch: 1 branch
BranchTrueFalseTotal
if $deps 0 0 0
97 0 my $subtree = join(' ', ($head, $deps));
98 0 return "(NT $subtree)";
99 }
100  
101  
102  
103 3 __PACKAGE__->meta->make_immutable();
104  
105 3 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.20151210
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.
« Treex/Block/Write/Treex.pm ↑ Summary Treex/Core/Zone.pm »