| 1 | package Text::Brew; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 2 | ▶ | use Exporter; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 2 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 2 | $VERSION = '0.02'; | |||||||||||||||||
| 9 | 2 | @ISA = qw(Exporter); | |||||||||||||||||
| 10 | 2 | @EXPORT = (); | |||||||||||||||||
| 11 | 2 | @EXPORT_OK = qw(&distance); | |||||||||||||||||
| 12 | 2 | %EXPORT_TAGS = (); | |||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | 2 | ▶ | use constant INITIAL => 'INITIAL'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 15 | 2 | ▶ | use constant DEL => 'DEL'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 16 | 2 | ▶ | use constant INS => 'INS'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 17 | 2 | ▶ | use constant SUBST => 'SUBST'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 18 | 2 | ▶ | use constant MATCH => 'MATCH'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | 2 | ▶ | use constant None => []; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | 118 | ▶ | sub _best { | ||||||||||||||||
|
Subroutine
_best: called CC=4 SCAR=13.9
| |||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | 118 | my ($sub_move,$ins_move,$del_move)=@_; | |||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 118 | my ($increment,$move1,$move2,$move3,$tb1,$tb2,$tb3); | |||||||||||||||||
| 26 | |||||||||||||||||||
| 27 | 118 | ($increment,$move1,$tb1)=@$sub_move; | |||||||||||||||||
| 28 | 118 | my $cost_with_sub=$increment+$tb1->[0]; | |||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | 118 | ($increment,$move2,$tb2)=@$ins_move; | |||||||||||||||||
| 31 | 118 | my $cost_with_ins=$increment+$tb2->[0]; | |||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | 118 | ($increment,$move3,$tb3)=@$del_move; | |||||||||||||||||
| 34 | 118 | my $cost_with_del=$increment+$tb3->[0]; | |||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 118 | my $best_cost=$cost_with_sub; | |||||||||||||||||
| 37 | 118 | my $move=$move1; | |||||||||||||||||
| 38 | 118 | my $tb=$tb1; | |||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 118 | ▶ | if ($cost_with_ins < $best_cost) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | 32 | $best_cost=$cost_with_ins; | |||||||||||||||||
| 43 | 32 | $move=$move2; | |||||||||||||||||
| 44 | 32 | $tb=$tb2; | |||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | |||||||||||||||||||
| 47 | 118 | ▶ | if ($cost_with_del < $best_cost) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 32 | $best_cost=$cost_with_del; | |||||||||||||||||
| 50 | 32 | $move=$move3; | |||||||||||||||||
| 51 | 32 | $tb=$tb3; | |||||||||||||||||
| 52 | } | ||||||||||||||||||
| 53 | |||||||||||||||||||
| 54 | 118 | ▶ | if ($best_cost == $tb->[0]) {$move=MATCH} | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 55 | 118 | return [$best_cost,$move,$tb]; | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | |||||||||||||||||||
| 58 | 8 | ▶ | sub _edit_path { | ||||||||||||||||
|
Subroutine
_edit_path: called CC=6 SCAR=17.9
| |||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 8 | my ($string1,$string2,$refc)=@_; | |||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | 8 | my $m=length($string1); | |||||||||||||||||
| 63 | 8 | my $n=length($string2); | |||||||||||||||||
| 64 | |||||||||||||||||||
| 65 | 8 | my ($matchCost,$insCost,$delCost,$substCost)=@$refc; | |||||||||||||||||
| 66 | 8 | my @d; | |||||||||||||||||
| 67 | |||||||||||||||||||
| 68 | 8 | $d[0][0]=[0,INITIAL,None]; | |||||||||||||||||
| 69 | |||||||||||||||||||
| 70 | 8 | foreach my $i (0 .. $m-1) { | |||||||||||||||||
| 71 | |||||||||||||||||||
| 72 | 31 | my $sofar= $d[$i][0][0]; | |||||||||||||||||
| 73 | |||||||||||||||||||
| 74 | # cost move tb | ||||||||||||||||||
| 75 | 31 | $d[$i+1][0]=[$sofar+$delCost, DEL , $d[$i][0]]; | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 8 | foreach my $j (0 .. $n-1) { | |||||||||||||||||
| 79 | |||||||||||||||||||
| 80 | 31 | my $sofar= $d[0][$j][0]; | |||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | # cost move tb | ||||||||||||||||||
| 83 | 31 | $d[0][$j+1]=[$sofar+$insCost, INS , $d[0][$j]]; | |||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | 8 | foreach my $i (0 .. $m-1) { | |||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 31 | my $string1_i=substr($string1,$i,1); | |||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | 31 | foreach my $j (0 .. $n-1) { | |||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | 118 | my $string2_i=substr($string2,$j,1); | |||||||||||||||||
| 93 | 118 | my $subst; | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 118 | ▶ | if ($string1_i eq $string2_i) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 29 | $subst=$matchCost; | |||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | } else { | ||||||||||||||||||
| 100 | |||||||||||||||||||
| 101 | 89 | $subst=$substCost; | |||||||||||||||||
| 102 | } | ||||||||||||||||||
| 103 | |||||||||||||||||||
| 104 | # cost move tb | ||||||||||||||||||
| 105 | 118 | $d[$i+1][$j+1]=_best([$subst, SUBST , $d[$i][$j]], | |||||||||||||||||
| 106 | [$insCost, INS , $d[$i+1][$j]], | ||||||||||||||||||
| 107 | [$delCost, DEL , $d[$i][$j+1]]); | ||||||||||||||||||
| 108 | } | ||||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | |||||||||||||||||||
| 111 | 8 | return $d[$m][$n]; | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 8 | ▶ | sub distance { | ||||||||||||||||
|
Subroutine
distance: called CC=15 SCAR=45.3
Pod
undocumented
| |||||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 8 | my ($string1,$string2,$optional_ref)=@_; | |||||||||||||||||
| 117 | 8 | my $output; | |||||||||||||||||
| 118 | my $cost; | ||||||||||||||||||
| 119 | |||||||||||||||||||
| 120 | 8 | ▶ | if ($optional_ref) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 0 | ▶ | if (ref($optional_ref) ne "HASH") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 123 | |||||||||||||||||||
| 124 | 0 | warn "Text::Brew: options not well formed, using default"; | |||||||||||||||||
| 125 | |||||||||||||||||||
| 126 | } else { | ||||||||||||||||||
| 127 | |||||||||||||||||||
| 128 | 0 | foreach my $key (keys %$optional_ref) { | |||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | 0 | ▶ | if ($key eq "-cost") { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | 0 | $cost=$$optional_ref{'-cost'}; | |||||||||||||||||
| 133 | 0 | ▶ | if (ref($cost) ne "ARRAY") { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 134 | |||||||||||||||||||
| 135 | 0 | require Carp; | |||||||||||||||||
| 136 | 0 | Carp::croak("Text::Brew: -cost option requires an array"); | |||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | } else { | ||||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | 0 | ▶ | if (@$cost < 4) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 141 | |||||||||||||||||||
| 142 | 0 | warn "Text::Brew: array cost not well formed, using default"; | |||||||||||||||||
| 143 | 0 | $cost=undef; | |||||||||||||||||
| 144 | } | ||||||||||||||||||
| 145 | } | ||||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | } elsif ($key eq "-output") { | ||||||||||||||||||
| 148 | |||||||||||||||||||
| 149 | 0 | $output=$$optional_ref{'-output'}; | |||||||||||||||||
| 150 | |||||||||||||||||||
| 151 | } else { | ||||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | 0 | require Carp; | |||||||||||||||||
| 154 | 0 | Carp::croak("Text::Brew: $key is not a valid option"); | |||||||||||||||||
| 155 | } | ||||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | } | ||||||||||||||||||
| 158 | } | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | 8 | ▶ | $cost ||= [0,1,1,1]; | ||||||||||||||||
|
Condition50%1/2
$cost ||= [0, 1, 1, 1]
MC/DC0%0/1
$cost ||= [0, 1, 1, 1]
$cost
Truth table50%1/2
A
A= $cost
| |||||||||||||||||||
| 161 | 8 | ▶ | $output='both' if (!defined $output); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 162 | |||||||||||||||||||
| 163 | 8 | ▶ | if ($output eq 'both') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 164 | |||||||||||||||||||
| 165 | 8 | my $tb=_edit_path($string1,$string2,$cost); | |||||||||||||||||
| 166 | 8 | my $distance=$tb->[0]; | |||||||||||||||||
| 167 | 8 | my $arrayref_edits; | |||||||||||||||||
| 168 | |||||||||||||||||||
| 169 | 8 | ▶ | while (defined $tb->[0]) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 170 | |||||||||||||||||||
| 171 | 44 | unshift @$arrayref_edits,$tb->[1]; | |||||||||||||||||
| 172 | 44 | $tb=$tb->[2]; | |||||||||||||||||
| 173 | } | ||||||||||||||||||
| 174 | |||||||||||||||||||
| 175 | 8 | return $distance,$arrayref_edits; | |||||||||||||||||
| 176 | |||||||||||||||||||
| 177 | } elsif ($output eq 'distance') { | ||||||||||||||||||
| 178 | |||||||||||||||||||
| 179 | 0 | my $tb=_edit_path($string1,$string2,$cost); | |||||||||||||||||
| 180 | 0 | my $distance=$tb->[0]; | |||||||||||||||||
| 181 | |||||||||||||||||||
| 182 | 0 | return $distance; | |||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | } elsif ($output eq 'edits') { | ||||||||||||||||||
| 185 | |||||||||||||||||||
| 186 | 0 | my $tb=_edit_path($string1,$string2,$cost); | |||||||||||||||||
| 187 | 0 | my $arrayref_edits; | |||||||||||||||||
| 188 | |||||||||||||||||||
| 189 | 0 | ▶ | while (defined $tb->[0]) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 190 | |||||||||||||||||||
| 191 | 0 | unshift @$arrayref_edits,$tb->[1]; | |||||||||||||||||
| 192 | 0 | $tb=$tb->[2]; | |||||||||||||||||
| 193 | } | ||||||||||||||||||
| 194 | |||||||||||||||||||
| 195 | 0 | return $arrayref_edits; | |||||||||||||||||
| 196 | |||||||||||||||||||
| 197 | } else { | ||||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | 0 | require Carp; | |||||||||||||||||
| 200 | 0 | Carp::croak("Text::Brew: -output option must be 'distance' or 'both' or 'edits', not $output"); | |||||||||||||||||
| 201 | } | ||||||||||||||||||
| 202 | } | ||||||||||||||||||
| 203 | |||||||||||||||||||
| 204 | 2 | 1; | |||||||||||||||||
| 205 | |||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||