Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Re: Finding dictionary words in a string.

by TilRMan (Friar)
on Mar 13, 2004 at 07:28 UTC ( [id://336341]=note: print w/replies, xml ) Need Help??


in reply to Re: Finding dictionary words in a string.
in thread Finding dictionary words in a string.

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; }

-- 
LP^>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found