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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |