Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.