Inflexion.pm

filter: Statementstmt 90.6% 87 / 96 Branchbran 60.0% 36 / 60 Conditioncond 63.8% 23 / 36 MC/DCmcdc 22.7% 5 / 22 Subroutinesub 83.3% 15 / 18 Podpod 80.0% 4 / 5 totaltotal 71.7% 170 / 237 CC 47 SCAR 29.7
CC
47
Cov
76%
__ANON__
72.0
__ANON__
24.2
wordlist
9.5
CRAP 19.5
« Inflexion/Nouns.pm ↑ Summary Inflexion/Indefinite.pm »
1 package Lingua::EN::Inflexion;
2 32 ▶use 5.010; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 32 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 32 our $VERSION = '0.002007';
6  
7 # Import noun, verb, and adj classes...
8 32 ▶use Lingua::EN::Inflexion::Term;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub import {
11 32 ▶ my (undef, @exports) = @_;
Subroutine import: called CC=5 SCAR=16.1
12  
13 # Export interface...
14 32 ▶ @exports = qw< noun verb adj inflect wordlist > if !@exports;
Branch: 1 branch
BranchTrueFalseTotal
unless @exports 31 1 32
15  
16 # Handle renames...
17 32 my %export_name;
18 32 ▶ @exports = map { ref eq 'HASH' ? do { @export_name{keys %$_} = values %$_; keys %$_ } : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'HASH' ? : 1 155 156
19 @exports;
20  
21 32 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 32 for my $func (@exports) {
23 158 ▶ *{caller().'::'.($export_name{$func}//$func)} = \&{$func};
Condition66%2/3
$export_name{$func} // $func
l!l&&r!l&&!r
31550
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 sub noun ($) {
30 21835 ▶ my ($noun) = @_;
Subroutine noun: called CC=1 SCAR=0.0
Pod documented
31 21835 return Lingua::EN::Inflexion::Noun->new($noun);
32 }
33  
34 # Verb constructor...
35 sub verb ($) {
36 4173 ▶ my ($verb) = @_;
Subroutine verb: called CC=1 SCAR=0.0
Pod documented
37 4173 return Lingua::EN::Inflexion::Verb->new($verb);
38 }
39  
40  
41 # Adjective constructor...
42 sub adj ($) {
43 152 ▶ my ($adj) = @_;
Subroutine adj: called CC=1 SCAR=0.0
Pod documented
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 sub wordlist {
50 29 ▶ my (@words, %opt);
Subroutine wordlist: called CC=9 SCAR=22.5
Pod undocumented
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 32 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 my $normalize_opts = sub {
93 64 ▶ my ($opts) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.6
94  
95 64 ▶ if ($opts =~ m{ [[:upper:]] }x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts =~ / [[:upper:]] /x) 0 64 64
96 0 $opts =~ s{ [[:lower:]] }{}gx;
97 }
98 64 return lc $opts;
99 32 };
100  
101 sub inflect($) {
102 22 ▶ my ($string) = @_;
Subroutine inflect: called CC=3 SCAR=12.0
Pod documented
103  
104 22 my $inflexion = 'singular';
105  
106 my $transform = {
107 'N' => sub{
108 21 ▶ my ($term, $opts) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.4
109 carp "Unknown '$_' option to <N:...> command"
110 21 for $opts =~ /([^cps])/;
111  
112 21 my $word = noun($term);
113 21 ▶ $word = $word->classical if $opts =~ /c/i;
Branch: 1 branch
BranchTrueFalseTotal
if $opts =~ /c/i 10 11 21
114  
115 21 ▶ return $opts =~ /p/i ? $word->plural
Branch: 2 branches
BranchTrueFalseTotal
$opts =~ /p/i ? : 0 21 21
$opts =~ /s/i ? : 0 21 21
116 : $opts =~ /s/i ? $word->singular
117 : $word->$inflexion;
118 },
119  
120 21 ▶ '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 '#' => sub{
125 22 ▶ my ($count, $opts) = @_;
Subroutine __ANON__: called CC=21 SCAR=31.9
126 22 $opts =~ s{e}{asw}g;
127 carp "Unknown '$_' option to <#:...> command"
128 22 for $opts =~ /([^acdefinosw\d])/;
129  
130 # Increment count if requested...
131 22 ▶ if ($opts =~ /i/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts =~ /i/i) 1 21 22
132 1 $count++;
133 }
134  
135 # Decide which inflexion the count requires...
136 $inflexion
137 22 ▶ = $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 ? : 4 18 22
Condition55%5/9
$count == 1 or $opts =~ /s/i and $count == 0 or $opts =~ /o/i
l!l&&r!l&&!r
4018
$count == 1 or $opts =~ /s/i and $count == 0
l!l&&r!l&&!r
4018
$opts =~ /s/i and $count == 0
!ll&&!rl&&r
1800
MC/DC25%1/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 table33%2/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 22 ▶ if ($count == 1 && $opts =~ /a/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1 and $opts =~ /a/i) 2 20 22
Condition100%3/3
$count == 1 and $opts =~ /a/i
!ll&&!rl&&r
1822
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 2 return "<#a:>";
143 }
144  
145 20 ▶ my $count_word = $opts =~ /w|o/i ? noun($count) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts =~ /w|o/i ? : 1 19 20
146 20 ▶ $count_word = $count_word->classical if $count_word && $opts =~ /c/i;
Branch: 1 branch
BranchTrueFalseTotal
if $count_word and $opts =~ /c/i 0 20 20
Condition66%2/3
$count_word and $opts =~ /c/i
!ll&&!rl&&r
1910
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 20 ▶ my $count_thresh = $opts =~ /w(\d+)/i ? $1 : 11;
Branch: 1 branch
BranchTrueFalseTotal
$opts =~ /w(\d+)/i ? : 1 19 20
149  
150 # Otherwise, interpolate count or its equivalent (deferring fuzzies)...
151 20 ▶ return $opts =~ /n|s/i && $count == 0 ? 'no'
Branch: 6 branches
BranchTrueFalseTotal
$opts =~ /n|s/i && $count == 0 ? : 2 18 20
$opts =~ /w/i && $opts =~ /o/i ? : 0 18 18
$opts =~ /w/i ? : 1 17 18
$opts =~ /o/i ? : 0 17 17
$opts =~ /f/i ? : 0 17 17
$opts =~ /d/ ? : 0 17 17
Condition83%5/6
$opts =~ /n|s/i and $count == 0
!ll&&!rl&&r
1082
$opts =~ /w/i and $opts =~ /o/i
!ll&&!rl&&r
1710
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 22 };
160  
161 # Inflect markups...
162 22 $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 64 my %parsed = %+;
171 64 my $opts = $normalize_opts->($parsed{OPTS});
172 64 ▶ 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
6400
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 64 my $replacement = $func->( $parsed{TERM}, $opts );
174 63 ▶ length $replacement > 0 ? $replacement . $parsed{TWS} : q{}
Branch: 1 branch
BranchTrueFalseTotal
length $replacement > 0 ? : 63 0 63
175 }gexmsi;
176  
177 # Inflect consequent A/AN's...
178 21 $string =~ s{ <[#]a:> \s*+ (?<next_word> \S++) }{ noun($+{next_word})->indefinite }gxe;
179 21 $string =~ s{ <[#]a:> \s*+ \Z }{ "a" }xe;
180  
181 # Inflect fuzzies...
182 state $fuzzy = sub {
183 0 ▶ my ($count, $is_postfix) = @_;
Subroutine __ANON__: not called CC=8 SCAR=42.8
184  
185 0 ▶ return $count >= 10 ? 'many'
Branch: 7 branches
BranchTrueFalseTotal
$count >= 10 ? : 0 0 0
$count >= 6 ? : 0 0 0
$count >= 3 ? : 0 0 0
$count == 2 ? : 0 0 0
$is_postfix ? : 0 0 0
$count == 1 ? : 0 0 0
$is_postfix ? : 0 0 0
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 21 };
193  
194 21 $string =~ s{ <\#f: (?<count> \d++) > (?= \s*+ [[:alpha:]]) }
195 0 { $fuzzy->($+{count}) }gxe;
196 21 $string =~ s{ <\#f: (?<count> \d++) > (?= [^[:alpha:]]*+ \Z) }
197 0 { $fuzzy->($+{count}, 'postfix') }xe;
198  
199 # And we're done...
200 21 return $string;
201 }
202  
203  
204 32 1; # Magic true value required at end of module
205 __END__
« Inflexion/Nouns.pm ↑ Summary Inflexion/Indefinite.pm »