Wednesday, April 14, 2010

Which term?

Sometimes I can't decide between using one term over the other. For example, is it multitouch or multi-touch? Potato or potato? Okay, the last example is a joke, but you know what I mean...

Google can help! Just search the two terms in Google and see which query gives you more results. There are two issues:
  1. The first is fairly obvious: you need to add quotes to the terms you're searching. Otherwise, you won't be searching for exact matches.
  2. Google often re-writes your query and show those results instead of your original. To disallow this, add nfpr=1 to the search URL.
With the two above, voila! A Ruby script to compare two terms:
#!/usr/bin/ruby

require 'net/http'
require 'uri'
require 'cgi'

NUM_RESULTS_REGEX = /#{"<p id=resultStats>&nbsp;Results"+
" <b>\\d+<\/b> - <b>\\d+<\/b>"+
" of about <b>([\\d,]+)<\/b> for <b>"}/
SEARCH_URL = "http://www.google.com/search?nfpr=1&q="

if ARGV.size != 2
$stderr.puts "Usage: ./which_usage.rb \"term 1\" \"term 2\""
exit(1)
end

ARGV.each { |q|
puts %Q{"#{q}": #{
(Net::HTTP.get URI.parse(
SEARCH_URL+CGI.escape(%Q{"#{q}"})
)).scan(NUM_RESULTS_REGEX).first.first} results.}
}
Enjoy!

No comments:

Post a Comment