Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Fast common substring matching

by GrandFather (Cardinal)
on Sep 18, 2005 at 09:53 UTC ( #492993=note: print w/ replies, xml ) Need Help??


in reply to Re: Fast common substring matching
in thread Fast common substring matching

Another update fixing the last round of issues.

use strict; use warnings; use Time::HiRes; use List::Util qw(min max); use Math::Pari qw(divisors); =head Written =cut my $allLCS = 1; my $subStrSize = 2; # Determines minimum match length. Should be less +than half # the minimum interesting match length. The larger this value is the f +aster the # search runs. if (@ARGV == 0) { print "Finds longest matching substring between any pair of test s +trings\n"; print "in the given file. Pairs of lines are expected with the fir +st of a\n"; print "pair being the string name and the second the test string." +; exit (1); } print "Minimum match length is $subStrSize\n"; my @strings; # Outside the loop so subs see it while (@ARGV) {# process each file # Read in the strings my $filename = shift; print "\nProcessing: $filename\n"; @strings = (); open inFile, "< $filename"; while (<inFile>) { chomp; my $strName = $_; $_ = <inFile>; chomp; push @strings, [$strName, $_]; } close inFile; my $lastStr = @strings - 1; my %bestMatches = ('len' => 0); # Best match details my $longest = 0; # Best match length so far (unexpanded) my $startTime = [Time::HiRes::gettimeofday ()]; # Do the search for my $curStr (0..$lastStr) {# each string my ($sourceName, $source) = @{$strings[$curStr]}; my @subStrs = generatePatterns ($source); my $lastSub = @subStrs-1; for my $targetStr (($curStr+1)..$lastStr) {# each remaining string my ($targetName, $target) = @{$strings[$targetStr]}; my $targetLen = length $target; my $localLongest = 0; my @localBests = [(0, 0, 0, 0, 0)]; for my $i (0..$lastSub) { my $offset = 0; while ($offset < $targetLen) { $offset = index $target, $subStrs[$i][0], $offset; last if $offset < 0; my $matchStr2 = substr $target, $offset; my $slipage = 0; my $bestSlip = 0; my $matchLen = 0; my $first = 1; while ($first || $slipage < $subStrSize && $subStrs[ +$i][1] < $subStrSize) { my $matchStr1 = substr $source, $i * $subStrSize - + $slipage; ($matchStr1 ^ $matchStr2) =~ /^\0*/; if ($matchLen < $+[0]) { $bestSlip = $slipage; $matchLen = $+[0]; } $slipage += $subStrs[$i][1]; $first = 0; } next if $matchLen < $localLongest - $subStrSize + 1; $localLongest = $matchLen; my @test = ($curStr, $targetStr, $i * $subStrSize - +$bestSlip, $offset, $matchLen); @test = expandMatch (@test); my $dm = $test[4] - $localBests[-1][4]; @localBests = () if $dm > 0; push @localBests, [@test] if $dm >= 0; $offset = $test[3] + $test[4]; next if $test[4] < $longest; $longest = $test[4]; $dm = $longest - $bestMatches{'len'}; next if $dm < 0; %bestMatches = ('len' => $test[4]) if $dm > 0; $bestMatches{"$test[0],$test[1],$test[2],$test[3]"} += $test[4]; $bestMatches{'len'} = $test[4]; } continue {++$offset;} } next if ! $allLCS; if (! @localBests) { print "Didn't find LCS for $sourceName and $targetName\n +"; next; } for (@localBests) { my @curr = @$_; printf "%03d:%03d L[%4d] (%4d %4d)\n", $curr[0], $curr[1], $curr[4], $curr[2], $curr[3]; } } } print "Completed in " . Time::HiRes::tv_interval ($startTime) . "\n" +; my $len = $bestMatches{'len'}; for (keys %bestMatches) { next if $_ eq 'len'; my @curr = split ',', $_; printf "Best match: %s - %s. %d characters starting at %d and %d.\ +n", $strings[$curr[0]][0], $strings[$curr[1]][0], $len, $curr[2], $cur +r[3]; } } sub expandMatch { my ($index1, $index2, $str1Start, $str2Start, $matchLen) = @_; my $maxMatch = max (0, min ($str1Start, $subStrSize + 10, $str2Start)) +; my $matchStr1 = substr ($strings[$index1][1], $str1Start - $maxMatch, +$maxMatch); my $matchStr2 = substr ($strings[$index2][1], $str2Start - $maxMatch, +$maxMatch); ($matchStr1 ^ $matchStr2) =~ /\0*$/; my $adj = $+[0] - $-[0]; $matchLen += $adj; $str1Start -= $adj; $str2Start -= $adj; return ($index1, $index2, $str1Start, $str2Start, $matchLen); } sub generatePatterns { my @subStrs; my $source = shift; my %strs; for (my $i = 0; $i < (length $source) - $subStrSize + 1; $i += $subStr +Size) { my $substr = substr $source, $i, $subStrSize; my ($cycleLen, $str) = findCycle ($substr); push @subStrs, [$substr, $cycleLen]; } #push @subStrs, [$_, $strs{$_}] for keys %strs; return @subStrs; } sub findCycle { my $str = shift; my $copy = $str; my $cycleLen = 0; my $strLen = length ($copy); for (0..($strLen - 1)) { $copy .= substr $copy, 0, 1, ''; $cycleLen = $_ + 1; ($str ^ $copy) =~ /^\0*/; return wantarray ? ($cycleLen, substr $str, 0, $cycleLen) : $cycleLen if $+[0] == $strLen; } return wantarray ? ($strLen, $str) : $strLen; } sub findCycle_1 { my $str = shift; my $strLen = length $str; for ( @{ divisors( $strLen ) } ) { my $copy = $str; $copy .= substr( $copy, 0, $_, '' ); return wantarray ? ($_, substr $str, 0, $_) : $_ if $str eq $copy; } }

Perl is Huffman encoded by design.


Comment on Re^2: Fast common substring matching
Download Code
Re^3: Fast common substring matching
by Roy Johnson (Monsignor) on Nov 14, 2005 at 21:42 UTC
    I came up with an algorithm inspired by bzip's algorithm of generating all substrings and then sorting them. I tried yours on a list of 20 strings of 1000 chars, and it ran in 153 seconds. Mine ran in 0.67 seconds, yielding the same results. 30 strings of 3000 chars runs in 20.3 seconds on mine; scaling up from there starts to get painful, but I would guess the OP's requirement of 300 strings of 3000 chars would run in under an hour, if it had plenty of memory (there will be 900,000 strings averaging 1500 chars in length).

    Give it a whirl.


    Caution: Contents may have been coded under pressure.

      Roy

      There is one difference between your algorithm and Grandfather's. His code returns the longest substring for each pair of input strings.

      With my original data set your code returns one substring. Grandfather's code returned over three thousand (where $minmatch = 256). On the other hand your code finds multiple occurrences of the longest common substrings, if they all have the same length, which I like.

      Mike

        Yes, after I came up with my algorithm, I realized what all the output from GrandFather's code meant. I had thought it was just some sort of cryptic progress meter. :-)

        The (reasonably) obvious way to get the longest substring for each pair of input strings would be to run my algorithm using each pair of strings as input rather than the whole list of strings. That's probably more work than GF's method, though. I thought about trying it, but something shiny caught my attention...

        Update: but now I've done it. It runs on 20 strings of 1000 characters in something under 10 seconds for me. 100 strings of 1000 characters takes about 4 minutes.


        Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://492993]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-11-21 04:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (105 votes), past polls