Here's the start of another approach, based on using Algorithm::Diff to count the number of common words (in sequence) between two arrays of strings. By computing a percentage based on the smaller of the arrays, you address both the case of the submitted quote being a subset of an existing quote, and vice versa.

Here's how it hands the "gonna" -> "going to" change.

use strict; use Algorithm::Diff qw(traverse_sequences); my @quote1 = split(' ', <<QUOTE); I walk. I talk. I shop, I sneeze. I'm gonna be a fireman when the floods roll back. There's trees in the desert since you moved out. And I don't sleep on a bed of bones. QUOTE my @quote2 = split(' ', <<QUOTE); I'm going to be a fireman when the floods roll back. There's trees in the desert since you moved out. QUOTE my $match = 0; traverse_sequences(\@quote1, \@quote2, { MATCH => sub { $match++ } } ); my $smaller = @quote1 < @quote2 ? @quote1 : @quote2; my $percent_match = int(($match / $smaller) * 100); print "$match words matched ($percent_match%)\n"; __END__ 18 words matched (90%)
To make this work in practice, you would need to do things like ensure that the shorter quote was at least n words long, so that a 1 word candidate quote didn't match at 100%.

A few minutes perusing the Algorithm::Diff POD might give you some additional ideas.


In reply to Re: Smart Comparison of Buffy Strings by dws
in thread Smart Comparison of Buffy Strings by Cody Pendant

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.