Text::Brew(3)
πΈ NAME πΈ
okay bestie, welcome to the Text::Brew(3) page β itβs a User Contributed Perl Documentation moment, and the NAME tea is literally: Text::Brew - An implementation of the Brew edit distance. slay. π
β¨ 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";
π¦ DESCRIPTION π¦
so this module implements the Brew edit distance, and itβs 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, bestie. for more info 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:
INITIALthat is theINITIALoperation (i.e. NO operation)MATCHthat is theMATCHoperation (0is the default cost)SUBSTthat is theSUBSTitution operation (1is the default cost)DELthat is theDELetion operation (1is the default cost)INSthat is theINSertion operation (1is 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:
- give both the same cost
- change the output string
DELtoINS/DEL(o whatever) - change the output string
INStoINS/DEL(o whatever)
π 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";
}
π OPTIONAL PARAMETERS π
distance($string1,$string2,{-cost=>[0,2,1,1],-output=>'edits'});
-outputβ accepted values are:distancemeans that the distance returns only the numeric distanceboththe distance returns both the numeric distance and the array of the editseditsmeans that the distance returns only the array of the edits
Default output is 'both'. π
-costβ accepted value is an array with4elements:- 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)
- 1st is the cost for the
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";
π CREDITS π
all the credits goes to Chris Brew, the author of the algorithm. π«Ά
π THANKS π
many thanks to Stefano L. Rodighiero <larsen at perlmonk.org> for the suggestions. π
π AUTHOR π
Copyright 2003 Dree Mistrut <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.
π SEE ALSO π
"Text::Levenshtein", "Text::WagnerFischer" π
π thatβs the Text::Brew(3) tea, served by perl v5.42.0 on 2014-03-28 β bye bestie! π
π¦ where this page comes from
- opensuse-leap 16.0: perl-Text-Brew 0.02-bp160.1.11 Β· GPL-2.0+ or Artistic-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms