Skip to content

Commit

Permalink
Adjust skos2json-script for more flexible processing input-files (#RP…
Browse files Browse the repository at this point in the history
…B-207)

* Create an output-folder
* Set "definition" as OPTIONAL in the SPARQL-query, as it is not given in all input-files.
  • Loading branch information
Petra Maier committed Nov 20, 2024
1 parent 036d33a commit 2ffe02c
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions scripts/skos2json.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import rdflib
from rdflib import Graph
import sys
import os

if not os.path.exists('output'):
os.makedirs('output')

g = rdflib.Graph()

# Load file in format ttl
g.parse("rpb-spatial.ttl", format="ttl")
input_file = sys.argv[1]
g.parse("./%s" % input_file, format="ttl")

# SPARQL query for URI and label of each concept
qres = g.query(
Expand All @@ -14,12 +19,15 @@
SELECT ?label ?concept ?definition
WHERE {
?concept a skos:Concept ;
skos:prefLabel ?label ;
skos:definition ?definition
skos:prefLabel ?label .
OPTIONAL {?concept skos:definition ?definition}
}""")

# Write results to file per line
with open("rpb-spatial.ndjson", "a") as output:
for row in qres:
output.write("{\"prefLabel\":\"%s\",\"uri\":\"%s\",\"definition\":\"%s\"}" % row)
output.write("\n") # add a new line delimiter to start new line
with open("output/%s.ndjson" % input_file.replace(".ttl",""), "w") as output:
for row in qres:
if row.definition is not None:
output.write("{\"prefLabel\":\"%s\",\"uri\":\"%s\",\"definition\":\"%s\"}\n" % row)
else:
row = row[0:2]
output.write("{\"prefLabel\":\"%s\",\"uri\":\"%s\"}\n" % row)

0 comments on commit 2ffe02c

Please sign in to comment.