Monday, July 30, 2007

Connection - A Google Suggest Game

A few days ago I was searching for a string method in Google. As soon as I had typed in "string", Google Suggest popped in with the following suggestions:

string cheese
string theory
string bikini

I was amused. Then I thought, what if there were a game where you had to go from cheese, theory, bikini back to "string"? After finding Google's convinient Suggest API, I wrote it in about 25 minutes. The game picks a common word and displays the suggest results - you have to find the connection between them. It's pretty fun. Here are some examples (find the missing word).

***back machine
***ne state university
*** back into love lyrics
*** back into love
***ne rooney
*** back machine
***ne gretzky
***nes world

***s inn
***s of our lives
***s of our lives spoilers
***ton daily news
***tona beach
*** of the dead
***light savings time
***s inn hotels

***** map
***** of warcraft
***** clock
***** time
***** war 2
***** weather
***** market
***** series of poker
***** bank

**** zones
**** warner cable
**** warner
**** zone
**** magazine
**** out

Python Code:
I have some ideas for improvements, but this is the half-hour version.
You'll need a word list. The nouns in worked well.
Enter q to quit, or n for the next word if you give up.
# Works in Python 2.5, needs internet connection
import urllib
import re
import random

g_restr = re.compile('<suggestion data="([^"]+)"/>')
astrWords = 'fact,child,problem,part,hand,case'.split(',')
# Put another word list, separated by commas,into the above string.

def main():
    i =0
    while True:
        res = nextword(astrWords[i])
        if res == False: return
def nextword(strWord):
    strUrl = '' + strWord
    f = urllib.urlopen(strUrl)
    strXml =
    strBlank = '*' * len(strWord)
    for match in g_restr.findall(strXml):
        print match.replace(strWord, strBlank)
    while True:
        strGuess = raw_input(':')
        if strGuess.lower() == strWord.lower():
            print 'Win!'
            return True
        if strGuess == 'q':
            print 'Exiting...'
            return False
        elif strGuess == 'n':
            print 'You gave up.'
            return True
Answers to the examples are "way", "day", "world", and "time." Have fun!

1 comment:

ErroneousFunk said...

It's difficult until you get to one like " gretzky" or "... or warcraft"
If only there were a way to filter the dead givaways out (which there really isn't :p )or, perhaps, use fewer clues. Are there any other search engines with suggestions?