~cpp
#!/usr/bin/perl
use LWP 5.65;
use URI::URL;
my $debug=0;
my $query = $ARGV[0];
print $query."\n";
my $close_counter=0;
while(1)
{
$query =~ m/\ (.*?)$/;
print "last_word :".$1."\n" if $debug;
$query = query_word($query);
unless($query)
{
$query = $1;
$close_counter++;
if($close_counter>1) { exit; }
}
else
{
print $query."\n";
$query = $1." ".$query;
$close_counter=0;
}
}
sub query_word
{
my $q = $_[0];
$q = '" '.$q.' "';
my $unencoded_url = 'http://www.google.com/search?hl=ko&num=10&q='.$q;
my $url = URI::URL->new($unencoded_url);
print "unencoded: " . $unencoded_url ."\n" if $debug;
print "encoded: " . $url->as_string . "\n" if $debug;
my $browser = LWP::UserAgent->new();
my $response = $browser->get($url->as_string, 'User-Agent'=>'Mozilla' );
if($response->is_success)
{
my $res = $response->content;
$res =~ s/<b>//gs;
$res =~ s/<\/b>//gs;
$res =~ s/\n//gs;
print "Response:\n$res\n" if $debug;
#$res =~ m/<\/a><br><font size=-1>(.*?)<br><font color=#008000>/i;
my @next_words = ();
while($res=~m/<\/a><br><font size=-1>(.*?)<br><font color=#008000>/g)
{
print $1."\n\n" if $debug;
my $s = $1;
my $next_word = get_next_words($s,$q);
if ($next_word) {push(@next_words,$next_word);}
pos($res)+1;
}
print "@next_words ".($#next_words+1)."\n" if $debug;
my $select = rand($#next_words+1);
print "selected :".$next_words[$select]."\n" if $debug;
return $next_words[$select];
}
else
{
print $response->error_as_HTML if $debug;
}
}
sub get_next_words
{
my($text,$q) = @_;
$q =~ s/"//g;
print "query :".$q."\n" if $debug;
$text =~ s/[a-zA-Z_&#|;:<>,?.~\*\^\$\[\]\-\+()\/=]//g;
print "text :".$text."\n" if $debug;
$text =~ m/($q)(\S*?)\ /;
print "match 1 |".$1."| 2 |".$2."| 3 |".$3."| 4 |".$4."|\n" if $debug;
return $2;
}