File Coverage

blib/lib/Treex/Core/Phrase/Term.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Treex::Core::Phrase::Term;
2             $Treex::Core::Phrase::Term::VERSION = '2.20150928';
3 1     1   58546 use utf8;
  1         2  
  1         9  
4 1     1   29 use namespace::autoclean;
  1         2  
  1         6  
5              
6 1     1   63 use Moose;
  1         3  
  1         8  
7 1     1   6669 use Treex::Core::Log;
  1         2  
  1         96  
8 1     1   27 use Treex::Core::Node;
  0            
  0            
9              
10             extends 'Treex::Core::Phrase';
11              
12              
13              
14             has 'node' =>
15             (
16             is => 'ro',
17             isa => 'Treex::Core::Node',
18             required => 1
19             );
20              
21             has 'deprel' =>
22             (
23             is => 'rw',
24             isa => 'Str',
25             required => 1
26             );
27              
28              
29              
30             #------------------------------------------------------------------------------
31             # This block will be called before object construction. It will copy the deprel
32             # attribute from the node (unless it has been supplied by the caller
33             # separately). Then it will pass all the attributes to the constructor.
34             #------------------------------------------------------------------------------
35             around BUILDARGS => sub
36             {
37             my $orig = shift;
38             my $class = shift;
39             # Call the default BUILDARGS in Moose::Object. It will take care of distinguishing between a hash reference and a plain hash.
40             my $attr = $class->$orig(@_);
41             if(defined($attr->{node}))
42             {
43             my $node = $attr->{node};
44             # Add deprel only if it has not been supplied separately.
45             if(!defined($attr->{deprel}))
46             {
47             if(defined($node->deprel()))
48             {
49             $attr->{deprel} = $node->deprel();
50             }
51             elsif(defined($node->afun()))
52             {
53             $attr->{deprel} = $node->afun();
54             }
55             elsif(defined($node->conll_deprel()))
56             {
57             $attr->{deprel} = $node->conll_deprel();
58             }
59             else
60             {
61             $attr->{deprel} = 'NR';
62             }
63             }
64             # Copy the initial value of is_member from the node to the phrase.
65             if(!defined($attr->{is_member}) && $node->is_member())
66             {
67             $attr->{is_member} = 1;
68             }
69             }
70             return $attr;
71             };
72              
73              
74              
75             #------------------------------------------------------------------------------
76             # Tells whether this phrase is terminal. We could probably use the Moose's
77             # methods to query the class name but this will be more convenient.
78             #------------------------------------------------------------------------------
79             sub is_terminal
80             {
81             my $self = shift;
82             return 1;
83             }
84              
85              
86              
87             #------------------------------------------------------------------------------
88             # Returns the list of dependents of the phrase. Terminal phrases return an
89             # empty list by definition.
90             #------------------------------------------------------------------------------
91             sub dependents
92             {
93             my $self = shift;
94             return ();
95             }
96              
97              
98              
99             #------------------------------------------------------------------------------
100             # Returns the list of children of the phrase. Terminal phrases return an empty
101             # list by definition.
102             #------------------------------------------------------------------------------
103             sub children
104             {
105             my $self = shift;
106             return ();
107             }
108              
109              
110              
111             #------------------------------------------------------------------------------
112             # Projects dependencies between the head and the dependents back to the
113             # underlying dependency structure. There is not much to do in the terminal
114             # phrase as it does not have any dependents. However, we will attach all nodes
115             # to the root, to prevent temporary cycles during the tree construction.
116             #------------------------------------------------------------------------------
117             sub project_dependencies
118             {
119             my $self = shift;
120             my $node = $self->node();
121             unless($node->is_root())
122             {
123             my $root = $node->get_root();
124             $node->set_parent($root);
125             }
126             # Reset the is_member flag.
127             # If we are converting to the Prague style, the flag will be set again where needed.
128             $node->set_is_member(0);
129             }
130              
131              
132              
133             #------------------------------------------------------------------------------
134             # Returns a textual representation of the phrase and all subphrases. Useful for
135             # debugging.
136             #------------------------------------------------------------------------------
137             sub as_string
138             {
139             my $self = shift;
140             my $node = $self->node();
141             my $form = '_';
142             if($node->is_root())
143             {
144             $form = 'ROOT';
145             }
146             elsif(defined($node->form()))
147             {
148             $form = $node->form();
149             }
150             my $ord = $node->ord();
151             my $deprel = defined($self->deprel()) ? '-'.$self->deprel() : '';
152             return "[ $form-$ord$deprel ]";
153             }
154              
155              
156              
157             __PACKAGE__->meta->make_immutable();
158              
159             1;
160              
161              
162              
163             =for Pod::Coverage BUILD
164              
165             =encoding utf-8
166              
167             =head1 NAME
168              
169             Treex::Core::Phrase::Term
170              
171             =head1 VERSION
172              
173             version 2.20150928
174              
175             =head1 SYNOPSIS
176              
177             use Treex::Core::Document;
178             use Treex::Core::Phrase::Term;
179              
180             my $document = new Treex::Core::Document;
181             my $bundle = $document->create_bundle();
182             my $zone = $bundle->create_zone('en');
183             my $root = $zone->create_atree();
184             my $phrase = new Treex::Core::Phrase::Term ('node' => $root);
185              
186             =head1 DESCRIPTION
187              
188             C<Term> is a terminal C<Phrase>. It contains (refers to) one C<Node> and it can
189             be part of nonterminal phrases (C<NTerm>).
190             See L<Treex::Core::Phrase> for more details.
191              
192             =head1 ATTRIBUTES
193              
194             =over
195              
196             =item node
197              
198             Refers to the C<Node> wrapped in this terminal phrase.
199              
200             =item deprel
201              
202             Any label describing the type of the dependency relation between this phrase
203             (its node) and the governing phrase (node of the first ancestor phrase where
204             this one does not act as head). This label is typically taken from the
205             underlying node when the phrase is built, but it may be translated or modified
206             and it is not kept synchronized with the underlying dependency tree during
207             transformations of the phrase structure. Nevertheless it is assumed that once
208             the transformations are done, the final dependency relations will be projected
209             back to the dependency tree.
210              
211             The C<deprel> attribute can also be supplied separately when creating the
212             C<Phrase::Term>. If it is not supplied, it will be copied from the C<Node>
213             to which the C<node> attribute refers.
214              
215             =item dependents
216              
217             Returns the list of dependents of the phrase. Terminal phrases return an
218             empty list by definition.
219              
220             =item children
221              
222             Returns the list of children of the phrase. Terminal phrases return an
223             empty list by definition.
224              
225             =item project_dependencies
226              
227             Projects dependencies between the head and the dependents back to the
228             underlying dependency structure. There is not much to do in the terminal
229             phrase as it does not have any dependents. However, we will attach all nodes
230             to the root, to prevent temporary cycles during the tree construction.
231              
232             =item as_string
233              
234             Returns a textual representation of the phrase and all subphrases. Useful for
235             debugging.
236              
237             =back
238              
239             =head1 AUTHORS
240              
241             Daniel Zeman <zeman@ufal.mff.cuni.cz>
242              
243             =head1 COPYRIGHT AND LICENSE
244              
245             Copyright © 2013, 2015 by Institute of Formal and Applied Linguistics, Charles University in Prague
246             This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.