Since all that a DFA has to keep track of is the position within the test string and the number of fuzzy matches made so far, it would need no more than (number of characters in the test string)*(number of fuzzies allowed + 1) states. That is only 75 states for a string of length 25 with two errors allowed.

I haven't coded the DFA yet, but here is looping code that illustrates the idea.

use strict; use warnings; my $test = "abcdefghijklmnopqrstuvwxy"; my @test = split //,$test; my $dict = "qqqabcdefghijklmnopqrstuvwxyqqq"; my @dict = split //,$dict; my $dict_pos = 0; # where in the dictionary word my $at_pos = 0; # where in the test word my $fuzzies = 2; # unused fuzzies remaining my $testlen = length($test); my $len = length($dict) - $testlen - 1; # This loop illustrates the states a finite state machine would # require. The states would be numbered 3*$at_pos + $fuzzies, # that is, 75 states in this case. while (1) { if ($fuzzies > 0) { if ($dict[$dict_pos + $at_pos] ne $test[$at_pos]) { --$fuzzies; } ++$at_pos; if ($at_pos >= $testlen) { print "Found at $dict_pos with $fuzzies remaining\n"; last; } } else { # Out of fuzzies. Rest of match must be exact, or start over # in the next position. my $s1 = substr($dict, $dict_pos + $at_pos, $testlen - $at_pos); my $s2 = substr($test, $at_pos); if ($s1 ne $s2) { last if (++$dict_pos > $len); $fuzzies = 2; $at_pos = 0; } else { print "Found at $dict_pos with $fuzzies remaining\n"; last; } } }

In reply to Re^4: Fuzzy Searching:Alg.Sel. (Why tries won't work for this application.) by tall_man
in thread Fuzzy Searching: Optimizing Algorithm Selection by Itatsumaki

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.