A while back someone asked about the best way to do "Fuzzy Matching" on a bulk scale. He wanted to find every possible "fuzzy match" of a set of fixed width strings of a restricted alphabet (ACGT) in a set of DNA data. By "fuzzy match" he meant a match where up to a certain number of chars in the matched string could be different. This node discusses what is involved setting up a reasonably rigourous comparison of the different solutions at hand and shows some comparison results of four solutions currently known to me. In replies we can cover each solution that comes up.

Now first off in order to even begin to compare a set of solutions to this problem we will need to define a common interface. We will use perl OO to make this as painless as possible. We will do this by defining a base class that represents the basic behaviour of a "fuzzy matcher". This class has the following methods:

my $obj=Fuzzy::Matcher->new($fuzz,$strlen,$words,$wordsize);
We need a way to create a new fuzzy matcher, and of course we will call it new() as convention demands. We specify that the interface should accept four possibly relevent parameters so that we can cater to as many specific implementations as possible with changing the constructor behavior
my $obj=Fuzz::Matcher->_init();
I find its useful when using inheritance to keep the actual constructor (where bless is called) seperate from a follow up init hook as it means you can avoid messing around with SUPER when you need to change the initialization behaviour of a subclass. In the baseclass this does nothing. We use a single '_' to denote that its "private" (or "friend") and not really meant to be called from outside of the class heirarchy. Its called in void context by new() immediately before returning the object to the user with any additional arguments that were provided to new() over those it is defined to accept. It should die on error.
my $obj=$Fuzz_Matcher->fuzz_store($str);
Tell the matcher it should match $str in a search. Returns nothing. Used to load the object with the strings being searched for.
my $obj=$Fuzzy_Matcher->prepare($str);
Its quite possible that a fuzzy matcher implementation could need to do some kind of pre-search preparation once it knows no more strings will be added to it. This method provides that hook. Before searching all users of the object should call this first. Returns nothing.

my $obj=$Fuzzy_Matcher->fuzz_search($str);
Search $str for any fuzzy matches that are present. It should return a list of triplets, $ofs, $diff, $str that represent each match. $ofs is the position of the matched string in $str. The number of characters different is returned in $diff and the string matched is returned in $str. It should return the empty list if there were no matches. The user is expected to call prepare() on the object before calling this method. If they have not done so the behaviour is undefined.

And with that simple definition (and even more useful, the code it represents) any different implementations that people come up with may be easily cross tested against each other. Isnt OO fun :-)

Next we need a tool to create tests. We will want to test with a variety of different combinations of input strings and search strings so we will pull out Getopt::Long and whip together "make_test.pl" that will produce a single file that represents one set of such tests. Naturally once we have a bunch of test files we need something to do with them. For that we put together "test_fuzz.pl". We want to do testing in a pristine memory condition so we use some tricks to allow test_fuzz.pl to spawn copies of itself for individual tests and then use the outer instance to aggregate the results. And naturally we will want some pretty tables of the results. :-)

We will want to control our testing in a few ways. One of the more important being how long we will allow tests to run for. If we are doing a large batch and we know that a given solution is intolerably slow at a given scale, then tests for a larger scale will be likewise. Thus we will want to provide a way to skip tests that take too long. The --limit parameter allows us to do this.

Once we put all of this together we can produce our comparisons. We generate a bunch of test files of different types and then let test_fuzz.pl rip.

Heres a table of the data (slightly munged for html purposes) we get back. The 'N/A' are because the test too longer than 100 seconds the previous try. (It resets when the wordcount changes.) "SC" stands for string count, "SL" for string length, "F" for Fuzz, "WC" for word count and "WL" for word length.

