Next post: Big Recursion

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 convenient 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).

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

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

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

4)
**** 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 Most_common_words_in_English 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():
    random.shuffle(astrWords)
    i =0
    while True:
        res = nextword(astrWords[i])
        if res == False: return
        i+=1
    
def nextword(strWord):
    strUrl = 'http://google.com/complete/search?output=toolbar&q=' + strWord
    f = urllib.urlopen(strUrl)
    strXml = f.read()
    f.close()
    
    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
main()
Answers to the examples are "way", "day", "world", and "time." Have fun!