Created
January 6, 2019 12:54
-
-
Save KPCCoiL/c717df1adb78a0ae3cf86f4c258bc1b0 to your computer and use it in GitHub Desktop.
script to figure out which English expression is common
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env perl6 | |
use v6.c; | |
use HTTP::UserAgent; | |
use XML::Document; | |
use HTML::Parser::XML; | |
sub get-popularity($user-agent, $phrase) { | |
my $url = 'https://www.google.com/search?lr=lang_en&as_epq=' ~ $phrase.subst(' ', '+', :g); | |
my $response = $user-agent.get($url); | |
my $parser = HTML::Parser::XML.new; | |
my $doc = $parser.parse($response.content); | |
my $result-stat = $doc.getElementById('resultStats'); | |
unless $result-stat ~~ /\&\#32004\;\s(<[\d,]>+)\s\&\#20214\;/ { | |
return -1; | |
} | |
$0.Str.subst(',', '', :g).Int; | |
} | |
sub MAIN(*@phrases) { | |
my $ua = HTTP::UserAgent.new; | |
@phrases.map(-> $phrase { | |
my $hits = await start { get-popularity($ua, $phrase) }; | |
$phrase => $hits; | |
}).sort(-*.value).map(&say); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment