Hello monks,
I have a first long list (array) of n-grams (array various length, typical: 10.000 elements) and a second even bigger reference list with n-grams (~500.000 elements). I need to check if elements of list 1 are present in list 2. The match must be fuzzy, i.e. both lists are words, and I need to match also very similar words (basically to match singular-plurals, small differences in spellings, and so on). My script (very naive) works, but I'd like to hear from you some suggestions to improve its speed (which is quite crucial in my application). I already had a BIG gain in time switching from Text::Fuzzy::PP to Text::Fuzzy, but surely the script could further improved. Data looks like this (each line is one element of multi word units):
IP exchange
IPX
Internet Protocol Exchange
DPM
Data Point Model
My code
use strict;
use warnings;
use Text::Fuzzy;
use Data::Dumper;
my @stringsToMatch=("extra-articular arthrodesis", "malnutritions");#m
+y first list. I want to check if its elements are in my reference lis
+t $referenceNgrams
my @goodmatches;
my $referenceNgrams="EN.txt";#my huge reference file (second list)
print "Loading n-grams from $referenceNgrams\n";
open my $inFH, '<:encoding(UTF-8)', $referenceNgrams or die;
chomp(my @Corpus = <$inFH>);
close $inFH;
#Matching with Fuzzy
foreach my $stringToMatch (@stringsToMatch){
print "Working on $stringToMatch\n";
foreach my $corpusElement (@Corpus){
#matching only if $stringToMatch has the same amount of elemen
+ts of $corpusElement (to save time?)
my $elementsInstringToMatch = 1 + ($stringToMatch =~ tr{ }{ })
+;
my $elementsIncorpusElement = 1 + ($corpusElement =~ tr{ }{ })
+;
if ($elementsIncorpusElement eq $elementsInstringToMatch){
my $tf = Text::Fuzzy->new ($stringToMatch);
my $distance= $tf->distance ($corpusElement);
if ($distance < 2){#sensibility
push (@goodmatches, $stringToMatch);
last;#go out of loop if match has been found
}
}
}
}
print "Good matches:\n";
print Dumper @goodmatches;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|