This is a solution by ysth. It exploits the fact that if a word of M characters is to be matched with N fuzz then if you were to divide the word into N+1 parts at least one of them would have to match exactly. He uses hashes and the regex engine to do his stuff quickly. This appears to scale reasonably well. Ill leave it to ysth to explain in more detail.

package Fuzzy::Matcher::Ysth; use strict; use warnings; use Fuzzy::Matcher; use vars qw/$VERSION @ISA/; @ISA=qw(Fuzzy::Matcher); $VERSION=0.01; # Original implementation by [ysth] # modified to fit Fuzzy::Matcher interface by [demerphq] sub prepare { my ($self)=@_; my $fuzz=$self->{fuzz}; my $length=$self->{strlen}; die "still hardcoded for fuzz 2" if $fuzz != 2; delete $self->{str_hash}; my $fuzz_strings = delete $self->{str_array}; my $chunks = join ")(", map "."x (int($_*$length/($fuzz+1))-int(($_-1)*$length/($fuzz+1) +)), 1..$fuzz+1; my @filters; my $qr = qr/(?=(($chunks)))./; for my $frag (@$fuzz_strings) { my @m = $frag =~ $qr or die "something's horribly wrong, $frag =~ $qr "; push @{$filters[$_]{$m[$_]}}, $frag for 1..$#m; } $self->{ysth_filters} = \@filters; $self->{ysth_qr} = $qr; } sub fuzz_search { my ($self, $seq) = @_; die "Unprepared to fuzz_search!" unless $self->{ysth_filters}; my $fuzz=$self->{fuzz}; my @filters = @{$self->{ysth_filters}}; my $qr = $self->{ysth_qr}; my @matches; my $empty = []; while ($seq =~ /$qr/g) { my %uniq; for (map @{$filters[$_-1]{substr $seq, $-[$_], $+[$_]-$-[$_]}| +|$empty}, 2..$#-) { # print "trying $1 against $_ len $length fuzz $fuzz seq $ +seq\n"; # die; if ( ($1 ^ $_) =~ tr/\0//c <= $fuzz && !$uniq{$_}++) { push @matches, $-[0], ($1 ^ $_) =~ tr/\0//c, $_; } } } return \@matches; } 1;
---
demerphq


In reply to Re^3: Algorithm Showdown: Fuzzy Matching (Ysth.pm) by demerphq
in thread 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.