Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created March 18, 2014 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoehrmann/9631975 to your computer and use it in GitHub Desktop.
Save hoehrmann/9631975 to your computer and use it in GitHub Desktop.
How to fetch Google Ngrams data
my $url = URI->new('http://books.google.com/ngrams/graph');
$url->query_form(
year_start => 1800,
year_end => 2000,
corpus => $corpus,
smoothing => 30,
content => encode_utf8($q),
);
my $res = $ua->get($url, accept_encoding => 'deflate,gzip');
my $s = $res->decoded_content(charset => 'none');
my ($year_from, $year_to) = $s =~ /drawD3Chart\(data, (\d+), (\d+)/;
my $duration = $year_to - $year_from;
$s =~ s/.*var data =//s;
$s =~ s/\];.*/]/s;
my $d = eval { JSON->new->decode($s) };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment