Grammarian.pm

filter: Statementstmt 55.2% 205 / 371 Branchbran 56.4% 70 / 124 Conditioncond 59.0% 36 / 61 MC/DCmcdc 34.4% 10 / 29 Subroutinesub 37.2% 22 / 59 Podpod 77.7% 7 / 9 totaltotal 53.6% 350 / 653 CC 95 SCAR 29.9
CC
95
Cov
56%
_problem_in
132.0
__ANON__
56.0
_build_vim_regex_from
20.0
CRAP 19.9
↑ Summary demo.pl »
1 package Lingua::EN::Grammarian;
2 1 our $VERSION = '0.000005';
3  
4 1 ▶use 5.010; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Standard config files...
8 1 my $CAUTIONS_FILE = 'grammarian_cautions';
9 1 my $ERRORS_FILE = 'grammarian_errors';
10  
11 # Standard config file search path...
12 1 my @CONFIG_PATH = ( '/usr/local/share/grammarian/', "$ENV{HOME}/", "$ENV{PWD}/" );
13  
14  
15 # Export interface...
16 1 my @DEF_EXPORTS = qw<
17 extract_cautions_from
18 extract_errors_from
19 >;
20  
21 1 my @ALL_EXPORTS = (
22 @DEF_EXPORTS,
23 qw<
24 get_coverage_stats
25 get_error_at
26 get_next_error_at
27 get_caution_at
28 get_next_caution_at
29 get_vim_error_regexes
30 get_vim_caution_regexes
31 >
32 );
33  
34 1 ▶sub import {
Subroutine import: called CC=6 SCAR=18.6
35 1 my $self = shift;
36 1 ▶ my @exports = @_ ? @_ : @DEF_EXPORTS;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 1 1
37 1 my $caller = caller;
38 1 ▶ for my $exported_sub (map { /^:all$/i ? @ALL_EXPORTS : $_ } @exports) {
Branch: 1 branch
BranchTrueFalseTotal
/^:all$/i ? : 0 2 2
39 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 my $impl = *{$exported_sub}{CODE};
41 2 ▶ croak "$self does not provide $exported_sub()"
Branch: 1 branch
BranchTrueFalseTotal
unless $exported_sub =~ /^(?:get_|extract_)/ and $impl 0 2 2
Condition33%1/3
$exported_sub =~ /^(?:get_|extract_)/ and $impl
!ll&&!rl&&r
002
MC/DC0%0/2
$exported_sub =~ /^(?:get_|extract_)/ and $impl
$exported_sub =~ /^(?:get_|extract_)/ $impl
Truth table33%1/3
A and B
A= $exported_sub =~ /^(?:get_|extract_)/B= $impl
ABresult
0X0
100
111
42 if $exported_sub !~ /^(?:get_|extract_)/ || !$impl;
43 2 *{$caller.'::'.$exported_sub} = $impl;
44 }
45 }
46  
47 # The data extracted from those files...
48 1 my %CAUTIONS_FOR;
49 my $CAUTIONS_REGEX;
50 1 my @VIM_CAUTION_REGEX_COMPONENTS;
51  
52 1 my %CORRECTIONS_FOR;
53 1 my %EXPLANATION_FOR;
54 1 my $ERRORS_REGEX;
55 1 my @VIM_ERROR_REGEX_COMPONENTS;
56  
57 1 my $VIM_REGEX_MAX_LEN = 20000;
58  
59 # Improved \b...
60 1 my $SPACE_TRANSITION = qr{
61 # Preceded by... And followed by...
62 (?<=[[:space:][:punct:]]) (?=[^[:space:][:punct:]])
63 | \A (?=[^[:space:][:punct:]])
64 | (?<=[^[:space:][:punct:]]) (?=[[:space:][:punct:]]|\z)
65 }xms;
66  
67 # Extract that data...
68 1 ▶if (! _load_cautions()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_load_cautions()) 0 1 1
69 warn qq{No "grammarian_cautions" file found in config search path:\n}
70 . qq{\n}
71 0 . join(q{}, map { qq{ $_\n} } @CONFIG_PATH)
72 . qq{\n}
73 . qq{(Did you forget to install it from the distribution?)\n};
74 }
75  
76 1 ▶if (! _load_errors()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_load_errors()) 0 1 1
77 warn qq{No "grammarian_errors" file found in config search path:\n}
78 . qq{\n}
79 0 . join(q{}, map { qq{ $_\n} } @CONFIG_PATH)
80 . qq{\n}
81 . qq{(Did you forget to install it from the distribution?)\n};
82 }
83  
84 50106 ▶sub _rewrite (&$) {
Subroutine _rewrite: called CC=2 SCAR=6.9
85 50106 my ($transform, $text) = @_;
86 50106 $transform->() for $text;
87 50106 return $text;
88 }
89  
90 10612 ▶sub _inflect_term {
Subroutine _inflect_term: called CC=15 SCAR=27.1
91 10612 my ($term) = @_;
92  
93 10612 my $PRONOUN_MARKER = qr{
94 < (?: I | s?he | we | me | him | us | my | his | our | mine | hers | ours) >
95 }xms;
96  
97 10612 my %PRONOUN_EXPANSION_FOR = (
98 '<I>' => q{I,you,she,he,it,we,they},
99 '<she>' => q{she,he},
100 '<he>' => q{he,she},
101 '<we>' => q{we,you,they},
102 '<me>' => q{me,you,her,him,it,us,them},
103 '<her>' => q{her,him},
104 '<him>' => q{him,her},
105 '<us>' => q{us,you,them},
106 '<my>' => q{my,your,hers,his,its,our,their},
107 '<his>' => q{her,his},
108 '<our>' => q{our,your,their},
109 '<mine>' => q{mine,yours,hers,his,its,ours,theirs},
110 '<hers>' => q{hers,his},
111 '<ours>' => q{ours,yours,theirs},
112 );
113  
114  
115 # Preprocess <pronoun> expansions...
116 10612 my @components = split /($PRONOUN_MARKER)/, $term;
117 10612 $term = q{};
118 10612 my $in_parens = 0;
119 10612 ▶ while (@components) {
Branch: 1 branch
BranchTrueFalseTotal
if (@components) 10714 10612 21326
120 10714 my ($prefix, $pronoun) = splice(@components, 0, 2);
121 10714 $in_parens += ($prefix=~tr/(//) - ($prefix=~tr/)//);
122 10714 $term .= $prefix;
123 10714 ▶ if ($pronoun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pronoun) 170 10544 10714
124 $term .= ($in_parens ? '' : '(')
125 170 ▶ . ($PRONOUN_EXPANSION_FOR{$pronoun} // $pronoun)
Branch: 2 branches
BranchTrueFalseTotal
$in_parens ? : 21 149 170
$in_parens ? : 21 149 170
Condition33%1/3
$PRONOUN_EXPANSION_FOR{$pronoun} // $pronoun
l!l&&r!l&&!r
17000
MC/DC0%0/2
$PRONOUN_EXPANSION_FOR{$pronoun} // $pronoun
$PRONOUN_EXPANSION_FOR{$pronoun} $pronoun
Truth table33%1/3
A // B
A= $PRONOUN_EXPANSION_FOR{$pronoun}B= $pronoun
ABresult
1X1
011
000
126 . ($in_parens ? '' : ')')
127 }
128 }
129  
130 # Convert any parenthesized or starred set of alternatives...
131 10612 my @inflexions;
132 10612 $term =~ s{ (?<root> \S*? (?<last_letter> \S? ) )
133 (?:
134 (?<e_star> e [*] )
135 | (?<ch_star> ch [*] )
136 | (?<y_star> (?<= [^aeiou] ) y [*] )
137 | (?<y_s> (?<= [^aeiou] ) y [(] s [)] )
138 | (?<double_star> [*][*] )
139 | (?<star> [*] )
140 | [(] (?<alts> [^)]+ ) [)]
141 )
142 }
143 {
144 6665 my $ll = $+{last_letter};
145 @inflexions = $+{e_star} ? ( 'e', 'es', 'ed', 'ing')
146 : $+{ch_star} ? ( 'ch', 'ches', 'ched', 'ching')
147 : $+{y_star} ? ( 'y', 'ies', 'ied', 'ying')
148 : $+{y_s} ? ( 'y', 'ies', )
149 : $+{double_star} ? ( '', 's', $ll.'ed', $ll.'ing')
150 : $+{star} ? ( '', 's', 'ed', 'ing')
151 6665 ▶ : $+{alts} ? ( ($+{root} ? '' : ()), split(',', $+{alts}) )
Branch: 8 branches
BranchTrueFalseTotal
$+{'e_star'} ? : 115 6550 6665
$+{'ch_star'} ? : 4 6546 6550
$+{'y_star'} ? : 2 6544 6546
$+{'y_s'} ? : 4 6540 6544
$+{'double_star'} ? : 4 6536 6540
$+{'star'} ? : 112 6424 6536
$+{'alts'} ? : 6424 0 6424
$+{'root'} ? : 145 6279 6424
152 : ();
153  
154 6665 qq{$+{root}*};
155 }xmse;
156  
157 10612 ▶ return @inflexions ? map { my $infl = $term; $infl =~ s{[*]}{$_}; $infl} @inflexions
Branch: 1 branch
BranchTrueFalseTotal
@inflexions ? : 6665 3947 10612
158 : $term;
159 }
160  
161 # Parse cautions file and convert to internal data structures...
162 1 ▶sub _load_cautions {
Subroutine _load_cautions: called CC=14 SCAR=26.4
163 # Gather config from current directory and home directory...
164 6 local @ARGV = grep { -e }
165 1 map { ("$_.$CAUTIONS_FILE", "$_$CAUTIONS_FILE") }
166 @CONFIG_PATH;
167  
168 # If no config, we're done...
169 1 ▶ return if !@ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 1 1
170  
171 # Store sets of terms together...
172 1 my @term_sets = { terms => [], defns => [], inflexions => [] };
173  
174 # Parse configuration file...
175 LINE:
176 1 ▶ while (my $next_line = readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $next_line = readline ARGV)) 646 1 647
177 # Ignore comments...
178 646 ▶ next LINE if $next_line =~ m{ \A \h* [#] }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $next_line =~ / \A \h* [#] /msx 9 637 646
179  
180 # Blank lines delimit new term sets...
181 637 ▶ if ($next_line =~ m{\A \h* \Z}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line =~ /\A \h* \Z/msx) 208 429 637
182 208 push @term_sets, { terms => [], defns => [], inflexions => [] };
183 208 next LINE;
184 }
185  
186 # Parse config line...
187 429 $next_line =~ m{
188 \A
189 (?<is_silent> -? )
190 \h* (?<term> [^:]*? )
191 (?:
192 \h* :
193 \h* (?<defn> .*? )
194 )?
195 \h*
196 \Z
197 }xms;
198  
199 # Unpack components...
200 429 my $term = $+{term};
201 429 ▶ my $defn = $+{defn} // q{};
Condition100%2/2
$+{"defn"} // ""
l!l
4227
MC/DC100%1/1
$+{"defn"} // ""
$+{"defn"}
Truth table100%2/2
A
A= $+{"defn"}
Aresult
11
00
202 429 my $is_silent = length($+{is_silent});
203  
204 # Warn of bad config...
205 429 ▶ if (!defined $term) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $term) 0 429 429
206 0 warn "Invalid entry in grammarian_cautions: $next_line";
207 0 next LINE;
208 }
209  
210 # Unpack any inflexions...
211 429 my @inflexions = _inflect_term($term);
212  
213 429 my $original = shift @inflexions;
214 429 ▶ if ($defn =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defn =~ /\S/) 422 7 429
215 422 push @{$term_sets[-1]{terms}}, $original;
216 422 push @{$term_sets[-1]{defns}}, $defn;
217 }
218  
219 # Store patterns to be matched...
220 429 my $order = 0;
221 429 for my $next_inflexion ($original, @inflexions) {
222 877 push @{ $term_sets[-1]{inflexions}[$order++] }, {silent => $is_silent, term => $next_inflexion};
223 }
224 }
225  
226  
227 # Compile list of cautions and the matching regex...
228 1 my @regex_components;
229 TERM_SET:
230 1 for my $term_set (@term_sets) {
231 209 ▶ next TERM_SET if !@{ $term_set->{terms} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$term_set->{"terms"};} 5 204 209
232  
233 1 ▶ use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 204 my $term_width = max map { length } @{ $term_set->{terms} };
235  
236 my $caution
237 = join q{},
238 422 map { sprintf("%-*s : %s\n", $term_width, $term_set->{terms}[$_], $term_set->{defns}[$_]) }
239 204 0..$#{ $term_set->{terms} };
240  
241 204 for my $inflexion_set (@{ $term_set->{inflexions} }) {
242 419 my $inflexions = [ map { $_->{term} } @{ $inflexion_set } ];
243 419 for my $term_data (@{ $inflexion_set }) {
244 877 my $term = $term_data->{term};
245 877 my $silent = $term_data->{silent};
246 877 $CAUTIONS_FOR{lc $term} = {
247 display => $silent,
248 explanation => $caution,
249 inflexions => $inflexions
250 };
251 877 ▶ if (!$silent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($silent) 752 125 877
252 752 ▶ push @regex_components, _rewrite { s{\h+}{\\s+}g } $term;
Subroutine __ANON__: called CC=1 SCAR=0.0
253 752 ▶ push @VIM_CAUTION_REGEX_COMPONENTS, _rewrite { s{\h+}{\\_s\\+}g } $term;
Subroutine __ANON__: called CC=1 SCAR=0.0
254 }
255 }
256 }
257  
258 }
259  
260 1 my $cautions_regex = '\b(?<term>' . join('|', reverse sort @regex_components) . ')\b';
261 1 $CAUTIONS_REGEX = qr{$cautions_regex}i;
262  
263 1 return 1;
264 }
265  
266 250 ▶sub _gen_pres_participle_for {
Subroutine _gen_pres_participle_for: called CC=12 SCAR=25.2
267 250 my ($verb) = @_;
268  
269 250 ▶ $verb =~ s/ie$/y/
Branch: 1 branch
BranchTrueFalseTotal
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/ or $verb =~ s/^(had)$/hav/ or $verb =~ s/(hoe)$/$1/ or $verb =~ s/([^e])e$/$1/ or $verb =~ /er$/ or $verb =~ /open$/ or $verb =~ s/([^aeiou][aeiouy]([bdgmnprst]))$/$1$2/ 58 192 250
Condition83%25/30
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/ or $verb =~ s/^(had)$/hav/ or $verb =~ s/(hoe)$/$1/ or $verb =~ s/([^e])e$/$1/ or $verb =~ /er$/ or $verb =~ /open$/
l!l&&r!l&&!r
571192
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/ or $verb =~ s/^(had)$/hav/ or $verb =~ s/(hoe)$/$1/ or $verb =~ s/([^e])e$/$1/ or $verb =~ /er$/
l!l&&r!l&&!r
543193
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/ or $verb =~ s/^(had)$/hav/ or $verb =~ s/(hoe)$/$1/ or $verb =~ s/([^e])e$/$1/
l!l&&r!l&&!r
450196
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/ or $verb =~ s/^(had)$/hav/ or $verb =~ s/(hoe)$/$1/
l!l&&r!l&&!r
31246
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/ or $verb =~ s/^(had)$/hav/
l!l&&r!l&&!r
30247
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/
l!l&&r!l&&!r
30247
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$//
l!l&&r!l&&!r
30247
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/
l!l&&r!l&&!r
30247
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/
l!l&&r!l&&!r
30247
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/
l!l&&r!l&&!r
21247
MC/DC54%6/11
$verb =~ s/ie$/y/ or $verb =~ s/ue$/u/ or $verb =~ s/([auy])e$/$1/ or $verb =~ s/ski$/ski/ or $verb =~ s/[^b]i$// or $verb =~ s/^(are|were)$/be/ or $verb =~ s/^(had)$/hav/ or $verb =~ s/(hoe)$/$1/ or $verb =~ s/([^e])e$/$1/ or $verb =~ /er$/ or $verb =~ /open$/
$verb =~ s/ie$/y/ $verb =~ s/ue$/u/ $verb =~ s/([auy])e$/$1/ $verb =~ s/ski$/ski/ $verb =~ s/[^b]i$// $verb =~ s/^(are|were)$/be/ $verb =~ s/^(had)$/hav/ $verb =~ s/(hoe)$/$1/ $verb =~ s/([^e])e$/$1/ $verb =~ /er$/ $verb =~ /open$/
Truth table58%7/12
A or B or C or D or E or F or G or H or I or J or K
A= $verb =~ s/ie$/y/B= $verb =~ s/ue$/u/C= $verb =~ s/([auy])e$/$1/D= $verb =~ s/ski$/ski/E= $verb =~ s/[^b]i$//F= $verb =~ s/^(are|were)$/be/G= $verb =~ s/^(had)$/hav/H= $verb =~ s/(hoe)$/$1/I= $verb =~ s/([^e])e$/$1/J= $verb =~ /er$/K= $verb =~ /open$/
ABCDEFGHIJKresult
1XXXXXXXXXX1
01XXXXXXXXX1
001XXXXXXXX1
0001XXXXXXX1
00001XXXXXX1
000001XXXXX1
0000001XXXX1
00000001XXX1
000000001XX1
0000000001X1
000000000011
000000000000
270 or $verb =~ s/ue$/u/
271 or $verb =~ s/([auy])e$/$1/
272 or $verb =~ s/ski$/ski/
273 or $verb =~ s/[^b]i$//
274 or $verb =~ s/^(are|were)$/be/
275 or $verb =~ s/^(had)$/hav/
276 or $verb =~ s/(hoe)$/$1/
277 or $verb =~ s/([^e])e$/$1/
278 or $verb =~ m/er$/
279 or $verb =~ m/open$/
280 or $verb =~ s/([^aeiou][aeiouy]([bdgmnprst]))$/$1$2/;
281  
282 250 return "${verb}ing";
283 }
284  
285 250 ▶sub _gen_verb_errors {
Subroutine _gen_verb_errors: called CC=5 SCAR=16.1
286 250 my ($pres, $third, $past, $pastp, $presp) = @_;
287  
288 return (
289 250 ▶ ($pres ne $third ? (
Branch: 4 branches
BranchTrueFalseTotal
$pres ne $third ? : 249 1 250
$past ne $pastp ? : 77 173 250
$third ne $pres ? : 249 1 250
$past ne $pastp ? : 77 173 250
290 "====[ Incorrect inflexion of verb for the specified pronoun ]=================",
291 " (he,she,it) $pres --> (he,she,it) $third ",
292 " (I,you,we,they) $third --> (I,you,we,they) $pres ",
293  
294 "====[ Incorrect inflexion of verb after a negated auxiliary ]=================",
295 "(did,would,should,could,must,might)n't $third "
296 ." --> (did,would,should,could,must,might)n't $pres",
297 ):()),
298  
299 "====[ Incorrect inflexion of verb after a negated auxiliary ]=================",
300 "(did,would,should,could,must,might)n't $past "
301 ." --> (did,would,should,could,must,might)n't $pres",
302  
303 ($past ne $pastp ? (
304 "====[ Incorrect use of participle instead of simple past or past perfect ]====",
305 " (I,you,we,they) $pastp --> (I,you,we,they) $past "
306 ." --> (I,you,we,they) have $pastp "
307 ." --> (I,you,we,they) had $pastp ",
308  
309 " (he,she,it) $pastp --> (he,she,it) $past "
310 ." --> (he,she,it) has $pastp "
311 ." --> (he,she,it) had $pastp ",
312  
313 "====[ Incorrect use of simple past instead of past participle ]================",
314 " (be,being,been,was,were) $past --> (be,being,been,was,were) $pastp ",
315 " (has,had,have,having) $past --> (has,had,have,having) $pastp ",
316 " (be,being,been,was,were) $past --> (be,being,been,was,were) $pastp ",
317  
318 "====[ Incorrect inflexion of verb after a negated auxiliary ]=================",
319 "(did,would,should,could,must,might)n't $pastp "
320 ." --> (did,would,should,could,must,might)n't $pres",
321 ):()),
322  
323 "====[ Incorrect use of infinitive instead of past participle ]=================",
324 " (be,being,been,was,were) $pres --> (be,being,been,was,were) $pastp ",
325 " (has,had,have,having) $pres --> (has,had,have,having) $pastp ",
326  
327 "====[ Incorrect use of participle instead of infinitive ]=================",
328 " to ($pastp,$presp) --> to $pres ",
329 ($third ne $pres ?
330 " to $third --> to $pres "
331 :()),
332 ($past ne $pastp ?
333 " to $past --> to $pres "
334 :()),
335  
336 "====[ Incorrect use of present participle instead of past participle ]=========",
337 " being $presp --> being $pastp ",
338  
339 "====[ Incorrect use of \"try and\" instead of \"try to\" ]=====================",
340 "try and ($pres,$past,$pastp,$presp) --> try to $pres ",
341  
342 "====[ Incorrect inflexion of verb after \"try to\" ]===========================",
343 " try to ($past,$pastp,$presp) --> try to $pres ",
344 " tried to ($past,$pastp,$presp) --> tried to $pres ",
345 " trying to ($past,$pastp,$presp) --> trying to $pres ",
346 );
347 }
348  
349 19 ▶sub _gen_absolute_adjective_errors {
Subroutine _gen_absolute_adjective_errors: called CC=4 SCAR=13.9
350 19 my ($adj, $modifier) = @_;
351 19 ▶ $modifier //= '';
Condition100%2/2
$modifier //= ""
l!l
514
MC/DC100%1/1
$modifier //= ""
$modifier
Truth table100%2/2
A
A= $modifier
Aresult
11
00
352  
353 19 my @QUALIFIERS = qw<
354 somewhat highly extremely totally completely absolutely utterly
355 >;
356 19 my $QUALIFIERS = '(' . join(',', @QUALIFIERS) . ')';
357  
358 19 my @errors = (
359 "====[ Incorrect use of modifier with ungradeable adjective ]===================",
360 " more $adj --> $adj ",
361 " most $adj --> $adj ",
362 " quite $adj --> $adj ",
363 " rather $adj --> $adj ",
364 " very $adj --> $adj ",
365 " $QUALIFIERS $adj --> $adj ",
366 );
367  
368 19 ▶ if ($modifier) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifier) 5 14 19
369 5 $modifier =~ s{ \A [(] | [)] \z}{}xgms;
370 5 for my $mod (split(',', $modifier)) {
371 6 $errors[1] .= " --> more $mod $adj";
372 6 $errors[2] .= " --> most $mod $adj";
373 6 $errors[3] .= " --> quite $mod $adj";
374 6 $errors[4] .= " --> rather $mod $adj";
375 6 $errors[5] .= " --> very $mod $adj";
376 }
377 }
378  
379 19 return @errors;
380 }
381  
382 1 ▶sub _load_errors {
Subroutine _load_errors: called CC=13 SCAR=25.7
383 # Gather config from search path
384 6 local @ARGV = grep { -e }
385 1 map { ("$_.$ERRORS_FILE", "$_$ERRORS_FILE") }
386 @CONFIG_PATH;
387  
388 # If no config, we're done...
389 1 ▶ return if !@ARGV;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 1 1
390  
391 # Extract corrections...
392 1 my @regex_components;
393 1 my $explanation = '????';
394 1 my $last_was_explanation = 1;
395 1 my @insertions;
396 LINE:
397 1 ▶ while (my $next_line = shift(@insertions) // readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next_line = shift @insertions // readline ARGV) 7666 1 7667
Condition100%3/3
shift @insertions // readline ARGV
l!l&&r!l&&!r
614715191
MC/DC100%2/2
shift @insertions // readline ARGV
shift @insertions readline ARGV
Truth table100%3/3
A // B
A= shift @insertionsB= readline ARGV
ABresult
1X1
011
000
398  
399 # Ignore comment and empty lines...
400 7666 ▶ next LINE if $next_line =~ m{\A \h* (?: [#] | \Z )}xms;
Branch: 1 branch
BranchTrueFalseTotal
if $next_line =~ /\A \h* (?: [#] | \Z )/msx 268 7398 7666
401  
402 # Handle explanation lines...
403 7398 ▶ if ($next_line =~ m{\A \h* ===\S* \h* (.*?) \h* \S*===.* \Z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line =~ /\A \h* ===\S* \h* (.*?) \h* \S*===.* \Z /msx) 2316 5082 7398
404 2316 ▶ $explanation = $last_was_explanation ? "$explanation\n$1" : $1;
Branch: 1 branch
BranchTrueFalseTotal
$last_was_explanation ? : 1 2315 2316
405 2316 $last_was_explanation = 1;
406 2316 next LINE;
407 }
408 5082 $last_was_explanation = 0;
409  
410 # Generate errors from a <verb> specification...
411 5082 ▶ if ($next_line =~ m{\A\h* <verb> \h* (?<pres>\S+) \h* (?<third>\S+) \h* (?<past>\S+) \h* (?<part>\S+)}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line =~ /\A\h* <verb> \h* (?<pres>\S+) \h* (?<third>\S+) \h* (?<past>\S+) \h* (?<part>\S+)/msx) 250 4832 5082
412 250 push @insertions, _gen_verb_errors(@+{qw<pres third past part>}, _gen_pres_participle_for($+{pres}));
413 250 next LINE;
414 }
415  
416 # Generate errors from an <absolute> specification...
417 4832 ▶ if ($next_line =~ m{\A\h* <absolute (?: \h*:\h* (?<modifier> \S+) \h*)?> \h* (?<adjective>\S+) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_line =~ /\A\h* <absolute (?: \h*:\h* (?<modifier> \S+) \h*)?> \h* (?<adjective>\S+) /msx) 19 4813 4832
418 19 push @insertions, _gen_absolute_adjective_errors( @+{qw< adjective modifier >} );
419 19 next LINE;
420 }
421  
422 # Extract error --> correction pair...
423 4813 $next_line =~ m{
424 \A \h*
425 (?<error> .*? )
426 \h* --> \h*
427 (?<correction> .*? )
428 \h* \Z
429 }xms;
430 4813 my ($error, $correction) = @+{'error', 'correction'};
431  
432 # Ignore invalid lines...
433 4813 ▶ next LINE if !defined $error;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $error 0 4813 4813
434  
435 # Expand inflected forms...
436 4813 my @error_inflexions = _inflect_term($error);
437 my @corrections_inflections
438 4813 = map {[_inflect_term($_)]}
439 split /\h+-->\h+/,
440 $correction;
441  
442 # Iterated inflections in parallel...
443 4813 for my $next (0..$#error_inflexions) {
444 15639 my $error = $error_inflexions[$next];
445  
446 # Build normalized transform from error to each correction...
447 15639 for my $correction (@corrections_inflections) {
448 17324 ▶ my $normalized_error = _rewrite { s{\h+}{ }gxms } lc $error;
Subroutine __ANON__: called CC=1 SCAR=0.0
449 17324 ▶ push @{$CORRECTIONS_FOR{$normalized_error}},
Condition66%2/3
$correction->[$next] // $correction->[-1]
l!l&&r!l&&!r
1445928650
MC/DC0%0/2
$correction->[$next] // $correction->[-1]
$correction->[$next] $correction->[-1]
Truth table66%2/3
A // B
A= $correction->[$next]B= $correction->[-1]
ABresult
1X1
011
000
450 $correction->[$next] // $correction->[-1];
451  
452 # Record explanation...
453 17324 $EXPLANATION_FOR{$normalized_error} = $explanation;
454 }
455  
456 # Remember error for eventual regexes (with generalized whitespace)...
457 15639 ▶ push @regex_components, _rewrite { s{\h+}{\\s+}g } $error;
Subroutine __ANON__: called CC=1 SCAR=0.0
458 15639 ▶ push @VIM_ERROR_REGEX_COMPONENTS, _rewrite { s{\h+}{\\_s\\+}g } $error;
Subroutine __ANON__: called CC=1 SCAR=0.0
459 }
460 }
461  
462 # Build error-detecting regex...
463 1 my $ERRORONEOUS_TERM = join('|', reverse sort @regex_components);
464 1 $ERRORS_REGEX = qr{
465 $SPACE_TRANSITION
466 ( $ERRORONEOUS_TERM | (?&REPEATED_WORD) )
467 $SPACE_TRANSITION
468  
469 (?(DEFINE)
470 (?<REPEATED_WORD> (?<WORD> \S++) \s++ \k<WORD> )
471 )
472 }ixms;
473  
474 1 return 1;
475 }
476  
477 # Apply regexes to detect offending terms...
478 0 ▶sub extract_cautions_from {
Subroutine extract_cautions_from: not called CC=3 SCAR=24.8
Pod documented
479 0 my ($text) = @_;
480  
481 0 state %cautions_cache;
482 0 ▶ if (!exists $cautions_cache{$text}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cautions_cache{$text}) 0 0 0
483 0 my $cache = $cautions_cache{$text} = [];
484 0 ▶ while ($text =~ m{\G .*? $CAUTIONS_REGEX}gcxms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G .*? $CAUTIONS_REGEX/cgmsx) 0 0 0
485 0 push @{$cache}, Lingua::EN::Grammarian::Caution->new($1,\$text);
486 }
487 }
488  
489 0 return @{ $cautions_cache{$text} };
490 }
491  
492 0 ▶sub extract_errors_from {
Subroutine extract_errors_from: not called CC=3 SCAR=24.8
Pod documented
493 0 my ($text) = @_;
494  
495 0 state %errors_cache;
496 0 ▶ if (!exists $errors_cache{$text}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $errors_cache{$text}) 0 0 0
497 0 my $cache = $errors_cache{$text} = [];
498 0 ▶ while ($text =~ m{\G .*? $ERRORS_REGEX}gcxms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G .*? $ERRORS_REGEX/cgmsx) 0 0 0
499 0 push @{$cache}, Lingua::EN::Grammarian::Error->new($1,\$text);
500 }
501 }
502  
503 0 return @{ $errors_cache{$text} };
504 }
505  
506 # Report coverage...
507 0 ▶sub get_coverage_stats {
Subroutine get_coverage_stats: not called CC=1 SCAR=6.9
Pod documented
508 return {
509 0 cautions => scalar keys %CAUTIONS_FOR,
510 errors => scalar keys %CORRECTIONS_FOR,
511 }
512 }
513  
514 # Identify offences (if any) at a particular location...
515  
516 0 ▶sub get_error_at {
Subroutine get_error_at: not called CC=1 SCAR=6.9
Pod documented
517 0 my ($text, $index_or_line, $col) = @_;
518 0 return _problem_in($text, [extract_errors_from($text)], $index_or_line, $col,\do{my $no_next});
519 }
520  
521 0 ▶sub get_next_error_at {
Subroutine get_next_error_at: not called CC=1 SCAR=6.9
Pod undocumented
522 0 my ($text, $index_or_line, $col) = @_;
523 0 state $prev_error_index = -1;
524 0 return _problem_in($text, [extract_errors_from($text)], $index_or_line, $col,\$prev_error_index);
525 }
526  
527 0 ▶sub get_caution_at {
Subroutine get_caution_at: not called CC=1 SCAR=6.9
Pod documented
528 0 my ($text, $index_or_line, $col) = @_;
529 0 return _problem_in($text, [extract_cautions_from($text)], $index_or_line, $col,\do{my $no_next});
530 }
531  
532 0 ▶sub get_next_caution_at {
Subroutine get_next_caution_at: not called CC=1 SCAR=6.9
Pod undocumented
533 0 my ($text, $index_or_line, $col) = @_;
534 0 state $prev_caution_index = -1;
535 0 return _problem_in($text, [extract_cautions_from($text)], $index_or_line, $col,\$prev_caution_index);
536 }
537  
538 0 ▶sub _problem_in {
Subroutine _problem_in: not called CC=11 SCAR=48.8
539 0 my ($text, $problems_ref, $index_or_line, $col, $prev_findex_ref) = @_;
540  
541 # Convert line/col to index...
542 0 ▶ if (defined $col) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col) 0 0 0
543 0 $index_or_line -= 1;
544 0 ▶ $text =~ m{( \A (?: [^\n]* \n){$index_or_line} [^\n]{$col} )}xms
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /( \A (?: [^\n]* \n){$index_or_line} [^\n]{$col} )/msx or return 0 0 0
545 or return;
546 0 $index_or_line = length($1);
547 }
548  
549 # Look for a hit...
550 0 for my $problem (@{$problems_ref}) {
551 0 my $findex = $problem->from->{index};
552 0 my $tindex = $problem->to->{index};
553  
554 # Cursor is "in" a problem...
555 0 ▶ if ($findex <= $index_or_line && $index_or_line <= $tindex && $findex != (${$prev_findex_ref} // -1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($findex <= $index_or_line and $index_or_line <= $tindex and $findex != (${$prev_findex_ref;} // -1)) { } 0 0 0
elsif ($findex > $index_or_line) { } 0 0 0
Condition0%0/8
$findex <= $index_or_line and $index_or_line <= $tindex and $findex != (${$prev_findex_ref;} // -1)
!ll&&!rl&&r
000
$findex <= $index_or_line and $index_or_line <= $tindex
!ll&&!rl&&r
000
${$prev_findex_ref;} // -1
l!l
00
MC/DC0%0/3
$findex <= $index_or_line and $index_or_line <= $tindex and $findex != (${$prev_findex_ref;} // -1)
$findex <= $index_or_line $index_or_line <= $tindex $findex != (${$prev_findex_ref;} // -1)
MC/DC0%0/1
${$prev_findex_ref;} // -1
${$prev_findex_ref;}
Truth table0%0/4
A and B and C
A= $findex <= $index_or_lineB= $index_or_line <= $tindexC= $findex != (${$prev_findex_ref;} // -1)
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= ${$prev_findex_ref;}
Aresult
11
00
556 0 ${$prev_findex_ref} = $findex;
557 0 ▶ return wantarray ? ($problem, 1) # There's a problem and the cursor *is* over it
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
558 : $problem;
559 }
560  
561 # Cursor not in a problem, so return next problem...
562 elsif ($findex > $index_or_line) {
563 0 ${$prev_findex_ref} = $findex;
564 0 ▶ return wantarray ? ($problem, 0) # There's a problem and the cursor *isn't* over it
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
565 : undef;
566 }
567 }
568  
569 # Otherwise, it's a miss...
570 0 return;
571 }
572  
573  
574 # Provide regexes for matching grammar problems in Vim...
575  
576 0 ▶sub get_vim_error_regexes {
Subroutine get_vim_error_regexes: not called CC=1 SCAR=6.9
Pod documented
577 0 _build_vim_regex_from(@VIM_ERROR_REGEX_COMPONENTS);
578 }
579  
580 0 ▶sub get_vim_caution_regexes {
Subroutine get_vim_caution_regexes: not called CC=1 SCAR=6.9
Pod documented
581 0 _build_vim_regex_from(@VIM_CAUTION_REGEX_COMPONENTS);
582 }
583  
584 0 ▶sub _build_vim_regex_from {
Subroutine _build_vim_regex_from: not called CC=4 SCAR=30.0
585 0 my @regex_components = reverse sort @_;
586  
587 0 my @regexes;
588 0 for my $alternative (@regex_components) {
589 0 $alternative =~ s/'/''/g;
590 0 ▶ if (@regexes && length($regexes[-1]) + length($alternative) + 10 < $VIM_REGEX_MAX_LEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (@regexes and length($regexes[-1]) + length($alternative) + 10 < $VIM_REGEX_MAX_LEN) { } 0 0 0
Condition0%0/3
@regexes and length($regexes[-1]) + length($alternative) + 10 < $VIM_REGEX_MAX_LEN
!ll&&!rl&&r
000
MC/DC0%0/2
@regexes and length($regexes[-1]) + length($alternative) + 10 < $VIM_REGEX_MAX_LEN
@regexes length($regexes[-1]) + length($alternative) + 10 < $VIM_REGEX_MAX_LEN
Truth table0%0/3
A and B
A= @regexesB= length($regexes[-1]) + length($alternative) + 10 < $VIM_REGEX_MAX_LEN
ABresult
0X0
100
111
591 0 $regexes[-1] .= '\\|' . $alternative;
592 }
593 else {
594 0 push @regexes, '\\c' . $alternative;
595 }
596 }
597 0 return map { '\<\%('.$_.'\)\>' } @regexes;
598 }
599  
600  
601 1 my $UPPER_CASE_PAT = qr{\A [[:upper:]]* \Z}xms;
602 1 my $LOWER_CASE_PAT = qr{\A [[:lower:]]* \Z}xms;
603 1 my $TITLE_CASE_PAT = qr{\A [[:upper:]][[:lower:]]* \Z}xms;
604  
605 # Convert a term to have the same capitalization as an original paradigm...
606 0 ▶my $_recase_like = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
607 0 my ($paradigm, $target) = @_;
608  
609 # Process two strings word-by-word...
610 0 my @paradigm_words = split($SPACE_TRANSITION, $paradigm);
611 0 my @target_words = split($SPACE_TRANSITION, $target );
612  
613 0 ▶ while (@paradigm_words < @target_words) {
Branch: 1 branch
BranchTrueFalseTotal
if (@paradigm_words < @target_words) 0 0 0
614 0 push @paradigm_words, $paradigm_words[-1];
615 }
616  
617 # Accumulate modified target by transforming each word...
618 0 my $modified_target = "";
619 0 for my $next_paradigm (@paradigm_words) {
620 # If target completely processed, we're done...
621 0 ▶ last if !@target_words;
Branch: 1 branch
BranchTrueFalseTotal
unless @target_words 0 0 0
622  
623 # Otherwise, convert target according to pattern of paradigm...
624 0 ▶ $modified_target .= $next_paradigm =~ $UPPER_CASE_PAT ? uc(shift @target_words)
Branch: 3 branches
BranchTrueFalseTotal
$next_paradigm =~ /$UPPER_CASE_PAT/ ? : 0 0 0
$next_paradigm =~ /$LOWER_CASE_PAT/ ? : 0 0 0
$next_paradigm =~ /$TITLE_CASE_PAT/ ? : 0 0 0
625 : $next_paradigm =~ $LOWER_CASE_PAT ? lc(shift @target_words)
626 : $next_paradigm =~ $TITLE_CASE_PAT ? ucfirst(lc(shift @target_words))
627 : shift @target_words
628 ;
629 }
630  
631 0 return $modified_target;
632 1 };
633  
634 package Lingua::EN::Grammarian::Error; {
635 1 ▶ use Hash::Util::FieldHash 'fieldhash';
Subroutine BEGIN: called CC=1 SCAR=0.0
636 1 *_rewrite = *Lingua::EN::Grammarian::_rewrite;
637  
638 1 fieldhash my %match_for;
639 1 fieldhash my %startpos_for;
640 1 fieldhash my %endpos_for;
641  
642 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
643 0 my ($class, $term, $source_ref) = @_;
644  
645 0 my $newobj = bless \do{my $scalar}, $class;
646  
647 0 my $endindex = pos(${$source_ref}) - 1;
648 0 my $startindex = pos(${$source_ref}) - length($term);
649 0 my $startline = 1 + substr(${$source_ref},0,$startindex) =~ tr/\n//;
650 0 my $endline = 1 + substr(${$source_ref},0,$endindex) =~ tr/\n//;
651 0 ▶ my $startcol = 1 + length(Lingua::EN::Grammarian::_rewrite {s{\A.*\n}{}xms} substr(${$source_ref},0,$startindex));
Subroutine __ANON__: not called CC=1 SCAR=6.9
652 0 ▶ my $endcol = 1 + length(Lingua::EN::Grammarian::_rewrite {s{\A.*\n}{}xms} substr(${$source_ref},0,$endindex));
Subroutine __ANON__: not called CC=1 SCAR=6.9
653  
654 0 $match_for{$newobj} = $term;
655 0 $startpos_for{$newobj} = { index => $startindex, line => $startline, column => $startcol };
656 0 $endpos_for{$newobj} = { index => $endindex, line => $endline, column => $endcol };
657  
658 0 return $newobj;
659 }
660  
661 0 ▶ sub match { my $self = shift; return $match_for{$self} }
Subroutine match: not called CC=1 SCAR=6.9
662 0 ▶ use overload q{""} => sub { my $self = shift; return $match_for{$self} };
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=0.6
663  
664 0 ▶ sub from { my $self = shift; return $startpos_for{$self} }
Subroutine from: not called CC=1 SCAR=6.9
665 0 ▶ sub to { my $self = shift; return $endpos_for{$self} }
Subroutine to: not called CC=1 SCAR=6.9
666  
667 0 ▶ sub explanation {
Subroutine explanation: not called CC=2 SCAR=17.9
668 0 my $self = shift;
669 0 ▶ return $EXPLANATION_FOR{lc Lingua::EN::Grammarian::_rewrite {s{\s+}{ }g} $match_for{$self}}
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/2
$EXPLANATION_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $match_for{$self})} // "Repeated word"
l!l
00
MC/DC0%0/1
$EXPLANATION_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $match_for{$self})} // "Repeated word"
$EXPLANATION_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $match_for{$self})}
Truth table0%0/2
A
A= $EXPLANATION_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $match_for{$self})}
Aresult
11
00
670 // "Repeated word";
671 }
672  
673 0 ▶ sub explanation_hash {
Subroutine explanation_hash: not called CC=1 SCAR=6.9
674 0 return {};
675 }
676  
677 0 ▶ sub suggestions {
Subroutine suggestions: not called CC=3 SCAR=24.8
678 0 my $self = shift;
679 0 my $term = $match_for{$self};
680  
681 # Locate suggestions...
682 my $corrections_ref
683 0 ▶ = $CORRECTIONS_FOR{lc Lingua::EN::Grammarian::_rewrite {s{\s+}{ }g} $term}
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$term =~ /\A (\S+) \s+ \1 \z/imsx ? : 0 0 0
Condition0%0/2
$CORRECTIONS_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $term)} // [$term =~ /\A (\S+) \s+ \1 \z/imsx ? $1 : ()]
l!l
00
MC/DC0%0/1
$CORRECTIONS_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $term)} // [$term =~ /\A (\S+) \s+ \1 \z/imsx ? $1 : ()]
$CORRECTIONS_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $term)}
Truth table0%0/2
A
A= $CORRECTIONS_FOR{lc &Lingua::EN::Grammarian::_rewrite(sub { s/\s+/ /g; } , $term)}
Aresult
11
00
684 // [$term =~ m{\A (\S+) \s+ \1 \z}ixms ? $1 : () ];
685  
686 # Adjust their casings...
687 0 return map { $_recase_like->($term, $_) } @{$corrections_ref};
688 }
689 }
690  
691 1 package Lingua::EN::Grammarian::Caution; {
692 1 ▶ use Hash::Util::FieldHash 'fieldhash';
Subroutine BEGIN: called CC=1 SCAR=0.0
693  
694 1 fieldhash my %match_for;
695 1 fieldhash my %startpos_for;
696 1 fieldhash my %endpos_for;
697  
698 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
699 0 my ($class, $term, $source_ref) = @_;
700  
701 0 my $newobj = bless \do{my $scalar}, $class;
702  
703 0 my $endindex = pos(${$source_ref}) - 1;
704 0 my $startindex = pos(${$source_ref}) - length($term);
705 0 my $startline = 1 + substr(${$source_ref},0,$startindex) =~ tr/\n//;
706 0 my $endline = 1 + substr(${$source_ref},0,$endindex) =~ tr/\n//;
707 0 ▶ my $startcol = 1 + length(Lingua::EN::Grammarian::_rewrite { s{\A.*\n}{}xms } substr(${$source_ref},0,$startindex));
Subroutine __ANON__: not called CC=1 SCAR=6.9
708 0 ▶ my $endcol = 1 + length(Lingua::EN::Grammarian::_rewrite { s{\A.*\n}{}xms } substr(${$source_ref},0,$endindex));
Subroutine __ANON__: not called CC=1 SCAR=6.9
709  
710 0 $match_for{$newobj} = $term;
711 0 $startpos_for{$newobj} = { index => $startindex, line => $startline, column => $startcol };
712 0 $endpos_for{$newobj} = { index => $endindex, line => $endline, column => $endcol };
713  
714 0 return $newobj;
715 }
716  
717 0 ▶ sub match { my $self = shift; return $match_for{$self} }
Subroutine match: not called CC=1 SCAR=6.9
718 0 ▶ use overload q{""} => sub { my $self = shift; return $match_for{$self} };
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=0.6
719  
720 0 ▶ sub from { my $self = shift; return $startpos_for{$self} }
Subroutine from: not called CC=1 SCAR=6.9
721 0 ▶ sub to { my $self = shift; return $endpos_for{$self} }
Subroutine to: not called CC=1 SCAR=6.9
722  
723 0 ▶ sub explanation {
Subroutine explanation: not called CC=2 SCAR=17.9
724 0 my $self = shift;
725 0 ▶ my $target = lc Lingua::EN::Grammarian::_rewrite {s{\s+}{ }g} $match_for{$self};
Subroutine __ANON__: not called CC=1 SCAR=6.9
726 0 my $suggested = $CAUTIONS_FOR{$target};
727 0 ▶ return if !defined $suggested;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suggested 0 0 0
728 0 return $suggested->{explanation};
729 }
730  
731 0 ▶ sub explanation_hash {
Subroutine explanation_hash: not called CC=2 SCAR=17.9
732 0 my $self = shift;
733 0 ▶ my $target = lc Lingua::EN::Grammarian::_rewrite {s{\s+}{ }g} $match_for{$self};
Subroutine __ANON__: not called CC=1 SCAR=6.9
734 0 my $suggested = $CAUTIONS_FOR{$target};
735 0 ▶ return if !defined $suggested;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suggested 0 0 0
736 0 return { split /\s+:\s+|\s*\n/, $suggested->{explanation} };
737 }
738  
739 0 ▶ sub suggestions {
Subroutine suggestions: not called CC=4 SCAR=30.0
740 0 my $self = shift;
741 0 ▶ my $target = lc Lingua::EN::Grammarian::_rewrite {s{\s+}{ }g} $match_for{$self};
Subroutine __ANON__: not called CC=1 SCAR=6.9
742 0 my $suggested = $CAUTIONS_FOR{$target};
743 0 ▶ return if !defined $suggested;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suggested 0 0 0
744  
745 # Reorder suggestions by relevance to term...
746 0 return map { $_recase_like->($match_for{$self}, $_) }
747 sort {
748 0 ▶ $a eq $target ? -1
Branch: 2 branches
BranchTrueFalseTotal
$a eq $target ? : 0 0 0
$b eq $target ? : 0 0 0
749 : $b eq $target ? +1
750 : $a cmp $b
751 }
752 0 @{ $suggested->{inflexions} }
753 }
754 }
755  
756  
757 1 1; # Magic true value required at end of module
758 __END__
↑ Summary demo.pl »