use strict; use WWW::Mechanize; my $url = "http://www.ai.mit.edu/projects/infolab/"; my $question = 'What is AI'; my $robot = new WWW::Mechanize; $robot->get($url); $robot->form_number('1'); $robot->set_fields('query' => $question); # ask a question $robot->click(); # Get the reply to my question my $html = $robot->content(); # Extract the answer my ($text) = $html =~ /(<H1>START(?:.|\n)*<HR>)/mg; # Reformat the text $text =~ s/<[^>]*>//g; # Strip HTML tags $text =~ s/(?<!\n)\n(?!\n)/ /mg; # Combine lines $text =~ y/ //s; # Squash multiple spaces
my $len; $text =~ s/((\S+)(?=\n|\s)|\n)/ # Reformat plain text if ($1 eq "\n") { # wrapping at col 60 $len = 0; $1 } elsif ($len + length($1) > 60) { $len = length($1) + 1; "\n$1" } else { $len += length($1) + 1; $1 } /mge;
And the formatted answer to my question is -# after some playing around, I came up with the # following regex that does wrapping at column # 60 perfectly. I love perl. ;-) $text =~ s/(.{50,60}(?<=\s\b))/$1\n/mg; print "$text\n";
START's reply ===> What is AI Artificial Intelligence is the study of the computations that make it possible to perceive, reason and act. From the perspective of this definition, artificial intelligence differs from most of psychology because of the greater emphasis on computation, and artificial intelligence differs from most of computer science because of the emphasis on perception, reasoning and action. The central goal of the Artificial Intelligence Laboratory is to develop a computational theory of intelligence extending from the manifestation of behavior to operations at the neural level. Current work focuses especially on understanding the role of vision, language, and motor computation in general intelligence.
In reply to Re: How to "fake" web browser from Perl
by Roger
in thread How to "fake" web browser from Perl
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |