-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonify.py
76 lines (59 loc) · 1.9 KB
/
jsonify.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
from graph_tool.all import *
g = load_graph('wnen30_core_noun_tree_longest_w_senses.graphml.xml.gz')
graph = {
'nodes': [],
'links': []
}
is_synset = g.vertex_properties['is_synset']
lemma = g.vertex_properties['lemma']
sensenum = g.vertex_properties['sensenum']
pos = g.vertex_properties['pos']
senseid = g.vertex_properties['senseid']
sensekey = g.vertex_properties['sensekey']
synsetid = g.vertex_properties['synsetid']
definition = g.vertex_properties['definition']
is_core_sense = g.vertex_properties['is_core_sense']
tree_link = g.edge_properties['tree_link']
for v in g.vertices():
if not is_synset[v]:
o = {
'type': 'sense',
'lemma': lemma[v],
'sensenum': sensenum[v],
'pos': pos[v],
'id': senseid[v], # sense and synset id does not collide in wnen30
'sensekey': sensekey[v]
}
if is_core_sense[v]:
o['is_core'] = True
else:
assert is_synset[v]
o = {
'type': 'synset',
'id': synsetid[v], # sense and synset id does not collide in wnen30
'defintion': definition[v],
'pos': pos[v]
}
graph['nodes'].append(o)
for e in g.edges():
source = e.source()
target = e.target()
if not is_synset[source]:
# source is a leaf (a sense)
source_id = senseid[source]
else:
assert is_synset[source]
source_id = synsetid[source]
if not is_synset[target]:
# target is a leaf (a sense)
target_id = senseid[target]
else:
assert is_synset[target]
target_id = synsetid[target]
o = {'source': source_id, 'target': target_id}
if tree_link[e]:
o['is_tree_link'] = True
graph['links'].append(o)
import json
with open('wnen30_core_n_longest.json','wb') as f:
f.write(json.dumps(graph))