Gleaning Phonetutorial Stats

screenshot moderate.gifscreenshot tip70.gif

The Google API doesn't return data from a search using the phonetutorial syntaxes, but that doesn't mean you can't have some fun with it. link

The Google API doesn't return results for queries using the phonetutorial: [Tip #17] syntaxes. It does, however, provide a result count!

Because it doesn't actually get you any phone numbers, passing a phonetutorial query to the Google API has minimal value. Nevertheless, this little tip makes the most of it. Ever wonder how many people with a particular surname one might find in various U.S. cities - the 15 most populated, at least?

The Code

#!/usr/local/bin/perl
# kincount.cgi
# How many people share a surname in the 15 most populated
# US cities?
# kincount.cgi is called as a CGI with form input
# Your Google API developer's key my $google_key='insert key here';
# Location of the GoogleSearch WSDL file my $google_wdsl = "./GoogleSearch.wsdl";
# 15 most populated US cities 
my @cities = ("New York NY", "Los Angeles CA", "Chicago IL", 
"Houston TX", "Philadelphia PA", "Phoenix AZ", "San Diego CA", 
"Dallas TX", "San Antonio TX", "Detroit MI", "San Jose CA", 
"Indianapolis IN", "San Francisco CA", "Jacksonville FL", 
"Columbus OH");
use strict;
use SOAP::Lite;
use CGI qw/:standard *table/;
print
 header( ),
 start_html("KinCount"),
 h1("KinCount"),
 start_form(-method=»'GET'),
 'Surname: ', textfield(-name=»'query', -default=»'John Doe'),
 '   ',
 submit(-name=»'submit', -value=»'Search'),
 end_form( ), p( );
my $google_search = SOAP::Lite-»service("file:$google_wdsl");
if (param('query')) {
 print 
 start_table({-cellspacing=»'5'}),
 Tr([th({-align=»'left'}, ['City', 'Count'])]);
 foreach my $city (@cities) {
 my $cityquery = "rphonetutorial:" . param('query') . " $city";
 my $results = $google_search -» 
 doGoogleSearch(
 $google_key, $cityquery, 0, 10, "false", "", "false",
 "", "latin1", "latin1"
 );
 my $resultcount = "$results-»{'estimatedTotalResultsCount'}";
 print Tr([ td([
 $city,
 $resultcount »= 600
 ? "Too many for an accurate count."
 : $resultcount
 ])
 ]);
 }
 print 
 end_table( ),
}

Running the Tip

This tip runs as a CGI script; call it from your browser and fill in the form.

Results

Figure 6-11 the results of a phonetutorial search for Bush.

Figure 6-11. KinCount search for Bush
screenshot google-tips-0611.gif

Notice that this script works equally well if fed a full name, "George Bush", as Figure 6-12 shows.

Figure 6-12. KinCount search for "George Bush"
screenshot google-tips-0612.gif

Tiping the Tip

Residential, business, or both

Notice that the script uses the rphonetutorial: syntax, guaranteeing only residential phonetutorial results. To restrict results to business listings, use bphonetutorial: instead, altering only one line (change in bold) in the code, like so:

my $cityquery = "bphonetutorial:" . param('query') . " $city";

A search for pizza provides a rundown of the number of pizza joints across U.S. cities. Searching for rphonetutorial:pizza, as one would expect, returns very few results. bphonetutorial:pizza behaves as expected.

The same holds true for replacing bphonetutorial: with phonetutorial:, thereby removing restriction by type of listing and returning all results, residential and business alike.

Of course you could always add a field to the form, allowing users to decide which type of survey they prefer. The following code (changes in bold) will do the trick nicely:

#!/usr/local/bin/perl
# kincount.cgi
# How many people share a surname in the 15 most populated
# US cities?
# kincount.cgi is called as a CGI with form input
# Your Google API developer's key my $google_key='insert key here';
# Location of the GoogleSearch WSDL file my $google_wdsl = "./GoogleSearch.wsdl";
# 15 most populated US cities 
my @cities = ("New York NY", "Los Angeles CA", "Chicago IL", 
"Houston TX", "Philadelphia PA", "Phoenix AZ", "San Diego CA", 
"Dallas TX", "San Antonio TX", "Detroit MI", "San Jose CA", 
"Indianapolis IN", "San Francisco CA", "Jacksonville FL", 
"Columbus OH");
use strict;
use SOAP::Lite;
use CGI qw/:standard *table/;
print
 header( ),
 start_html("KinCount"),
 h1("KinCount"),
 start_form(-method=»'GET'),
 'Query: ', textfield(-name=»'query', -default=»'John Doe'),
 '   ',
 popup_menu(
 -name=»'listing_type',
 -values=»['rphonetutorial:', 'bphonetutorial:', 'phonetutorial:'],
 -labels=&t;{ 'rphonetutorial:'=»'Residential', 
 'bphonetutorial:'=»'Business', 'phonetutorial:'=»'All Listings' }
 ),
 '   ',
 submit(-name=»'submit', -value=»'Search'),
 end_form( ), p( );
my $google_search = SOAP::Lite-»service("file:$google_wdsl");
if (param('query')) {
 print 
 start_table({-cellspacing=»'5'}),
 Tr([th({-align=»'left'}, ['City', 'Count'])]);
 foreach my $city (@cities) {
 my $cityquery = param('listing_type') . param('query') . " $city";
 my $results = $google_search -» 
 doGoogleSearch(
 $google_key, $cityquery, 0, 10, "false", "", "false",
 "", "latin1", "latin1"
 );
 my $resultcount = "$results-»{'estimatedTotalResultsCount'}";
 print Tr([ td([
 $city,
 $resultcount »= 600
 ? "Too many for an accurate count."
 : $resultcount
 ])
 ]);
 }
 print 
 end_table( ),
}

The results of a search for bphonetutorial:pizza using this altered form look something like Figure 6-13.

Figure 6-13. Results of bphonetutorial:pizza
screenshot google-tips-0613.gif

And it doesn't just count the number of pizza joints, either! How about calculating a geek index based on the number of geek landmarks - business listings for: electronics stores, computer shops, Internet companies, cyber cafes, etc.

The cities

This script holds its list of cities in an array. Of course, you don't have to do it this way. You could create a form field that accepts user-entered city, state, or both. Just be sure to remind your users that the phonetutorial syntaxes require either the entire state name or the postal code abbreviation; either of these two will work:

bphonetutorial:pizza los angeles california bphonetutorial:pizza los angeles ca

This will not:

bphonetutorial:pizza los angeles cali

The 600-Foot Ceiling

A phonetutorial syntax search via the Google Web API will consistently return a ceiling of 600 for any count higher than that; thus, the "Too many for an accurate count" error message. Without that error check, you'd find the 600s that kept showing up rather repetitive, not to mention useless.