From c468b4ebdfbe9c2387c9428fe92ece81e42999c9 Mon Sep 17 00:00:00 2001 From: David Rouquet <david.rouquet@tetras-libre.fr> Date: Tue, 19 Jan 2021 10:50:49 +0100 Subject: [PATCH] unl to rdf --- scripts/unlizeToRdf.py | 7 ++++++- scripts/unlizeXml.py | 17 ++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/scripts/unlizeToRdf.py b/scripts/unlizeToRdf.py index e201298..17dcf76 100755 --- a/scripts/unlizeToRdf.py +++ b/scripts/unlizeToRdf.py @@ -3,6 +3,7 @@ import click from lxml import etree, objectify from unlizeXml import remove_namespace, unlize, nestedBody2Str, unl2rdf +from rdflib import Graph @click.command() @click.argument('input', nargs=1, @@ -14,6 +15,7 @@ from unlizeXml import remove_namespace, unlize, nestedBody2Str, unl2rdf @click.option('--dry-run/--no-dry-run', default=False, help='if true do not send request to unl.ru') def unlizeXmlRdf(input, output, lang, dry_run): + graph = Graph() parser = etree.XMLParser(remove_comments=True) doc = objectify.parse(input, parser=parser) remove_namespace(doc) @@ -26,7 +28,10 @@ def unlizeXmlRdf(input, output, lang, dry_run): unl = unlize(nestedBody2Str(node), lang, dry_run) print(unl2rdf(unl)) else: - print(unl2rdf(getText(node, 'unl'))) + ttl = unl2rdf(getText(node, 'unl')) + graph.parse(data=ttl, format='ttl') + graph.serialize(destination='output.ttl', format='turtle') + # Save #with open(output, 'w') as f: # nbf.write(nb, f) diff --git a/scripts/unlizeXml.py b/scripts/unlizeXml.py index 6b3049f..ec0bdaa 100755 --- a/scripts/unlizeXml.py +++ b/scripts/unlizeXml.py @@ -115,7 +115,6 @@ def unl2rdf(text, path='./unl-tools-main-0.9.jar'): with tempfile.NamedTemporaryFile() as temp: out_name = os.path.basename(temp.name) out_dir = os.path.dirname(temp.name) - with tempfile.NamedTemporaryFile(mode="w") as in_file: # Remove CRLF and flush output to avoid java errors in_file.write(text.replace("\r\n", "\n")) @@ -129,24 +128,20 @@ def unl2rdf(text, path='./unl-tools-main-0.9.jar'): # generate dot output fname = '{}/{}.ttl'.format(out_dir, out_name) - with Popen(cmd, stdout=PIPE, stderr=STDOUT) as p: p.wait() p.stdout.flush() if p.returncode != 0: print("Error in unl2rdf: \n\n"+p.stdout.read().decode()) - print('UNL;') print(text) + else: try: with open(fname) as f: - print('RDF:') - print(f.read()) + ttl = f.read() + return ttl except FileNotFoundError: pass - else: - ttl = p.stdout.read().decode() - os.remove(fname) - return ttl + os.remove(fname) return "" @@ -163,7 +158,7 @@ def unl2rdf(text, path='./unl-tools-main-0.9.jar'): help='Add svg node representing unl graph') @click.option('--unltools-path', nargs=1, type=click.Path(dir_okay=False), - default='unl2rdf-app-1.0-SNAPSHOT-jar-with-dependencies.jar', + default='unl-tools-main-0.9.jar', help='Path of the unltools jar') @click.option('--output-type', default='dot', type=click.Choice(['dot', 'rdf']), @@ -186,7 +181,7 @@ def unlizeXml(input, output, output_type, lang, dry_run, svg, unltools_path): if(svg and not dry_run): addSubElement(node, 'svg', unl2dot(unl.text, unltools_path)) elif output_type=='rdf': - unl2rdf(unl.text, unltools_path) + unl2rdf(unl.text, unltools_path) with open(output, 'w') as out: out.write(etree.tostring(doc, pretty_print=True).decode('utf-8')) -- GitLab