-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOntology.py
58 lines (48 loc) · 1.97 KB
/
Ontology.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
from rdflib import Graph, RDF
import yaml
from YARRRMLMapper import parse_to_rdf_mapping
import argparse
def main(mapping, destination):
try:
mapping = yaml.safe_load(mapping)
print('YARRRML Mapping Parsing: OK')
except (yaml.parser.ParserError, yaml.scanner.ScannerError) as exception:
print(f'YARRRML Mapping Syntax Error: {exception}')
return
mapping = parse_to_rdf_mapping(mapping)
classes, properties = get_classes_properties(mapping)
rdf_ontology = get_ontology(classes, properties)
destination.write(rdf_ontology.serialize(format='ttl').decode('utf-8'))
print('Ontology exported to destination file !')
def get_ontology(classes, properties):
rdf_ontology = Graph()
concepts = classes | properties
for concept in concepts:
graph = get_rdf_graph(f'{concept}')
for s, p, o in graph.triples((concept, None, None)):
rdf_ontology.add((s, p, o))
return rdf_ontology
def get_rdf_graph(concept):
rdf_graph = Graph()
try:
if 'schema.org' in concept:
rdf_graph.parse(f'{concept}.ttl', format='ttl')
else:
rdf_graph.parse(f'{concept}', format='xml')
except Exception as e:
print(f'A problem occured when trying to retrieve rdf at {concept}: {e}')
return rdf_graph
def get_classes_properties(rdf_mapping):
properties = set()
classes = set()
for _, _, o in rdf_mapping.triples((None, RDF.type, None)):
classes.add(o)
for _, p, _ in rdf_mapping.triples((None, None, None)):
properties.add(p)
return classes, properties
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Get ontology of the mapping file.')
parser.add_argument('mapping', type=argparse.FileType('r'), help='yarmmml mapping file')
parser.add_argument('destination', type=argparse.FileType('w'), help='ttl file that will contain the ontology')
args = parser.parse_args()
main(args.mapping, args.destination)