Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello roopa,

welcome to the monastery!
Please read the nodes given by gmargo above.

That said,

I am trying to compare string similarity based on the function sort_by_similarity().

It is next to impossible to help without a description (or better: code) of that subroutine. See I know what I mean. Why don't you?

I need to compare each of the strings in the inputsubs with the querysubs and get the best matching string for each of these strings using the function sort_by_similarity(),but I am unable to understand the value returned by the function and unable to access the values and print them.

I'm unable, too, since you provided no code. Anyways, thinking about the problem... here's how I would tackle it.

In Perl you want to use sort to sort a collection. sort optionally takes a subroutine or block which will carry out the comparison of the items to be sorted. That evaluation must return -1, 0, or 1, for sort to determine their order in the collection that will be returned. Inside that block or function the two items to be compared magically appear in the variables $a and $b.

Now, you want to sort a collection by similarity. Similarity is a relation between two elements. Sorting by that relation means: "are (a,b) more, less or equally similar to (a,c)?", i.e. sorting tuples by the amount of their similarity.

To do that you need a way to measure that similarity between the elements of a tuple, then you can sort the tuples by the amount of similarity.

use Math::Combinatorics qw( combine); my @collection; # list of strings my @tuples = combine( 2, @collection); my @similarity = sort { similarity($a) <=> similarity($b) } @tuples; sub similarity { my $tupel = shift; my $value; ... # determine similarity # between $tupel->[0] and $tupel->[1] # and store into $value ... return $value; }

To avoid the maybe expensive evaluation of similarity() for each tuple at each comparison, it is convenient to determine those beforehand. i.e. using the Schwartzian Transform (see map):

my @similarity = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, similarity($_) ] } @tuples;

Further optimization comprises populating @tuples with indices into @collection, which is left as an exercise to the reader.


In reply to Re: string similarity by using a function sort_by_similarity() by shmem
in thread string similarity by using a function sort_by_similarity() by roopa

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (13)
    As of 2014-07-25 17:09 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (174 votes), past polls