-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
253 lines (225 loc) · 7.81 KB
/
util.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import string
import random
import os
import chardet
def get_random_string(length=4):
return ''.join(random.choice(string.ascii_lowercase) for i in range(length))
def get_headers(file_dir, file_type):
"""
:param file_dir: abs dir of the input file that is already downloaded
:param file_type: ext
:return:
"""
ext = file_dir.strip().split('.')[-1].lower().strip()
if ext == 'csv' or file_type == "csv":
return get_headers_csv(file_dir)
elif ext == 'json' or file_type == "json":
print("json is not supported")
return []
else:
return []
def get_headers_csv(file_dir):
f = open(file_dir, encoding='utf-8')
header_str = ""
for line in f.readlines():
header_str = line
break
header_str = header_str.replace('\n', '')
header = []
start_q = False
start_idx = 0
# detected_encoding = chardet.detect(s)['encoding']
# logger.debug("detected encoding %s for %s" % (detected_encoding, fname))
# decoded_s = s.decode(detected_encoding)
# print("to be detected: ")
# print(header_str)
# detected_encoding = chardet.detect(header_str)['encoding']
# print("detected encoding %s " % (detected_encoding))
# decoded_s = header_str.decode(detected_encoding)
# header_str = decoded_s.encode('utf-8')
print("header_string: %s " % header_str)
for idx, ch in enumerate(header_str):
if ch == '"' and start_q == True:
start_q = False
elif ch=='"':
start_q = True
elif ch=="," and start_q == False:
curr = header_str[start_idx:idx]
header.append(curr)
start_idx = idx+1
header.append(header_str[start_idx:])
return header
def generate_r2rml_mappings(mapping_file_dir, file_name, entity_class, entity_column, mappings):
mapping_id = get_random_string(10)
single_property_mapping = u"""
rr:predicateObjectMap [
rr:predicateMap [ rr:constant <%s> ];
rr:objectMap [ rr:termType rr:Literal; rr:column "\\"%s\\""; ];
];
"""
proper_mappings_list = [single_property_mapping % (m["val"], m["key"].upper()) for m in mappings]
property_column_mapping = "\n".join(proper_mappings_list)
print("predicate object mappings: ")
print(property_column_mapping)
table_name = file_name.upper()
if table_name[-4:] == ".CSV":
table_name = table_name[:-4]
else:
print("Note that the filename is not terminated with .CSV")
mapping_content = u"""
@prefix rr: <http://www.w3.org/ns/r2rml#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix mpv: <http://mappingpedia.linkeddata.es/vocab/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix schema: <http://schema.org/> .
@base <http://mappingpedia.linkeddata.es/resource/> .
<%s>
rr:logicalTable [
rr:tableName "\\"%s\\""
];
rr:subjectMap [
a rr:Subject; rr:termType rr:IRI; rr:class <%s>;
rr:column "\\"%s\\"";
];
%s
.
""" % (mapping_id, table_name, entity_class, entity_column.upper(), property_column_mapping)
print(mapping_content)
f = open(mapping_file_dir, 'w', encoding='utf-8')
f.write(mapping_content)
# f.write(mapping_content.encode('utf8'))
f.close()
def generate_rml_mappings_csv(mapping_file_dir, file_name, entity_class, entity_column, mappings):
mapping_id = get_random_string(10)
single_property_mapping = u"""
rr:predicateObjectMap [
rr:predicate <%s>;
rr:objectMap [ rml:reference "%s" ]
];
"""
proper_mappings_list = [single_property_mapping % (m["val"], m["key"]) for m in mappings]
property_column_mapping = "\n".join(proper_mappings_list)
mapping_file = u"""
@prefix rr: <http://www.w3.org/ns/r2rml#>.
@prefix rml: <http://semweb.mmlab.be/ns/rml#> .
@prefix ql: <http://semweb.mmlab.be/ns/ql#> .
@prefix mail: <http://example.com/mail#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix ex: <http://www.example.com/> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>.
@prefix transit: <http://vocab.org/transit/terms/> .
@prefix wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>.
@prefix schema: <http://schema.org/>.
@prefix gn: <http://www.geonames.org/ontology#>.
@prefix geosp: <http://www.telegraphis.net/ontology/geography/geography#> .
@base <http://morph.linkeddata.es/resource/> .
<%s> a rr:TriplesMap;
rml:logicalSource [
rml:source "%s";
rml:referenceFormulation ql:CSV
];
rr:subjectMap [
rr:template "http://morph.linkeddata.es/resource/{%s}";
rr:class <%s>
];
%s
.
""" % (mapping_id, file_name, entity_column, entity_class, property_column_mapping)
print(mapping_file)
mapping_file_path = mapping_file_dir
print('mapping file path:')
print(mapping_file_path)
f = open(mapping_file_path, 'w', encoding='utf-8')
f.write(mapping_file)
f.close()
return mapping_file_path
def get_json_path(j):
max_no = 0
json_path = []
for k in j.keys():
if isinstance(j[k], list):
# print "list (%d): " % len(j[k])
# print j[k]
if len(j[k]) > max_no:
# print "max list (%d): " % len(j[k])
# print j[k]
max_no = len(j[k])
json_path = [k]
elif isinstance(j[k], dict):
j_dict = get_json_path(j[k])
if j_dict["max_no"] > max_no:
max_no = j_dict["max_no"]
json_path = [k] + j_dict["json_path"]
# else:
# print j[k]
# print type(j[k])
return {"max_no": max_no, "json_path": json_path}
def generate_yarrrml_mappings_csv(mapping_file_dir, file_name, entity_class, entity_column, mappings):
mapping_id = get_random_string(10)
single_property_mapping = u""" - [%s, %s]"""
concept_name = entity_class.split('/')[-1].split('#')[-1]
proper_mappings_list = [single_property_mapping % (m["val"], m["key"]) for m in mappings]
class_as_po = single_property_mapping % ("a", entity_class)
proper_mappings_list.append(class_as_po)
property_column_mapping = "\n".join(proper_mappings_list)
mapping_file = u"""
prefixes:
ex: http://www.example.com/
e: http://myontology.com/
base: http://mappingpedia.linkeddata.es/resource/
dbo: http://dbpedia.org/ontology/
mappings:
%s:
sources:
- ['%s~csv']
s: base:$(%s)
po:
%s
""" % (concept_name, file_name, entity_column, property_column_mapping)
print(mapping_file)
mapping_file_path = mapping_file_dir
# mapping_file_path = os.path.join(BASE_DIR, 'local', mapping_id+'.rml.ttl')
print('mapping file path:')
print(mapping_file_path)
f = open(mapping_file_path, 'w', encoding='utf-8')
# f.write(mapping_file.encode('utf8'))
f.write(mapping_file)
f.close()
return mapping_file_path
def get_classes_from_file(odir):
"""
:param odir:
:return:
"""
f = open(odir, encoding='utf-8')
classes = f.read().split('\n')
f.close()
return classes
def get_properties_as_list(ontologies, data_dir):
"""
:param ontologies:
:return:
"""
properties = []
for o in ontologies:
odir = os.path.join(data_dir, o, 'properties.txt')
properties += get_classes_from_file(odir)
return properties
def get_classes_as_txt(ontologies, data_dir):
"""
:param ontologies:
:return:
"""
classes = []
for o in ontologies:
odir = os.path.join(data_dir, o, 'classes.txt')
classes += get_classes_from_file(odir)
# return classes
txt = ""
for c in classes:
txt += '"' + c + '", '
return txt