#!/usr/bin/perl
use LWP::Simple;
open (STDOUT, "| tee -ai log.txt");
$URL = "http://google.com/complete/search?output=toolbar&q=";
$q = "how can i ";
process($q);
close (STDOUT);

sub process {
  sleep 1;
  local $query = $_[0];
  print "[$query]\n";
  $content = get($URL.$query);
  my @values = split('data=\"', $content);
  my @freq = split('int=\"', $content);
  print "[".$#values."]\n";
  if ($#values==0) {return();}
  if ($#values<10){
    local $i;
    for ($i=1;$i<@values;$i++) {
      my @val = split('\"',$values[$i]);
      print ($val[0]."\t");
      my @val = split('\"',$freq[$i]);
      print ($val[0]."\n");
    }
  } else {
    local $j;
    for ($j=0;$j<26;$j++){
      process($query.(chr($j+97)));
    }
    $last = substr $query,-1,1;
    if (($last ne "'") && ($last ne " ")) {process($query." ");}
    if (($last ne "'") && ($last ne " ")) {process($query."'");}
  }
  return();
}