-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoxdict.py
84 lines (74 loc) · 2.39 KB
/
oxdict.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
import requests
#new username is brads
#password is fowl with special char at end
app_id = '7b7ce7fa' #alt: 'e2682864'
app_key = 'e30c7d55f087909349ccc994cb40e6f9' #alt: 'e71a2e787482944c1ab230671331836b'
url = 'https://od-api.oxforddictionaries.com:443/api/v1/entries/en/'
def lookup(word):
r = requests.get(url + word.lower(), headers = {'app_id': app_id, 'app_key': app_key})
if r.status_code != 200:
if r.status_code != 404:
print "OXDICT returned code", r.status_code
return None
return r.json()['results'][0]['lexicalEntries']
#r is oxdict.lookup response object
#pos is like "Verb" or "Noun"
#returns object with the following keys: ['pronunciations', 'text', 'lexicalCategory', 'language', 'entries'], entries is most useful
def getpos(r,pos):
for e in r:
if e['lexicalCategory'] == pos:
return e
return None
#takes a word and a POS ("Verb","Noun") and returns True if OxDict says that word is that POS
def ispos(word,pos):
r = lookup(word)
if r is None:
return False
return getpos(r,pos) is not None
gf = 'grammaticalFeatures'
verbdict = {}
#returns True if Ox Dict thinks the word is a verb and it is intransitive and its not dated or archaic, False otherwise
def checkVerb(word):
if word in verbdict:
return verbdict[word]
else:
r = lookup(word)
if r is None:
return True #accept all if oxdict goes down (and don't cache)
v = getpos(r,'Verb')
if v is None:
verbdict[word] = False
return False #not a verb
for i in [x[gf] for x in v['entries'] if gf in x]:
for e in i:
if 'text' in e and (e['text'] == "Transitive" or 'with object' in e['text']):
verbdict[word] = False
return False #transitive
for i in v['entries']:
if 'senses' in i:
for s in i['senses']:
if 'registers' in s:
for rg in s['registers']:
if rg in ['archaic','dated']:
verbdict[word] = False
return False
verbdict[word] = True
return True
mpdict = {}
#this is important for "punchies", but I think it's not perfect (example: 'time')
def isMassOrProper(word):
if word in mpdict:
return mpdict[word]
else:
r = lookup(word)
if r is None:
return False #don't cache in case it's unavailable
v = getpos(r,'Noun')
if v is not None:
for i in [x[gf] for x in v['entries'] if gf in x]:
for e in i:
if 'text' in e and e['text'] in ["Mass", "Proper"]:
mpdict[word] = True
return True
mpdict[word] = False
return False