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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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^>


In reply to Re: Re: Finding dictionary words in a string. by TilRMan
in thread Finding dictionary words in a string. by ehdonhon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-16 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found