-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_triples.py
76 lines (52 loc) · 1.97 KB
/
convert_to_triples.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
'''
Convert WordNet to RDF triples in the form (subject, predicate, object)
subject & object - entities
predicate - relation between entities
Remove near-duplicate and inverse relations
'''
import json
import csv
from sklearn.model_selection import train_test_split
wordnet = json.load(open("wordnet.json","r"))
word2idx = json.load(open("indices/word2idx.json","r"))
relation_map = relation_map = json.load(open("indices/relations.json","r"))
# Inverse relations
relations_to_remove = [
'_holonym','_hyponym'
]
def relation_mapper(id):
relation = relation_map[id[-2:]].lower()
if relation.endswith("verb"):
return "verb"
elif relation.startswith("anto"):
return "antonym"
elif relation.startswith("grad"):
return "gradation"
elif relation.startswith("mero"):
return "meronym"
elif relation.startswith("holo"):
return "holonym"
else:
return relation
def convert_to_triples(wordnet):
triples = []
for synset_id, synset in wordnet.items():
for other_synset_id, relation_id in synset['relation_with'].items():
try:
x = wordnet[other_synset_id]
triples.append((synset_id, '_' + relation_mapper(relation_id), other_synset_id))
except KeyError:
print("Synset " + other_synset_id + " not found. Skipping...")
return triples
def write_to_file(data, filename):
path = "data/openke/"
with open(path+filename,"w+", encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerows(data)
triples = convert_to_triples(wordnet)
_triples = [(e1, r, e2) for (e1, r, e2) in triples if r not in relations_to_remove]
train, valid = train_test_split(_triples, test_size=0.1, random_state=22, shuffle=True)
valid, test = train_test_split(valid, test_size=0.5, random_state=22, shuffle=True)
write_to_file(train, "train.csv")
write_to_file(valid, "valid.csv")
write_to_file(test, "test.csv")