Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 88 GooPoetry

figs/beginner.giffigs/hack88.gif

Google's got the soul of a poet, or at least knows how to toss a good word salad.

Perhaps you didn't realize it, but with a little help from a hack, Google can churn out poetry that will bring a tear to your eye. Okay, perhaps not. But Google sure can mix a mean word salad.

This hack takes a query and uses random words from the titles returned by the query to spit out a poem of random length. The user can specify a poetry "flavor," adding words to the array to be used. The flavors in this version of the hack include: hippie, beatnik, and Swedish Chef. Here's a paean to the O'Reilly Camel Book, flavored by Shakespeare:

-- 3rd alas! to the
O'Reilly thee |
2nd Camel Book
Catalog: | hither Book Welcome oreilly.com Edition --
2000 Programming The
-- Dictionary] Book sirrah alas!
-- Perl 2000 2nd
2000 node: Camel Dictionary] Better node: Jargon oreilly.com
thee thee -- oreilly.com Programming 2nd oreilly.com

88.1 The Code

#!/usr/local/bin/perl
# goopoetry.cgi
# Generates a mean word salad.
# goopoetry.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";

# Number of lines per poem
my $numlines = 10;

# Number of words per line
my $numwords = 6;

use strict;

use SOAP::Lite;
use CGI qw/:standard/;

my $flavors = {
  'Hippie' => ['wow', 'groovy man!', 'far out!', 'Right on!', 
    'funky', 'outta sight', 'Like,','peace out!', 
    'munchies'],
  'Beatnik' => ['daddy-o', 'long gone', 'hepcat', 'jazzy',
    'cool', 'hip','cool','jazzman','zoot'], 
  'Shakespeare' => ['thee', 'hark!', 'forsooth,', 'alas!', 'sirrah', 
    'hither', 'hence'],
  'Swedish Chef' => ['bork bork bork!', 'hmdordeborkbork', 'BORK!', 
    'hrm de hr', 'bork?', 'hur chikee chikee'],
  'Default' => ['...', '!', '(?)', '---']
};

print
  header(  ),
  start_html("GooPoetry"),
  h1("GooPoetry"),
  start_form(-method=>'GET'),
  'Query: ', textfield(-name=>'query'),
  br(  ),
  'Flavor: ', popup_menu(
    -name=>'flavor', -values=>[keys %$flavors], -default=>'Default'
  ),
  br(  ),
  submit(-name=>'submit', -value=>'Toss that Word Salad'),
  end_form(), p(  );

if (param('flavor')) {
  my $google_search = SOAP::Lite->service("file:$google_wdsl");

  # Create an array for the random words
  my @words;
  # Mix in the flavored words
  push @words, @{$flavors->{param('flavor')}};

  # Query Google
  my $results = $google_search -> 
    doGoogleSearch(
      $google_key, param('query'), 0, 10, "false", "", "false",
      "", "latin1", "latin1"
    );

  # Glean and clean title words from results
  foreach my $result (@{$results->{'resultElements'}}) {
    $result->{title} =~ s!\n!!g; # drop spurious newlines
    $result->{title} =~ s!!!g; # drop all HTML tags
    push @words, split /\s+/, $result->{title};
  }

  for (my $l = 0; $l <= $numlines; $l++) {
    # Randomly decide the number of words in this sentence
    for (my $w = 0; $w <= int(rand($numwords))+3; $w++) {
      print lc $words[rand(scalar @words)] . ' ';
    }
    print "";
  }
}

88.2 Running the Hack

Point your browser at the CGI script, fill out the form, and click the "Toss that Word Salad" button. Figure 7-1 shows an example.

Figure 7-1. Google-generated poetry
figs/gooH_0701.gif

88.3 Hacking the Hack

You may have noticed that this code does not have an error message, if the query submitted does not get any results. That's on purpose; because there is always a "flavor" array pushed into the "words" array, even a query that gets no results will create a poem. For example, if you searched for an query that got no results, and were using the "beatnik" flavor, you'd get a poem with lines like this:

cool jazzy
long gone jazzman long gone hepcat zoot
cool zoot zoot jazzman hepcat jazzman zoot long gone

As you can see, it's just words from the beatnik flavor repeated over and over, as there's nothing else in the @words array.

You can add flavors to your heart's content. Simply add another entry in the $flavors data structure. Say, for instance, you wanted to add a "Confused" flavor; you'd add the following bolded line just after the opening my $flavors = {:

my $flavors = {
  'Confused' => ['huh?', 'duh', 'what?', 'say again?', 
    'do what now?', 'wubba?'],
  'Hippie' => ['wow', 'groovy man!', 'far out!', 'Right on!', 
    'funky', 'outta sight', 'Like,','peace out!', 
    'munchies'],
  'Beatnik' => ['daddy-o', 'long gone', 'hepcat', 'jazzy',
    'cool', 'hip','cool','jazzman','zoot'], 
  'Shakespeare' => ['thee', 'hark!', 'forsooth,', 'alas!', 'sirrah', 
    'hither', 'hence'],
  'Swedish Chef' => ['bork bork bork!', 'hmdordeborkbork', 'BORK!', 
    'hrm de hr', 'bork?', 'hur chikee chikee'],
  'Default' => ['...', '!', '(?)', '---']
};

That's all there is to it. You've successfully added a new flavor to the hack.

You can also change the number of lines and maximum words per line of the generated poem by changing the values of $numlines and $numwords, respectively. I did find, however, that the defaults are pretty optimal for creating interesting "poetry"; less than 10 lines and there wasn't much flavor, more than 10 and it repeated itself far too often.

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Dedication
    Credits
    Foreword
    Preface
    Chapter 1. Searching Google
    Chapter 2. Google Special Services and Collections
    Chapter 3. Third-Party Google Services
    Chapter 4. Non-API Google Applications
    Chapter 5. Introducing the Google Web API
    Chapter 6. Google Web API Applications
    Chapter 7. Google Pranks and Games
    7.1 Hacks #86-92
    Hack 86 The No-Result Search (Prank)
    Hack 87 Google Whacking
    Hack 88 GooPoetry
    Hack 89 Creating Google Art
    Hack 90 Google Bounce
    Hack 91 Google Mirror
    Hack 92 Finding Recipes
    Chapter 8. The Webmaster Side of Google
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele