forked from YourOpenDAta/mqa-scoring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqa_calculators.py
424 lines (376 loc) · 15.4 KB
/
mqa_calculators.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import os
import csv
import re
import json
import traceback
import requests
from rdflib import Graph
# load the vocabulary
URL_EDP = 'https://data.europa.eu/api/mqa/shacl/validation/report'
HEADERS = {'content-type': 'application/rdf+xml'}
MACH_READ_FILE = os.path.join('edp-vocabularies', 'edp-machine-readable-format.rdf')
NON_PROP_FILE = os.path.join('edp-vocabularies', 'edp-non-proprietary-format.rdf')
LICENSE_FILE = os.path.join('edp-vocabularies', 'edp-licences-skos.rdf')
ACCESSRIGHTS_FILE = os.path.join('edp-vocabularies', 'access-right-skos.rdf')
MEDIATYPE_FILE_APPLICATION = os.path.join('edp-vocabularies', 'edp-mediatype-application.csv')
MEDIATYPE_FILE_AUDIO = os.path.join('edp-vocabularies', 'edp-mediatype-audio.csv')
MEDIATYPE_FILE_FONT = os.path.join('edp-vocabularies', 'edp-mediatype-font.csv')
MEDIATYPE_FILE_IMAGE = os.path.join('edp-vocabularies', 'edp-mediatype-image.csv')
MEDIATYPE_FILE_MESSAGE = os.path.join('edp-vocabularies', 'edp-mediatype-message.csv')
MEDIATYPE_FILE_MODEL = os.path.join('edp-vocabularies', 'edp-mediatype-model.csv')
MEDIATYPE_FILE_MULTIPART = os.path.join('edp-vocabularies', 'edp-mediatype-multipart.csv')
MEDIATYPE_FILE_TEXT = os.path.join('edp-vocabularies', 'edp-mediatype-text.csv')
MEDIATYPE_FILE_VIDEO = os.path.join('edp-vocabularies', 'edp-mediatype-video.csv')
MEDIATYPE_FILE_VIDEO = os.path.join('edp-vocabularies', 'edp-mediatype-video.csv')
# converts the metric to a string containing just the name of the metric, ex: dct:title
def str_metric(val, g):
valStr=str(val)
for prefix, ns in g.namespaces():
if val.find(ns) != -1:
metStr = valStr.replace(ns,prefix+":")
return metStr
# converts the vocabulary to a graph and get the list of available properties
# "application/rdf+xml"
# "text/csv"
def load_edp_vocabulary(file, format):
g = Graph()
g.parse(file, format=format)
voc = []
for sub, pred, obj in g:
voc.append(str(sub))
return voc
# check if the response of edp validator contains the property "shacl:conforms" and return the value
def valResult(d):
if 'shacl:conforms' in d:
return d['shacl:conforms']
for k in d:
if isinstance(d[k], list):
for i in d[k]:
if 'shacl:conforms' in i:
return i['shacl:conforms']
# send the request to the EDP validator and get the response
def edp_validator(file: str):
check = False
try:
r_edp = requests.post(URL_EDP, data=bytes(file, 'utf-8'), headers=HEADERS)
r_edp.raise_for_status()
except requests.exceptions.HTTPError as err:
print(traceback.format_exc())
raise SystemExit(err)
report = json.loads(r_edp.text)
if valResult(report):
check = True
return check
# create object and add attributes to avoid some properties are missing
def prepareResponse():
class Object(object):
pass
response = Object()
response.title = ''
response.accessURL = 400
response.downloadURL = False
response.downloadURLResponseCode = 400
response.format = False
response.dctFormat_dcatMediaType = False
response.formatMachineReadable = False
response.formatNonProprietary = False
response.license = False
response.licenseVocabulary = False
response.mediaType = False
response.issued = False
response.modified = False
response.rights = False
response.byteSize = False
return response
def distribution_calc(str):
mach_read_voc = []
non_prop_voc = []
license_voc = []
# distribution object
response = prepareResponse()
g = Graph()
g.parse(data = str)
# load the vocabulary
try:
mach_read_voc = load_edp_vocabulary(MACH_READ_FILE,"application/rdf+xml")
non_prop_voc = load_edp_vocabulary(NON_PROP_FILE,"application/rdf+xml")
license_voc = load_edp_vocabulary(LICENSE_FILE,"application/rdf+xml")
except:
print(traceback.format_exc())
mach_read_voc = '-1'
non_prop_voc = '-1'
license_voc = '-1'
accessURL_List = []
downloadURLResponseCode_List = []
dctFormat_dcatMediaType_List = []
# iterate over the properties of the distribution and check if they value are good
# some metrics just check if the property is present
# others need to check the url or the value of the property
# others need to check if they are in the vocabulary
# full list can be found https://data.europa.eu/mqa/methodology?locale=en with relative weights
for sub, pred, obj in g:
met = str_metric(pred, g)
if met == "dct:title" and response.title == '':
response.title = obj
elif met == "dcat:accessURL":
try:
res = requests.get(obj)
accessURL_List.append(res.status_code)
except:
print(traceback.format_exc())
accessURL_List.append(400)
elif met == "dcat:downloadURL":
response.downloadURL = True
try:
res = requests.get(obj)
downloadURLResponseCode_List.append(res.status_code)
except:
print(traceback.format_exc())
downloadURLResponseCode_List.append(400)
# in catalogue formats the property dct:MediaTypeOrExtent is inside an empty dct:format tag. The empty tag must be skipped and not counted
elif (met == "dct:format" and obj != '' and obj != None) or met == "dct:MediaTypeOrExtent":
response.format = True
try:
if (obj) in mach_read_voc:
response.formatMachineReadable = True
else:
response.formatMachineReadable = False
if (obj) in non_prop_voc:
response.formatNonProprietary = True
else:
response.formatNonProprietary = False
except:
print(traceback.format_exc())
response.formatMachineReadable = False
response.formatNonProprietary = False
try:
g2 = Graph()
g2.parse(obj, format="application/rdf+xml")
if (obj, None, None) in g2:
dctFormat_dcatMediaType_List.append(True)
else:
dctFormat_dcatMediaType_List.append(False)
except:
print(traceback.format_exc())
dctFormat_dcatMediaType_List.append(False)
elif met == "dct:license":
response.license = True
try:
if (obj) in license_voc:
response.licenseVocabulary = True
else:
response.licenseVocabulary = False
except:
print(traceback.format_exc())
response.licenseVocabulary = False
elif met == "dcat:mediaType":
response.mediaType = True
try:
# removes the prefix from the url to check if it is in the vocabulary
mediatype = obj.replace('http://www.iana.org/assignments/media-types/','')
mediatype = mediatype.replace('https://www.iana.org/assignments/media-types/','')
found = False
try:
vocabularies = [MEDIATYPE_FILE_APPLICATION, MEDIATYPE_FILE_AUDIO, MEDIATYPE_FILE_FONT, MEDIATYPE_FILE_IMAGE, MEDIATYPE_FILE_MESSAGE, MEDIATYPE_FILE_MODEL, MEDIATYPE_FILE_MULTIPART, MEDIATYPE_FILE_TEXT, MEDIATYPE_FILE_VIDEO]
for voc in vocabularies:
with open(voc, 'rt') as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
for field in row:
if field == mediatype:
found = True
break
if found == True:
break
if found == True:
dctFormat_dcatMediaType_List.append(True)
except:
print(traceback.format_exc())
dctFormat_dcatMediaType_List.append(False)
except:
print(traceback.format_exc())
dctFormat_dcatMediaType_List.append(False)
elif met == "dct:issued":
response.issued = True
elif met == "dct:modified":
response.modified = True
elif met == "dct:rights":
response.rights = True
elif met == "dcat:byteSize":
response.byteSize = True
response.accessURL = most_frequent(accessURL_List)
response.downloadURLResponseCode = most_frequent(downloadURLResponseCode_List)
temp = True
for el in dctFormat_dcatMediaType_List:
if el == False:
temp = False
break
response.dctFormat_dcatMediaType = temp
return response
# get the most frequent value in a list
def most_frequent(List):
counter = 0
if(len(List) == 0):
return 400
num = List[0]
for i in List:
curr_frequency = List.count(i)
if(curr_frequency> counter):
counter = curr_frequency
num = i
return num
def dataset_calc(dataset_str, pre):
class Object(object):
pass
response = Object()
response.distributions = []
accessRights_voc = []
dt_copy = dataset_str
# cut off all the tags on datasets level, and leave just the tags on distribution level to analyze them separately
distribution_start = [m.start() for m in re.finditer('(?=<dcat:distribution>)', dataset_str)]
distribution_finish = [m.start() for m in re.finditer('(?=</dcat:distribution>)', dataset_str)]
if len(distribution_start) == len(distribution_finish):
for index, item in enumerate(distribution_start):
distr_tag = dataset_str[distribution_start[index]:distribution_finish[index]+20]
# cut off the distribution tag from the dataset string to obtain just the dataset properties to analyze them separately
dt_copy = dt_copy.replace(distr_tag, '')
# variable pre is always required from rdf files and it contains at least the rdf tag: <rdf:RDF ...> and can also contain the xml tag: <?xml version="1.0"?>
distribution = pre + '<dcat:Dataset>' + distr_tag + '</dcat:Dataset>' +'</rdf:RDF>'
response.distributions.append(distribution_calc(distribution))
dt_copy = dt_copy.replace(dt_copy[dt_copy.rfind('<adms:identifier>'):dt_copy.rfind('</adms:identifier>')+18], '')
g = Graph()
g.parse(data = dt_copy)
# sets initial values to avoid some properties are missing
response.title = ''
response.issued = 0
response.modified = False
response.keyword = False
response.issuedDataset = False
response.modifiedDataset = False
response.theme = False
response.spatial = False
response.temporal = False
response.contactPoint = False
response.publisher = False
response.accessRights = False
response.accessRightsVocabulary = False
response.accessURL = []
response.downloadURL = 0
response.downloadURLResponseCode = []
response.format = 0
response.dctFormat_dcatMediaType = 0
response.formatMachineReadable = 0
response.formatNonProprietary = 0
response.license = 0
response.licenseVocabulary = 0
response.mediaType = 0
response.rights = 0
response.byteSize = 0
try:
accessRights_voc = load_edp_vocabulary(ACCESSRIGHTS_FILE,"application/rdf+xml")
except:
print(traceback.format_exc())
accessRights_voc = '-1'
try:
res = edp_validator(dataset_str)
if res == True:
response.shacl_validation = True
else:
response.shacl_validation = False
except:
print(traceback.format_exc())
response.shacl_validation = 0
# iterate over the properties of the datasets and check if they value are good
# some metrics just check if the property is present
# others need to check the url or the value of the property
# others need to check if they are in the vocabulary
# full list can be found https://data.europa.eu/mqa/methodology?locale=en with relative weights
for sub, pred, obj in g:
met = str_metric(pred, g)
if met == "dct:title" and response.title == '':
response.title = obj
elif met == "dct:issued":
response.issued += 1
response.issuedDataset = True
elif met == "dct:modified":
response.modified = True
response.modifiedDataset = True
elif met == "dcat:keyword":
response.keyword = True
elif met == "dcat:theme":
response.theme = True
elif met == "dct:spatial":
response.spatial = True
elif met == "dct:temporal":
response.temporal = True
elif met == "dcat:contactPoint":
response.contactPoint = True
elif met == "dct:publisher":
response.publisher = True
elif met == "dct:accessRights":
response.accessRights = True
try:
if str(obj) in accessRights_voc:
response.accessRightsVocabulary = True
else:
response.accessRightsVocabulary = False
except:
print(traceback.format_exc())
response.accessRightsVocabulary = False
tempArrayDownloadUrl = []
tempArrayAccessUrl = []
# iterate over the distributions metrics to count positive values
for distr in response.distributions:
if distr.issued == True:
response.issued += 1
if distr.downloadURL == True:
response.downloadURL += 1
tempArrayDownloadUrl.append(distr.downloadURLResponseCode)
tempArrayAccessUrl.append(distr.accessURL)
if distr.license == True:
response.license += 1
if distr.licenseVocabulary == True:
response.licenseVocabulary += 1
if distr.byteSize == True:
response.byteSize += 1
if distr.rights == True:
response.rights += 1
if distr.format == True:
response.format += 1
if distr.formatMachineReadable == True:
response.formatMachineReadable += 1
if distr.formatNonProprietary == True:
response.formatNonProprietary += 1
if distr.mediaType == True:
response.mediaType += 1
if distr.dctFormat_dcatMediaType == True:
response.dctFormat_dcatMediaType += 1
# calculate the percentage of positive values for each metric
response.issued = round(response.issued / (len(response.distributions)+ 1) * 100)
response.downloadURL = round(response.downloadURL / len(response.distributions) * 100)
list_unique = (list(set(tempArrayDownloadUrl)))
# create a list for each response code and gives the percentage of each code
for el in list_unique:
response.downloadURLResponseCode.append({"code": el, "percentage": round(tempArrayDownloadUrl.count(el) / len(response.distributions) * 100)})
list_unique = (list(set(tempArrayAccessUrl)))
# create a list for each response code and gives the percentage of each code
for el in list_unique:
response.accessURL.append({"code": el, "percentage": round(tempArrayAccessUrl.count(el) / len(response.distributions) * 100)})
response.license = round(response.license / len(response.distributions) * 100)
response.licenseVocabulary = round(response.licenseVocabulary / len(response.distributions) * 100)
response.byteSize = round(response.byteSize / len(response.distributions) * 100)
response.rights = round(response.rights / len(response.distributions) * 100)
response.format = round(response.format / len(response.distributions) * 100)
response.formatMachineReadable = round(response.formatMachineReadable / len(response.distributions) * 100)
response.formatNonProprietary = round(response.formatNonProprietary / len(response.distributions) * 100)
response.mediaType = round(response.mediaType / len(response.distributions) * 100)
response.dctFormat_dcatMediaType = round(response.dctFormat_dcatMediaType / (len(response.distributions)*2) * 100)
# modified needs to be checked on both dataset and distribution level, but only needs to be true once, so if it is true on dataset level, it is not necessary to check on distribution level
if(response.modified == False):
for distr in response.distributions:
if distr.modified == True:
response.modified = True
break
else:
return -1
return response