-
Notifications
You must be signed in to change notification settings - Fork 12
/
converter.py
66 lines (46 loc) · 2.32 KB
/
converter.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
from argparse import ArgumentParser
import logging
import os, codecs
from pathlib import Path
from pyrml.pyrml_mapper import RMLConverter
from pyrml.functions import *
class PyrmlCMDTool:
def __init__(self):
parser = ArgumentParser()
parser.add_argument("-o", "--output", dest="output",
help="Output file. If no choice is provided then standard output is assumed as default.", metavar="RDF out file")
parser.add_argument("-f", "--output-format", dest="format",
help="Output file format. Possible values are n3, nquads, nt, pretty-xml, trig, trix, turtle, and xml. If no choice is provided then NTRIPLES is assumed as default.", metavar="RDF out file")
parser.add_argument("-m", action="store_true", default=False,
help="Eneble conversion based on multiproccessing for fastening the computation.")
parser.add_argument("input", help="The input RML mapping file for enabling RDF conversion.")
self.__args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
def do_map(self):
rml_converter =Framework.get_mapper()
#Inizio aggiunta per recogito
#rml_converter.register_function("get_id", get_id)
#rml_converter.register_function("get_uri", get_uri)
#Fine aggiunta per recogito
g = rml_converter.convert(self.__args.input, self.__args.m)
if self.__args.format is not None:
format = self.__args.format
else:
format = 'nt'
if self.__args.output is not None:
dest_folder = Path(self.__args.output).parent
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
with codecs.open(self.__args.output, 'w', encoding='utf8') as out_file:
out_file.write(g.serialize(format=format))
else:
logging.info(g.serialize(format=format))
def get_id(string):
return string.split(":")[1]
def get_uri(string):
prefix = string.split(":")[0]
id = string.split(":")[1]
prefix_map = {"l0":"https://w3id.org/italia/onto/l0/"}
return prefix_map[prefix]+id
if __name__ == '__main__':
PyrmlCMDTool().do_map()