Test File DFA Ysth Xor2 Xor
FT-SC0100-SL0001000-F02-WC0000001-WL0250.01812.34850.03220.3562
FT-SC0100-SL0001000-F02-WC0000005-WL0250.02252.30350.12110.9044
FT-SC0100-SL0001000-F02-WC0000010-WL0250.03212.32610.23101.5358
FT-SC0100-SL0001000-F02-WC0000050-WL0250.09182.33701.12446.7897
FT-SC0100-SL0001000-F02-WC0000100-WL0250.15402.38112.307113.5992
FT-SC0100-SL0001000-F02-WC0000500-WL0250.61982.471111.261764.8216
FT-SC0100-SL0001000-F02-WC0001000-WL0251.10312.609523.1594133.1065
FT-SC0100-SL0001000-F02-WC0005000-WL0254.05112.8281113.3197N/A
FT-SC0100-SL0001000-F02-WC0010000-WL0256.54963.1301N/AN/A
FT-SC0100-SL0001000-F02-WC0050000-WL02515.75485.1598N/AN/A
FT-SC0100-SL0001000-F02-WC0100000-WL02520.72357.5497N/AN/A
FT-SC0100-SL0001000-F02-WC0500000-WL02567.338725.3479N/AN/A
FT-SC0100-SL0001000-F02-WC1000000-WL025149.081548.0690N/AN/A
FT-SC0100-SL0010000-F02-WC0000001-WL0250.139226.74170.27293.7779
FT-SC0100-SL0010000-F02-WC0000005-WL0250.188326.89201.05549.2859
FT-SC0100-SL0010000-F02-WC0000010-WL0250.304828.78532.036116.3141
FT-SC0100-SL0010000-F02-WC0000050-WL0250.243127.14039.710769.3049
FT-SC0100-SL0010000-F02-WC0000100-WL0250.304827.414819.4087134.0691
FT-SC0100-SL0010000-F02-WC0000500-WL0250.776028.627196.4305N/A
FT-SC0100-SL0010000-F02-WC0001000-WL0251.282729.1948192.0604N/A
FT-SC0100-SL0010000-F02-WC0005000-WL0254.440330.6293N/AN/A
FT-SC0100-SL0010000-F02-WC0010000-WL0257.249431.9665N/AN/A
FT-SC0100-SL0010000-F02-WC0050000-WL02518.077141.9295N/AN/A
FT-SC0100-SL0010000-F02-WC0100000-WL02525.141747.7157N/AN/A
FT-SC0100-SL0010000-F02-WC0500000-WL025155.8075N/AN/AN/A
FT-SC0100-SL0010000-F02-WC1000000-WL025N/AN/AN/AN/A
FT-SC0100-SL0100000-F02-WC0000001-WL0251.0911536.88132.998937.2198
FT-SC0100-SL0100000-F02-WC0000005-WL0251.0271N/A11.426991.2324
FT-SC0100-SL0100000-F02-WC0000010-WL0251.0351N/A21.8465161.7554
FT-SC0100-SL0100000-F02-WC0000050-WL0251.4329N/A105.2142N/A
FT-SC0100-SL0100000-F02-WC0000100-WL0251.5358N/AN/AN/A
FT-SC0100-SL0100000-F02-WC0000500-WL0252.0794N/AN/AN/A
FT-SC0100-SL0100000-F02-WC0001000-WL0252.7480N/AN/AN/A
FT-SC0100-SL0100000-F02-WC0005000-WL0258.1556N/AN/AN/A
FT-SC0100-SL0100000-F02-WC0010000-WL02513.3646N/AN/AN/A
FT-SC0100-SL0100000-F02-WC0050000-WL02539.0437N/AN/AN/A
FT-SC0100-SL0100000-F02-WC0100000-WL02569.8990N/AN/AN/A
FT-SC0100-SL0100000-F02-WC0500000-WL0251050.3425N/AN/AN/A
FT-SC0100-SL0100000-F02-WC1000000-WL025N/AN/AN/AN/A

All the files involved will be attached via replies to keep the root a reasonable size. Discussion of the various approaches can follow those nodes.

Cheers

---
demerphq


In reply to Algorithm Showdown: Fuzzy Matching by demerphq

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.