Last active
December 29, 2015 10:19
-
-
Save jacroe/7655803 to your computer and use it in GitHub Desktop.
A way to receive definitions via terminal. Tested only in Ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "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