Hello all. I'm using String::Approx to find the best match for a given string against a list of candidates. Following the module's POD, here's my code:

# get best match by getting relative distances and # taking the lowest value my %dist; @dist{@input} = map { abs } adistr($target, @input); my ($best) = sort { $dist{$a} <=> $dist{$b} } @input; # re-check match to make sure it's good enough if ($best and amatch($target, $best)) { # got a match... } else { # no such luck. }

My problem is that String::Approx isn't picking the best match in many cases. For example, given:

$target = 'source'; @inputs = qw(donor_dedupe_source indicted_video_source source1);

the code picks "donor_dedupe_source" instead of "source1". That's because String::Approx considers them to be equally perfect matches, giving them both a adistr score of 0.

What's the best way to solve this? I could do another pass over scores with equal values sorting by closeness to length($target), but that seems gross.

-sam


In reply to Tuning an approximate match to prefer closer lengths by samtregar

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.