-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocd-mkgdex
executable file
·54 lines (40 loc) · 1.32 KB
/
ocd-mkgdex
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
#!/usr/bin/python
import urllib, urllib2, json, sys
def kwic2str(kwic):
out = ""
for s in kwic:
if "coll" in s["class"]:
out += "<h>%s</h>" % s["str"]
elif not s["class"]:
out += s["str"]
return out.encode("utf-8")
base_url = 'https://api.sketchengine.co.uk/corpus/'
method = 'view'
corp = sys.argv[1]
usr = sys.argv[2]
apikey = sys.argv[3]
attrs = dict(corpname=corp, format='json', api_key=apikey,
change_gdex=False, viewmode="sen")
def get_examples (cql):
attrs['q'] = ["q" + cql, "e200"]
encoded_attrs = urllib.quote(json.dumps(attrs))
url = base_url + method + '?username=%s;json=%s' % (usr, encoded_attrs)
request = urllib2.Request(url)
file = urllib2.urlopen(request)
data = file.read()
file.close()
data = json.loads(data)
examples = []
if 'Lines' in data:
for l in data["Lines"]:
examples.append(kwic2str (l["Left"]) + kwic2str (l["Kwic"]) + kwic2str (l["Right"]))
return examples
for h in sys.stdin:
entry = json.loads(h)
if "senses" in entry:
for s in entry["senses"]:
s["examples"] = get_examples(s["conclink"])
else:
conclink = entry.get('conclink', '')
entry["examples"] = conclink and get_examples(conclink) or []
print json.dumps(entry)