Skip to content

Commit 771e59a

Browse files
Merge pull request #29 from marko03kostic/fix/perutnina-nquads-parsing
Fix/perutnina nquads parsing
2 parents 833ea95 + ac239bc commit 771e59a

File tree

2 files changed

+540
-31
lines changed

2 files changed

+540
-31
lines changed

src/knowledge-collection-tools.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,16 @@ export function groupNquadsBySubject(nquadsArray, sort = false) {
234234
const grouped = {};
235235

236236
parser.parse(nquadsArray.join("")).forEach((quad) => {
237-
const { subject, predicate, object } = quad;
237+
const { subject } = quad;
238238

239239
let subjectKey;
240240
if (subject.termType === "Quad") {
241241
const nestedSubject = subject.subject.value;
242242
const nestedPredicate = subject.predicate.value;
243243
const nestedObject =
244244
subject.object.termType === "Literal"
245-
? `"${escapeLiteral(subject.object.value)}"`
246-
: `<${escapeLiteral(subject.object.value)}>`;
245+
? `"${subject.object.value}"`
246+
: `<${subject.object.value}>`;
247247
subjectKey = `<<<${nestedSubject}> <${nestedPredicate}> ${nestedObject}>>`;
248248
} else {
249249
subjectKey = `<${subject.value}>`;
@@ -253,12 +253,14 @@ export function groupNquadsBySubject(nquadsArray, sort = false) {
253253
grouped[subjectKey] = [];
254254
}
255255

256-
const objectValue =
257-
object.termType === "Literal"
258-
? `"${escapeLiteral(object.value)}"`
259-
: `<${escapeLiteral(object.value)}>`;
256+
const writer = new N3.Writer({ format: "N-Quads" });
257+
let quadString = "";
258+
writer.addQuad(quad);
259+
writer.end((error, result) => {
260+
if (error) throw error;
261+
quadString = result.trim();
262+
});
260263

261-
const quadString = `${subjectKey} <${predicate.value}> ${objectValue} .`;
262264
grouped[subjectKey].push(quadString);
263265
});
264266

@@ -401,17 +403,3 @@ ${nquadsArray.join('\n')}
401403
function isEmptyObject(obj) {
402404
return Object.keys(obj).length === 0 && obj.constructor === Object;
403405
}
404-
405-
function escapeLiteral(value) {
406-
const ESCAPE_MAP = {
407-
'"': '\\"',
408-
"\\": "\\\\",
409-
"\b": "\\b",
410-
"\f": "\\f",
411-
"\n": "\\n",
412-
"\r": "\\r",
413-
"\t": "\\t",
414-
};
415-
416-
return value.replace(/["\\\b\f\n\r\t]/g, (char) => ESCAPE_MAP[char]);
417-
}

0 commit comments

Comments
 (0)