Skip to content

Instantly share code, notes, and snippets.

@fayesafe
Created July 15, 2016 11:07
Show Gist options
  • Save fayesafe/21c44bbda3b148fbf4f8b3ba334f572c to your computer and use it in GitHub Desktop.
Save fayesafe/21c44bbda3b148fbf4f8b3ba334f572c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
import json
import requests
import sys
BASE_URL = 'http://thesaurus.altervista.org/thesaurus/v1'
KEY = ''
def main(args):
if (args.language == 'en'):
lang = 'en_US'
elif (args.language == 'de'):
lang = 'de_DE'
else:
print('No valid language provided!')
sys.exit(1)
params = {'key': KEY, 'language': lang, 'word': args.word,
'output': 'json'}
syn_req = requests.get(BASE_URL, params=params)
if (syn_req.status_code == 200):
syns = json.loads(syn_req.text)['response']
for entry in syns:
syn = entry['list']
print('* Category: {}'.format(syn['category'].replace('(', '')
.replace(')', '')
.title()))
print('* Synonyms: ')
print('\t==> {}\n'.format(syn['synonyms']
.replace('|', '\n\t==> ')))
else:
print('No Results found')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-l', '--language', nargs='?', const='en',
default='en', type=str, help="Language of word")
parser.add_argument('word', help='Word to search synonyms for')
args = parser.parse_args()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment