-
Notifications
You must be signed in to change notification settings - Fork 3
/
Yago_subclass_extractor.py
40 lines (36 loc) · 1.4 KB
/
Yago_subclass_extractor.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
"""
This Python file is used to extract the subclass relationships between Yago types.
The input is yago-wd-class.nt and yago-wd-schema.nt.
"""
classes = open("../yago/yago_original/yago-wd-class.nt", "r", encoding='utf-8')
schema = open("../yago/yago_original/yago-wd-schema.nt", "r", encoding='utf-8')
dict = {}
for line in classes.readlines():
triple = line.split()
predicate = triple[1]
if predicate.find("subClassOf") != -1:
subject = triple[0][1:-1].split('/')[-1]
object = triple[2][1:-1].split('/')[-1]
if subject != object:
if dict.get(object):
if subject not in dict.get(object):
list = dict.get(object).append(subject)
else:
list = [subject,]
dict[object] = list
for line in schema.readlines():
triple = line.split()
predicate = triple[1]
if predicate.find("subClassOf") != -1:
subject = triple[0][1:-1].split('/')[-1]
object = triple[2][1:-1].split('/')[-1]
if subject != object:
if dict.get(object):
if subject not in dict.get(object):
list = dict.get(object).append(subject)
else:
list = [subject,]
dict[object] = list
relationship = open("yago-subclass", 'w', encoding='utf-8')
for i in dict.items():
relationship.write(i[0] + ','+ str(i[1]) + '\n')