#!/usr/bin/perl -w

# This is what a google search needs to look like.
# http://www.google.com/search?q=+intitle:%22a+fate+worse+than+*%22+-death&hl=en&lr=&ie=UTF-8&start=10&sa=N

use CGI qw(:standard);

my @cliches = (
"A bee in your bonnet",
"A bird in the hand is worth two in the bush",
"A blast from the past",
"A blot on the landscape",
"A bolt from the blue",
"A diamond in the rough",
"A drop in the bucket",
"A fate worse than death",
"A fool's paradise",
"A friend in need is a friend indeed",
"A frog in the throat",
"A house divided against itself cannot stand",
"A little knowledge is a dangerous thing",
"A load of codswallop",
"A man after my own heart",
"A picture paints a thousand words",
"A place for everything and everything in its place",
"A plague on both your houses",
"A riddle wrapped up in an enigma",
"A rose by any other name would smell as sweet",
"A rose is a rose is a rose",
"A small step for man, a giant leap for mankind",
"A sticky wicket",
"A stone's throw",
"A stool pigeon",
"A whipping boy",
"Abandon all hope ye who enter here",
"Absence makes the heart grow fonder",
"Absent without leave",
"Absolute power corrupts absolutely",
"Accidentally on purpose",
"Alas, poor Yorick! I knew him, Horatio",
"All good things come to he who waits",
"All singing, all dancing",
"All that glitters is not gold",
"All the world's a stage",
"All things to all men",
"All you need is love",
"All's well that ends well",
"Am I my brother's keeper?",
"An albatross round his neck",
"An eye for an eye, a tooth for a tooth",
"And thereby hangs a tale",
"Angry young man",
"As fit as a fiddle",
"As mad as a hatter",
"As mad as a march hare",
"As old as the hills",
"As pure as the driven snow",
"As rich as Croesus",
"As you sow so shall you reap",
"Ashes to ashes dust to dust",
"Bad hair day",
"Barking up the wrong tree",
"Batten down the hatches",
"Beat about the bush",
"Beat swords into ploughshares",
"Beauty is in the eye of the beholder",
"Beauty is only skin deep",
"Bed of roses",
"Bee in your bonnet",
"Been there, done that",
"Behind every great man there's a great woman",
"Behind the eight ball",
"Bet your bottom dollar",
"Better to have loved and lost than never to have loved at all",
"Between a rock and a hard place",
"Beware of Greeks bearing gifts",
"Beware the ides of March",
"Beyond our ken",
"Beyond the pale",
"Bird in the hand is worth two in the bush",
"Birds of a feather flock together",
"Bite the bullet",
"Bite the dust",
"Blood is thicker than water",
"Bob's your uncle",
"Bolt from the blue",
"Born again",
"Bought the farm",
"Brevity is the soul of wit",
"Bricks and mortar",
"Bring home the bacon",
"Brownie points",
"Bun in the oven",
"Burning the midnight oil",
"Bury the hatchet",
"Bury your head in the sand",
"The Butler did it",
"By the skin of your teeth",
"Call a spade a spade",
"Carry coals to Newcastle",
"Catch 22",
"Cat's Cradle",
"Caught with your pants down",
"Cherchez la femme",
"Chip on his shoulder",
"Climb onto the bandwagon",
"Close, but no cigar",
"Cock and bull story",
"Cogito ergo sum",
"Cold comfort",
"Cold shouldered",
"Cold turkey",
"Come a cropper",
"Come hell or high water",
"Come on down",
"Come the three corners of the world in arms",
"Come what may",
"Comparisons are odious",
"Cop an attitude",
"Cor blimey",
"Cordon bleu",
"Counting sheep",
"Crocodile tears",
"Cut off your nose to spite your face",
"Cut the mustard",
"Cut to the chase",
"aisy roots",
"Dance attendance",
"Davy Jones' locker",
"Dead cat bounce",
"Dead ringer",
"Diamond in the rough",
"Discretion is the better part of valour",
"Doctor Livingstone I presume",
"Don't change horses in midstream",
"Don't let the bastards grind you down",
"Don't look a gift horse in the mouth",
"Don't upset the apple-cart",
"Drink like a fish",
"Dropping like flies",
"Drummed out of the army",
"Eat drink and be merry",
"Eaten out of house and home",
"Economical with the truth",
"Elementary my dear Watson",
"Elvis has left the building",
"Et tu, Brute",
"Excuse my French",
"Fly on the wall",
"Foam at the mouth",
"Fool's gold",
"For crying out loud",
"Foregone conculsion",
"For ever and a day",
"For everything there is a season",
"Forbidden fruit",
"Forgive them for they know not what they do",
"Foul play",
"Four by two",
"Four corners of the earth",
"Frailty, thy name is woman",
"Friends, Romans, Countrymen, lend me your ears",
"From sea to shining sea",
"From strength to strength",
"Fruits of your loins",
"Full of piss and vinegar",
"Generation X",
"Genius is one percent inspiration and 99 percent perspiration",
"Get off on the wrong foot",
"Get thee behind me Satan",
"Give up the ghost",
"Go and boil your head",
"Go by the book",
"Go off at half cock",
"Go out on a limb",
"Go to hell in a handbasket",
"Got my mojo working",
"Halcyon days",
"Happy clappy",
"Have a nice day",
"Have an axe to grind",
"He who can, does; he who cannot, teaches",
"His beard was as white as snow",
"History is bunk",
"Hit the ground running",
"Hoist by your own petard",
"A horse, a horse, my kingdom for a horse",
"Hot off the press",
"Houston, we have a problem",
"How are the mighty fallen",
"How now brown cow?",
"Blood toil tears and sweat",
"I spy with my little eye",
"I will wear my heart upon my sleeve",
"If I had my druthers",
"If music be the food of love, play on",
"In like Flynn",
"In the twinkling of an eye",
"Is the Pope Catholic?",
"It came like a bolt from the blue",
"It never rains but it pours",
"It's all grist to the mill",
"It's better to give than to receive",
"It's better to light a candle than curse the darkness",
"Katy bar the door",
"Keep your nose to the grindstone",
"Keep your powder dry",
"Kick the bucket",
"Kit and caboodle",
"Knee jerk reaction",
"Know which way the wind blows",
"Lamb to the slaughter",
"Last but not least",
"Laughing stock",
"Less is more",
"Let not the sun go down on your wrath",
"Let one thousand flowers bloom",
"Let the cat out of the bag",
"Let them eat cake",
"Let there be light",
"Let your hair down",
"Level playing field",
"Life's not all beer and skittles",
"Like a chicken with its head cut off",
"Like a moth to a flame",
"Like the Dickens",
"Live long and prosper",
"Living off the fat of the land",
"Living on borrowed time",
"Lock stock and barrel",
"Loose lips sink ships",
"Love is blind",
"Make a beeline for",
"Make him an offer he can't refuse",
"Make my day",
"Man after my own heart",
"Man does not live by bread alone",
"Man's inhumanity to man",
"Many are called but few are chosen",
"Many happy returns",
"May you live in interesting times",
"Milk of human kindness",
"More in sorrow than in anger",
"Moveable feast",
"Much of a muchness",
"Mumbo jumbo",
"Music has charms to soothe the savage breast",
"My cup runneth over",
"Nasty, brutish and short",
"Neither a borrower nor a lender be",
"Neither here nor there",
"Never give a sucker an even break",
"Never the twain shall meet",
"New kid on the block",
"Nip in the bud",
"No man is an island",
"No manager ever got fired for buying IBM",
"No rest for the wicked",
"No room to swing a cat",
"Not playing with a full deck",
"Not worth a plugged nickel",
"Nothing is certain but death and taxes",
"Now is the winter of our discontent",
"O Romeo, Romeo! wherefore art thou Romeo",
"O ye, of little faith",
"Off with his head",
"Oh, that way madness lies",
"On a wing and a prayer",
"On the shoulders of giants",
"Once in a blue moon",
"One for the road",
"One over the eight",
"Out of sight, out of mind",
"Out of the jaws of death",
"Out of the mouths of babes and sucklings",
"Overpaid, oversexed, and over here",
"Paint the town red",
"Pass the buck",
"Pearls before swine",
"Physician heal thyself",
"Pie in the sky",
"Play by ear",
"Play fast and loose",
"Play it again Sam",
"Play the race card",
"Plug and play",
"Politically correct",
"Point to point",
"Pop goes the weasel",
"Pound of flesh",
"Pour oil on troubled waters",
"Power corrupts; absolute power corrupts absolutely",
"Pull out all the stops",
"Pull yourself up by your bootstraps",
"Push the envelope",
"Put a sock in it",
"Put a spanner in the works",
"Put on the wooden overcoat",
"Put on your thinking cap",
"Put your best foot forward",
"Put your nose out of joint",
"Pyrrhic victory",
"Raining cats and dogs",
"Read the riot act",
"Red letter day",
"Religion is the opiate of the masses",
"Rise and shine",
"Run out of steam",
"Safe pair of hands",
"Saved by the bell",
"Say goodnight Gracie",
"Shot in the arm",
"Shot in the dark",
"Shuffle off this mortal coil",
"Shut your cake hole",
"Smiling like a Cheshire cat",
"Something is rotten in the state of Denmark",
"Something old, something new, something borrowed, something blue",
"Space, the final frontier",
"Spare the rod and spoil the child",
"Speak softly and carry a big stick",
"Straight from the horse's mouth",
"Survival of the fittest",
"Take with a pinch of salt",
"The apple of his eye",
"The Ball is in your court",
"The bee's knees",
"The blind leading the blind",
"The buck stops here",
"The call of the wild",
"The customer is always right",
"The Devil Incarnate",
"The Devil take the hindmost",
"The die has been cast",
"The exception which proves the rule",
"The face that launched a thousand ships",
"The game is afoot",
"The King is dead, long live the King",
"The lady doth protest too much, methinks",
"The love of money is the root of all evil",
"The love that dare not speak its name",
"The moving finger writes",
"The pot calling the kettle black",
"The quality of mercy is not strained",
"The reports of my death are greatly exaggerated",
"The salt of the earth",
"The slings and arrows of outrageous fortune",
"The spirit is willing but the flesh is weak",
"The tail wagging the dog",
"The thin red line",
"The third degree",
"The wages of sin is death",
"The whole nine yards",
"Thereby hangs a tale",
"There's method in my madness",
"There's more than one way to skin a cat",
"There's one born every minute",
"Things that go bump in the night",
"Third time's a charm",
"Thou shalt not kill",
"Three strikes and you are out",
"Till the cows come home",
"To boldly go where no man has gone before",
"To cast the first stone",
"Tomorrow is another day",
"Too much of a good thing",
"Trick or treat",
"Truth will out",
"Vorsprung Durch Technik",
"We are not amused",
"What's up Doc?",
"You are what you eat",
"Your money or your life");

# these are words to *not* create links for.
my %stopwords = (
		 "aboard" => 1,
		 "about" => 1,
		 "above" => 1,
		 "across" => 1,
		 "after" => 1,
		 "against" => 1,
		 "along" => 1,
		 "amid" => 1,
		 "among" => 1,
		 "anti" => 1,
		 "around" => 1,
		 "as" => 1,
		 "at" => 1,
		 "before" => 1,
		 "behind" => 1,
		 "below" => 1,
		 "beneath" => 1,
		 "beside" => 1,
		 "besides" => 1,
		 "between" => 1,
		 "beyond" => 1,
		 "but" => 1,
		 "by" => 1,
		 "concerning" => 1,
		 "considering" => 1,
		 "despite" => 1,
		 "down" => 1,
		 "during" => 1,
		 "except" => 1,
		 "excepting" => 1,
		 "excluding" => 1,
		 "following" => 1,
		 "for" => 1,
		 "from" => 1,
		 "in" => 1,
		 "inside" => 1,
		 "into" => 1,
		 "like" => 1,
		 "minus" => 1,
		 "near" => 1,
		 "of" => 1,
		 "off" => 1,
		 "on" => 1,
		 "onto" => 1,
		 "opposite" => 1,
		 "outside" => 1,
		 "over" => 1,
		 "past" => 1,
		 "per" => 1,
		 "plus" => 1,
		 "regarding" => 1,
		 "round" => 1,
		 "save" => 1,
		 "since" => 1,
		 "than" => 1,
		 "through" => 1,
		 "to" => 1,
		 "toward" => 1,
		 "towards" => 1,
		 "under" => 1,
		 "underneath" => 1,
		 "unlike" => 1,
		 "until" => 1,
		 "up" => 1,
		 "upon" => 1,
		 "versus" => 1,
		 "via" => 1,
		 "with" => 1,
		 "within" => 1,
		 "without" => 1,
		 "I" => 1,
		 "me" => 1,
		 "my" => 1,
		 "mine" => 1,
		 "he" => 1,
		 "him" => 1,
		 "his" => 1,
		 "she" => 1,
		 "her" => 1,
		 "hers" => 1,
		 "it" => 1,
		 "its" => 1,
		 "they" => 1,
		 "them" => 1,
		 "their" => 1,
		 "theirs" => 1,
		 "you" => 1,
		 "your" => 1,
		 "yours" => 1,
		 "we" => 1,
		 "our" => 1,
		 "ours" => 1,
		 "a" => 1,
		 "an" => 1,
		 "the" => 1,
		 "be" => 1,
		 "is" => 1,
		 "are" => 1,
		 "was" => 1,
		 "were" => 1,
		 "has" => 1,
		 "have" => 1,
		 "had" => 1,
		 "or" => 1,
		 "and" => 1
		 );

# return a random phrase ...
sub random_cliche {
    return @cliches[rand scalar(@cliches)];
}

# straight from the Programming Perl book ...
sub downcase {
    my @params = @_;
    for (@params) { tr/A-Z/a-z/ }
    return wantarray ? @params : $params[0];
}

# makes a linked search from one word in a list. Params are index, list of words.
# probably should make 'intitle' an option. Returns string.
sub make_one_wild {
    my $index = shift(@_);
    my @words = @_;
    my $str = '<a href="http://www.google.com/search?q=intitle:%22'; 
    for (my $j = 0; $j < scalar(@words); $j++)
    {
	if (($index==$j)) {$str .= "+*";} else {$str .= "+".$words[$j];}
    }
    $str .= "%22";
    return $str .+ "+-$words[$index]\" target=_new>$words[$index]</a>";
}

# returns a string with all of the possible links.
sub make_cliche_link {
    my $cliche = shift(@_);
    my @words = split(" ",$cliche);
    my $str = "";
    for (my $i = 0; $i < scalar(@words); $i++) {
	my $word = downcase($words[$i]);
	my $is_stop = $stopwords{$word};
	if (!$is_stop) {
	    $str .= make_one_wild($i,@words) . "&nbsp;";
	}
	else {
	    $str .= $words[$i] . "&nbsp;";
	}
    }
    $str .= '(<a href="http://www.google.com/search?q=%22' . $cliche . '%22"'
	. '>all</a>)';
    return $str;
}

