funkikitech commited on
Commit
a1195ff
·
verified ·
1 Parent(s): 5c662d2

Create scripts/build-jsonl.mjs

Browse files
Files changed (1) hide show
  1. scripts/build-jsonl.mjs +84 -0
scripts/build-jsonl.mjs ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import crypto from "node:crypto";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
7
+ const dataDir = path.join(root, "data");
8
+ const files = [
9
+ "printer_brand_research_matrix.csv",
10
+ "competitor_product_research_matrix.csv",
11
+ "source_library.csv"
12
+ ];
13
+
14
+ function parseCsv(input) {
15
+ const rows = [];
16
+ let row = [];
17
+ let field = "";
18
+ let quoted = false;
19
+
20
+ for (let i = 0; i < input.length; i += 1) {
21
+ const char = input[i];
22
+ const next = input[i + 1];
23
+ if (quoted && char === '"' && next === '"') {
24
+ field += '"';
25
+ i += 1;
26
+ } else if (char === '"') {
27
+ quoted = !quoted;
28
+ } else if (char === "," && !quoted) {
29
+ row.push(field);
30
+ field = "";
31
+ } else if ((char === "\n" || char === "\r") && !quoted) {
32
+ if (char === "\r" && next === "\n") i += 1;
33
+ row.push(field);
34
+ if (row.some((value) => value !== "")) rows.push(row);
35
+ row = [];
36
+ field = "";
37
+ } else {
38
+ field += char;
39
+ }
40
+ }
41
+
42
+ if (field !== "" || row.length > 0) {
43
+ row.push(field);
44
+ rows.push(row);
45
+ }
46
+
47
+ const [headers, ...values] = rows;
48
+ return values.map((cells, rowIndex) => {
49
+ if (cells.length !== headers.length) {
50
+ throw new Error(`Column mismatch on row ${rowIndex + 2}: expected ${headers.length}, received ${cells.length}`);
51
+ }
52
+ return Object.fromEntries(headers.map((header, index) => [header, cells[index]]));
53
+ });
54
+ }
55
+
56
+ const manifest = {
57
+ name: "Graphictac Knowledge Base Database",
58
+ version: "0.1.0",
59
+ published: "2026-07-18",
60
+ license: "CC-BY-4.0",
61
+ datasets: []
62
+ };
63
+
64
+ for (const file of files) {
65
+ const csvPath = path.join(dataDir, file);
66
+ const input = fs.readFileSync(csvPath, "utf8");
67
+ const records = parseCsv(input);
68
+ const jsonlName = file.replace(/\.csv$/, ".jsonl");
69
+ const jsonlPath = path.join(dataDir, jsonlName);
70
+ const jsonl = `${records.map((record) => JSON.stringify(record)).join("\n")}\n`;
71
+ fs.writeFileSync(jsonlPath, jsonl, "utf8");
72
+
73
+ manifest.datasets.push({
74
+ name: file.replace(/\.csv$/, ""),
75
+ csv: `data/${file}`,
76
+ jsonl: `data/${jsonlName}`,
77
+ rows: records.length,
78
+ csv_sha256: crypto.createHash("sha256").update(input).digest("hex"),
79
+ jsonl_sha256: crypto.createHash("sha256").update(jsonl).digest("hex")
80
+ });
81
+ }
82
+
83
+ fs.writeFileSync(path.join(root, "dataset_manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`, "utf8");
84
+ console.log(JSON.stringify(manifest, null, 2));