Phonology/Rules.pm

filter: Statementstmt 96.3% 317 / 329 Branchbran 89.5% 111 / 124 Conditioncond 66.6% 10 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 50 / 50 Podpod 91.6% 11 / 12 totaltotal 93.1% 503 / 540 CC 88 SCAR 19.1
CC
88
Cov
94%
_load_from_struct
19.6
_parse_ling
15.0
_check_rule
7.0
CRAP 6.7
« Phonology/Segment.pm ↑ Summary Phonology/Features.pm »
1 #!/usr/bin/perl
2  
3 package Lingua::Phonology::Rules;
4  
5 =head1 NAME
6  
7 Lingua::Phonology::Rules - a module for defining and applying
8 phonological rules.
9  
10 =head1 SYNOPSIS
11  
12 use Lingua::Phonology;
13 $phono = new Lingua::Phonology;
14  
15 $rules = $phono->rules;
16  
17 # Adding and manipulating rules is discussed in the "WRITING RULES"
18 # section
19  
20 =head1 DESCRIPTION
21  
22 This module allows for the creation of linguistic rules, and the application of
23 those rules to "words" of Segment objects. You, the user, add rules to a Rules
24 object, defining various parameters and code references that actually perform
25 the action of the rule. Lingua::Phonology::Rules will take care of the guts of
26 applying and creating rules.
27  
28 The rules you create may have the following parameters. This is just a brief
29 description of the parameters--a more detailed discussion of their effect is in
30 the L<"WRITING RULES"> section.
31  
32 =over 4
33  
34 =item * domain
35  
36 Defines the domain within which the rule applies. This should be
37 the name of a feature in the featureset of the segments which the rule is
38 applied to.
39  
40 =item * tier
41  
42 Defines the tier on which the rule applies. Must be the name of a
43 feature in the feature set for the segments of the word you pass in.
44  
45 =item * direction
46  
47 Defines the direction that the rule applies in. Must be
48 either 'leftward' or 'rightward.' If no direction is given, defaults to
49 'rightward'.
50  
51 =item * filter
52  
53 Defines a filter for the segments that the rule applies on.
54 Must a code reference that returns a truth value.
55  
56 =item * linguistic
57  
58 Defines a linguistic-style rule to be parsed. When you provide a
59 linguistic-style rule, it is parsed into code references that take the place of
60 the C<where> and C<do> properties listed below. The format of linguistic rules
61 is described in L<Lingua::Phonology::FileFormatPOD/"LINGUISTIC-STYLE RULES">.
62  
63 =item *
64  
65 B<where> - defines the condition or conditions where the rule applies. Must be a
66 coderef that returns a truth value. If no value is given, defaults to
67 always true.
68  
69 =item *
70  
71 B<do> - defines the action to take when the C<where> condition is met. Must be
72 a code reference. If no value is given, does nothing.
73  
74 =back
75  
76 Lingua::Phonology::Rules is flexible and powerful enough to handle any
77 sequential type of rule system. It cannot handle Optimality Theory-style
78 processes, because those require a fundamentally different kind of
79 algorithm.
80  
81 =cut
82  
83 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 8 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 8 ▶use Lingua::Phonology::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 8 ▶use Lingua::Phonology::Word;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 8 ▶use Lingua::Phonology::Segment::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 8 ▶use Lingua::Phonology::Segment::Boundary;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 8 ▶use Lingua::Phonology::Segment::Tier;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 8 our $VERSION = 0.3;
94  
95 23 ▶sub err ($) { _err($_[0]) if warnings::enabled() };
Subroutine err: called CC=2 SCAR=7.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled() 0 23 23
96  
97 # This variable is created the first time someone tries to parse a lingustic
98 # rule, and reused thereafter
99 8 our $PARSER;
100  
101 # Define valid properties for rules, name => default format. undef's for no default
102 our %property = (
103 175 ▶ where => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
104 39 ▶ do => sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
105 8 tier => undef,
106 filter => undef,
107 result => undef,
108 domain => undef,
109 direction => 'rightward'
110 );
111  
112 # Hash of property => validating coderef
113 our %valid = (
114 389 ▶ where => sub { _is($_[0], 'CODE') },
Subroutine __ANON__: called CC=1 SCAR=0.0
115 395 ▶ do => sub { _is($_[0], 'CODE') },
Subroutine __ANON__: called CC=1 SCAR=0.0
116 16 ▶ result => sub { _is($_[0], 'CODE') },
Subroutine __ANON__: called CC=1 SCAR=0.0
117 13 ▶ filter => sub { _is $_[0], 'CODE' },
Subroutine __ANON__: called CC=1 SCAR=0.0
118 12 ▶ tier => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
119 13 ▶ domain => sub {1},
Subroutine __ANON__: called CC=1 SCAR=0.0
120 89 ▶ direction => sub { $_[0] = lc $_[0]; $_[0] eq 'rightward' || $_[0] eq 'leftward' }
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "rightward" or $_[0] eq "leftward" 41 48 89
121 8 );
122  
123 # List of properties passed on to Lingua::Phonology::Word
124 8 our %worder = (
125 filter => undef,
126 tier => undef,
127 domain => undef,
128 direction => undef
129 );
130  
131 8 for my $method (keys %worder) {
132 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
133 1532 ▶ *$method = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
134 1532 my $self = shift;
135 1532 my $rule = shift;
136 1532 ▶ return err "No such rule '$rule'" unless exists $self->{RULES}->{$rule};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RULES"}{$rule} 4 1528 1532
137 1528 $self->{RULES}->{$rule}->{word}->$method(@_);
138 }
139 32 }
140  
141 # Additional arrays that list properties expecting code and text respectively
142 8 our @code = qw/where do filter result/;
143 8 our @text = qw/tier domain direction/;
144  
145 # Build accessors for properties
146 8 foreach my $method (keys %valid) {
147 56 ▶ next if exists $worder{$method};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Lingua::Phonology::Rules::worder{$method} 32 24 56
148 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
149 1153 ▶ *$method = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
150 1153 my $self = shift;
151 1153 my $rule = shift;
152 1153 ▶ return err "No such rule '$rule'" unless exists $self->{RULES}->{$rule};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RULES"}{$rule} 2 1151 1153
153 1151 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 620 531 1151
154 # When defined, check for validity and add
155 620 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 422 198 620
156 422 ▶ if ($valid{$method}->($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Lingua::Phonology::Rules::valid{$method}->($_[0])) { } 422 0 422
157 422 $self->{RULES}->{$rule}->{$method} = $_[0];
158 }
159 else {
160 0 return err "Bad argument to $method()";
161 }
162 }
163 # Otherwise, delete the key
164 else {
165 198 delete $self->{RULES}->{$rule}->{$method};
166 }
167 }
168 1151 return $self->{RULES}->{$rule}->{$method};
169 24 };
170 }
171  
172 # Constructor
173 36 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
174 36 my $proto = shift;
175 36 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
1350
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
176 36 my $self = {
177 RULES => { }, # list of rules
178 ORDER => [ ], # rule order
179 PERSIST => [ ], # list of persistent rules
180 COUNT => 0, # count of times a rule applied, set by apply or apply_all
181 };
182 36 bless ($self, $class);
183 36 return $self;
184 }
185  
186 # Add a rule. Called as $rules->add_rule( Name => { ... } );
187 67 ▶sub add_rule {
Subroutine add_rule: called CC=4 SCAR=13.9
Pod documented
188 67 my ($self, %rules) = @_;
189 67 my $err = 0;
190  
191 67 RULE: for my $rule (keys(%rules)) {
192 # Check rules or complain
193 217 ▶ $self->_check_rule($rules{$rule}, $rule) or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->_check_rule($rules{$rule}, $rule) or do { $err = 1; next RULE } 205 12 217
194 12 $err = 1;
195 12 next RULE;
196 };
197  
198 # Drop existing rules
199 205 $self->drop_rule($rule);
200  
201 # Add new rules
202 205 $self->_add_rule($rule, $rules{$rule});
203  
204 }
205 67 ▶ return $err ? () : 1;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 12 55 67
206 }
207  
208 # Drop a rule
209 213 ▶sub drop_rule {
Subroutine drop_rule: called CC=2 SCAR=6.9
Pod documented
210 213 my $self = shift;
211 213 delete $self->{RULES}->{$_} for @_;
212 213 return scalar @_;
213 }
214  
215 # Like add_rule, but check that the rule exists
216 2 ▶sub change_rule {
Subroutine change_rule: called CC=5 SCAR=16.3
Pod documented
217 2 my ($self, %rules) = @_;
218 2 my $err = 0;
219  
220 2 RULE: for my $rule (keys %rules) {
221 # Complain when the rule doesn't exist
222 2 ▶ if (not exists $self->{RULES}->{$rule}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"RULES"}{$rule}) 1 1 2
223 1 err("No such rule '$rule'");
224 1 $err = 1;
225 1 next RULE;
226 }
227  
228 # Check rules
229 1 ▶ $self->_check_rule($rules{$rule}, $rule) or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->_check_rule($rules{$rule}, $rule) or do { $err = 1; next RULE } 1 0 1
230 0 $err = 1;
231 0 next RULE;
232 };
233  
234 # Add rules
235 1 $self->_add_rule($rule, $rules{$rule});
236 }
237 2 ▶ return $err ? () : 1;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 1 1 2
238 }
239  
240 218 ▶sub _check_rule {
Subroutine _check_rule: called CC=7 SCAR=19.5
241 218 my ($self, $href, $name) = @_;
242 # Parse ling rules
243 218 ▶ if (exists $href->{linguistic}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $href->{"linguistic"}) 10 208 218
244 10 ($href->{where}, $href->{do}) = _parse_ling($href->{linguistic});
245 10 ▶ unless ($href->{where} && $href->{do}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($href->{"where"} and $href->{"do"}) 8 2 10
Condition66%2/3
$href->{"where"} and $href->{"do"}
!ll&&!rl&&r
802
MC/DC50%1/2
$href->{"where"} and $href->{"do"}
$href->{"where"} $href->{"do"}
Truth table66%2/3
A and B
A= $href->{"where"}B= $href->{"do"}
ABresult
0X0
100
111
246 8 return err "Couldn't parse linguistic rule for '$name'";
247 }
248 2 $href->{where} = _parse_ext($href->{where});
249 2 $href->{do} = _parse_ext($href->{do});
250 }
251  
252 # Validate keys
253 210 for (keys %$href) {
254 507 ▶ if (exists $valid{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Lingua::Phonology::Rules::valid{$_}) 505 2 507
255 505 ▶ unless ($valid{$_}->($href->{$_})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Lingua::Phonology::Rules::valid{$_}->($href->{$_})) 4 501 505
256 4 return err("Invalid value for $_ in rule '$name'");
257 }
258 }
259 }
260 206 return 1;
261 }
262  
263 206 ▶sub _add_rule {
Subroutine _add_rule: called CC=3 SCAR=11.0
264 206 my ($self, $rule, $href) = @_;
265  
266 206 $self->{RULES}->{$rule} = {};
267 206 $self->{RULES}->{$rule}->{word} = Lingua::Phonology::Word->new();
268 206 for (keys %property) {
269 1442 ▶ $self->$_($rule, $href->{$_} || $property{$_});
Condition100%3/3
$href->{$_} || $Lingua::Phonology::Rules::property{$_}
l!l&&r!l&&!r
501162779
MC/DC100%2/2
$href->{$_} || $Lingua::Phonology::Rules::property{$_}
$href->{$_} $Lingua::Phonology::Rules::property{$_}
Truth table100%3/3
A || B
A= $href->{$_}B= $Lingua::Phonology::Rules::property{$_}
ABresult
1X1
011
000
270 }
271 }
272  
273 14 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
274 14 my $self = shift;
275 14 $self->{RULES} = {};
276 14 $self->{ORDER} = [];
277 14 $self->{PERSIST} = [];
278 14 return 1;
279 }
280  
281 7 ▶sub loadfile {
Subroutine loadfile: called CC=3 SCAR=11.0
Pod documented
282 7 my ($self, $file) = @_;
283  
284 # Calling loadfile() w/o a file loads a default, but there is no default
285 # rule set. So do nothing and return true.
286 7 ▶ return 1 if not defined $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file 4 3 7
287  
288 3 my $parse;
289 3 eval { $parse = _parse_from_file($file, 'rules') };
290 3 ▶ return err($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 2 3
291 2 $self->_load_from_struct($parse);
292 }
293  
294 4 ▶sub _load_from_struct {
Subroutine _load_from_struct: called CC=16 SCAR=29.8
295 4 my ($self, $parse) = @_;
296 4 my $err = 0;
297  
298 # Handle rule declarations
299 4 RULE: for my $href (@{$parse->{rule}}) {
300 20 my $parm = {};
301  
302 # If we ONLY have content, make it into a href
303 20 ▶ if (not _is($href, 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_is($href, "HASH")) 0 20 20
304 0 $href = { content => $href };
305 }
306  
307 # Take linguistic-style rules from the content
308 20 ▶ if (exists $href->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $href->{"content"}) 14 6 20
309 # _parse_ling() returns where and do
310 14 ($href->{where}, $href->{do}) = _parse_ling($href->{content});
311 14 ▶ unless ($href->{where} && $href->{do}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($href->{"where"} and $href->{"do"}) 0 14 14
Condition33%1/3
$href->{"where"} and $href->{"do"}
!ll&&!rl&&r
0014
MC/DC0%0/2
$href->{"where"} and $href->{"do"}
$href->{"where"} $href->{"do"}
Truth table33%1/3
A and B
A= $href->{"where"}B= $href->{"do"}
ABresult
0X0
100
111
312 0 err "Couldn't parse linguistic rule '$href->{name}'";
313 0 $err = 1;
314 0 next RULE;
315 }
316 }
317  
318 # Iterate over elements
319 # code elements
320 20 for (@code) {
321 80 ▶ next if not exists $href->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $href->{$_} 28 52 80
322  
323 # If we ONLY have content
324 52 ▶ if (not _is $href->{$_}, 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (&_is($href->{$_}, "HASH")) 44 8 52
325 44 $href->{$_} = { content => $href->{$_} };
326 }
327  
328 52 ▶ if ((not exists $href->{$_}->{type}) || $href->{$_}->{type} ne 'plain') {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $href->{$_}{'type'} or $href->{$_}{'type'} ne 'plain') { } 44 8 52
Condition66%2/3
not exists $href->{$_}{"type"} or $href->{$_}{"type"} ne "plain"
l!l&&r!l&&!r
4408
MC/DC50%1/2
not exists $href->{$_}{"type"} or $href->{$_}{"type"} ne "plain"
not exists $href->{$_}{"type"} $href->{$_}{"type"} ne "plain"
Truth table66%2/3
not(A) or B
A= not exists $href->{$_}{"type"}B= $href->{$_}{"type"} ne "plain"
ABresult
1X1
011
000
329 44 eval { $parm->{$_} = _parse_ext $href->{$_}->{content} };
330 }
331 else {
332 8 eval { $parm->{$_} = _parse_plain $href->{$_}->{content} };
333 }
334
335 # Always check $@ after parsing
336 52 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 52 52
337 0 err("Error processing rule $href->{name}: $@\n");
338 0 $err = 1;
339 0 next RULE;
340 }
341 }
342 # text elements
343 20 for (@text) {
344 60 ▶ next if not exists $href->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $href->{$_} 42 18 60
345 18 $parm->{$_} = $href->{$_}->{value};
346 }
347 # Future types to be added here
348  
349 0 ▶ $self->add_rule($href->{name} => $parm) || do { $err = 1 };
Branch: 1 branch
BranchTrueFalseTotal
$self->add_rule($href->{"name"}, $parm) or do { $err = 1 } 20 0 20
350  
351 }
352  
353 # Handle ordering rules
354 4 $self->order( map { [ map { $_->{name} } @{$_->{rule}} ] } @{$parse->{order}} );
355  
356 # Handle persistent rules
357 4 $self->persist( map { $_->{name} } @{$parse->{persist}} );
358  
359 4 ▶ return $err ? () : 1;
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 0 4 4
360 }
361
362 24 ▶sub _parse_ling {
Subroutine _parse_ling: called CC=15 SCAR=27.1
363 24 my $str = shift;
364  
365 24 ▶ if (not $PARSER) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Lingua::Phonology::Rules::PARSER) 1 23 24
366 1 require Lingua::Phonology::RuleParser;
367 1 $PARSER = Lingua::Phonology::RuleParser->new();
368 }
369  
370 24 my $parse = $PARSER->Rule($str);
371 24 ▶ return if not $parse;
Branch: 1 branch
BranchTrueFalseTotal
unless $parse 8 16 24
372 16 ▶ return err "Unbalanced rule" unless @{$parse->{from}} == @{$parse->{to}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$parse->{"from"};} == @{$parse->{"to"};} 0 16 16
373  
374 16 my (@do, @where); # Holds the statements built
375 16 my $nulls = 0; # Counts the nulls encountered so far
376  
377 # Iterate over $parse->{from}, adding elements to @where and @do
378 16 for my $i (0 .. $#{$parse->{from}}) {
379 # The FROM item is not '__NULL': we have a real seg and need to make a test statement
380 21 ▶ if ($parse->{from}[$i] ne '__NULL') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parse->{'from'}[$i] ne '__NULL') { } 15 6 21
elsif ($parse->{'from'}[$i] eq '__NULL') { } 6 0 6
381 15 my $idx = $i - $nulls;
382 15 push @where, _test_seg($idx, $parse->{from}[$i]);
383 15 push @do, _set_seg($idx, $parse->{to}[$i]);
384 }
385 # The FROM item is '__NULL'--insert a segment
386 elsif ($parse->{from}[$i] eq '__NULL') {
387 6 push @do, _insert_seg($i - $nulls, $parse->{to}[$i]);
388 6 $nulls++;
389 }
390 }
391  
392 # Special case for when we ONLY have nulls in FROM and the next segment is
393 # a boundary. Normally this would generate a $_[0]->BOUNDARY statement, but
394 # that can never succeed, so we roll focus back one seg.
395 16 my $backstep = 0;
396 16 ▶ if ($nulls == @{$parse->{from}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nulls == @{$parse->{"from"};}) 4 12 16
397 4 foreach (@{$parse->{when}}) {
398 4 ▶ if ($_->[1][0] eq '__BOUNDARY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[1][0] eq "__BOUNDARY") 2 2 4
399 # Redact the existing statements
400 2 foreach (@do) {
401 3 s/\[(-?\d+)\]/\[$1 + 1\]/g;
402 }
403 2 $backstep = 1;
404 2 last;
405 }
406 }
407 }
408  
409 # Build the strings based on the "when" property
410 16 my @conds;
411 16 for my $cond (@{$parse->{when}}) {
412 14 my @thiscond;
413 # The pre '_' segments are in [0], go through them backwards
414 14 ▶ for (my $i = -1; $i >= -@{$cond->[0]}; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= -@{$cond->[0];}) 7 14 21
415 7 push @thiscond, _test_seg($i + $backstep, $cond->[0][$i]);
416 }
417 # The post '_' statements are in $cond->[1].
418 14 for my $i (0 .. $#{$cond->[1]}) {
419 11 my $idx = $i + $backstep + scalar @{$parse->{from}} - $nulls;
420 11 push @thiscond, _test_seg($idx, $cond->[1][$i]);
421 }
422 14 push @conds, '(' . join(' && ', @thiscond) . ')';
423 }
424 16 ▶ push @where, '(' . join(' || ', @conds) . ')' if @conds;
Branch: 1 branch
BranchTrueFalseTotal
if @conds 13 3 16
425  
426 # Final joins - return (where, do)
427 16 return join(" && ", @where), join("\n", @do);
428 }
429  
430 # Segs should be an array ref, a hash ref, '__BOUNDARY', or a string
431 36 ▶sub _test_seg {
Subroutine _test_seg: called CC=5 SCAR=16.1
432 36 my ($i, $seg) = @_;
433 36 ▶ if (ref $seg eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $seg eq 'ARRAY') { } 1 35 36
elsif (ref $seg eq 'HASH') { } 16 19 35
elsif ($seg eq '__BOUNDARY') { } 6 13 19
434 1 return '(' . join(' || ', map { _test_seg($i, $_) } @$seg) . ')';
435 }
436 elsif (ref $seg eq 'HASH') {
437 # Special case for empty hash - corresponds to "[]" in input, which we
438 # want to be always true, instead of compiling to "()", which would be
439 # always false
440 16 ▶ return 1 if not keys %$seg;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$seg 0 16 16
441  
442 # General case
443 16 return '(' . join(' && ', map { _test_feature($i, $_, $seg->{$_}) } keys %$seg) . ')';
444 }
445 elsif ($seg eq '__BOUNDARY') {
446 6 return "(\$_[$i]->BOUNDARY)";
447 }
448 else {
449 13 $seg = quotemeta $seg;
450 13 return "(\$_[$i]->spell eq \"$seg\")";
451 }
452 }
453  
454 # Set seg cannot take an array ref, otherwise the same as _test_seg.
455 21 ▶sub _set_seg {
Subroutine _set_seg: called CC=3 SCAR=11.0
456 21 my ($i, $seg) = @_;
457 21 ▶ if (ref $seg eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $seg eq 'HASH') { } 7 14 21
elsif ($seg eq '__NULL') { } 2 12 14
458 7 return join "\n", map { _set_feature($i, $_, $seg->{$_}) } keys %$seg;
459 }
460 elsif ($seg eq '__NULL') {
461 2 return "\$_[$i]->DELETE;\n";
462 }
463 else {
464 12 return "Lingua::Phonology::Functions::change(\$_[$i], \"$seg\");\n";
465 }
466 }
467  
468 # Features should be '__TRUE', '__FALSE', a number, or a string
469 19 ▶sub _test_feature {
Subroutine _test_feature: called CC=4 SCAR=13.9
470 19 my ($i, $feat, $val) = @_;
471 8 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
472  
473 19 ▶ if ($val eq '__TRUE') {
Branch: 3 branches
BranchTrueFalseTotal
if ($val eq '__TRUE') { } 12 7 19
elsif ($val eq '__FALSE') { } 4 3 7
elsif ($val eq int $val) { } 2 1 3
474 12 return "(\$_[$i]->value(\"$feat\"))";
475 }
476 elsif ($val eq '__FALSE') {
477 4 return "(not \$_[$i]->value(\"$feat\"))";
478 }
479 elsif ($val eq int $val) {
480 2 return "(\$_[$i]->value(\"$feat\") == $val)";
481 }
482 else {
483 1 return "(\$_[$i]->value(\"$feat\") eq \"$val\")";
484 }
485 }
486  
487 7 ▶sub _set_feature {
Subroutine _set_feature: called CC=4 SCAR=13.9
488 7 my ($i, $feat, $val) = @_;
489 8 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
490  
491 7 ▶ $val = 1 if $val eq '__TRUE';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "__TRUE" 1 6 7
492 7 ▶ if ($val eq '__FALSE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($val eq '__FALSE') { } 3 4 7
elsif ($val eq int $val) { } 3 1 4
493 3 return "\$_[$i]->delink(\"$feat\");";
494 }
495 elsif ($val eq int $val) {
496 3 return "\$_[$i]->value(\"$feat\", $val);";
497 }
498 else {
499 1 return "\$_[$i]->value(\"$feat\", \"$val\");";
500 }
501 }
502  
503 6 ▶sub _insert_seg {
Subroutine _insert_seg: called CC=1 SCAR=0.0
504 6 my ($i, $seg) = @_;
505 # Make an unlikely-to-repeat variable name
506 6 my $var = sprintf "\$new%06d", rand(1_000_000);
507 # Take the new segment from $_[+0], which won't be touched by backstepping
508 6 my $rv = "my $var = \$_[+0]->new;\n";
509 # Do a normal _set_seg w/ '~' placeholder and then s/// the result
510 6 $rv .= _set_seg('~', $seg);
511 6 $rv =~ s/\$_\[~\]/$var/g;
512
513 6 $rv .= "\$_[$i]->INSERT_LEFT($var);\n";
514 6 return $rv;
515 }
516  
517 2 ▶sub _to_str {
Subroutine _to_str: called CC=7 SCAR=19.5
518 2 my ($self, $file) = @_;
519  
520 2 require B::Deparse;
521 2 my $dpar = B::Deparse->new('-x7', '-p', '-si6');
522 2 $dpar->ambient_pragmas(strict => 'all', warnings => 'all');
523  
524 # Hashref structure
525 2 my $href = { rule => {}, order => { block => [] }, persist => [] } ;
526  
527 # Construct href entries for rules
528 2 for my $rule (keys %{$self->{RULES}}) {
529 # Add our name
530 18 for (@text) {
531 54 ▶ $href->{rule}->{$rule}->{$_} = { value => $self->$_($rule) }
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->$_($rule) 22 32 54
532 if defined $self->$_($rule);
533 }
534 18 for (@code) {
535 72 ▶ next if not defined $self->$_($rule);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$_($rule) 32 40 72
536 40 ▶ my $str = _deparse_ext $self->$_($rule), $dpar or err($@);
Branch: 1 branch
BranchTrueFalseTotal
my $str = &_deparse_ext(scalar $self->$_($rule), $dpar) or &err($@) 40 0 40
537 40 $href->{rule}->{$rule}->{$_} = [ $str . ' ' ]; # Extra whitespace to help alignment
538 }
539 }
540  
541 # Href entries for order and persist
542 2 $href->{order}->{block} = [ map { { rule => [ map { { name => $_ } } @$_ ] } } $self->order ];
543 2 $href->{persist} = { rule => [ map { { name => $_ } } $self->persist ] };
544  
545 2 return eval { _string_from_struct({ rules => $href }) };
546 }
547  
548 148 ▶sub apply {
Subroutine apply: called CC=6 SCAR=17.9
Pod documented
549 148 my ($self, $rule, $orig) = @_;
550  
551 148 ▶ return err("No such rule '$rule'") unless exists $self->{RULES}->{$rule};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"RULES"}{$rule} 1 147 148
552 147 ▶ return err "Argument not an array reference" unless _is $orig, 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless &_is($orig, "ARRAY") 1 146 147
553  
554 # Get the word
555 my ($word, $where, $do, $result)
556 146 = @{$self->{RULES}->{$rule}}{('word', 'where', 'do', 'result')};
557  
558 # Attempt to set this to the word
559 146 ▶ $word->set_segs(@$orig) || return err($@);
Branch: 1 branch
BranchTrueFalseTotal
$word->set_segs(@$orig) or return &err($@) 145 1 146
560 145 $word->rule({ map { $_ => $self->$_($rule) } keys %property });
561
562 # Reset the counter
563 145 $self->{COUNT} = 0;
564  
565 # Iterate over the segments
566 145 ▶ while ($word->next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word->next) 1182 145 1327
567 1182 my @word = $word->get_working_segs;
568 1182 ▶ if ($where->(@word)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$where(@word)) 465 717 1182
569 # Apply the rule
570 465 $do->(@word);
571 465 $self->{COUNT}++;
572 }
573 }
574  
575 145 @$orig = $word->get_orig_segs;
576 145 $word->clear; # Free up memory
577
578 145 return @$orig;
579 }
580  
581 # Makes rules appliable by their name
582 8 our $AUTOLOAD;
583 29 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
584 29 my $self = shift;
585 29 my $method = $AUTOLOAD;
586 29 $method =~ s/.*:://;
587  
588 # For calling rules by name
589 29 ▶ if ($self->{RULES}->{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"RULES"}{$method}) 29 0 29
590 # Compile functions which are rules
591 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
592 29 ▶ *$method = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
593 29 my ($self, $word) = @_;
594 29 $self->apply($method, $word);
595 29 };
596  
597 # Go to the rule
598 29 return $self->$method(@_);
599 }
600 0 die "No such method: $AUTOLOAD, called";
601 }
602  
603 13 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
604 13 $_[0]->clear;
605 }
606  
607 17 ▶sub apply_all {
Subroutine apply_all: called CC=5 SCAR=16.1
Pod documented
608 17 my ($self, $word) = @_;
609 17 my %count = ();
610  
611 17 my @persist = $self->persist; # Only get this once, for speed
612 17 for ($self->order) {
613 99 for (@persist) {
614 9 $self->apply($_, $word);
615 9 $count{$_} += $self->{COUNT};
616 }
617 99 for (@$_) {
618 101 $self->apply($_, $word);
619 101 $count{$_} += $self->{COUNT};
620 }
621 }
622 17 for (@persist) {
623 5 $self->apply($_, $word);
624 5 $count{$_} += $self->{COUNT};
625 }
626  
627 # Set COUNT to be the hashref
628 17 $self->{COUNT} = \%count;
629  
630 }
631  
632 41 ▶sub order {
Subroutine order: called CC=4 SCAR=13.9
Pod documented
633 41 my ($self, @items) = @_;
634 41 foreach (@items) {
635 106 ▶ $_ = [ $_ ] if not ref $_;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 97 9 106
636 }
637 41 ▶ $self->{ORDER} = \@items if @items;
Branch: 1 branch
BranchTrueFalseTotal
if @items 20 21 41
638 41 return @{$self->{ORDER}};
639 }
640  
641 27 ▶sub persist {
Subroutine persist: called CC=2 SCAR=6.9
Pod documented
642 27 my $self = shift;
643 27 ▶ $self->{PERSIST} = \@_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 21 27
644 27 return @{$self->{PERSIST}};
645 }
646  
647 2 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
648 2 return $_[0]->{COUNT};
649 }
650  
651 8 1;
652  
653 __END__
« Phonology/Segment.pm ↑ Summary Phonology/Features.pm »