Graph Google Search Results

9

When you search for something on Google, it conveniently gives a message near the top of the page saying something like About 53,000,000 results (0.22 seconds). (The numbers change depending on what was searched of course.)

In this challenge you will write a program that draws a logarithmic ASCII graph of the number of results given by Google when all the non-empty prefixes of a given search-phrase are searched.

A search-phrase is defined as one or more strings of lowercase alphanumeric characters, separated by one space from each other. In Regex a search-phrase is (?:[a-z0-9]+ )*[a-z0-9]+.

So im ok, r, and 1a 2 are all search-phrases, but I'm OK, R, 1a 2, and , are not.

(The character restrictions are in place because Google rarely takes case or special symbols into account. Escaping non-alphanumeric characters in URLs is also a hassle.)

Spec

Your program must take in a search-phrase and a positive floating point number H from either stdin or the command line. (You can assume they are valid and it's fine if you need quotes or something around the search-phrase.)

As a working example let's assume the search phrase is a car and H = 0.75.

Step 1:
Gather the non-empty prefixes of your search-phrase, and put them in double quotes. The quotes ensure that that the exact phrase will be searched for, avoiding any 'did you mean...' redirections.

Exclude all prefixes that end in a space such as a[space].

Prefixes
"a"
"a c"
"a ca"
"a car"

Step 2:
Search each of these terms exactly as they appear using https://www.google.com, and note the number of results returned.

Search Term    Message                                       Results
"a"            About 6,950,000,000 results (0.27 seconds)    6950000000
"a c"          About 861,000,000 results (0.27 seconds)      861000000 
"a ca"         About 2,990,000 results (0.30 seconds)        2990000
"a car"        About 53,900,000 results (0.39 seconds)       53900000

If the search term did not match any documents, put a 0 in the Results column.

Step 3:
Compute y = floor(H * log10(r + 1)) for each row, where r is the Results value. H is still 0.75 here.

Search Term    Results       y
"a"            6950000000    7
"a c"          861000000     6
"a ca"         2990000       4
"a car"        53900000      5

Step 4:
Arrange y number of vertical bars (|) above the last character of each unquoted search term, using spaces to fill empty areas, in a sort of bar graph.

|
| |
| | |
| |||
| |||
| |||
| |||
a car

This graph is the final result of your program and the only thing it needs to output. It should go to stdout.

Scoring

This is , so the shortest program in bytes wins.

Notes

  • You may use URL shorteners or other search tools/APIs as long as the results would be the same as searching https://www.google.com.
  • I know that double quotes are not a surefire way to exclude "did you mean..." redirections. Adding &nfpr=1 to the URL doesn't always work either. Don't worry about these inaccuracies. Just look for the About X results... message no matter what pops up, or set Results to 0 if there is none.
  • There is an empty column above any space in the search-phrase in the graph.
  • The graph should not be wider or taller than it needs to be (e.g. with whitespace).
  • It's ok if your program has side effects like opening a web browser so the cryptic Google html/js pages can be read as they are rendered.

Calvin's Hobbies

Posted 2014-09-18T23:13:16.493

Reputation: 84 000

I know it's not typical to get zero results, but when you do there is no "About x results...". I assume that should be detected and shown as 0 bars?

– Geobits – 2014-09-19T02:17:44.720

@Geobits Yes, assume 0 results. – Calvin's Hobbies – 2014-09-19T03:00:23.713

1Just so you know - don't hammer google - it will respond with a captcha if you hit it too hard/too often, which might break your program – SeanC – 2014-09-19T18:42:44.950

Answers

4

Ruby, 316 295 bytes

require 'open-uri'
s,h=*$*
r=[]
s.size.times{|i|r<<(s[i]==' '?'':?|*(h.to_f*Math.log10((URI.parse("http://google.com/search?q=#{URI::encode ?"+s[0..i]+?"}").read[/About [\d,]+ results/]||?0).gsub(/\D/,'').to_i+1)).floor)+s[i]}
puts r.map{|l|l.rjust(r.map(&:size).max).chars}.transpose.map &:join

Unfortunately, the requests just stopped working on the online tester I was using, so I need to golf this down further tonight or tomorrow.

Explanation: I'm taking the input via ARGV. Then I'm just sending a request for each substring that doesn't end in a space, find the results via regex (and default to 0 if the regex doesn't match), and then build the histogram with horizontal bars. At the end, I'm reversing all lines and transposing them to create the vertical histogram.

Martin Ender

Posted 2014-09-18T23:13:16.493

Reputation: 184 808