Skip to content

Commit 4f21781

Browse files
committed
fixed language and type support for nquads
1 parent 20fe95d commit 4f21781

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

dkg/utils/knowledge_collection_tools.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dkg.types import JSONLD, NQuads
66
from pyld import jsonld
77
from dkg.constants import DEFAULT_RDF_FORMAT, DEFAULT_CANON_ALGORITHM, ESCAPE_MAP
8-
from rdflib import Graph, BNode, URIRef, Literal as RDFLiteral
8+
from rdflib import Graph, BNode, URIRef, Dataset
99
from rdflib.exceptions import ParserError as RDFParserError
1010
from uuid import uuid4
1111
from web3 import Web3
@@ -182,16 +182,13 @@ def replace_blank_node(term):
182182
def group_nquads_by_subject(nquads_list: list[str], sort: bool = False):
183183
grouped = {}
184184

185-
# Process each quad in original order
186-
for nquad in nquads_list:
187-
if not nquad.strip(): # Skip empty lines
188-
continue
185+
all_nquads = "\n".join(nquad for nquad in nquads_list if nquad.strip())
189186

190-
# Parse single quad
191-
g = Graph()
192-
g.parse(data=nquad, format="nquads")
193-
quad = next(iter(g))
194-
subject, predicate, obj = quad
187+
d = Dataset()
188+
d.parse(data=all_nquads, format="nquads")
189+
190+
for quad in d:
191+
subject, predicate, obj, graph = quad
195192

196193
# Get subject key
197194
subject_key = (
@@ -204,11 +201,8 @@ def group_nquads_by_subject(nquads_list: list[str], sort: bool = False):
204201
if subject_key not in grouped:
205202
grouped[subject_key] = []
206203

207-
# Format object
208-
object_value = f'"{obj}"' if isinstance(obj, RDFLiteral) else f"<{obj}>"
209-
210204
# Add quad to group
211-
quad_string = f"{subject_key} <{predicate}> {object_value} ."
205+
quad_string = f"{subject.n3()} {predicate.n3()} {obj.n3()} ."
212206
grouped[subject_key].append(quad_string)
213207

214208
# Return grouped quads (sorted if requested)

0 commit comments

Comments
 (0)