Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Edit distance between two strings

by AppleFritter (Vicar)
on Jul 08, 2014 at 09:44 UTC ( [id://1092697]=note: print w/replies, xml ) Need Help??


in reply to Edit distance between two strings

The standard metric for that is called the Levenshtein distance. A quick CPAN search suggests Text::Levenshtein or Text::Fuzzy. The latter also has the ability to show you what edits were made in which positions.

Note that the Levenshtein distance allows for substitutions in addition to insertions and deletions. I'm not aware of a "standard" edit distance metric that only uses insertions and deletions, but you could fiddle with the weights in Text::WagnerFischer to rule out substitutions.

#!/usr/bin/perl use feature qw/say/; use warnings; use strict; use Text::Levenshtein; use Text::Fuzzy; use Text::WagnerFischer; my $old = "This is an old question."; my $new = "This is a new question."; say Text::Levenshtein::distance($old, $new); my $tf = Text::Fuzzy->new($old); say $tf->distance($new); my ($distance, $edits) = Text::Fuzzy::distance_edits($old, $new); say $edits; say Text::WagnerFischer::distance([0, 1, 999], $old, $new);

This prints:

$ perl test.pl 4 4 kkkkkkkkkdkrrrkkkkkkkkkk 7 $

Take your pick.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1092697]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found