/usr/share/perl5/Text/Brew.pm is in libtext-brew-perl 0.02-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | package Text::Brew;
use strict;
use warnings;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
$VERSION = '0.02';
@ISA = qw(Exporter);
@EXPORT = ();
@EXPORT_OK = qw(&distance);
%EXPORT_TAGS = ();
use constant INITIAL => 'INITIAL';
use constant DEL => 'DEL';
use constant INS => 'INS';
use constant SUBST => 'SUBST';
use constant MATCH => 'MATCH';
use constant None => [];
sub _best {
my ($sub_move,$ins_move,$del_move)=@_;
my ($increment,$move1,$move2,$move3,$tb1,$tb2,$tb3);
($increment,$move1,$tb1)=@$sub_move;
my $cost_with_sub=$increment+$tb1->[0];
($increment,$move2,$tb2)=@$ins_move;
my $cost_with_ins=$increment+$tb2->[0];
($increment,$move3,$tb3)=@$del_move;
my $cost_with_del=$increment+$tb3->[0];
my $best_cost=$cost_with_sub;
my $move=$move1;
my $tb=$tb1;
if ($cost_with_ins < $best_cost) {
$best_cost=$cost_with_ins;
$move=$move2;
$tb=$tb2;
}
if ($cost_with_del < $best_cost) {
$best_cost=$cost_with_del;
$move=$move3;
$tb=$tb3;
}
if ($best_cost == $tb->[0]) {$move=MATCH}
return [$best_cost,$move,$tb];
}
sub _edit_path {
my ($string1,$string2,$refc)=@_;
my $m=length($string1);
my $n=length($string2);
my ($matchCost,$insCost,$delCost,$substCost)=@$refc;
my @d;
$d[0][0]=[0,INITIAL,None];
foreach my $i (0 .. $m-1) {
my $sofar= $d[$i][0][0];
# cost move tb
$d[$i+1][0]=[$sofar+$delCost, DEL , $d[$i][0]];
}
foreach my $j (0 .. $n-1) {
my $sofar= $d[0][$j][0];
# cost move tb
$d[0][$j+1]=[$sofar+$insCost, INS , $d[0][$j]];
}
foreach my $i (0 .. $m-1) {
my $string1_i=substr($string1,$i,1);
foreach my $j (0 .. $n-1) {
my $string2_i=substr($string2,$j,1);
my $subst;
if ($string1_i eq $string2_i) {
$subst=$matchCost;
} else {
$subst=$substCost;
}
# cost move tb
$d[$i+1][$j+1]=_best([$subst, SUBST , $d[$i][$j]],
[$insCost, INS , $d[$i+1][$j]],
[$delCost, DEL , $d[$i][$j+1]]);
}
}
return $d[$m][$n];
}
sub distance {
my ($string1,$string2,$optional_ref)=@_;
my $output;
my $cost;
if ($optional_ref) {
if (ref($optional_ref) ne "HASH") {
warn "Text::Brew: options not well formed, using default";
} else {
foreach my $key (keys %$optional_ref) {
if ($key eq "-cost") {
$cost=$$optional_ref{'-cost'};
if (ref($cost) ne "ARRAY") {
require Carp;
Carp::croak("Text::Brew: -cost option requires an array");
} else {
if (@$cost < 4) {
warn "Text::Brew: array cost not well formed, using default";
$cost=undef;
}
}
} elsif ($key eq "-output") {
$output=$$optional_ref{'-output'};
} else {
require Carp;
Carp::croak("Text::Brew: $key is not a valid option");
}
}
}
}
$cost ||= [0,1,1,1];
$output='both' if (!defined $output);
if ($output eq 'both') {
my $tb=_edit_path($string1,$string2,$cost);
my $distance=$tb->[0];
my $arrayref_edits;
while (defined $tb->[0]) {
unshift @$arrayref_edits,$tb->[1];
$tb=$tb->[2];
}
return $distance,$arrayref_edits;
} elsif ($output eq 'distance') {
my $tb=_edit_path($string1,$string2,$cost);
my $distance=$tb->[0];
return $distance;
} elsif ($output eq 'edits') {
my $tb=_edit_path($string1,$string2,$cost);
my $arrayref_edits;
while (defined $tb->[0]) {
unshift @$arrayref_edits,$tb->[1];
$tb=$tb->[2];
}
return $arrayref_edits;
} else {
require Carp;
Carp::croak("Text::Brew: -output option must be 'distance' or 'both' or 'edits', not $output");
}
}
1;
__END__
=head1 NAME
Text::Brew - An implementation of the Brew edit distance
=head1 SYNOPSIS
use Text::Brew qw(distance);
my ($distance,$arrayref_edits)=distance("four","foo");
my $sequence=join",",@$arrayref_edits;
print "The Brew distance for (four,foo) is $distance\n";
print "obtained with the edits: $sequence\n\n";
=head1 DESCRIPTION
This module implements the Brew edit distance that is very close to the
dynamic programming technique used for the Wagner-Fischer (and so for the
Levenshtein) edit distance. Please look at the module references below.
For more information about the Brew edit distance see:
<http://ling.ohio-state.edu/~cbrew/795M/string-distance.html>
The difference here is that you have separated costs for the DELetion and
INSertion operations (but with the default to 1 for both, you obtain the
Levenshtein edit distance). But the most interesting feature is that you
can obtain the description of the edits needed to transform the first string
into the second one (not vice versa: here DELetions are separated from INSertions).
The difference from the original algorithm by Chris Brew is that I have
added the SUBST operation, making it different from MATCH operation.
The symbols used here are:
INITIAL that is the INITIAL operation (i.e. NO operation)
MATCH that is the MATCH operation (0 is the default cost)
SUBST that is the SUBSTitution operation (1 is the default cost)
DEL that is the DELetion operation (1 is the default cost)
INS that is the INSertion operation (1 is the default cost)
and you can change the default costs (see below).
You can make INS and DEL the same operation in a simple way:
1) give both the same cost
2) change the output string DEL to INS/DEL (o whatever)
3) change the output string INS to INS/DEL (o whatever)
=head2 USAGE
use strict;
use Text::Brew qw(distance);
my ($distance,$arrayref_edits)=distance("four","foo");
my $sequence=join",",@$arrayref_edits;
print "The Brew distance for (four,foo) is $distance\n";
print "obtained with the edits: $sequence\n\n";
my $string1="foo";
my @strings=("four","foo","bar");
my (@dist,@edits);
foreach my $string2 (@strings) {
my ($dist,$edits)=distance($string1,$string2);
push @dist,$dist;
push @edits,(join ",",@$edits);
}
foreach my $i (0 .. $#strings) {
print "The Brew distance for ($string1,$strings[$i]) is $dist[$i]\n";
print "obtained with the edits: $edits[$i]\n\n";
}
=head2 OPTIONAL PARAMETERS
distance($string1,$string2,{-cost=>[0,2,1,1],-output=>'edits'});
-output
accepted values are:
distance means that the distance returns
only the numeric distance
both the distance returns both the
numeric distance and the array of the edits
edits means that the distance returns only the
array of the edits
Default output is 'both'.
-cost
accepted value is an array with 4 elements:
1st is the cost for the MATCH
2nd is the cost for the INS (INSertion)
3rd is the cost for the DEL (DELetion)
4th is the cost for the SUBST (SUBSTitution)
Default array is [0,1,1,1] .
Examples are:
my $distance=distance("four","foo",{-output=>'distance'});
print "The Brew distance for (four,foo) is $distance\n\n";
my $arrayref_edits=distance("four","foo",{-output=>'edits'});
my $sequence=join",",@$arrayref_edits;
print "The Brew sequence for (four,foo) is $sequence\n\n";
my ($distance,$arrayref_edits)=distance("four","foo",{-cost=>[0,2,1,1]});
my $sequence=join",",@$arrayref_edits;
print "The Brew distance for (four,foo) is $distance\n";
print "obtained with the edits: $sequence\n\n";
($distance,$arrayref_edits)=distance("foo","four",{-cost=>[0,2,1,1]});
$sequence=join",",@$arrayref_edits;
print "The Brew distance for (foo,four) is $distance\n";
print "obtained with the edits: $sequence\n\n";
=head1 CREDITS
All the credits goes to Chris Brew the author of the algorithm.
=head1 THANKS
Many thanks to Stefano L. Rodighiero <F<larsen at perlmonk.org>> for the
suggestions.
=head1 AUTHOR
Copyright 2003 Dree Mistrut <F<dree@friuli.to>>
This package is free software and is provided "as is" without express
or implied warranty. You can redistribute it and/or modify it under
the same terms as Perl itself.
=head1 SEE ALSO
C<Text::Levenshtein>, C<Text::WagnerFischer>
=cut
|