[Thamesvalley-pm] Fuzzy Search on text

Iain Emsley print.crimes at yatterings.com
Wed Jun 4 13:39:12 PDT 2008


Hi,

I've been playing around with an algorithm to work out a fuzzy search on 
a word (as edit distances aren't quite what I need at the moment) but 
I've not been able to get the print quite there.

Currently it prints out the sentence that the fuzzy search matches but I 
really just want the word matched. Does any one have any ideas on the 
best way of getting it?

MTIA,

Iain

use strict;
use warnings;

my $word = "marley";
my @find = map ([split //], $word);
my $find_len = length($word);
my $fuzzy    = 3;

while (my $search = <DATA>) {
  chomp $search;
  $search = [split //, $search];
    for my $i ( 0..@$search-$find_len ) {
        FIND:
        for my $find ( @find ) {
            my $misses = 0;
            for my $j ( 0..$find_len-1 ) {
                $misses++ if $search->[$i+$j] ne $find->[$j];
                next FIND if $misses > $fuzzy;
            }
           
            print "Line $. Match ($misses) at $i, @$search\n";
        }
    }
} 

__DATA__
STAVE I:  MARLEY'S GHOST

MARLEY was dead: to begin with. There is no doubt
whatever about that. The register of his burial was
signed by the clergyman, the clerk, the undertaker,
and the chief mourner. Scrouge signed it: and
Scrooge's name was good upon 'Change, for anything he
chose to put his hand to. Old Marley was as dead as a
door-nail.

Mind! I don't mean to say that I know, of my
own knowledge, what there is particularly dead about
a door-nail. I might have been inclined, myself, to
regard a coffin-nail as the deadest piece of ironmongery
in the trade. But the wisdom of our ancestors
is in the simile; and my unhallowed hands
shall not disturb it, or the Country's done for. You
will therefore permit me to repeat, emphatically, that
Marley was as dead as a door-nail.

Scrooge knew he was dead? Of course he did.
How could it be otherwise? Scrooge and he were
partners for I don't know how many years. Scrooge
was his sole executor, his sole administrator, his sole
assign, his sole residuary legatee, his sole friend, and
sole mourner. And even Scrooge was not so dreadfully
cut up by the sad event, but that he was an excellent
man of business on the very day of the funeral,
and solemnised it with an undoubted bargain.

The mention of Marley's funeral brings me back to
the point I started from. There is no doubt that Marley
was dead. This must be distinctly understood, or
nothing wonderful can come of the story I am going
to relate. If we were not perfectly convinced that
Hamlet's Father died before the play began, there
would be nothing more remarkable in his taking a
stroll at night, in an easterly wind, upon his own ramparts,
than there would be in any other middle-aged
gentleman rashly turning out after dark in a breezy
spot--say Saint Paul's Churchyard for instance--
literally to astonish his son's weak mind.

Scrooge never painted out Old Marley's name.
There it stood, years afterwards, above the warehouse
door: Scrooge and Marlie. The firm was known as
Scrooge and Marley. Sometimes people new to the
business called Scrooge Scrooge, and sometimes Marlee,
but he answered to both names. It was all the
same to him.



More information about the Thamesvalley-pm mailing list