blib/lib/Lingua/SoundChange.pm

filter: Statementstmt 91.4% 96 / 105 Branchbran 65.9% 29 / 44 Conditioncond 63.6% 14 / 22 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 80.0% 12 / 15 Podpod 37.5% 3 / 8 totaltotal 75.9% 158 / 208 CC 36 SCAR 24.5
CC
36
Cov
81%
compile_rules
22.8
new
6.0
change
6.0
CRAP 11.6
↑ Summary
1 package Lingua::SoundChange;
2  
3 4 ▶use 5.005_03;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # use warnings;
7  
8 # for debugging:
9 4 ▶use constant PRINT_RULES => $ENV{LINGUA_SOUNDCHANGE_PRINTRULES} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'LINGUA_SOUNDCHANGE_PRINTRULES'} || 0
l!l
04
MC/DC0%0/1
$ENV{'LINGUA_SOUNDCHANGE_PRINTRULES'} || 0
$ENV{'LINGUA_SOUNDCHANGE_PRINTRULES'}
Truth table50%1/2
A
A= $ENV{'LINGUA_SOUNDCHANGE_PRINTRULES'}
Aresult
11
00
10 4 ▶use constant DEBUG => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 $VERSION = '0.05';
14  
15 sub compile_rules($$$$);
16 sub compile_vars($$);
17  
18 22 ▶sub new {
Subroutine new: called CC=6 SCAR=18.0
Pod documented
19 22 my($class, $vars, $rules, $opts) = @_;
20  
21 22 ▶ $opts ||= { };
Condition100%2/2
$opts ||= {}
l!l
715
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
22  
23 22 ▶ croak '$vars must be a hash reference!' unless ref $vars eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vars eq "HASH" 0 22 22
24 22 ▶ croak '$rules must be an array reference!' unless ref $rules eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules eq "ARRAY" 0 22 22
25  
26 22 my $obj = {
27 raw_vars => $vars,
28 raw_rules => $rules,
29 opts => $opts,
30 };
31  
32 22 $obj->{vars} = compile_vars($vars, $opts);
33 22 ▶ if($opts->{longVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"longVars"}) 6 16 22
34 6 $obj->{raw_vars}->{"\Q<$_>\E"} = $vars->{$_} for keys %$vars;
35 }
36 ( $obj->{rules},
37 22 $obj->{code} ) = compile_rules($rules, $obj->{vars}, $obj->{raw_vars}, $opts);
38  
39 22 bless $obj, $class;
40 }
41  
42 22 ▶sub change {
Subroutine change: called CC=6 SCAR=18.0
Pod documented
43 22 my($self, $words) = @_;
44  
45 22 ▶ $words = [] unless defined $words;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $words 0 22 22
46  
47 22 ▶ croak("change needs an array reference") unless ref($words) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $words eq "ARRAY" 0 22 22
48  
49 22 foreach my $word (@$words) {
50 54 my $origword = $word;
51 54 my @rules;
52 my $ruleout;
53 54 foreach my $rule (@{$self->rules}) {
54 168 ($word, $ruleout) = $rule->($word);
55 168 ▶ push @rules, $ruleout if defined $ruleout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ruleout 61 107 168
56 }
57  
58 54 $word = { orig => $origword, word => $word, rules => \@rules };
59 }
60  
61 22 $words;
62 }
63  
64  
65 # Private methods
66  
67 22 ▶sub compile_rules ($$$$) {
Subroutine compile_rules: called CC=20 SCAR=31.3
Pod undocumented
68 22 my($rules, $vars, $varstring, $opt) = @_;
69  
70 22 ▶ croak "rules not an array reference" unless ref $rules eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $rules eq "ARRAY" 0 22 22
71 22 ▶ croak "vars not a hash reference" unless ref $vars eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vars eq "HASH" 0 22 22
72 22 ▶ croak "varstring not a hash reference" unless ref $varstring eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $varstring eq "HASH" 0 22 22
73 22 ▶ croak "opt not a hash reference" unless ref $opt eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt eq "HASH" 0 22 22
74  
75 22 my @compiledrules;
76 my %code;
77  
78 # Rules: change from a sound (one or more letters) or a category
79 # to another sound or category, in a certain environment.
80 # Categories may only be one letter long and are usually uppercase.
81 # Environments must contain a _ symbol to show where the replacement
82 # takes place; it may also contain letters, categories, and the special
83 # symbols ( ) (to enclose optional parts) and # (beginning or end of
84 # word).
85 # Rules can only change sounds to sounds, and categories to categories.
86 # If a category is to be changed to another category, they should be
87 # the same length. Otherwise the second category will have its laster
88 # letter repeated until it has the same length as the first (if it is
89 # shorter), or characters in the second category that don't match
90 # characters in the first will not be produced by such a range. Note
91 # that this is an artefact of the use of tr/// and is not guaranteed
92 # behaviour.
93 # Don't use regex metacharacters (except for the parentheses which
94 # show optional elements) in the environment or in names of categories
95 # or sounds.
96 # These include: . * + ? ^ $ [ ]
97  
98 22 foreach my $rule (@$rules) {
99 40 ▶ if( $rule =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ m[ ^ ( [^/]+ ) # "change from" to $1 / # slash ( [^/]* ) # "change to" to $2 (may be blank) / # slash ( .+ ) # "environment" to $3 (may not be blank) $ ]x) 40 0 40
100 ^
101 ( [^/]+ ) # "change from" to $1
102 / # slash
103 ( [^/]* ) # "change to" to $2 (may be blank)
104 / # slash
105 ( .+ ) # "environment" to $3 (may not be blank)
106 $
107 }x )
108 {
109 40 my($from, $to, $env) = ($1, $2, $3);
110 40 my($subfrom, $subto) = ('', '');
111  
112 80 ▶ my $option = sub {
Subroutine __ANON__: called CC=1 SCAR=0.4
113 80 my $word = shift;
114  
115 # Change parentheses round one element to a question
116 # mark following it, ...
117 80 $word =~ s{
118 \( # open parenthesis
119 (.) # one character, to $1
120 \) # close parenthesis
121 }{
122 0 $1 . '?'
123 }gex;
124  
125 # ... and parentheses around multiple elements by
126 # non-capturing parentheses followed by a question mark
127 # (but nested parentheses are not allowed!)
128 80 $word =~ s{
129 \( # open parenthesis
130 ( [^()]+ ) # one or more non-parenthesis characters,
131 # to $1
132 \) # close parenthesis
133 }{
134 0 '(?:' . $1 . ')?'
135 }gex;
136  
137 80 $word;
138 40 };
139  
140 # Escape dollar signs and at signs in $env, which would
141 # otherwise try to interpolate a variable into the regular
142 # expression
143 40 $env =~ s/([\$\@])/\\$1/g;
144  
145 # Get the bits before and after the underscore
146 # and put them in capturing parentheses in $subfrom
147 40 ▶ if($env =~ /^(#?)([^_#]*)(_)([^_#]*)(#?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env =~ /^(#?)([^_#]*)(_)([^_#]*)(#?)$/) 40 0 40
148 # leading #
149 40 ▶ $subfrom .= '^' if defined $1 && length $1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 and length $1 1 39 40
Condition66%2/3
defined $1 and length $1
!ll&&!rl&&r
0391
MC/DC50%1/2
defined $1 and length $1
defined $1 length $1
Truth table66%2/3
A and B
A= defined $1B= length $1
ABresult
0X0
100
111
150  
151 # preceding stuff, so we don't need $`
152 # non-greedy
153 # If match anchored at beginning, don't add this
154 40 ▶ if(defined $1 && length $1) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $1 and length $1) { } 1 39 40
Condition66%2/3
defined $1 and length $1
!ll&&!rl&&r
0391
MC/DC50%1/2
defined $1 and length $1
defined $1 length $1
Truth table66%2/3
A and B
A= defined $1B= length $1
ABresult
0X0
100
111
155 1 $subfrom .= '()';
156 } else {
157 39 $subfrom .= '(.*?)';
158 }
159  
160 # pre-environment
161 40 $subfrom .= '(' . $option->(quotemeta $2) . ')';
162  
163 # underscore
164 40 $subfrom .= "(\Q$from\E)";
165  
166 # post-environment
167 40 $subfrom .= '(' . $option->(quotemeta $4) . ')';
168  
169 # trailing #
170 40 ▶ $subfrom .= '$' if defined $5 && length $5;
Branch: 1 branch
BranchTrueFalseTotal
if defined $5 and length $5 7 33 40
Condition66%2/3
defined $5 and length $5
!ll&&!rl&&r
0337
MC/DC50%1/2
defined $5 and length $5
defined $5 length $5
Truth table66%2/3
A and B
A= defined $5B= length $5
ABresult
0X0
100
111
171 }
172  
173 # Now expand categories
174 40 ▶ if($opt->{longVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'longVars'}) { } 9 31 40
175 9 ▶ $subfrom =~ s{(\\<[^>]+\\>)}{$vars->{$1} || $1}eg;
Condition33%1/3
$vars->{$1} || $1
l!l&&r!l&&!r
1100
MC/DC0%0/2
$vars->{$1} || $1
$vars->{$1} $1
Truth table33%1/3
A || B
A= $vars->{$1}B= $1
ABresult
1X1
011
000
176 } else {
177 31 ▶ $subfrom =~ s{(\\?)(.)}{$vars->{$2} || $1 . $2}eg;
Condition66%2/3
$vars->{$2} || $1 . $2
l!l&&r!l&&!r
143820
MC/DC0%0/2
$vars->{$2} || $1 . $2
$vars->{$2} $1 . $2
Truth table66%2/3
A || B
A= $vars->{$2}B= $1 . $2
ABresult
1X1
011
000
178 }
179  
180 # Show where the rule matches
181 40 $subto .= qq(\$rule = "\Q$from\E->\Q$to\E /\Q$env\E applies to \$word at " . (length(\$1)+1) . "\\n"; );
182  
183 40 $subto .= '$1 . $2 . ';
184 $subto .= ($vars->{quotemeta $from}
185 40 ▶ ? "do { my \$char = \$3; \$char =~ tr{$varstring->{quotemeta $from}}{" . ($varstring->{quotemeta $to} || $to) . "}; \$char }"
Branch: 1 branch
BranchTrueFalseTotal
$$vars{quotemeta $from} ? : 10 30 40
Condition66%2/3
$$varstring{quotemeta $to} || $to
l!l&&r!l&&!r
820
MC/DC0%0/2
$$varstring{quotemeta $to} || $to
$$varstring{quotemeta $to} $to
Truth table66%2/3
A || B
A= $$varstring{quotemeta $to}B= $to
ABresult
1X1
011
000
186 : "q{" . $to . "}");
187 40 $subto .= ' . $4';
188  
189 40 if(PRINT_RULES) {
190 print "[", $#compiledrules + 1, "] $rule --> s{$subfrom}{$subto}eg\n";
191 }
192  
193 40 my $code = <<"EOF";
194 sub {
195 my \$word = shift;
196 my \$rule;
197 my \$orig;
198 # print qq(Working on '\$word'; \Q$from\E->\Q$to\E /\Q$env\E; from is '\Q$subfrom\E and to is '\Q$subto\E'\\n);
199 1 while ((\$orig = \$word) =~ s{$subfrom}{$subto}e) && (\$orig ne \$word) && (\$word = \$orig);
200 return ( \$word, \$rule );
201 }
202 EOF
203  
204 40 push @compiledrules, eval $code;
205 40 ▶ croak "Problem with '$code'" unless $compiledrules[-1];
Branch: 1 branch
BranchTrueFalseTotal
unless $compiledrules[-1] 0 40 40
206 40 $code{$compiledrules[-1]} = $code;
207 }
208 }
209  
210 22 ( \@compiledrules, \%code );
211 }
212  
213 22 ▶sub compile_vars ($$) {
Subroutine compile_vars: called CC=5 SCAR=16.2
Pod undocumented
214 22 my($vars, $opt) = @_;
215  
216 22 ▶ croak "\$vars is not a hash reference" unless ref $vars eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vars eq "HASH" 0 22 22
217 22 ▶ croak "\$opt is not a hash reference" unless ref $opt eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opt eq "HASH" 0 22 22
218  
219 22 my %compiledvars;
220  
221 22 foreach my $var (keys %$vars) {
222 26 my $list = $vars->{$var};
223 # Escape at signs and dollars in the list
224 26 $list =~ s/([\$\@])/\\$1/g;
225  
226 26 ▶ if($opt->{longVars}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'longVars'}) { } 12 14 26
227 12 $compiledvars{"\Q<$var>\E"} = qr/[$list]/;
228 12 print qq[($var => $list // $compiledvars{"\Q<$var>\E"})\n] if PRINT_RULES;
229 } else {
230 14 $compiledvars{$var} = qr/[$list]/;
231 14 print "($var => $list // $compiledvars{$var})\n" if PRINT_RULES;
232 }
233 }
234  
235 22 \%compiledvars;
236 }
237  
238 54 ▶sub rules {
Subroutine rules: called CC=1 SCAR=0.0
Pod documented
239 54 my($self) = @_;
240  
241 54 $self->{rules};
242 }
243  
244 0 ▶sub vars {
Subroutine vars: not called CC=1 SCAR=6.9
Pod undocumented
245 0 my($self) = @_;
246  
247 0 $self->{vars};
248 }
249  
250 0 ▶sub varstring {
Subroutine varstring: not called CC=1 SCAR=6.9
Pod undocumented
251 0 my($self) = @_;
252  
253 0 $self->{raw_vars};
254 }
255  
256 0 ▶sub code {
Subroutine code: not called CC=2 SCAR=17.9
Pod undocumented
257 0 my($self, $token, $code) = @_;
258  
259 0 ▶ $self->{code}->{$token} = $code if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 0 0 0
260  
261 0 $self->{code}->{$token};
262 }
263  
264  
265 4 1;
266 __END__
↑ Summary