-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoxfordapi.py
170 lines (135 loc) · 4.31 KB
/
oxfordapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
"""
Created on Saturday, March 31st 2018
@author: Sagar Kishore
API wrapper that fetches word definitions, example sentences, synonyms,
antonyms, audio file link for the given word. Handles exceptions as well
for specific cases.
"""
import requests
from requests.exceptions import HTTPError
from word import Word
from pprint import PrettyPrinter
def getdefinitions(word_object, word_id):
"""
Adds definitions to the word_object. Returns an error status indicating
if the GET failed.
"""
# Maybe encrypt these information?
APP_ID = '5c0f7093'
APP_KEY = 'd10576f4a34a64e8236472e947af906f'
LANGUAGE = 'en'
url_head = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/'
url = url_head + LANGUAGE + '/' + word_id.lower()
r = requests.get(url, headers={'app_id': APP_ID, 'app_key': APP_KEY})
try:
r.raise_for_status()
status = True, r.status_code
except HTTPError:
status = False, r.status_code
return status
response_dict = r.json()
header = response_dict['results'][0]
name = header['word']
lex_entries = {}
AudioLink = ''
lex_list = header['lexicalEntries']
for lex in lex_list:
lexentry = {}
category = lex['lexicalCategory']
try:
AudioLink = lex['pronunciations'][0]['audioFile']
except KeyError:
pass
try:
sense = lex['entries'][0]['senses'][0]
try:
lexentry['definition'] = sense['definitions']
except KeyError:
continue
except KeyError:
derivativeOf = lex['derivativeOf'][0]['text']
status = False, r.status_code, derivativeOf
return status
lexentry['examples'] = []
try:
for examples in sense['examples']:
lexentry['examples'].append(examples['text'])
except KeyError:
pass
lex_entries[category] = lexentry
word_object.name = name
word_object.definitions = lex_entries
word_object.audio = AudioLink
return status
def getsynant(word_object, word_id):
"""
Adds Synonyms and Antonyms to the word_object. Returns an error status
indicating if the GET failed.
"""
app_id = '5c0f7093'
app_key = 'd10576f4a34a64e8236472e947af906f'
language = 'en'
url_head = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/'
url = url_head + language + '/' + word_id.lower() + '/synonyms;antonyms'
r = requests.get(url, headers={'app_id': app_id, 'app_key': app_key})
try:
r.raise_for_status()
status = True, r.status_code
except HTTPError:
status = False, r.status_code
return status
response_dict = r.json()
header = response_dict['results'][0]
lex_entries = {}
lex_list = header['lexicalEntries']
for lex in lex_list:
lexentry = {}
category = lex['lexicalCategory']
sense = lex['entries'][0]['senses'][0]
try:
lex_ant_list = sense['antonyms']
except KeyError:
lex_ant_list = []
try:
lex_syn_list = sense['synonyms']
except KeyError:
lex_syn_list = []
antonyms_list = []
for antonyms in lex_ant_list:
antonyms_list.append(antonyms['text'])
synonyms_list = []
for synonyms in lex_syn_list:
synonyms_list.append(synonyms['text'])
lexentry['synonyms'] = synonyms_list
lexentry['antonyms'] = antonyms_list
lex_entries[category] = lexentry
word_object.synant = lex_entries
return status
def addword(word_object):
word_id = word_object.name
status1 = getdefinitions(word_object, word_id)
status2 = getsynant(word_object, word_id)
return (word_object, status1, status2)
# DEBUGGING
def main(name=None):
pp = PrettyPrinter(indent=4)
if name is None:
name = 'hypochondriac'
a = Word(name)
a, status1, status2 = addword(a)
pp.pprint(a)
pp.pprint(status1)
pp.pprint(status2)
if __name__ == '__main__':
main('venality')
# -i debugging
# name = 'precipitate'
# pp = PrettyPrinter(indent=4)
# a = Word(name)
# a, status1, status2 = addword(a)
# repr(a)
# pp.pprint(a)
# if(status1[2]):
# a = Word(status1[2])
# a, s1, s2 = addword(a)