Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have adopted the following definition of a keyword:

Word

  • Case ignored.
  • Min length: 3, Max length: 20
  • Can include a hyphen or an apostrophe but not at either end (these are stripped). Possessive 's (cat's) also stripped. (can't, won't, hasn't etc. are in the stop list).
  • All other puctuation ignored.
  • Four digit numbers between 1000 and 3000 with an optional trailing s (1960s). Anything else with a number in it is skipped.
  • Skip common words (stop words).

HTML

To preserve the apostrophe ’ and ' are replaced with '.

All other HTML entity punctuation is then removed and the HTML decoded. Apart from punctuation it is all Latin1 (I've checked it - at length!).

Update 2
I should have mentioned that the text has already been stripped from an HTML file. (!)
Apologies for any confusion

#!/usr/bin/perl use strict; use warnings; use HTML::Entities; my $config = { minw => 3, maxw => 20, }; my $stop = get_stop(); my $punc = get_punc(); my $text = q| cat&#39;s dogs O&rsquo;Reilly ad-hoc &ldquo;broken&rdquo; hyphen- the and |; my $words_all = {}; # contrived loop to show usage for my $t ($text){ get_word($t); } print "$_\n" for keys %{$words_all}; sub get_word{ my ($text, $file_key) = @_; my ($min, $max) = ($config->{minw}, $config->{maxw}); for ($text){ s/&rsquo;|&#39;/'/g; s/(&#?\w+;)/exists $punc->{$1}?' ':$1/eg; } decode_entities($text); $text =~ s/[^\w'-]/ /g; my @words = split ' ', $text; for (@words){ s/^['-]//g; s/['-]s?$//; next if length() < $min or length() > $max; next if exists $stop->{$_}; next if /\d/ and not /^[12]\d{3}s?$/; next if /--/; push @{$words_all->{$_}}, $file_key; } } sub get_stop{ # sample return { qw( and '' any '' the '' they '' ) }; } sub get_punc{ # sample return { '&rsquo;' => undef, '&lsquo;' => undef, '&rdquo;' => undef, '&ldquo;' => undef, }; } __DATA__ ---------- Capture Output ---------- > "C:\Perl\bin\perl.exe" _new.pl hyphen cat ad-hoc O'Reilly broken dogs > Terminated with exit code 0.

At the moment the full app is run locally on a copy of the web site.

It's generating 42k words but I'm working on the stop file to try and bring it down.

If this turns out to be fairly stable I'm considering compiling the regexes outside of the loop.

What do you reckon?

winxp, activestate 5.8

Update:
Corrected get_stop() sub


In reply to Extracting keywords from HTML by wfsp

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? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-01-29 15:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?