Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't think of any situation in which this would be really useful. Even if I could, I suspect the added CPU cycles needed to sort fuzzy values would be more than those "extra comparisons" you allude to. But I'll still offer a suggestion.</p> <p>Consider this possibility (all strings use the 27 characters a-z and _):</p> <pre><code> 11111111112 12345678901234567890 1/ now_is_the_time 2/ now_is_never 3/ now_we_have_to_go 4/ aaa 5/ ___ </code></pre> <p>Obviously strings 1 and 2 are more similar that 1 and 3 and <em>much</em> more similar than 1 and 4.</p> <p>One approach is to scale the difference value for each identical character position and use the first different character to set the last position.</p> <p>Putting aside signs for the moment, comparing string 1 with 2, the differ in position 8 by 'n' - 't'. That's a difference of 6. In order to turn that into a single digit 1-9, we use the formula:</p> <pre><code>digit = ceiling(9 * abs(diff) / 27) </code></pre> <p>since the maximum difference is 26. The minimum difference of 1 becomes the digit 1. The maximum difference of 26 becomes the digit 9. Our difference of 6 becomes 3.</p> <p>And because the difference is in position 8, out comparison function will return 3x10<sup>-8</sup> (actually it will return the negative of that since string 1 comes <em>after</em> string 2.</p> <p>Using a similar process for strings 1 and 4, the comparison function returns -5x10<sup>-1</sup>. The highest possible return (strings 4 and 5) has a difference in position 1 of '-' - 'a' (26) which generates the digit 9 and hence gives us 9x10<sup>-1</sup>.</p> <p>Take these suggestions and use them as you see fit. I'd be interested in knowing how your fuzzy comparison code ends up working out.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload