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.


In reply to Re: Edit distance between two strings by AppleFritter
in thread Edit distance between two strings by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.