Okay, then, how fast is a not-so-naive solution for you? It's still not a real algorithm for finding likely English words, though. (Seems like there should be one out there, but I've never seen it.) So I improvise. This one generates a list of letter triples from the word list and finds those first. That speeds things up considerably.
One thing that is bothering me is that you said you need to score each string by how well the words fit together. That sounds like a hairy problem if there are lots of ways to divide a string into words, each with different leftover garbage letters, different word lengths, etc. Whatever method you use to find the words will probably need to know something about how you score the strings so that it can look for the best fit.
my @words = #...;
my %triples;
foreach (@words) {
for (my $i = 0; $i < length($_) - 2; $i++) {
push @{$triples{substr $_, $i, 3} ||= []}, $_;
}
}
# Keep most popular triples
my @triples = grep { @{$triples{$_}} > 10 } keys %triples;
my %lensum;
foreach my $t (@triples) {
foreach my $w (@{$triples{$t}}) {
$lensum{$t} += length $w;
}
}
@triples = sort { $lensum{$b} <=> $lensum{$a} } @triples;
my $re = join '|', map quotemeta, @triples;
while (<>) {
print;
my @hopes;
while (/$re/gio) {
push @hopes, @{$triples{$&}};
}
my $word = join '|', map quotemeta,
sort { length($b) <=> length($a) } @hopes;
1 while s/$word/[*]/i;
print;
}
-
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.