Skip to content

Instantly share code, notes, and snippets.

@xavieryao
Last active December 27, 2016 11:51
Show Gist options
  • Save xavieryao/6f01b8bbb76cd761fd808470570e6bfc to your computer and use it in GitHub Desktop.
Save xavieryao/6f01b8bbb76cd761fd808470570e6bfc to your computer and use it in GitHub Desktop.
word.py
#!/usr/bin/env python3
import csv
import re
import requests
import argparse
def main(args):
with open(args.input, 'r') as in_list, open(args.output, 'w') as out:
writer = csv.writer(out)
words_lines = [x.strip() for x in in_list.readlines()]
words = []
for line in words_lines:
for w in line.split(' '):
words.append(w.strip())
for w in words:
request_data = {
'Action': 'search',
'Word': w,
'Format': 'jsonwv'
}
try:
r = requests.post('http://xtk.azurewebsites.net/BingService.aspx', data=request_data).json()
meaning = []
for k in r.keys():
if k.startswith('mn'):
idx = re.match('mn(\d+)', k).group(1)
meaning.append("{} {}".format(r['pos'+idx], r[k]))
if len(meaning) > 0:
amep = r.get('amep', '').replace("æ", "æ")
brep = r.get('brep', '').replace("æ", "æ")
writer.writerow([w, amep, brep, ' '.join(meaning)])
except:
pass
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='take word list, output dictionary')
parser.add_argument('--input')
parser.add_argument('--output')
args = parser.parse_args()
if args.input is None or args.output is None:
print('You must provide input and output.')
exit()
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment