Hi, all, I have wrote a program to compute the largest common substrings of two sentences on the word level. Below is the code:
sub lc_substr { my ($s, $t) = @_; my $z = 0; my @S = (undef, split(/ /, $s)); my @T = (undef, split(/ /, $t)); my $m = scalar @S; my $n = scalar @T; my @LCS = (); my @start = (); my @end = (); for my $i ( 1 .. $m ) { for my $j ( 1 .. $n ) { if ($S[$i] =~ m/^$T[$j]$/ig && $S[$i] ne undef && $T[$j] ne unde +f &&$S[$i] ne " "&&$T[$j] ne " ") { $L[$i-1][$j-1] ||= 0; $L[$i][$j] = $L[$i-1][$j-1] + 1; if ($L[$i][$j] > $z) { $z = $L[$i][$j]; @ret = (); @start = (); @end = (); @prod = (); } if ($L[$i][$j] == $z) { $start = $i-$z+1; $end = $i; push(@start,$start); push(@end,$end); push(@prod,$t); } } } } $num = scalar @start; for ($n = 0; $n < $num;$n++){ if($prod[$n] ne ""){ $str = $prod[$n]." "."[".$start[$n].",".$end[$n]."]"; push (@LCS, $str); } } return @LCS; }
This function would return all the longest consecutive words between these two sentnces. The problem is right now I have two lists of over 4000 sentnces to compare, while using this function is extremly slow. Is there a way to accelerate this function? thanks in advance. Zhe

In reply to LCS efficiency problem by zhe

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.