Treex/Core/Node/Interset.pm

filter: Statementstmt 47.3% 54 / 114 Branchbran 11.9% 5 / 42 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 68.4% 13 / 19 Podpod n/a totaltotal 35.2% 72 / 204 CC 57 SCAR 42.7
CC
57
Cov
33%
__ANON__
199.5
match_iset
199.5
__ANON__
20.0
CRAP 71.8
« Treex/Core/TredView/TreeLayout.pm ↑ Summary Treex/Core/DocumentReader/Base.pm »
1 package Treex::Core::Node::Interset;
2 38 $Treex::Core::Node::Interset::VERSION = '2.20151210';
3 38 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 38 parameter interset_attribute => (
6 isa => 'Str',
7 default => 'iset',
8 );
9  
10 38 ▶use Treex::Core::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 38 ▶use List::Util qw(first); # TODO: this wouldn't be needed if there was Treex::Core::Common for roles
Subroutine BEGIN: called CC=1 SCAR=0.0
12 38 ▶use Lingua::Interset 2.050;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 38 ▶use Lingua::Interset::FeatureStructure;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 38 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # "role" is a semi-keyword imported from MooseX::Role::Parameterized
17 role {
18 72 ▶my $role_parameters = shift;
Subroutine __ANON__: called CC=1 SCAR=3.7
19 72 my $interset_attribute = $role_parameters->interset_attribute;
20  
21 72 has $interset_attribute => (
22 # Unfortunatelly, the old interface uses $anode->set_iset('tense', 'past'),
23 # so set_iset cannot be used as a setter for the whole structure
24 # $anode->set_iset(Lingua::Interset::FeatureStructure->new(tense=>'past'))
25 is => 'ro',
26 isa => 'Lingua::Interset::FeatureStructure',
27 lazy_build => 1,
28 #builder => "_build_$interset_attribute",
29 handles => [qw(
30 matches
31 upos
32 set_upos
33 is_abbreviation
34 is_abessive
35 is_ablative
36 is_absolute_superlative
37 is_absolutive
38 is_accusative
39 is_active
40 is_additive
41 is_adessive
42 is_adjective
43 is_adposition
44 is_adverb
45 is_affirmative
46 is_allative
47 is_animate
48 is_aorist
49 is_archaic
50 is_article
51 is_associative
52 is_benefactive
53 is_cardinal
54 is_colloquial
55 is_comitative
56 is_common_gender
57 is_comparative
58 is_conditional
59 is_conjunction
60 is_conjunctive
61 is_coordinator
62 is_dative
63 is_definite
64 is_delative
65 is_demonstrative
66 is_desiderative
67 is_destinative
68 is_determiner
69 is_diminutive
70 is_distributive
71 is_dual
72 is_elative
73 is_ergative
74 is_essive
75 is_exclamative
76 is_factive
77 is_feminine
78 is_finite_verb
79 is_first_person
80 is_foreign
81 is_future
82 is_genitive
83 is_gerund
84 is_gerundive
85 is_hyph
86 is_illative
87 is_imperative
88 is_imperfect
89 is_inanimate
90 is_indefinite
91 is_indicative
92 is_inessive
93 is_infinitive
94 is_informal
95 is_instructive
96 is_instrumental
97 is_interjection
98 is_interrogative
99 is_intransitive
100 is_jussive
101 is_lative
102 is_locative
103 is_masculine
104 is_mediopassive
105 is_middle_voice
106 is_modal
107 is_motivative
108 is_multiplicative
109 is_narrative
110 is_necessitative
111 is_negative
112 is_nominative
113 is_nonhuman
114 is_neuter
115 is_noun
116 is_numeral
117 is_optative
118 is_ordinal
119 is_participle
120 is_particle
121 is_partitive
122 is_past
123 is_perfect
124 is_personal
125 is_personal_pronoun
126 is_pluperfect
127 is_plural
128 is_polite
129 is_positive
130 is_possessive
131 is_potential
132 is_present
133 is_prolative
134 is_pronominal
135 is_pronoun
136 is_proper_noun
137 is_progressive
138 is_prospective
139 is_punctuation
140 is_quotative
141 is_rare
142 is_reciprocal
143 is_reflexive
144 is_relative
145 is_second_person
146 is_singular
147 is_subjunctive
148 is_sublative
149 is_subordinator
150 is_superessive
151 is_superlative
152 is_supine
153 is_symbol
154 is_temporal
155 is_terminative
156 is_third_person
157 is_total
158 is_transgressive
159 is_transitive
160 is_translative
161 is_typo
162 is_verb
163 is_vocative
164 is_wh
165 )],
166 # Note that we cannot export
167 # $anode->iset->is_auxiliary as it would clash with the existing $anode->is_auxiliary
168 # $tnode->dset->is_passive as it would clash with the existing $tnode->is_passive
169  
170 );
171  
172 method "_build_$interset_attribute" => sub {
173 9 ▶ return Lingua::Interset::FeatureStructure->new();
Subroutine _build_iset: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
174 72 };
175  
176 # Interset 1.0 legacy method (works with both Interset 1.0 and 2.0 feature structures)
177 method is_preposition => sub {
178 0 ▶ my $self = shift;
Subroutine is_preposition: not called CC=1 SCAR=6.9
179 0 return $self->iset->pos =~ /^(prep|adp)$/;
180 72 };
181  
182  
183  
184 #------------------------------------------------------------------------------
185 # Takes the Interset feature structure as a hash reference (as output by an
186 # Interset decode() or get_iset_structure() function). For all hash keys that
187 # are known Interset feature names, sets the corresponding iset attribute.
188 #
189 # If the first argument is not a hash reference, the list of arguments is
190 # considered a list of features and values. Usage examples:
191 #
192 # set_iset(\%feature_structure);
193 # set_iset('pos', 'noun');
194 # set_iset('pos' => 'noun', 'gender' => 'masc', 'number' => 'sing');
195 #
196 # TODO: Note that this is not a proper setter method yet.
197 # For backward compatibility, it only *adds* features to the Interset feature structure.
198 # For example:
199 # $anode->set_iset(case=>'nom', gender=>'fem');
200 # $anode->set_iset(case=>'gen', pos=>'noun');
201 # Now $anode->get_iset_structure() would return
202 # {case=>'gen', pos=>'noun', gender=>'fem'}
203 # If you want to delete a feature, you must explicitely set it to an empty string
204 # $anode->set_iset(gender=>'');
205 # Now: {case=>'gen', pos=>'noun', gender=>''} which is equivalent to
206 # {case=>'gen', pos=>'noun'}
207 #------------------------------------------------------------------------------
208 method set_iset => sub {
209 16 ▶ my $self = shift;
Subroutine set_iset: called CC=4 SCAR=21.1
210 16 my @assignments;
211 16 ▶ if ( ref( $_[0] ) =~ /(HASH|Lingua::Interset::FeatureStructure)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($_[0]) =~ /(HASH|Lingua::Interset::FeatureStructure)/) { } 16 0 16
212 # We cannot interpret the hash/object as a set of assignments for add() as below.
213 # Lingua::Interset::FeatureStructure may contain private attributes that are not features.
214 # Using merge_hash_hard() is safer because it only takes known features from the hash and ignores the rest.
215 16 return $self->$interset_attribute->merge_hash_hard($_[0]);
216 }
217 else {
218 0 ▶ log_fatal "No parameters for 'set_iset'" if @_ == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 0 0
219 0 ▶ log_fatal "Odd parameters for 'set_iset'" if @_%2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 0 0
220 0 @assignments = @_;;
221 0 return $self->$interset_attribute->add(@assignments);
222 }
223 72 };
224  
225  
226  
227 #------------------------------------------------------------------------------
228 # Gets the value of an Interset feature. Makes sure that the result is never
229 # undefined so the use/strict/warnings creature keeps quiet. It returns undef
230 # only if we ask for the value of an unknown feature.
231 #
232 # If there is a disjunction of values (such as "fem|neut"), this function
233 # returns just a string with vertical bars as delimiters. The caller can use
234 # a split() function to get an array, or call get_iset_structure() instead.
235 #------------------------------------------------------------------------------
236 method get_iset => sub {
237 0 ▶ my ($self, $feature) = @_;
Subroutine get_iset: not called CC=4 SCAR=30.0
238 0 my $value = $self->get_attr("$interset_attribute/$feature");
239 # convert arrayref to string, e.g. "fem|neut"
240 0 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 0 0 0
241 0 $value = join '|', @$value;
242 }
243 0 ▶ return $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
244  
245 # Check valid feature name only when the feature is missing.
246 # TODO: convert all Treex code to Interset 2.0, so that no checking is needed.
247 0 ▶ if (!Lingua::Interset::FeatureStructure::feature_valid($feature)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Lingua::Interset::FeatureStructure::feature_valid($feature)) 0 0 0
248 0 log_warn("Querying unknown Interset feature $feature");
249 }
250  
251 # Return empty string instead of undef.
252 0 return '';
253 72 };
254  
255  
256  
257 #------------------------------------------------------------------------------
258 # Gets the values of all Interset features and returns a hash. Any multivalues
259 # (such as "fem|neut") will be converted to arrays referenced from the hash
260 # (same as the result of decode() functions in Interset tagset drivers).
261 #------------------------------------------------------------------------------
262 method get_iset_structure => sub
263 {
264 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
265 0 my $iset = $self->$interset_attribute; # iset or dset
266 0 my %f;
267 0 foreach my $feature ( $iset->get_nonempty_features() )
268 {
269 0 $f{$feature} = $iset->get_joined($feature);
270 0 ▶ if ( $f{$feature} =~ m/\|/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($f{$feature} =~ /\|/) 0 0 0
271 {
272 0 my @values = split( /\|/, $f{$feature} );
273 0 $f{$feature} = \@values;
274 }
275 }
276 0 return \%f;
277 72 };
278  
279 #------------------------------------------------------------------------------
280 # Return the values of all non-empty Interset features (except for the "tagset" and "other" features).
281 #------------------------------------------------------------------------------
282 method get_iset_values => sub
283 {
284 0 ▶ my $self = shift;
Subroutine get_iset_values: not called CC=1 SCAR=6.9
285 0 return map {$self->get_iset($_)} grep {$_ !~ 'tagset|other'} $self->$interset_attribute->get_nonempty_features();
286 72 };
287  
288 #------------------------------------------------------------------------------
289 # The inverse of iset->as_string_conllx -- takes a feat string which is the
290 # result of calling iset->as_string_conllx, and sets Interset feature values
291 # according to that string.
292 #------------------------------------------------------------------------------
293 method set_iset_conll_feat => sub {
294 0 ▶ my ($self, $feat_string) = @_;
Subroutine __ANON__: not called CC=2 SCAR=12.4
Subroutine set_iset_conll_feat: called CC=2 SCAR=12.4
295 0 my @pairs = split /\|/, $feat_string;
296 0 foreach my $pair (@pairs) {
297 0 $pair =~ s/;/|/g;
298 0 my ($feature, $value) = split /=/, $pair;
299 0 $self->set_iset($feature, $value);
300 }
301 0 return;
302 72 };
303  
304 #------------------------------------------------------------------------------
305 # Tests multiple Interset features simultaneously. Input is a list of feature-
306 # value pairs, return value is 1 if the node matches all these values. This
307 # function is an abbreviation for a series of get_iset() calls in an if
308 # statement:
309 #
310 # if($node->match_iset('pos' => 'noun', 'gender' => 'masc')) { ... }
311 #------------------------------------------------------------------------------
312 method match_iset => sub {
313 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=14 SCAR=53.0
314 0 my @req = @_;
315 0 ▶ for ( my $i = 0; $i <= $#req; $i += 2 )
Branch: 2 branches
BranchTrueFalseTotal
if ($i <= $#req) 0 0 0
if ($i <= $#req) 12 0 12
316 {
317 0 my $feature = $req[$i];
318 0 my $expected = $req[$i+1];
319 0 ▶ confess("Undefined feature") unless ($feature);
Branch: 2 branches
BranchTrueFalseTotal
unless $feature 0 0 0
unless $feature 0 0 0
320 0 my $value = $self->get_iset($feature);
321 0 ▶ my $comp =
Branch: 4 branches
BranchTrueFalseTotal
$expected =~ s/^\!\~// ? : 0 0 0
$expected =~ s/^\!// ? : 0 0 0
$expected =~ s/^\~// ? : 0 0 0
$expected =~ s/^\!\~// ? : 0 0 0
322 $expected =~ s/^\!\~// ? 'nr' :
323 $expected =~ s/^\!// ? 'ne' :
324 $expected =~ s/^\~// ? 're' : 'eq';
325 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($comp eq "eq" and $value ne $expected or $comp eq "ne" and $value eq $expected or $comp eq "re" and not $value =~ /$expected/ or $comp eq "nr" and $value =~ /$expected/) 0 0 0
Condition0%0/21
$comp eq "eq" and $value ne $expected or $comp eq "ne" and $value eq $expected or $comp eq "re" and not $value =~ /$expected/ or $comp eq "nr" and $value =~ /$expected/
l!l&&r!l&&!r
000
$comp eq "eq" and $value ne $expected or $comp eq "ne" and $value eq $expected or $comp eq "re" and not $value =~ /$expected/
l!l&&r!l&&!r
000
$comp eq "eq" and $value ne $expected or $comp eq "ne" and $value eq $expected
l!l&&r!l&&!r
000
$comp eq "eq" and $value ne $expected
!ll&&!rl&&r
000
$comp eq "ne" and $value eq $expected
!ll&&!rl&&r
000
$comp eq "re" and not $value =~ /$expected/
!ll&&!rl&&r
000
$comp eq "nr" and $value =~ /$expected/
!ll&&!rl&&r
000
MC/DC0%0/8
$comp eq "eq" and $value ne $expected or $comp eq "ne" and $value eq $expected or $comp eq "re" and not $value =~ /$expected/ or $comp eq "nr" and $value =~ /$expected/
$comp eq "eq" $value ne $expected $comp eq "ne" $value eq $expected $comp eq "re" not $value =~ /$expected/ $comp eq "nr" $value =~ /$expected/
Truth table0%0/31
A and B or C and D or E and not(F) or G and H
A= $comp eq "eq"B= $value ne $expectedC= $comp eq "ne"D= $value eq $expectedE= $comp eq "re"F= not $value =~ /$expected/G= $comp eq "nr"H= $value =~ /$expected/
ABCDEFGHresult
11XXXXXX1
0X11XXXX1
1011XXXX1
0X0X11XX1
0X1011XX1
100X11XX1
101011XX1
0X0X0X111
0X0X10111
0X100X111
0X1010111
100X0X111
100X10111
10100X111
101010111
0X0X0X0X0
0X0X0X100
0X0X100X0
0X0X10100
0X100X0X0
0X100X100
0X10100X0
0X1010100
100X0X0X0
100X0X100
100X100X0
100X10100
10100X0X0
10100X100
1010100X0
101010100
326 $comp eq 'eq' && $value ne $expected ||
327 $comp eq 'ne' && $value eq $expected ||
328 $comp eq 're' && $value !~ m/$expected/ ||
329 $comp eq 'nr' && $value =~ m/$expected/
330 )
331 {
332 0 return 0;
333 }
334 }
335 0 return 1;
336 72 };
337  
338 # Goal: convert multivalues from arrays to strings:
339 # e.g. iset/gender = ["fem", "neut"] becomes iset/gender = "fem|neut"
340 # to enable storing in a PML file.
341 method serialize_iset => sub {
342 28 ▶ my ($self) = @_;
Subroutine serialize_iset: called CC=3 SCAR=16.5
343 28 foreach my $feature ( $self->$interset_attribute->get_nonempty_features() ) {
344 0 my $value = $self->get_iset($feature);
345 0 ▶ unless ( $value eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value eq "") 0 0 0
346 0 $self->set_attr("$interset_attribute/$feature", $value);
347 }
348 }
349 28 return;
350 72 };
351  
352  
353  
354 # Goal: convert multivalues from strings to arrays:
355 # e.g. iset/gender = "fem|neut" becomes iset/gender = ["fem", "neut"]
356 method deserialize_iset => sub {
357 28 ▶ my ($self) = @_;
Subroutine deserialize_iset: called CC=5 SCAR=20.9
358  
359 28 ▶ if (! $Treex::Core::Config::running_in_tred) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Treex::Core::Config::running_in_tred) 28 0 28
360 # iset
361 # ttred does not like arrayrefs so only unserilaize if not in ttred
362 28 ▶ if ($self->$interset_attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$interset_attribute) 28 0 28
363 # this looks a bit weird,
364 # but it ensures correct deserialization of multivalues,
365 # i.e. turning e.g. "fem|neut" into ["fem", "neut"]
366 28 $self->set_iset($self->$interset_attribute);
367 }
368 }
369  
370 # iset_dump
371 # (backward compatibility for files
372 # created when iset_dump was used to store iset)
373 28 ▶ if ( $self->{iset_dump} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"iset_dump"}) 0 28 28
374 0 $self->set_iset( eval "my " . $self->{iset_dump} . '; return $VAR1' ); ## no critic (ProhibitStringyEval)
375 # iset_dump is deprecated
376 0 delete $self->{iset_dump};
377 0 ▶ if ($Treex::Core::Config::running_in_tred) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Treex::Core::Config::running_in_tred) 0 0 0
378 # ttred does not like arrayrefs so serialize back to strings for it
379 0 $self->serialize_iset();
380 }
381 }
382  
383 28 return;
384 72 };
385  
386  
387 38 }; # end of "role {"
388  
389 38 1;
390  
391 __END__
392  
393 =encoding utf-8
394  
395 =head1 NAME
396  
397 Treex::Core::Node::Interset
398  
399 =head1 VERSION
400  
401 version 2.20151210
402  
403 =head1 DESCRIPTION
404  
405 Moose role for nodes that have the Interset feature structure.
406  
407 =head1 ATTRIBUTES
408  
409 =over
410  
411 =item iset/*
412  
413 Attributes corresponding to Interset features.
414  
415 =back
416  
417 =head1 METHODS
418  
419 =head2 Access to Interset features
420  
421 =over
422  
423 =item my $boolean = $node->match_iset('pos' => 'noun', 'gender' => '!masc', ...);
424  
425 Do the feature values of this node match the specification?
426 (Values of other features do not matter.)
427 A value preceded by exclamation mark is tested on string inequality.
428 A value preceded by a tilde is tested on regex match.
429 A value preceded by exclamation mark and tilde is tested on regex mismatch.
430 Other values are tested on string equality.
431  
432 =back
433  
434  
435 =head1 AUTHOR
436  
437 Dan Zeman <zeman@ufal.mff.cuni.cz>
438  
439 Martin Popel <popel@ufal.mff.cuni.cz>
440  
441 =head1 COPYRIGHT AND LICENSE
442  
443 Copyright © 2011, 2013, 2014, 2015 by Institute of Formal and Applied Linguistics, Charles University in Prague
444  
445 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
« Treex/Core/TredView/TreeLayout.pm ↑ Summary Treex/Core/DocumentReader/Base.pm »