This is related to the Longest Repeated Substring Problem. According to the article I linked to, the problem can be solved in linear time after building a Suffix Tree. It turns out there's a module on CPAN that works with suffix trees: Tree::Suffix.
Tree::Suffix is a big pain to install. You have to visit http://www.icir.org/christian/libstree/, download and unpack its tarball, and then sudo ./configure. After it's installed you have to force the installation of Tree::Suffix: cpanm -f Tree::Suffix. I haven't taken the time to uncover what's wrong with it that causes some failures. But it seems to work once installed.
After that's all said and done, it becomes easy:
use Tree::Suffix; my $string = 'The cat jumped over the dog. Smart cat! He jumped over the dog. + ' . 'The cat jumped over the dog.'; my $tree = Tree::Suffix->new($string); print "$_\n" for $tree->lrs;
The output...
The cat jumped over the dog.
$tree->lrs is a convenient alias to $tree->longest_repeated_substrings; you can use either method name.
If anyone knows of a better module for building suffix trees (one that installs cleanly), please follow-up with suggestions.
An aside: If anyone knows how to get in touch with the author for SuffixTree please let me know. His listed email address is no longer valid. I've also sent a message to module-authors@perl.org trying to locate him so that I can work on applying the patches that have been sitting for a few years in the RT. In the meantime an unofficial release has been uploaded.
Dave
In reply to Re: Finding duplicate text in a paragraph
by davido
in thread Finding duplicate text in a paragraph
by Jester
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |