Inflexion.pm

filter: Statementstmt 95.8% 92 / 96 Branchbran 93.3% 56 / 60 Conditioncond 72.2% 26 / 36 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 88.8% 16 / 18 Podpod 80.0% 4 / 5 totaltotal 84.8% 201 / 237 CC 47 SCAR 23.6
CC
47
Cov
91%
__ANON__
21.5
wordlist
9.5
__ANON__
8.0
CRAP 10.6
« Inflexion/Indefinite.pm ↑ Summary Inflexion/Term.pm »
1 package Lingua::EN::Inflexion;
2 33 ▶use 5.010; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 33 our $VERSION = '0.002009';
6  
7 # Import noun, verb, and adj classes...
8 33 ▶use Lingua::EN::Inflexion::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 33 ▶sub import {
Subroutine import: called CC=5 SCAR=16.1
11 33 my (undef, @exports) = @_;
12  
13 # Export interface...
14 33 ▶ @exports = qw< noun verb adj inflect wordlist > if !@exports;
Branch: 1 branch
BranchTrueFalseTotal
unless @exports 32 1 33
15  
16 # Handle renames...
17 33 my %export_name;
18 33 ▶ @exports = map { ref eq 'HASH' ? do { @export_name{keys %$_} = values %$_; keys %$_ } : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 1 160 161
19 @exports;
20  
21 33 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 33 for my $func (@exports) {
23 163 ▶ *{caller().'::'.($export_name{$func}//$func)} = \&{$func};
Condition66%2/3
$export_name{$func} // $func
l!l&&r!l&&!r
31600
MC/DC0%0/2
$export_name{$func} // $func
$export_name{$func} $func
Truth table66%2/3
A // B
A= $export_name{$func}B= $func
ABresult
1X1
011
000
24 }
25 }
26  
27  
28 # Noun constructor...
29 23013 ▶sub noun ($) {
Subroutine noun: called CC=1 SCAR=0.0
Pod documented
30 23013 my ($noun) = @_;
31 23013 return Lingua::EN::Inflexion::Noun->new($noun);
32 }
33  
34 # Verb constructor...
35 4274 ▶sub verb ($) {
Subroutine verb: called CC=1 SCAR=0.0
Pod documented
36 4274 my ($verb) = @_;
37 4274 return Lingua::EN::Inflexion::Verb->new($verb);
38 }
39  
40  
41 # Adjective constructor...
42 152 ▶sub adj ($) {
Subroutine adj: called CC=1 SCAR=0.0
Pod documented
43 152 my ($adj) = @_;
44 152 return Lingua::EN::Inflexion::Adjective->new($adj);
45 }
46  
47  
48 # Convert a list of words to...a list of words in a single string...
49 29 ▶sub wordlist {
Subroutine wordlist: called CC=9 SCAR=22.5
Pod undocumented
50 29 my (@words, %opt);
51  
52 # Unpack the argument list...
53 29 my $sep = ',';
54 29 my $conj = 'and';
55 29 for my $arg (@_) {
56 101 my $argtype = ref($arg);
57  
58 101 ▶ if ($argtype eq q{}) { push @words, $arg; $sep = ';' if $arg =~ /,/; }
Branch: 3 branches
BranchTrueFalseTotal
if ($argtype eq '') { } 77 24 101
elsif ($argtype eq 'HASH') { } 24 0 24
if $arg =~ /,/ 6 71 77
59 24 elsif ($argtype eq q{HASH}) { @opt{keys %$arg} = values %$arg }
60 0 else { croak 'Invalid $argtype argument to wordlist' }
61 }
62  
63 # Fill in defaults...
64 29 ▶ $conj = $opt{conj} // $conj;
Condition66%2/3
$opt{"conj"} // $conj
l!l&&r!l&&!r
14150
MC/DC0%0/2
$opt{"conj"} // $conj
$opt{"conj"} $conj
Truth table66%2/3
A // B
A= $opt{"conj"}B= $conj
ABresult
1X1
011
000
65 29 ▶ $sep = $opt{sep} // $sep;
Condition33%1/3
$opt{"sep"} // $sep
l!l&&r!l&&!r
0290
MC/DC0%0/2
$opt{"sep"} // $sep
$opt{"sep"} $sep
Truth table33%1/3
A // B
A= $opt{"sep"}B= $sep
ABresult
1X1
011
000
66  
67 # Set the Oxford comma...
68 29 ▶ my $oxford = $opt{final_sep} // $sep;
Condition66%2/3
$opt{"final_sep"} // $sep
l!l&&r!l&&!r
15140
MC/DC0%0/2
$opt{"final_sep"} // $sep
$opt{"final_sep"} $sep
Truth table66%2/3
A // B
A= $opt{"final_sep"}B= $sep
ABresult
1X1
011
000
69  
70 # Construct the list phrase...
71 29 ▶ my $list = @words < 3
Branch: 1 branch
BranchTrueFalseTotal
@words < 3 ? : 11 18 29
72 ? join(" $conj ", @words)
73 : join("$sep ", @words[0..$#words-1]) . "$oxford $conj $words[-1]";
74  
75 # Condense any extra whitespace...
76 29 $list =~ s/(\s)\s+/$1/g;
77  
78 29 return $list;
79 }
80  
81  
82 # All-in-one inflexions...
83 33 my %word_for_number = (
84 0 => 'zero', 5 => 'five',
85 1 => 'one', 6 => 'six',
86 2 => 'two', 7 => 'seven',
87 3 => 'three', 8 => 'eight',
88 4 => 'four', 9 => 'nine',
89 10 => 'ten',
90 );
91  
92 442 ▶my $normalize_opts = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
93 442 my ($opts) = @_;
94  
95 442 ▶ if ($opts =~ m{ [[:upper:]] }x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts =~ / [[:upper:]] /x) 2 440 442
96 2 $opts =~ s{ [[:lower:]] }{}gx;
97 }
98 442 return lc $opts;
99 33 };
100  
101 162 ▶sub inflect($) {
Subroutine inflect: called CC=3 SCAR=11.0
Pod documented
102 162 my ($string) = @_;
103  
104 162 my $inflexion = 'singular';
105  
106 my $transform = {
107 150 ▶ 'N' => sub{
Subroutine __ANON__: called CC=5 SCAR=16.1
108 150 my ($term, $opts) = @_;
109 carp "Unknown '$_' option to <N:...> command"
110 150 for $opts =~ /([^cps])/;
111  
112 150 my $word = noun($term);
113 150 ▶ $word = $word->classical if $opts =~ /c/i;
Branch: 1 branch
BranchTrueFalseTotal
if $opts =~ /c/i 58 92 150
114  
115 150 ▶ return $opts =~ /p/i ? $word->plural
Branch: 2 branches
BranchTrueFalseTotal
$opts =~ /p/i ? : 2 148 150
$opts =~ /s/i ? : 2 146 148
116 : $opts =~ /s/i ? $word->singular
117 : $word->$inflexion;
118 },
119  
120 130 ▶ 'V' => sub{ return verb(shift)->$inflexion; },
Subroutine __ANON__: called CC=1 SCAR=0.0
121  
122 0 ▶ 'A' => sub{ return adj(shift)->$inflexion; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
123  
124 162 ▶ '#' => sub{
Subroutine __ANON__: called CC=21 SCAR=30.7
125 162 my ($count, $opts) = @_;
126 162 $opts =~ s{e}{asw}g;
127 carp "Unknown '$_' option to <#:...> command"
128 162 for $opts =~ /([^acdefinosw\d])/;
129  
130 # Increment count if requested...
131 162 ▶ if ($opts =~ /i/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts =~ /i/i) 4 158 162
132 4 $count++;
133 }
134  
135 # Decide which inflexion the count requires...
136 $inflexion
137 162 ▶ = $count == 1 || $opts =~ /s/i && $count == 0 || $opts =~ /o/i ? 'singular'
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 || $opts =~ /s/i && $count == 0 || $opts =~ /o/i ? : 44 118 162
Condition88%8/9
$count == 1 or $opts =~ /s/i and $count == 0 or $opts =~ /o/i
l!l&&r!l&&!r
440118
$count == 1 or $opts =~ /s/i and $count == 0
l!l&&r!l&&!r
3212118
$opts =~ /s/i and $count == 0
!ll&&!rl&&r
863212
MC/DC75%3/4
$count == 1 or $opts =~ /s/i and $count == 0 or $opts =~ /o/i
$count == 1 $opts =~ /s/i $count == 0 $opts =~ /o/i
Truth table66%4/6
A or B and C or D
A= $count == 1B= $opts =~ /s/iC= $count == 0D= $opts =~ /o/i
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
138 : 'plural';
139  
140 # Defer handling of A/AN...
141 162 ▶ if ($count == 1 && $opts =~ /a/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1 and $opts =~ /a/i) 9 153 162
Condition100%3/3
$count == 1 and $opts =~ /a/i
!ll&&!rl&&r
130239
MC/DC100%2/2
$count == 1 and $opts =~ /a/i
$count == 1 $opts =~ /a/i
Truth table100%3/3
A and B
A= $count == 1B= $opts =~ /a/i
ABresult
0X0
100
111
142 9 return "<#a:>";
143 }
144  
145 153 ▶ my $count_word = $opts =~ /w|o/i ? noun($count) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts =~ /w|o/i ? : 57 96 153
146 153 ▶ $count_word = $count_word->classical if $count_word && $opts =~ /c/i;
Branch: 1 branch
BranchTrueFalseTotal
if $count_word and $opts =~ /c/i 0 153 153
Condition66%2/3
$count_word and $opts =~ /c/i
!ll&&!rl&&r
96570
MC/DC0%0/2
$count_word and $opts =~ /c/i
$count_word $opts =~ /c/i
Truth table66%2/3
A and B
A= $count_wordB= $opts =~ /c/i
ABresult
0X0
100
111
147  
148 153 ▶ my $count_thresh = $opts =~ /w(\d+)/i ? $1 : 11;
Branch: 1 branch
BranchTrueFalseTotal
$opts =~ /w(\d+)/i ? : 14 139 153
149  
150 # Otherwise, interpolate count or its equivalent (deferring fuzzies)...
151 153 ▶ return $opts =~ /n|s/i && $count == 0 ? 'no'
Branch: 6 branches
BranchTrueFalseTotal
$opts =~ /n|s/i && $count == 0 ? : 17 136 153
$opts =~ /w/i && $opts =~ /o/i ? : 0 136 136
$opts =~ /w/i ? : 50 86 136
$opts =~ /o/i ? : 0 86 86
$opts =~ /f/i ? : 24 62 86
$opts =~ /d/ ? : 8 54 62
Condition83%5/6
$opts =~ /n|s/i and $count == 0
!ll&&!rl&&r
795717
$opts =~ /w/i and $opts =~ /o/i
!ll&&!rl&&r
86500
MC/DC100%2/2
$opts =~ /n|s/i and $count == 0
$opts =~ /n|s/i $count == 0
MC/DC0%0/2
$opts =~ /w/i and $opts =~ /o/i
$opts =~ /w/i $opts =~ /o/i
Truth table100%3/3
A and B
A= $opts =~ /n|s/iB= $count == 0
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $opts =~ /w/iB= $opts =~ /o/i
ABresult
0X0
100
111
152 : $opts =~ /w/i && $opts =~ /o/i ? $count_word->ordinal($count_thresh)
153 : $opts =~ /w/i ? $count_word->cardinal($count_thresh)
154 : $opts =~ /o/i ? $count_word->ordinal(0)
155 : $opts =~ /f/i ? "<#f:$count>"
156 : $opts =~ /d/ ? q{}
157 : $count;
158 },
159 162 };
160  
161 # Inflect markups...
162 162 $string =~ s{ (?<ORIG>
163 < (?<FUNC> (?-i: [#NVA] ) ) # FUNC is case-sensitive
164 (?<OPTS> [^:]* ) \s*
165 : \s* (?<TERM> [^>]+? ) \s*
166 >
167 (?<TWS> \s* )
168 )
169 }{
170 442 my %parsed = %+;
171 442 my $opts = $normalize_opts->($parsed{OPTS});
172 0 ▶ my $func = $transform->{ uc $parsed{FUNC} } // sub{shift};
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$$transform{uc $parsed{"FUNC"}} // sub { shift(); }
l!l&&r!l&&!r
44200
MC/DC0%0/2
$$transform{uc $parsed{"FUNC"}} // sub { shift(); }
$$transform{uc $parsed{"FUNC"}} sub { shift(); }
Truth table33%1/3
A // B
A= $$transform{uc $parsed{"FUNC"}}B= sub { shift(); }
ABresult
1X1
011
000
173 442 my $replacement = $func->( $parsed{TERM}, $opts );
174 442 ▶ length $replacement > 0 ? $replacement . $parsed{TWS} : q{}
Branch: 1 branch
BranchTrueFalseTotal
length $replacement > 0 ? : 434 8 442
175 }gexmsi;
176  
177 # Inflect consequent A/AN's...
178 162 $string =~ s{ <[#]a:> \s*+ (?<next_word> \S++) }{ noun($+{next_word})->indefinite }gxe;
179 0 $string =~ s{ <[#]a:> \s*+ \Z }{ "a" }xe;
180  
181 # Inflect fuzzies...
182 24 ▶ state $fuzzy = sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
183 24 my ($count, $is_postfix) = @_;
184  
185 24 ▶ return $count >= 10 ? 'many'
Branch: 7 branches
BranchTrueFalseTotal
$count >= 10 ? : 4 20 24
$count >= 6 ? : 8 12 20
$count >= 3 ? : 6 6 12
$count == 2 ? : 2 4 6
$is_postfix ? : 1 1 2
$count == 1 ? : 2 2 4
$is_postfix ? : 1 1 2
186 : $count >= 6 ? 'several'
187 : $count >= 3 ? 'a few'
188 : $count == 2 ? 'a couple' . ($is_postfix ? q{} : ' of')
189 : $count == 1 ? 'one'
190 : ($is_postfix ? 'none' : 'no')
191 ;
192 162 };
193  
194 162 $string =~ s{ <\#f: (?<count> \d++) > (?= \s*+ [[:alpha:]]) }
195 12 { $fuzzy->($+{count}) }gxe;
196 162 $string =~ s{ <\#f: (?<count> \d++) > (?= [^[:alpha:]]*+ \Z) }
197 12 { $fuzzy->($+{count}, 'postfix') }xe;
198  
199 # And we're done...
200 162 return $string;
201 }
202  
203  
204 33 1; # Magic true value required at end of module
205 __END__
« Inflexion/Indefinite.pm ↑ Summary Inflexion/Term.pm »