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

Extracting the sentences from a webpage

by Anonymous Monk
on Dec 07, 2009 at 22:05 UTC ( #811632=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I want to extract the sentences from a webpage and store them in an array. My attempt is below. I'm sure this can be done in a better way and that I haven't thought of all the possibilities. Any advices or tips?

Thanks!
use strict; use warnings; use LWP::Simple; use HTML::Strip; my $url = "http://www.expressen.se/ekonomi/1.1805368/reinfeldt-forsvar +ar-nya-sjukregler"; my $html = get($url); defined $html or die "Can't fetch HTML from: ",$url; my $hs = HTML::Strip->new(); my $clean_text = $hs->parse( $html ); $hs->eof; #sentences spread across multiple lines $clean_text =~ s/(\n)/ /g; $clean_text =~ s/(\r)/ /g; #compact double white space into dots $clean_text =~ s/(\s){2,}/./g; #compact dots into single dots $clean_text =~ s/(\.){2,}/\./g; #split sentances on dots my @sentences = split(/\./, $clean_text); my @realSentences = (); #ignore sentences with single words foreach my $sentence (@sentences) { if(($sentence =~ tr/ //)>1) { push @realSentences, $sentence; } } foreach (@realSentences) { print "$_\n"; }

Replies are listed 'Best First'.
Re: Extracting the sentences from a webpage
by kyle (Abbot) on Dec 07, 2009 at 22:31 UTC

    I've never used Text::Sentence or Lingua::EN::Sentence, but they look like good places to start.

    It looks as if you're trying to take a double space as the end of a sentence. Is that a special feature of your input text?

    I'd do your @realSentences filtering with grep.

    my @realSentence = grep { / / } @sentences;

    In fact, from the split on down could be reduced to:

    print "$_\n" for grep { / / } split /\./, $clean_text;
Re: Extracting the sentences from a webpage
by Lu. (Hermit) on Dec 07, 2009 at 22:35 UTC
    Hi, there First, not all dots are at the end of sentences. Example : "P. Howard was back in the U.S.S.R, with only 52.4 roubles in his pockets..." Second, why reinvent the wheel ?
      a little more accurate is: split /\. /, $text

      but yeah, don't re-invent...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://811632]
Approved by ww
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2018-07-20 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (435 votes). Check out past polls.

    Notices?