Skip to content

Commit

Permalink
Forgot to commit a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
IKCAP committed Jul 5, 2024
1 parent 8dae0ad commit 8d921a4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions pylipd/utils/json_to_rdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from rdflib.graph import URIRef, Literal
from rdflib.namespace import XSD

from ..globals.urls import ONTONS

class JSONToRDF:
def __init__(self, graph, graphurl):
self.graph = graph
self.graphurl = graphurl

def _load_triple_into_graph(self, subject, propid, value):
for val in value:
valitem = None
if val["@type"] == "uri":
valitem = URIRef(val["@id"])
elif val["@type"] == "literal":
dtype = val["@datatype"]
if dtype:
valitem = Literal(value, datatype=(XSD[dtype] if dtype in XSD else None))
else:
valitem = Literal(value)
if valitem:
self.graph.add((
URIRef(subject),
URIRef(propid),
valitem,
URIRef(self.graphurl)
))

def _clear_subgraph(self):
for ctx in self.graph.contexts():
id = str(ctx.identifier)
if id == self.graphurl:
self.graph.remove((None, None, None, id))

def load_data_in_graph(self, data):
# Clear the subgraph
self._clear_subgraph()

# Load data
for subject in data:
for propid in data[subject]:
value = data[subject][propid]
self._load_triple_into_graph(subject, ONTONS + propid, value)

0 comments on commit 8d921a4

Please sign in to comment.