-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_lookup.py
155 lines (140 loc) · 4.33 KB
/
generate_lookup.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import rdflib
import os
import string
import util
from collections import Counter
formats = ['xml', 'ttl']
def generate_lookup(f_dir, dataset_name, data_dir):
"""
Extract classes and properties from a given ontology
:param f_dir:
:param dataset_name:
:return:
"""
dataset_dir = os.path.join(data_dir, dataset_name)
if not os.path.exists(dataset_dir):
print("dataset_dir: "+dataset_dir)
os.makedirs(dataset_dir)
g = rdflib.Graph()
found = False
for format in formats:
try:
g.parse(f_dir, format=format)
found = True
break
except:
pass
if found:
classes = get_all_classes(g)
classes_f_name = os.path.join(dataset_dir, 'classes.txt')
f = open(classes_f_name, 'w', encoding='utf-8')
for c in classes:
f.write(c+"\n")
f.close()
properties = get_all_properties(g)
properties_f_dir = os.path.join(dataset_dir, 'properties.txt')
f = open(properties_f_dir, 'w', encoding='utf-8')
for p in properties:
f.write(p+"\n")
f.close()
build_property_lookup(dataset_name,properties_f_dir, data_dir)
else:
print("Unable to parse: "+f_dir)
def get_all_properties(g):
"""
:param g:
:return:
"""
q = """
PREFIX owl: <http://www.w3.org/2002/07/owl#>
select ?a
where{
{?a ?b owl:ObjectProperty} UNION
{?a a rdf:Property } UNION
{?a ?b owl:DatatypeProperty}
}
"""
result = g.query(q)
properties = [str(row[0]).strip() for row in result]
for r in properties:
print(r)
return properties
def get_all_classes(g):
"""
:param g:
:return:
"""
q = """
PREFIX owl: <http://www.w3.org/2002/07/owl#>
select distinct ?class
where{
{?class rdf:type rdfs:Class} UNION {?class rdf:type owl:Class} .
}
"""
result = g.query(q)
classes = [str(row[0]).strip() for row in result]
for r in classes:
print(r)
return classes
def build_property_lookup(dataset_name,properties_fdir, data_dir):
"""
Build a property lookup
:param properties_fdir:
:return:
"""
print("build_property_lookup> dataset_name: "+dataset_name)
properties = util.get_properties_as_list([dataset_name], data_dir=data_dir)
# start_idx = predict_base_URL(properties)
lookup_name = 'lookup'
lookup_folder_dir = os.path.join(data_dir,dataset_name,lookup_name)
if not os.path.exists(lookup_folder_dir):
os.makedirs(lookup_folder_dir)
# for start in string.ascii_lowercase:
# start_dir = os.path.join(lookup_folder_dir, start+".txt")
for p in properties:
# p_name = p[start_idx:]
p_name = p.split('/')[-1].split('#')[-1]
if len(p_name) == 0:
continue
# print("pname: <"+p_name+">")
write_lookup_for(lookup_folder_dir, p_name.lower()[0], p)
def write_lookup_for(base_dir, letter, property_uri):
fdir = os.path.join(base_dir, letter+".txt")
f = open(fdir, 'a+', encoding='utf-8')
f.write(property_uri+"\n")
f.close()
# def predict_base_URL(uris, checks=20, restrictive=True):
# """
# :param uris: a list of strings, each string is a URI
# :param checks: the number of checks to perform
# :param restrictive: if true, it will take the highest stop_idx instead of the most common
# :return:
# """
# upper = uris[:len(uris)/2]
# lower = uris[len(uris)/2:]
# stop_idx = []
# for u in upper[:checks]:
# for l in lower[:checks]:
# if u == l:
# continue
# for i in range(min(len(u),len(l))):
# if u[i] != l[i]:
# # print("-------")
# # print("u: "+u)
# # print("l: "+l)
# # print(i)
# stop_idx.append(i)
# break
# if restrictive:
# max_stop_idx = min(stop_idx)
# print("predicted_base: "+upper[0][:max_stop_idx])
# return max_stop_idx
# else:
# c = Counter(stop_idx)
# max_counts = 0
# idx_of_max = 0
# for k in c:
# if c[k] > max_counts:
# idx_of_max = k
# max_counts = c[k]
# return idx_of_max