jcb_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am having trouble using Text::Levenshtein. Here's an example:
use Text::Levenshtein qw(fastdistance distance); print fastdistance("history","hustory"), "\n"; # prints "2"
IMHO, this should print "1" because there is only one substitution necessary to get from "history" to "hustory", but instead it returns "2". Interestingly enough, this works as expected:
print fastdistance("gistory","history"), "\n"; # prints "1"
Any idea what I am doing wrong? Or do I have the wrong notion about how Levenshtein distance actually works?? I also tried using distance, but then something seriously goes wrong:
print distance("gistory","history"), "\n"; # prints "1234561234561"
I am using ActivePerl on Windows 7. The input file is ANSI. I am thankful for all hints!

Cheers and thanks, Christian

Replies are listed 'Best First'.
Re: Text::Levenshtein - trouble
by Corion (Pope) on Jul 11, 2012 at 14:51 UTC

      Indeed, the module is buggy. The comment here helped unbreak a script I wrote using the module. The lines that need to be commented are:

      # in distance() foreach my $i (1 .. $n) { #if ($i != $n && substr($s,$i) eq substr($t,$i)) { # push @result,$i;next; #} $d[$i][0]=$i; } foreach my $j (1 .. $m) { #if ($j != $m && substr($s,$j) eq substr($t,$j)) { # push @result,$j;next; #} $d[0][$j]=$j; } # and the two early returns in fastdistance()
Re: Text::Levenshtein - trouble
by Khen1950fx (Canon) on Jul 12, 2012 at 13:25 UTC
    Text::Levenshtein is returning in list context, but scalar context is what should be happening. You could force scalar context by using scalar. For example:
    #!/usr/bin/perl -l use autodie; use strictures 1; use Devel::SimpleTrace; use strict qw/refs/; use Text::LevenshteinXS qw/distance/; my $x = scalar distance("history", "history"); my $y = scalar distance("gistory", "history"); my $z = scalar distance("history", "hustory"); print $x, "\n", $y, "\n", $z; use strict qw/refs/; use Text::Levenshtein::Damerau qw/edistance/; $x = scalar edistance("history", "history"); $y = scalar edistance("gistory", "history"); $z = scalar edistance("history", "hustory"); print $x, "\n", $y, "\n", $z; use strict qw/refs/; use Text::Levenshtein; $x = scalar distance("history", "history"); $y = scalar distance("gistory", "history"); $z = scalar distance("history", "hustory"); print $x, "\n", $y, "\n", $z;