Skip to content

Instantly share code, notes, and snippets.

@jacroe
Last active December 29, 2015 10:19
Show Gist options
  • Save jacroe/7655803 to your computer and use it in GitHub Desktop.
Save jacroe/7655803 to your computer and use it in GitHub Desktop.
A way to receive definitions via terminal. Tested only in Ubuntu
import requests, sys
# ------------------------------------------------------------
APIKEY = "a2a73e7b926c924fad7001ca3111acd55af2ffabf50eb4ae5" # Taken from their live demo
# ------------------------------------------------------------
def main(word, onlyOne=False):
r = requests.get("http://api.wordnik.com/v4/word.json/" + word + "/definitions?limit=200&includeRelated=true&sourceDictionaries=wiktionary&useCanonical=false&includeTags=false&api_key=" + APIKEY)
data = r.json()
if onlyOne is False:
for i in range(0, len(data)):
print str(i+1) + ". [" + pos(data[i]['partOfSpeech']) + "] " + data[i]['text']
else:
num = onlyOne - 1
if len(data) < num:
num = 0
print word + ". " + data[num]['partOfSpeech'] +". " + data[num]['text'].encode('ascii','ignore')
def pos(posType):
types = dict(noun="n", interjection="int", adjective="adj", verb="v")
if posType in types:
return types[posType]
else:
return posType
if len(sys.argv) < 2:
print "Usage: dictionary.py word [returnOne]"
print
print "returnOne: If set, it will return only one definition. This is useful for piping and TTS."
print "Example: 'dictionary.py phone 2' returns the second definition of 'phone'"
quit(1)
elif len(sys.argv) is 2:
main(sys.argv[1], False)
else:
main(sys.argv[1], int(sys.argv[2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment