| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 11 | ▶ | $this->{'caseless'} = 1 unless exists $this->{'caseless'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 11 | ▶ | $this->{'minRatio'} = 0.5 unless exists $this->{'minRatio'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 11 | ▶ | $this->{'words'} = _w() unless exists $this->{'words'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | my $minRatio = shift; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | unless ($minRatio >= 0 && $minRatio <= 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$minRatio >= 0 and $minRatio <= 1
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | my $words = shift; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 1 | ▶ | unless (ref $words eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $file and -s $file
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
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | open my $fh, $file or die "open '$file': $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | chomp(@words = <$fh>); | |||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | return \@words; | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (defined $ENV{'CONTRACTION_WORDS'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (-e $ENV{'CONTRACTION_WORDS'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 25 | ▶ | my $word = $this->{caseless} ? uc $this->{words}[$i] : $this->{words}[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 22 | my $char = substr($contraction, $i, 1); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 22 | my $maxLength = "Inf"; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 22 | ▶ | if ($this->{minRatio} > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 52 | ▶ | last if defined $bestScore && $score > $bestScore; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $bestScore and $score > $bestScore
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
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 22 | ▶ | if ($score > 0 && (! defined $bestScore || $score < $bestScore)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$score > 0 and !defined($bestScore) || $score < $bestScore
not defined $bestScore or $score < $bestScore
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 15 | $maxLength = "Inf"; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 15 | ▶ | if ($this->{minRatio} > 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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/
MC/DC100%2/2
length $_ <= $maxLength && /$pattern/
length $_ <= $maxLength
/$pattern/
Truth table100%3/3
A && B
A= length $_ <= $maxLengthB= /$pattern/
| |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | 37 | ▶ | sub min { $_[0] < $_[1] ? $_[0] : $_[1] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
min: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||||||||||||||||