.NET

gc – a command-line interface to (parts of) Google calculator

Recently I discovered that the suggestions that are built into the search bar of Internet Explorer and Firefox expose results from the Google calculator:

Google Suggestions returning the result of a calculation

So I fired up Fiddler to look where those suggestion requests where going to and found the following URL (for the request 1+1):

http://sugges­tqueries.google­.com/complete/se­arch?output=fi­refox&client=I­E8&hl=en&qu=1%2B1

The answer to that request was a little JSON which looked like the following:

[„1+1“,
 [„= 2“,
  „1+1 канал“,
  „1+1 0“,„1+1 tv“,
  „1+1 internet“,
  „1+1 hosting“,
  „1+1.ua“,
  „1+1 ukraine“,
  „1+1.com“,
  „1+1 webmail“,
  „1+1 2 proof“
  ]
 ]

I pretty-printed the output a bit, since it comes out as one long line. As you can see, there are two nested arrays, the first element of the outer one being the search query and the inner one containing the suggestions. If the query was processed by the Google calculator the first element of the suggestions is the result.

Knowing that I hacked together a little program that sends its arguments as a search query to Google and prints the first suggestion (which hopefully is a calculator result):

X:\>gc 1+1
= 2

X:\>gc the speed of light in dozen fathoms per quarter fortnight
= 4.13099844e12 (dozen fathoms) per (quarter fortnight)

X:\>gc mass of earth in lbs
= 1.31708565e25 pounds

X:\>gc 7 liters / 1 kilometer=
= 7.0e-6 m2

Mostly I like the ability for unit conversion. Although not everything works. And unfortunately requests for physical constants don't yield a result, which they do, however, when using Google search directly. So that may be an area of improvement.

The program was written in C# 2.0 and uses LitJSON for JSON parsing. It runs on Mono as well, though I haven't tried to build it there, but since it's all .NET 2.0 it should work without problems.

Executable as well as source code is attached.

UPDATE (2008–10–26 00:34): I have updated source and executable as follows:

  1. The output encoding is now Unicode (I finally found out how)
  2. If no calculator result is returned (i. e. no = as the first char in the first result), then all results are returned. This is mainly to leverage the suggestions as intended and sometimes that can be fun, too:
X:\>gc mankind is
mankind is obsolete
mankind is obsolete lyrics
mankind is obsolete myspace
mankind is doomed
Using Google as an oracle:
X:\>gc the future will be
the future will be better tomorrow
Syndicate content