Phonology/Word.pm

filter: Statementstmt 97.1% 203 / 209 Branchbran 89.1% 66 / 74 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 39 / 39 Podpod 0.0% 0 / 1 totaltotal 95.5% 318 / 333 CC 46 SCAR 13.5
CC
46
Cov
95%
next
9.2
_flatten
7.0
__ANON__
6.0
CRAP 3.8
« Phonology/Segment/Boundary.pm ↑ Summary Phonology.pm »
1 #!/usr/bin/perl
2  
3 # A Word contains circular references, so we make a very thin wrapper around
4 # it. Word itself is quite small, with WordWrapped doing most of the work.
5  
6 package Lingua::Phonology::Word;
7  
8 8 our $VERSION = 0.1;
9  
10 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 207 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
14 207 my $class = shift;
15 207 my $word = Lingua::Phonology::WordWrapped->new(@_);
16 207 bless \$word, $class;
17 }
18  
19 8 our $AUTOLOAD;
20 66 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=1 SCAR=0.0
21 66 my $self = shift;
22 66 my $method = $AUTOLOAD;
23 66 $method =~ s/.*:://;
24 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 66 ▶ *$method = sub { ${shift()}->$method(@_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 66 $self->$method(@_);
27 }
28  
29 21 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
30 21 ▶ return if not defined ${$_[0]};
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$_[0];} 0 21 21
31 21 ${shift()}->_release;
32 }
33  
34 package Lingua::Phonology::WordWrapped;
35  
36 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 8 ▶use Lingua::Phonology::Segment::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 8 ▶use Lingua::Phonology::Segment::Boundary;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 8 ▶use Lingua::Phonology::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 8 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
42 8 RIGHT => 0,
43 LEFT => 1
44 8 };
45  
46 207 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
47 207 my $class = shift;
48  
49 my $self = {
50 orig => \@_,
51 working => [],
52 curidx => 0,
53 curdom => 0,
54 direction => RIGHT,
55 domain => undef,
56 tier => undef,
57 41 ▶ filter => sub {1}
Subroutine __ANON__: called CC=1 SCAR=0.0
58 207 };
59 207 $self->{lbound} = Lingua::Phonology::Segment::Rules->new($self, Lingua::Phonology::Segment::Boundary->new());
60 207 $self->{rbound} = Lingua::Phonology::Segment::Rules->new($self, Lingua::Phonology::Segment::Boundary->new());
61  
62 207 bless $self, $class;
63 }
64  
65 my %valid = (
66 217 ▶ direction => sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
67 217 $_[0] = lc $_[0];
68 217 ▶ return LEFT if $_[0] eq 'leftward';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "leftward" 57 160 217
69 160 ▶ return RIGHT if $_[0] eq 'rightward';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "rightward" 160 0 160
70 0 return;
71 },
72 0 ▶ filter => sub { return $_[0] if _is $_[0], 'CODE'; return; },
Subroutine __ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if &_is($_[0], "CODE") 13 0 13
73 14 ▶ tier => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
74 14 ▶ domain => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 0 ▶ rule => sub { return $_[0] if _is $_[0], 'HASH'; return; }
Subroutine __ANON__: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if &_is($_[0], "HASH") 145 0 145
76 8 );
77  
78 8 for my $method (keys %valid) {
79 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1675 ▶ *$method = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
81 1675 my $self = shift;
82 1675 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 984 691 1675
83 984 ▶ if (not defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0]) { } 581 403 984
84 581 delete $self->{$method};
85 }
86 else {
87 403 my $ok = $valid{$method}->(@_);
88 403 ▶ if (defined $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ok) { } 403 0 403
89 403 $self->{$method} = $ok;
90 }
91 else {
92 0 return;
93 }
94 }
95 }
96 1675 $self->{resync} = 1;
97 1675 ▶ return $self->{$method} unless $method eq 'direction';
Branch: 1 branch
BranchTrueFalseTotal
unless $method eq "direction" 1269 406 1675
98 406 ▶ return $self->{$method} == RIGHT ? 'rightward' : 'leftward';
Branch: 1 branch
BranchTrueFalseTotal
$self->{$method} == 0 ? : 290 116 406
99 40 };
100 }
101  
102  
103 # Call this func with an array ref
104 153 ▶sub set_segs {
Subroutine set_segs: called CC=3 SCAR=11.0
105 153 my ($self, @ary) = @_;
106 # Return undef and set $@ for errors
107 153 for (@ary) {
108 1214 ▶ unless (_is_seg $_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_is_seg($_)) 1 1213 1214
109 1 $@ = "Element in array not a segment";
110 1 return;
111 }
112 }
113  
114 152 $self->{orig} = \@ary;
115 152 $self->_rehash;
116 152 $self->{resync} = 0;
117 152 $self->_prepare;
118 152 $self->reset;
119 }
120  
121 # Reset the iterator
122 152 ▶sub reset {
Subroutine reset: called CC=2 SCAR=6.9
123 152 my $self = shift;
124 152 $self->{curdom} = 0;
125 152 ▶ if ($self->{direction} == RIGHT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'direction'} == 0) { } 104 48 152
126 104 $self->{curidx} = 0;
127 }
128 else {
129 48 $self->{curidx} = $#{$self->{working}[0]};
130 }
131 152 $self->{first} = 1;
132 152 1;
133 }
134  
135 # Advance to the next segment
136 1333 ▶sub next {
Subroutine next: called CC=9 SCAR=22.2
137 1333 my $self = shift;
138  
139 # We should resync if needed before moving the iterator
140 1333 ▶ $self->_resync if $self->{resync};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"resync"} 159 1174 1333
141  
142 1333 ▶ if ($self->{first}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'first'}) { } 151 1182 1333
143 151 $self->{first} = 0;
144 }
145 else {
146 1182 ▶ if ($self->{direction} == RIGHT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'direction'} == 0) { } 765 417 1182
elsif ($self->{'direction'} == 1) { } 417 0 417
147 765 ▶ if (not defined $self->{working}[$self->{curdom}][++$self->{curidx}]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"working"}[$self->{"curdom"}][++$self->{"curidx"}]) 101 664 765
148 101 ▶ return unless defined $self->{working}[++$self->{curdom}];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"working"}[++$self->{"curdom"}] 97 4 101
149 4 $self->{curidx} = 0;
150 }
151 }
152 elsif ($self->{direction} == LEFT) {
153 417 ▶ if (--$self->{curidx} < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$self->{"curidx"} < 0) 48 369 417
154 48 ▶ return unless defined $self->{working}[++$self->{curdom}];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"working"}[++$self->{"curdom"}] 48 0 48
155 0 $self->{curidx} = $#{$self->{working}[$self->{curdom}]};
156 }
157 }
158 }
159 1188 return 1;
160 }
161  
162 146 ▶sub get_orig_segs {
Subroutine get_orig_segs: called CC=1 SCAR=0.0
163 146 return @{$_[0]->{orig}};
164 }
165  
166 1193 ▶sub get_working_segs {
Subroutine get_working_segs: called CC=2 SCAR=6.9
167 1193 my $self = shift;
168 1193 ▶ $self->_resync if ($self->{resync});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"resync"} 5 1188 1193
169 return
170 1193 @{$self->{working}[$self->{curdom}]}[$self->{curidx} .. $#{$self->{working}[$self->{curdom}]}],
171 $self->{rbound},
172 $self->{lbound},
173 1193 @{$self->{working}[$self->{curdom}]}[0 .. ($self->{curidx} - 1)];
174 }
175  
176 # Clear out current segments
177 166 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
178 166 my $self = shift;
179 166 $self->{orig} = [];
180 166 $self->{working} = [];
181 166 1;
182 }
183  
184  
185 # Called by child segments, inserts a segment into the word
186 15 ▶sub _insert {
Subroutine _insert: called CC=3 SCAR=11.0
187 15 my ($self, $id, $pos, $ins) = @_;
188  
189 # Adjust position according to the slot
190 15 ▶ if ($id == $self->{lbound}->_getid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($id == $self->{'lbound'}->_getid) { } 1 14 15
elsif ($id == $self->{'rbound'}->_getid) { } 4 10 14
191 1 unshift @{$self->{orig}}, $ins;
192 }
193 elsif ($id == $self->{rbound}->_getid) {
194 4 push @{$self->{orig}}, $ins;
195 }
196 else {
197 10 $pos += $self->{slot}{$id};
198 10 splice @{$self->{orig}}, $pos, 0, $ins;
199 }
200  
201 15 $self->_rehash;
202 }
203  
204 # Called by child segments, removes a segment from the word
205 6 ▶sub _delete {
Subroutine _delete: called CC=1 SCAR=0.0
206 6 my ($self, $id) = @_;
207 6 splice @{$self->{orig}}, $self->{slot}{$id}, 1;
208 6 $self->_rehash;
209 }
210  
211 # Rebuild the ref => index hash
212 173 ▶sub _rehash {
Subroutine _rehash: called CC=2 SCAR=6.9
213 173 my $self = shift;
214  
215 173 my $count;
216 173 for (@{$self->{orig}}) {
217 1294 $self->{slot}{int $_} = $count++;
218 }
219 173 $self->{resync} = 1;
220 }
221  
222 164 ▶sub _resync {
Subroutine _resync: called CC=3 SCAR=11.0
223 164 my $self = shift;
224  
225 # When tier or domain is in effect, ignore
226 164 ▶ unless ($self->{tier} || $self->{domain}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"tier"} or $self->{"domain"}) 143 21 164
Condition100%3/3
$self->{"tier"} or $self->{"domain"}
l!l&&r!l&&!r
192143
MC/DC100%2/2
$self->{"tier"} or $self->{"domain"}
$self->{"tier"} $self->{"domain"}
Truth table100%3/3
A or B
A= $self->{"tier"}B= $self->{"domain"}
ABresult
1X1
011
000
227 # Get the id of the seg at our current position
228 143 my $oldid = $self->{working}[$self->{curdom}][$self->{curidx}]->_getid;
229  
230 # Rebuild the working hash
231 143 $self->_prepare;
232  
233 # Find out where we left off
234 143 $self->_find($oldid);
235 }
236 164 $self->{resync} = 0;
237 }
238  
239  
240 # Find out where our current segment now is
241 143 ▶sub _find {
Subroutine _find: called CC=4 SCAR=13.9
242 143 my $self = shift;
243  
244 143 my $oldid = shift;
245 143 for my $outer (0 .. $#{$self->{working}}) {
246 143 for (0 .. $#{$self->{working}[$outer]}) {
247 422 ▶ if ($self->{working}[$outer][$_]->_getid == $oldid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"working"}[$outer][$_]->_getid == $oldid) 140 282 422
248 140 $self->{curdom} = $outer;
249 140 $self->{curidx} = $_;
250 140 return 1;
251 }
252 }
253 }
254 # Getting here indicates that we couldn't find the working seg--it was
255 # probably deleted. So do nothing, and hope where we left off is okay
256 3 1;
257 }
258
259 # Set up $self->{working}
260 295 ▶sub _prepare {
Subroutine _prepare: called CC=5 SCAR=16.1
261 295 my $self = shift;
262 295 $self->{working} = [];
263 295 for (_make_domain($self->{domain}, @{$self->{orig}})) {
264 299 my @sect = map { Lingua::Phonology::Segment::Rules->new($self, $_) } _make_tier($self->{tier}, @$_);
265  
266 299 my @keep;
267 299 ▶ if ($self->{filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'filter'}) { } 32 267 299
268 32 push @sect, $self->{rbound}, $self->{lbound};
269 32 for (0 .. ($#sect - 2)) {
270 175 ▶ push @keep, $sect[0] if $self->{filter}->(@sect);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"filter"}->(@sect) 171 4 175
271 175 push @sect, (shift @sect);
272 }
273 }
274 else {
275 267 @keep = @sect;
276 }
277 299 push @{$self->{working}}, \@keep;
278 }
279 }
280  
281 # Make a domain
282 315 ▶sub _make_domain ($@) {
Subroutine _make_domain: called CC=5 SCAR=16.1
283 315 my $domain = shift;
284 315 ▶ return (\@_) if not defined $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $domain 275 40 315
285 40 my @return = ();
286  
287 40 my $i = 0;
288 40 ▶ while ($i < scalar @_) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 156 40 196
289 156 my @domain = ($_[$i]);
290  
291 # Keep adding segments as long as they have the same reference for $domain
292 8 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
293 156 ▶ while (defined $_[$i + 1] &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[$i + 1] and _flatten([$_[$i]->value_ref($domain)]) eq _flatten([$_[$i + 1]->value_ref($domain)])) 123 156 279
Condition100%3/3
defined $_[$i + 1] and _flatten([$_[$i]->value_ref($domain)]) eq _flatten([$_[$i + 1]->value_ref($domain)])
!ll&&!rl&&r
40116123
MC/DC100%2/2
defined $_[$i + 1] and _flatten([$_[$i]->value_ref($domain)]) eq _flatten([$_[$i + 1]->value_ref($domain)])
defined $_[$i + 1] _flatten([$_[$i]->value_ref($domain)]) eq _flatten([$_[$i + 1]->value_ref($domain)])
Truth table100%3/3
A and B
A= defined $_[$i + 1]B= _flatten([$_[$i]->value_ref($domain)]) eq _flatten([$_[$i + 1]->value_ref($domain)])
ABresult
0X0
100
111
294 _flatten([$_[$i]->value_ref($domain)]) eq _flatten([$_[$i+1]->value_ref($domain)])) {
295 123 $i++;
296 123 push (@domain, $_[$i]);
297 }
298  
299 156 push (@return, \@domain);
300 156 $i++;
301 }
302  
303 40 return @return;
304 }
305  
306 # A quick func to flatten hashrefs into easily comparable strings
307 4026 ▶sub _flatten {
Subroutine _flatten: called CC=7 SCAR=19.5
308 4026 my ($ref, $seen) = @_;
309 4026 ▶ return '' if not defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 910 3116 4026
310 3116 ▶ $seen = {} if not $seen;
Branch: 1 branch
BranchTrueFalseTotal
unless $seen 478 2638 3116
311 3116 ▶ if (ref $ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ref) 3116 0 3116
312 3116 ▶ return $ref if exists $seen->{$ref};
Branch: 1 branch
BranchTrueFalseTotal
if exists $seen->{$ref} 466 2650 3116
313 2650 $seen->{$ref} = undef;
314 }
315  
316 2650 ▶ if (_is $ref, 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_is($ref, "ARRAY")) 1342 1308 2650
317 1342 return join '', map { _flatten($_, $seen) } @$ref;
318 }
319 1308 ▶ if (_is($ref, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_is($ref, "HASH")) 432 876 1308
320 432 return join '', map { $_, _flatten($ref->{$_}, $seen) } sort keys %$ref;
321 }
322 876 return "$ref";
323 }
324  
325 299 ▶sub _make_tier {
Subroutine _make_tier: called CC=2 SCAR=6.9
326 299 my $tier = shift;
327 299 ▶ return @_ if not defined $tier;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tier 279 20 299
328 132 return map { Lingua::Phonology::Segment::Tier->new(@$_) }
329 20 _make_domain $tier, grep { defined $_->value($tier) }
330 @_;
331 }
332  
333 # Prepare for destruction
334 21 ▶sub _release {
Subroutine _release: called CC=1 SCAR=0.0
335 21 my $self = shift;
336 21 $self->clear;
337 21 delete $self->{lbound};
338 21 delete $self->{rbound};
339 }
340  
341 8 1;
« Phonology/Segment/Boundary.pm ↑ Summary Phonology.pm »