# read lines from standard in; each line should contain a phrase.
#while (my $cliche=<STDIN>) 
#{
#    my $foo = make_cliche_link($cliche);
#    print "<li>$foo</li>\n";
#}

my $req_cliche = param("cliche");

## -- OK, the Page actually begins here ...

print 
    header(),
    start_html("MemeWatch (with apologies to Jorn Barger)"),
    h1("MemeWatch"), p('A <a href="http://www.entish.org">Will.Whim</a> toy');

if ($req_cliche) {
    print p("Your watched meme: " . make_cliche_link($req_cliche));
}		 
    print p,
	start_form,
	"Enter a meme to watch: ",textfield('cliche'), 
	submit,
	end_form;


print hr;

print "<UL>";
for (my $i = 0; $i < 5; $i++) 
{
    my $cli = random_cliche();
    print li(make_cliche_link($cli));
}
print "</UL>";
print p("(Press submit to add your meme, or just to get a new list)");
print <<THE_END;
<hr>
<h2>What's going on?</h2>

<P>Jorn Barger's <a href="http://www.robotwisdom.com">Robot Wisdom</a> weblog
has, on occasion, a 'memewatch,' in which he takes some phrase, for example,
"met a man who had no feet," and turns it into a Google Search for the
phrase, for example: "met.+a.man.+who.had.no -feet". Google returns searches
for "man who had no Golden Globe," "no hair," "no pants," "no spats," etc. It's 
fun! Try it!

<p>I decided that this could be somewhat automated, so with the help of
<a href="http://www.perl.org">Perl</a>, a list of some of the sayings from 
<a href="http://phrases.shu.ac.uk/meanings/index.html">The Phrase Finder</a>,
and a list of stop words, I created this page. If you have an especially
good meme to watch, <a href="mailto:will.fitzgerald\@pobox.com">let me know</a>,
and I'll add it to the hall of fame.

<h2>The Hall of Fame</h2>

<p>Here are Jorn Barger's Google MemeWatch searches as of September 2, 2003:

<ul>
<li><a href="http://www.google.com/search?q=fun.%2Bto.say&num=30&hl=en&lr=&ie=UTF-8&safe=off&scoring=d&sa=N&tab=gw">Webpages listing words that are fun-to-say</a>
<li><a href="http://groups.google.com/groups?num=50&hl=en&lr=&ie=ISO-8859-1&safe=off&scoring=d&q=met.%2Ba.man.%2Bwho.had.no+-feet+-socks++-class+-head">met.+a.man.+who.had.no -feet</a>
<li><a href="http://groups.google.com/groups?num=100&safe=off&scoring=d&site=groups&q=like.typing.detected">'[cat]-like typing detected'</a>
<li><a href="http://groups.google.com/groups?q=strong.letter.follows&num=30&hl=en&lr=&ie=UTF-8&sa=N&tab=wg">strong.letter.follows</a>
<li><a href="http://www.google.com/search?num=30&q=and.then.a.miracle.occurs">and.then.a.miracle.occurs</a>
</ul>
<p><a href="http://www.entish.org/">Entish.org</a>
THE_END

print end_html;



