Phonology/Segment.pm

filter: Statementstmt 95.7% 136 / 142 Branchbran 86.5% 45 / 52 Conditioncond 91.3% 21 / 23 MC/DCmcdc 71.4% 10 / 14 Subroutinesub 96.3% 26 / 27 Podpod 91.6% 11 / 12 totaltotal 92.2% 249 / 270 CC 39 SCAR 21.1
CC
39
Cov
93%
value_ref
20.4
new
6.0
_children
6.0
CRAP 8.3
« Phonology/Symbols.pm ↑ Summary Phonology/Rules.pm »
1 #!/usr/bin/perl -w
2  
3 package Lingua::Phonology::Segment;
4  
5 =head1 NAME
6  
7 Lingua::Phonology::Segment - a module to represent a segment as a bundle
8 of feature values.
9  
10 =head1 SYNOPSIS
11  
12 use Lingua::Phonology;
13 $phono = new Lingua::Phonology;
14  
15 # Define a feature set
16 $features = $phono->features;
17 $features->loadfile;
18  
19 # Make a segment
20 $segment = $phono->segment;
21  
22 # Set some values
23 $segment->labial(1);
24 $segment->continuant(0);
25 $segment->voice(1);
26 # Segment is now voiced labial stop, i.e. [b]
27  
28 # Reset the segment
29 $segment->clear;
30  
31 =head1 DESCRIPTION
32  
33 A Lingua::Phonology::Segment object provides a programmatic representation
34 of a linguistic segment. Such a segment is associated with a
35 Lingua::Phonology::Features object that lists the available features and
36 the relationships between them. The segment itself is a list of the values
37 for those features. This module provides methods for returning and setting
38 these feature values. A segment may also be associated with a
39 Lingua::Phonology::Symbols object, which allows the segment to return the
40 symbol that it best matches.
41  
42 =cut
43  
44 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 9 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 9 ▶use Lingua::Phonology::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 9 ▶use Lingua::Phonology::Features;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 9 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 9 REF => 0,
51 NUM => 1,
52 TXT => 2
53 9 };
54  
55 # Magical stuff:
56 # Automatically spell segments in string context
57 use overload
58 # The fun stuff
59 480 ▶ '""' => sub { defined $_[0]->{SYMBOLS} ? $_[0]->spell : overload::StrVal($_[0]) },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'SYMBOLS'} ? : 480 0 480
60 2 ▶ 'cmp' => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
61 2 my ($l, $r, $swap) = @_;
62 0 ▶ if ($swap) { return "$r" cmp "$l" }
Branch: 1 branch
BranchTrueFalseTotal
if ($swap) { } 0 2 2
63 2 else { return "$l" cmp "$r" } },
64
65 # A rediculous hack to return the non-overloaded number value. In theory,
66 # '0+' => sub { $_[0] } *should* do this, but it makes the debugger
67 # segfault. This procedure is borrowed from overload.pm itself.
68 4218 ▶ '0+' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
69 4218 my $package = ref $_[0];
70 4218 bless $_[0], 'my::Fake';
71 4218 my $rv = int $_[0];
72 4218 bless $_[0], $package;
73 4218 return $rv },
74 9 'fallback' => 1;
75  
76 9 our $VERSION = 0.3;
77  
78 3 ▶sub err ($) { warnings::warnif(shift); return; }
Subroutine err: called CC=1 SCAR=0.0
Pod undocumented
79  
80 # New segment
81 849 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
82 849 my $proto = shift;
83 849 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1666830
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
84 849 my $self = {
85 FEATURES => undef,
86 SYMBOLS => undef,
87 WANT => REF, # REF, NUM, or TXT
88 VALUES => { }
89 };
90  
91 849 my $featureset = shift; # An object in class Features
92 849 my $values = shift; # A hashref
93  
94 # When an object method, copy feature and symbol settings
95 849 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) { } 166 683 849
96 166 ▶ $self->{FEATURES} = $featureset || $proto->{FEATURES};
Condition66%2/3
$featureset || $proto->{'FEATURES'}
l!l&&r!l&&!r
31630
MC/DC0%0/2
$featureset || $proto->{'FEATURES'}
$featureset $proto->{'FEATURES'}
Truth table66%2/3
A || B
A= $featuresetB= $proto->{'FEATURES'}
ABresult
1X1
011
000
97 166 $self->{SYMBOLS} = $proto->{SYMBOLS};
98 }
99  
100 # When a class method
101 else {
102 683 $self->{FEATURES} = $featureset;
103 }
104  
105 # Require a $featureset of the proper type
106 849 ▶ return err("No featureset (or bad featureset) given for new $class") unless _is_features($self->{FEATURES});
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_features($self->{"FEATURES"}) 2 847 849
107  
108 # Gesundheit
109 847 bless $self, $class;
110  
111 # Set initial values
112 847 $self->value_ref($_, $values->{$_}) for keys %$values;
113  
114 847 return $self;
115 }
116  
117 725 ▶sub featureset {
Subroutine featureset: called CC=3 SCAR=11.0
Pod documented
118 725 my $self = shift;
119 725 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 723 725
120 2 ▶ return err("Bad feature set") unless _is_features($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_features($_[0]) 0 2 2
121 2 $self->{FEATURES} = shift;
122 }
123 725 return $self->{FEATURES};
124 }
125  
126 702 ▶sub symbolset {
Subroutine symbolset: called CC=3 SCAR=11.0
Pod documented
127 702 my $self = shift;
128 702 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 685 17 702
129 685 ▶ return err("Bad symbol set") unless _is_symbols($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless &_is_symbols($_[0]) 0 685 685
130 685 $self->{SYMBOLS} = shift;
131 }
132 702 return $self->{SYMBOLS};
133 }
134  
135 # These functions locally set WANT, so that the structure generated in
136 # value_ref() can be built the right way the first time
137  
138 4479 ▶sub value {
Subroutine value: called CC=1 SCAR=0.0
Pod documented
139 4479 my $self = shift;
140 4479 local $self->{WANT} = NUM;
141 4479 $self->value_ref(@_);
142 }
143  
144 3 ▶sub value_text {
Subroutine value_text: called CC=1 SCAR=0.0
Pod documented
145 3 my $self = shift;
146 3 local $self->{WANT} = TXT;
147 3 $self->value_ref(@_);
148 }
149  
150 22092 ▶sub value_ref {
Subroutine value_ref: called CC=20 SCAR=30.2
Pod documented
151 22092 my ($self, $feature, $val, $hash) = @_;
152 22092 ▶ return unless $self->{FEATURES}->feature($feature);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"FEATURES"}->feature($feature) 0 22092 22092
153  
154 # Assign primary values, checking the size of @_ because $val could be
155 # undef. Skip this part if $val is undef and we have 4 args
156 22092 ▶ if (@_ > 2 and not (@_ > 3 and not defined $val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2 and not @_ > 3 && !defined($val)) 3706 18386 22092
Condition100%6/6
@_ > 2 and not @_ > 3 && !defined($val)
!ll&&!rl&&r
1838423706
@_ > 3 && !defined($val)
!ll&&!rl&&r
3679272
MC/DC100%3/3
@_ > 2 and not @_ > 3 && !defined($val)
@_ > 2 @_ > 3 !defined($val)
Truth table100%4/4
A and not(B && not(C))
A= @_ > 2B= @_ > 3C= !defined($val)
ABCresult
0XX0
1110
10X1
1101
157  
158 # If given a plain scalar ref, replace the old ref
159 3706 ▶ if (ref($val) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'SCALAR') { } 101 3605 3706
160 # This errs if $$val is not an lvalue
161 101 eval {
162 101 $$val = $self->{FEATURES}->number_form($feature, $$val);
163 };
164  
165 # ...in which case we work around
166 101 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 2 99 101
167 2 my $nval = $self->{FEATURES}->number_form($feature, $$val);
168 2 $self->{VALUES}->{$feature} = \$nval;
169 }
170 else {
171 99 $self->{VALUES}->{$feature} = $val;
172 }
173 }
174  
175 # Otherwise, change the val via the current ref
176 else {
177 3605 $val = $self->{FEATURES}->number_form($feature, $val);
178  
179 # If this feature is already defined, assign via existing ref
180 3605 ▶ if (my $ref = $self->{VALUES}->{$feature}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $self->{'VALUES'}{$feature}) { } 270 3335 3605
181 270 $$ref = $val;
182 }
183
184 # If it's not defined, assign as a ref
185 else {
186 3335 $self->{VALUES}->{$feature} = \$val;
187 }
188  
189 }
190 }
191  
192 # Assign child values
193 22092 ▶ HASH: if (@_ > 3 && defined $hash) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3 and defined $hash) 1 22091 22092
Condition100%3/3
@_ > 3 and defined $hash
!ll&&!rl&&r
22063281
MC/DC100%2/2
@_ > 3 and defined $hash
@_ > 3 defined $hash
Truth table100%3/3
A and B
A= @_ > 3B= defined $hash
ABresult
0X0
100
111
194 1 ▶ unless (_is($hash, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_is($hash, "HASH")) 0 1 1
195 0 err("Third argument to value() must be a hash reference");
196 0 last HASH;
197 }
198  
199 # Get children that are also in our hash (implicitly ignoring bad children in $hash)
200 1 for (grep { exists $hash->{$_} } $self->{FEATURES}->children($feature)) {
201 # For pairs like $feature => [ $val, { child vals } ]
202 1 ▶ if (_is($hash->{$_}, 'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_is($hash->{$_}, 'ARRAY')) { } 0 1 1
203 0 $self->value_ref($_, @{$hash->{$_}});
204 }
205  
206 # For pairs like $feature => $val
207 else {
208 1 $self->value_ref($_, $hash->{$_});
209 }
210 }
211 }
212  
213 # Quit in void context
214 22092 ▶ return if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 3574 18518 22092
215  
216 # Find the return value or a ref to undef
217 18518 ▶ my $retval = $self->{VALUES}->{$feature} || \undef;
Condition100%2/2
$self->{'VALUES'}{$feature} || \(undef)
l!l
424114277
MC/DC100%1/1
$self->{'VALUES'}{$feature} || \(undef)
$self->{'VALUES'}{$feature}
Truth table100%2/2
A
A= $self->{'VALUES'}{$feature}
Aresult
11
00
218 {
219 # Less strictness here for when $retval is undef
220 #no strict 'refs';
221 #no warnings 'uninitialized';
222  
223 18518 ▶ if ($self->{WANT} == TXT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'WANT'} == 2) { } 6 18512 18518
elsif ($self->{'WANT'} == 1) { } 16777 1735 18512
224 6 $retval = $self->{FEATURES}->text_form($feature, $$retval);
225 }
226 elsif ($self->{WANT} == NUM) {
227 16777 $retval = $$retval;
228 }
229 }
230  
231 # Return these in scalar context
232 18518 ▶ if (not wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 4002 14516 18518
233 # Return the actual value if the feature is defined
234 4002 ▶ return $retval if exists $self->{VALUES}->{$feature} && defined ${$self->{VALUES}->{$feature}};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"VALUES"}{$feature} and defined ${$self->{"VALUES"}{$feature};} 1587 2415 4002
Condition100%3/3
exists $self->{"VALUES"}{$feature} and defined ${$self->{"VALUES"}{$feature};}
!ll&&!rl&&r
2360551587
MC/DC100%2/2
exists $self->{"VALUES"}{$feature} and defined ${$self->{"VALUES"}{$feature};}
exists $self->{"VALUES"}{$feature} defined ${$self->{"VALUES"}{$feature};}
Truth table100%3/3
A and B
A= exists $self->{"VALUES"}{$feature}B= defined ${$self->{"VALUES"}{$feature};}
ABresult
0X0
100
111
235  
236 # Otherwise return the child hashref
237 2415 return $self->_children($feature);
238 }
239  
240 # Get the hashref to return if we want an array
241 14516 my $rethash = $self->_children($feature);
242 14516 return $retval => $rethash; # if (defined $rethash || defined $retval);
243  
244 # When everything's undef
245 0 return;
246 }
247  
248 # Build a hashref of child values, or undef if you're childless
249 16931 ▶sub _children {
Subroutine _children: called CC=6 SCAR=17.9
250 16931 my ($self, $feature) = @_;
251 16931 my $rethash = {};
252  
253 # Nodes w/ children
254 16931 ▶ if (my @kids = $self->{FEATURES}->children($feature)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@kids) = $self->{'FEATURES'}->children($feature)) { } 6279 10652 16931
255 6279 for (@kids) {
256 13935 my ($val, $kids) = $self->value_ref($_);
257 13935 ▶ if (defined $val || defined $kids) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val or defined $kids) 6101 7834 13935
Condition100%3/3
defined $val or defined $kids
l!l&&r!l&&!r
307930227834
MC/DC100%2/2
defined $val or defined $kids
defined $val defined $kids
Truth table100%3/3
A or B
A= defined $valB= defined $kids
ABresult
1X1
011
000
258 6101 $rethash->{$_} = [ $val, $kids ];
259 }
260 }
261 }
262  
263 # Terminal nodes
264 else {
265 10652 return;
266 }
267  
268 # Nodes with no defined children
269 6279 ▶ return if not keys %$rethash;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$rethash 1972 4307 6279
270  
271 # Normal case
272 4307 return $rethash;
273 }
274  
275 1662 ▶sub delink {
Subroutine delink: called CC=2 SCAR=6.9
Pod documented
276 1662 my $self = shift;
277 1662 my @return = ();
278 1662 for (@_) {
279 1471 push @return, delete($self->{VALUES}->{$_});
280 1471 push @return, $self->delink($self->{FEATURES}->children($_));
281 }
282 1662 return @return;
283 }
284  
285 6589 ▶sub all_values {
Subroutine all_values: called CC=2 SCAR=6.9
Pod documented
286 6589 my $self = shift;
287  
288 # Get the real values for each feature
289 6589 my %h = map { $_ => ${$self->{VALUES}->{$_}} } keys %{$self->{VALUES}};
290 6589 ▶ return wantarray ? %h : \%h;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6572 17 6589
291 }
292  
293 542 ▶sub spell {
Subroutine spell: called CC=2 SCAR=6.9
Pod documented
294 542 my ($self) = @_;
295  
296 542 ▶ return err "No symbol set defined for spell()" if not $self->{SYMBOLS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"SYMBOLS"} 1 541 542
297 541 return $self->{SYMBOLS}->spell($self);
298 }
299  
300 157 ▶sub duplicate {
Subroutine duplicate: called CC=1 SCAR=0.0
Pod documented
301 157 my $self = shift;
302 157 return $self->new(undef, { $self->all_values });
303 }
304  
305 5 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
306 5 my $self = shift;
307 5 $self->{VALUES} = {};
308 5 return 1;
309 }
310  
311 # Allows you to call changes to feature settings directly
312 # with syntax like $segment->feature_name($value)
313 9 our $AUTOLOAD;
314 70 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
315 70 my $feature = $AUTOLOAD;
316 70 $feature =~ s/.*:://;
317 70 my $self = shift;
318  
319 9 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
320 4278 ▶ *$feature = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
321 4278 my $self = splice @_, 0, 1, $feature;
322 4278 $self->value(@_);
323 70 };
324  
325 70 $self->$feature(@_);
326 }
327  
328 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
329  
330 9 1;
331  
332 __END__
« Phonology/Symbols.pm ↑ Summary Phonology/Rules.pm »