Google dictionary API

Long time back Google has Dictionary service site called dictionary.google.com . I found that very useful. It lets us learn new words, get the synonym, usages in sentence etc. We can have our own collection of words in our personal favorite lists by adding a star while seeing. I don’t know how many of you have used it. But now Google doesn’t have this dictionary.google.com separately. I think they might have integrated it in to theirs default Google search itself. We can use it by typing “define” along with our keyword in the Google search.

A week before while surfing around I’ve found a post about Google’s dictionary API. It returns the result in the form of JSON format. The API that I’ve found is like this

http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=keyword&sl=en&tl=en&restrict=pr%2Cde&client=te

You can try this out in your browser itself, replace the keyword with your search word. You will get the result in a big paragraph like stuff, it’s the JSON format result.

Just for time-pass I wrote a python script. Even though it is a command-line application I found it interesting when prettily printing the search result in a user understandable format. My python code is below, have a try friends. I really welcome your valuable comments.

My_Google_Dictionary_API.py

from urllib2 import urlopen
keyword = raw_input("Enter the keyword for search: ")

null = None
data = urlopen("http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q="+keyword+"&sl=en&tl=en&restrict=pr%2Cde&client=te").read()[25:-1]

d = eval('('+data+')')
if d[1] == 200:
    result = d[0]
    print '> Query:',result.get('query')

    if 'primaries' in result:
        print '> Primaries'
        primaries = result.get('primaries')

        for primary in primaries:
            print '-\tTerms'
            for term in primary.get('terms'):
                if 'labels' in term:
                    for label in term.get('labels'):
                        print '\t',label['title'],' '*6,':',label['text']
                print '\t',term.get('type'),' '*(20 - len(term.get('type'))),':',term.get('text')
            print '-\tEntries'
            for entry in primary.get('entries'):
                for term in entry.get('terms'):
                    print '\t',term.get('text'),
                    if 'labels' in term:
                        for label in term.get('labels'):
                            print '-',label['text']
                    else: print
            print '*'*40

    if 'webDefinitions' in result:
        webd = result.get('webDefinitions')[0]
        print '> webDefinitions'
        entries = webd.get('entries')
        for entry in entries:
            for term in entry.get('terms'):
                if term.get('type') == 'text':
                    print '*\t',term.get('text')

If you run this python code you will probably get the result like this ↓

>E:\Python27\pythonw -u "g_search.py"
Enter the keyword for search: wallflower
> Query: wallflower
> Primaries
-	Terms
	Part-of-speech        : Noun
	text                  : wall·flow·er
	phonetic              : /ˈwôlˌflouər/
	sound                 : http://www.gstatic.com/dictionary/static/sounds/de/0/wallflower.mp3
-	Entries
	wallflowers - plural
	A southern European plant of the cabbage family, with fragrant yellow, orange-red, dark red, or brown flowers, cultivated for its early spring blooming
	A person who has no one to dance with or who feels shy, awkward, or excluded at a party
****************************************
> webDefinitions
*	any of numerous plants of the genus Erysimum having fragrant yellow or orange or brownish flowers
*	perennial of southern Europe having clusters of fragrant flowers of all colors especially yellow and orange; often naturalized on old walls or cliffs; sometimes placed in genus Erysimum
*	remains on sidelines at social event
*	Erysimum (wallflowers) is a genus that includes about 180 species, both popular garden plants and many wild forms. The genus Cheiranthus is sometimes included herein whole or in part. Erysimum has recently adscribed to a monogeneric cruciferous tribe, Erysimeae. ...
*	Wallflower is the second and final album by hard rock band My Sister's Machine. The song "Enemy" was included on the compilation album Hard Music, Volume 1 in 1994. A music video for the song was also made .
*	Wallflower were an alternative rock band from London, UK. They were active between 1994-1998, and released two EPs and a 10" single.
*	"Wallflower" is a song written and recorded in 1971 by Bob Dylan. Dylan's own recording, however, was not released until twenty years later as part of The Bootleg Series Volumes 1–3 (Rare & Unreleased) 1961–1991. ...
*	Wallflower (Laurie Collins) is a fictional character, a mutant in the Marvel Universe, one of the student body in the Xavier Institute, and a member of the former New Mutants squad therein. After the events of M-day transpired, she was one of a handful of mutants to keep her powers. ...
*	In social situations, a wallflower is a slang term used to describe shy or unpopular individuals who do not socialize or participate in activities at social events. It is most often used to describe someone who stays close to a wall and out of the main area of social activity. ...
*	Peter Gabriel is the fourth album released by the British progressive rock musician Peter Gabriel. This album was originally released as Gabriel’s fourth eponymous album, but was released in the United States as Security, Gabriel's first album with Geffen Records in the US and Canada.
*	"The Wallflower" (also known as "Roll with Me Henry" and "Dance with Me Henry") is a popular song. It was one of several answer songs to "Work With Me Annie" and has the same 12-bar blues melody.
*	The Wallflowers is a rock band from Los Angeles, California, fronted by Jakob Dylan. Formed in 1989 and originally known as The Apples, the ensemble has gone through numerous personnel changes with Dylan the only constant.
*	(The Wallflowers (album)) The Wallflowers is The Wallflowers' self-titled debut album.
*	Several short-lived herbs or shrubs of the Erysimum genus with bright yellow to red flowers;  A person who is socially awkward, especially one who does not dance at a party due to shyness
*	Stock that has fallen out of favor with investors; stock that tends to have a low P/E (price-to-earnings ratio).
*	A plant of the genus Cheiranthus (N.O. Cruciferae), esp. C. Cheiri, growing wild on old walls, on rocks, in quarries, etc., and cultivated in gardens for its fragrant flowers (normally yellow or orange, though other colours are produced by cultivation). Also called gilliflower. See notes on herbs.
*	(Cheiranthus).These favourite hardy perennials prefer a rich, light, sandy soil, and a dry situation. The seed may be sown where it is intended for them to bloom either in autumn or spring. Thin out to 2 ft. apart. They may also be increased by shoots torn from the stems of old plants. ...
*	A security that is no longer preferred by investors. Wallflower securities are apt to have low price/earnings ratios.
>Exit code: 0

This is almost same as the Original Google’s result that you can find here –> https://www.google.co.in/search?q=define+wallflower&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a#hl=en&client=firefox-a&hs=gDq&rls=org.mozilla:en-US:official&q=wallflower&tbs=dfn:1&tbo=u&sa=X&ei=7wBeUNHCKsm3rAeLxoHwCg&ved=0CCAQkQ4&fp=1&biw=1366&bih=648&bav=on.2,or.r_gc.r_pw.r_qf.&cad=b&sei=mgReUMzeKMPTrQen3oHIBA

Cheers!!, Hope you have found something interesting…
Rajiv M
(also known as Python-Rajiv among friends haha!!)..
Hey!! don’t forget to leave your comment my friend ;-)

Advertisements

5 thoughts on “Google dictionary API

  1. Thanks a lot, this is perfect for me. I’am learning English, and want to have a phrasebook, where I can just click on the words and see how to pronounce them and the meaning. With this API I just need to build a friendly GUI for myself. Thanks again!

  2. I have spent the last several minutes (bordering on an hour and a half) trying to figure out how to elegantly parse the json that the google dictionary api returns. Thank you for posting your solution, I intend to heavily borrow from your code to complete the project that I am currently working on.

Share your blooming thoughts here . . .

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s