U E D R , A S I H C RSS

Google/Google Talk

from

http://douweosinga.com/projects/googletalk 를 참조하여서 kldp.net 에 aero 님께서 올리신 글입니다.
Korean Google Talk

code

~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;
}

result

google_talks.pl "내가 보건데"
~cpp 
내가 보건데
한국의
대학이
어떻게
수용할
경우에는
양도세를
내야
한다
세금을
내야
하지
않겠소이까
자칭
선배라는
사람의
입장도
생각하는
도량은
있어야지요
20050109
33 
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:19
Processing time 0.0109 sec