blib/lib/Text/Contraction.pm

filter: Statementstmt 75.2% 76 / 101 Branchbran 57.1% 32 / 56 Conditioncond 55.5% 10 / 18 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 75.0% 9 / 12 Podpod 87.5% 7 / 8 totaltotal 66.9% 138 / 206 CC 39 SCAR 30.2
CC
39
Cov
67%
_w
72.0
minRatio
20.0
match
17.2
CRAP 20.5
↑ Summary
1 package Text::Contraction;
2  
3 1 ▶use 5.006002;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Carp;
8 1 require POSIX;
9  
10 1 our $VERSION = '0.02';
11  
12 11 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
13 11 my($type, %args) = @_;
14  
15 11 my $this = bless \%args, $type;
16  
17 11 ▶ $this->{'prefix'} = '^' unless exists $this->{'prefix'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"prefix"} 10 1 11
18 11 ▶ $this->{'caseless'} = 1 unless exists $this->{'caseless'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"caseless"} 10 1 11
19 11 ▶ $this->{'minRatio'} = 0.5 unless exists $this->{'minRatio'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"minRatio"} 10 1 11
20 11 ▶ $this->{'words'} = _w() unless exists $this->{'words'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"words"} 0 11 11
21  
22 11 return $this;
23 }
24  
25 0 ▶sub prefix {
Subroutine prefix: not called CC=2 SCAR=17.9
Pod documented
26 0 my $this = shift;
27 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
28 0 return $this->{'prefix'} = shift;
29 }
30 0 return $this->{'prefix'};
31 }
32  
33 1 ▶sub caseless {
Subroutine caseless: called CC=2 SCAR=7.6
Pod documented
34 1 my $this = shift;
35 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
36 1 return $this->{'caseless'} = shift;
37 }
38 0 return $this->{'caseless'};
39 }
40  
41 0 ▶sub minRatio {
Subroutine minRatio: not called CC=4 SCAR=30.0
Pod documented
42 0 my $this = shift;
43 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
44 0 my $minRatio = shift;
45 0 ▶ unless ($minRatio >= 0 && $minRatio <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($minRatio >= 0 and $minRatio <= 1) 0 0 0
Condition0%0/3
$minRatio >= 0 and $minRatio <= 1
!ll&&!rl&&r
000
MC/DC0%0/2
$minRatio >= 0 and $minRatio <= 1
$minRatio >= 0 $minRatio <= 1
Truth table0%0/3
A and B
A= $minRatio >= 0B= $minRatio <= 1
ABresult
0X0
100
111
46 0 Carp::croak "Text::Contraction::minRatio must be between 0 and 1, inclusive.";
47 }
48 0 return $this->{'minRatio'} = $minRatio;
49 }
50 0 return $this->{'minRatio'};
51 }
52  
53 1 ▶sub words {
Subroutine words: called CC=3 SCAR=12.0
Pod documented
54 1 my $this = shift;
55 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 0 1
56 1 my $words = shift;
57 1 ▶ unless (ref $words eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $words eq "ARRAY") 0 1 1
58 0 Carp::croak "Text::Contraction::words must be an array reference."
59 }
60  
61 1 delete $this->{'_words'};
62 1 return $this->{'words'} = $words;
63 }
64 0 return $this->{'words'};
65 }
66  
67 1 my @words;
68 0 ▶sub _w {
Subroutine _w: not called CC=8 SCAR=42.8
69 0 ▶ return \@words if @words;
Branch: 1 branch
BranchTrueFalseTotal
if @words 0 0 0
70 0 foreach my $file ($ENV{'CONTRACTION_WORDS'},
71 qw(/dict/words
72 /usr/dict/words
73 /usr/share/dict/words
74 /usr/share/lib/spell/words
75 /usr/ucblib/dict/words
76 /usr/lib/dict/words)) {
77 0 ▶ if (defined $file && -s $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and -s $file) 0 0 0
Condition0%0/3
defined $file and -s $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and -s $file
defined $file -s $file
Truth table0%0/3
A and B
A= defined $fileB= -s $file
ABresult
0X0
100
111
78 0 ▶ open my $fh, $file or die "open '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, $file or die "open '${file}': $!" 0 0 0
79 0 chomp(@words = <$fh>);
80 0 return \@words;
81 }
82 }
83  
84 0 ▶ if (defined $ENV{'CONTRACTION_WORDS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ENV{'CONTRACTION_WORDS'}) { } 0 0 0
85 0 ▶ if (-e $ENV{'CONTRACTION_WORDS'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $ENV{'CONTRACTION_WORDS'}) { } 0 0 0
86 0 Carp::croak "Dictionary '$ENV{q(CONTRACTION_WORDS)}' is empty.\n";
87 } else {
88 0 Carp::croak "Could not find dictionary '$ENV{q(CONTRACTION_WORDS)}'.\n";
89 }
90 } else {
91 0 Carp::croak "Could not find dictionary. Try setting environment variable\n".
92 "CONTRACTION_WORDS to the path of your dictionary.\n";
93 }
94 }
95  
96 12 ▶sub study {
Subroutine study: called CC=4 SCAR=13.9
Pod documented
97 12 my $this = shift;
98  
99 12 my @words;
100 12 ▶ for (my $i = 0; $i < @{ $this->{words} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$this->{"words"};}) 25 12 37
101 25 ▶ my $word = $this->{caseless} ? uc $this->{words}[$i] : $this->{words}[$i];
Branch: 1 branch
BranchTrueFalseTotal
$this->{'caseless'} ? : 23 2 25
102 25 my $j = 0;
103 25 for (split //, $word) {
104 54 push @{ $words[ord $_][$j++] }, $i;
105 }
106 }
107  
108 12 $this->{_words} = \@words;
109 }
110  
111 15 ▶sub match {
Subroutine match: called CC=17 SCAR=28.4
Pod documented
112 15 my($this, $contraction) = @_;
113  
114 15 $contraction =~ y/'//d;
115  
116 15 my $prefix;
117 15 ▶ if ($this->{caseless}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'caseless'}) { } 13 2 15
118 13 $contraction = uc $contraction;
119 13 $prefix = '(?i)' . $this->{prefix};
120 } else {
121 2 $prefix = $this->{prefix};
122 }
123  
124 15 ▶ $this->study unless $this->{_words};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"_words"} 11 4 15
125  
126 # find most discriminating character
127 15 my($bestChar, $bestIndex, $bestScore) =
128 (substr($contraction, -1, 1), length($contraction) - 1, undef );
129  
130 15 ▶ for (my $i = length($contraction) - 1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 22 15 37
131 22 my $char = substr($contraction, $i, 1);
132 22 my $maxLength = "Inf";
133 22 ▶ if ($this->{minRatio} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"minRatio"} > 0) 22 0 22
134 22 $maxLength = POSIX::ceil(($i + 1) / $this->{minRatio});
135 }
136  
137 22 my $words = $this->{_words}[ord $char];
138  
139 22 my $score = 0;
140 22 for (@$words[$i..min($#$words, $maxLength)]) {
141 52 ▶ $score += @$_ if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 37 15 52
142 52 ▶ last if defined $bestScore && $score > $bestScore;
Branch: 1 branch
BranchTrueFalseTotal
if defined $bestScore and $score > $bestScore 0 52 52
Condition66%2/3
defined $bestScore and $score > $bestScore
!ll&&!rl&&r
35170
MC/DC0%0/2
defined $bestScore and $score > $bestScore
defined $bestScore $score > $bestScore
Truth table66%2/3
A and B
A= defined $bestScoreB= $score > $bestScore
ABresult
0X0
100
111
143 }
144  
145 22 ▶ if ($score > 0 && (! defined $bestScore || $score < $bestScore)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($score > 0 and !defined($bestScore) || $score < $bestScore) 17 5 22
Condition83%5/6
$score > 0 and !defined($bestScore) || $score < $bestScore
!ll&&!rl&&r
0517
not defined $bestScore or $score < $bestScore
l!l&&r!l&&!r
1525
MC/DC66%2/3
$score > 0 and !defined($bestScore) || $score < $bestScore
$score > 0 not defined $bestScore $score < $bestScore
Truth table75%3/4
A and not(B) or C
A= $score > 0B= not defined $bestScoreC= $score < $bestScore
ABCresult
0XX0
1000
11X1
1011
146 17 ($bestChar, $bestIndex, $bestScore) = ($char, $i, $score);
147 }
148 }
149  
150 # get all the words using the most discriminating character
151 15 my $maxLength = "Inf";
152 15 ▶ if ($this->{minRatio} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"minRatio"} > 0) 15 0 15
153 15 $maxLength = POSIX::ceil(($bestIndex + 1) / $this->{minRatio});
154 }
155  
156 15 my $pattern = $prefix . join "[ A-Za-z']*", split //, $contraction;
157 15 $pattern = qr($pattern);
158  
159 15 my $words = $this->{_words}[ord $bestChar];
160  
161 15 my %match;
162 15 for (@$words[$bestIndex..min($#$words, $maxLength)]) {
163 29 ▶ @match{@$_} = (1) x @$_ if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 26 3 29
164 }
165  
166 15 $maxLength = "Inf";
167 15 ▶ if ($this->{minRatio} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"minRatio"} > 0) 15 0 15
168 15 $maxLength = POSIX::ceil((length $contraction) / $this->{minRatio});
169 }
170 15 ▶ return grep { length() <= $maxLength && /$pattern/ } @{ $this->{words} }[keys %match];
Condition100%3/3
length $_ <= $maxLength && /$pattern/
!ll&&!rl&&r
2223
MC/DC100%2/2
length $_ <= $maxLength && /$pattern/
length $_ <= $maxLength /$pattern/
Truth table100%3/3
A && B
A= length $_ <= $maxLengthB= /$pattern/
ABresult
0X0
100
111
171 }
172  
173 37 ▶sub min { $_[0] < $_[1] ? $_[0] : $_[1] }
Subroutine min: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 29 8 37
174  
175 1 1;
176 __END__
↑ Summary