-
Notifications
You must be signed in to change notification settings - Fork 0
/
word.py
78 lines (55 loc) · 2.44 KB
/
word.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
class Word:
# class to hold word returned from the Oxford API
def __init__(self, raw_text):
# init parameters of entry -- needs only a raw JSON object
self.raw_text = raw_text
self.entries = []
try:
self._parse()
except KeyError:
print('Unable to parse word. Please debug.')
def _parse(self):
# iterate over results
for result in self.raw_text['results']:
# iterate over entry
for e in result['lexicalEntries']:
# create and entry
entry = Entry(str(e['lexicalCategory']['id']))
# # iterate over sense in entry
for s in e['entries'][0]['senses']:
# create sense object with supplementary information
sense = Sense(str(s['definitions'][0]), s)
entry.senses.append(sense)
self.entries.append(entry)
class Entry:
# class to hold a sense for a given word organised under grammatical category
def __init__(self, grammatical_category):
# init parameters of entry -- needs only a grammatical_category
self.grammatical_category = grammatical_category
self.senses = []
class Sense:
# class to hold a given sense in an entry
def __init__(self, definition, raw_information):
# init parameters of sense -- needs only a defintion and raw JSON object
self.definition = definition
self.examples = []
self.domains = []
self.registers = []
self.subsenses = []
self._add_supplementary_information(raw_information)
def _add_supplementary_information(self, raw_information):
# adds information supplementary to a given sense --
# may be extended to any of the various supplementary information provided by the Oxford API
# domains
if 'domains' in raw_information:
self.domains = [i['text'] for i in raw_information['domains']]
# examples
if 'examples' in raw_information:
self.examples = [i['text'] for i in raw_information['examples']]
# registers
if 'registers' in raw_information:
self.registers = [i['text'] for i in raw_information['registers']]
# subsenses
if 'subsenses' in raw_information:
self.subsenses = [Sense(i['definitions'][0], i)
for i in raw_information['subsenses']]