This randomly generates numbers (sets) -- 82% valid, 9% with a digit missing, 9% with an extra digit -- and builds 4 indexes as it runs:
It requires a 64-bit perl > 5.18; consumes a fixed 3GB ram when running, and processes the numbers at a rate of 4000 per second; with a run of 10 million taking 40 minutes.
The bitmaps can be saved to disk after a run using the option -SAVE and restored at the beginning of another run using the option -LOAD. The mechanism is currently quite crude using a fixed name of "$0.bin".
It currently prints out whether a number matches a known number: exactly or with 1 deletion, insertion or substitution.
It does not currently tell you which known number(s) it matches -- storing the information required to do that would require terabytes of data -- but given the process of generating the possibilities for any given 1-digit edit is so fast, repeating the process just for those detected -- approximately 0.01% for my randoms -- prior to doing your full message text match is no hardship.
The comments are sparse, but should gives some cluebats. Yell if you want more.
#! perl -slw use strict; use Time::HiRes qw[ time ]; use Data::Dump qw[ pp ]; sub receive{ my $n = join '', map 1+int( rand 9 ), 1 .. 9; chop $n if rand() < 0.1; $n .= 1+int( rand 9 ) if rand() > 0.9; return $n; } $|++; our $SAVE //= 0; our $LOAD //= 0; our $SRAND //= 0; srand( $SRAND ) if $SRAND; our $N //= 1e5; my( $knowns, $deletions, $insertions, $transformations ) = ( chr(0)x12 +5_000_000, chr(0)x12_500_000, chr(0)x1_250_000_000, chr(0)x125_000_00 +0 ); my( $known, $new, $deletion, $insertion, $transformed ) = (0) x 5; if( $LOAD ) { open BIN, '<:raw', "$0.bin" or die $!; { local $/ = \1_250_000_000; $insertions = <BIN>; } { local $/ = \ 125_000_000; $knowns = <BIN>; } { local $/ = \ 125_000_000; $transformations = <BIN>; } { local $/ = \ 12_500_000; $deletions = <BIN>; } close BIN; $known = unpack '%32b*', $knowns; } my $start = time; for ( 1 .. $N ) { printf STDERR "\r%10d\t", $_ unless $_ % 1000; my $received = receive(); if( length( $received ) == 9 ) { if( vec( $knowns, $received, 1 ) ) { print( "$received matched a known number" ); ++$known; next; } elsif( vec( $transformations, $received, 1 ) ) { print "$received matches a known number with a substitutio +n."; ++$transformed; next; } } elsif( length( $received ) == 8 ) { if( vec( $deletions, $received, 1 ) ) { print "$received matches a known number with a deletion."; ++$deletion; } next; } elsif( length( $received ) == 10 ) { if( vec( $insertions, $received, 1 ) ) { print "$received matches a known number with an insertion. +"; ++$insertion; } next; } ++$new, vec( $knowns, $received, 1 ) = 1; ## new number for my $pos ( 0 .. 8 ) { my $copy; vec( $deletions, substr( $copy = $received, $pos, 1, '' ), 1 + ) = 1; ## add all possible 1-digit deletions to th +eir index vec( $insertions, substr( $copy = $received, $pos, 1, $_ ), 1 + ) = 1 for 1 .. 9; ## add all possible 1-digit insertions to t +heir index my $digit = substr( $received, $pos, 1 ); vec( $transformations, substr( $copy = $received, $pos, 1, $_ +), 1 ) = 1 for 1 .. $digit-1, $digit+1 .. 9; ## all poss 1-digit sub +stitutions } vec( $insertions, $received . $_, 1 ) = 1 for 1 .. 9; + ## all possible insertions after last digi +t. } printf STDERR "From %d received there were: %d new; %d known; %d delet +ions; %d insertions; %d transformations.\n", $N, $new, $known, $deletion, $insertion, $transformed; printf STDERR "Numbers processed at a rate of %.f/second\n", $N / ( ti +me() - $start ); if( $SAVE ) { open BIN, '>:raw', "$0.bin" or die $!; printf BIN "%s%s%s%s", $knowns, $deletions, $insertions, $transfor +mations; close BIN; } __END__ C:\test>\Perl22\bin\perl.exe 1172842.pl -N=1e7 -SAVE > null 10000000 From 10000000 received there were: 8116086 new; 86155 +known; 0 deletions; 9459 insertions; 0 transformations. Numbers processed at a rate of 4082/second C:\test>\Perl22\bin\perl.exe 1172842.pl -N=1e7 -SAVE -LOAD > null 10000000 From 10000000 received there were: 7963211 new; 836492 +9 known; 0 deletions; 24187 insertions; 0 transformations. Numbers processed at a rate of 4218/second
In reply to Re: Finding Nearly Identical Sets (Updated:4200/sec)
by BrowserUk
in thread Finding Nearly Identical Sets
by Limbic~Region
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |