| 1 | #!/usr/bin/env node |
| 2 | // 把 schema.org 的 TTL 切成前 N 个类的子集,给退化曲线用。 |
| 3 | // |
| 4 | // 曲线要回答的是:**内联多少词汇量之后抽取开始掉东西**。那条曲线定 |
| 5 | // `ONTOLOGY_PROMPT_BUDGET` 与每块检索多少个候选,不测就是拍脑袋。 |
| 6 | // |
| 7 | // 为什么不用"导入全量再限制内联数":那样量到的是"检索选得准不准", |
| 8 | // 混进了检索的质量。切子集 + 全量内联,量的才是纯粹的规模效应。 |
| 9 | // |
| 10 | // 用法:node scripts/bench/subset.mjs /tmp/schemaorg.ttl 100 > /tmp/schemaorg-100.ttl |
| 11 | |
| 12 | import fs from "node:fs"; |
| 13 | |
| 14 | const [, , src, nRaw] = process.argv; |
| 15 | const N = Number(nRaw); |
| 16 | if (!src || !Number.isFinite(N)) { |
| 17 | console.error("用法: subset.mjs <schemaorg.ttl> <类数>"); |
| 18 | process.exit(2); |
| 19 | } |
| 20 | |
| 21 | const text = fs.readFileSync(src, "utf8"); |
| 22 | const lines = text.split("\n"); |
| 23 | |
| 24 | // 前缀块原样保留:切掉它文件就解析不了 |
| 25 | const prefixEnd = lines.findIndex((l) => l.startsWith("@prefix") === false && l.trim() && !l.startsWith("#")); |
| 26 | const prefixes = lines.slice(0, prefixEnd).join("\n"); |
| 27 | |
| 28 | // 按空行分块,但**必须知道自己在不在三引号字符串里**。 |
| 29 | // |
| 30 | // 前两版都栽在这上面。按 `/\.\s*$/` 收尾不行:schema.org 的 rdfs:comment 里 |
| 31 | // 有以句点结尾的行,块从描述中间被劈开(导入报 `Accountancy is not a valid |
| 32 | // subject`)。改按空行也不行:`"""…"""` 里也有真正的空行,同样劈开 |
| 33 | //(`A is not a valid subject`,"A" 是 BreadcrumbList 那段描述的第一个词)。 |
| 34 | // |
| 35 | // TTL 里没有词法上下文就切不动这个文件——数一下 `"""` 出现过几次, |
| 36 | // 偶数才算在字符串外面。 |
| 37 | const blocks = []; |
| 38 | { |
| 39 | let cur = []; |
| 40 | let inLiteral = false; |
| 41 | for (const line of lines.slice(prefixEnd)) { |
| 42 | const quotes = (line.match(/"""/g) || []).length; |
| 43 | if (!inLiteral && quotes % 2 === 0 && !line.trim() && cur.length) { |
| 44 | blocks.push(cur.join("\n").trim()); |
| 45 | cur = []; |
| 46 | continue; |
| 47 | } |
| 48 | cur.push(line); |
| 49 | if (quotes % 2 === 1) inLiteral = !inLiteral; |
| 50 | } |
| 51 | if (cur.length) blocks.push(cur.join("\n").trim()); |
| 52 | } |
| 53 | |
| 54 | const subjectOf = (b) => (b.match(/^\s*(\S+)\s+a\s/m) || [])[1] || ""; |
| 55 | const isClass = (b) => /\ba\s+rdfs:Class\b/.test(b); |
| 56 | const isProp = (b) => /\ba\s+rdf:Property\b/.test(b); |
| 57 | |
| 58 | // 取前 N 个类。**保序而不是随机取**:同一个 N 每次得到同一份子集, |
| 59 | // 两次跑出的差别才归因得到别处 |
| 60 | const classes = blocks.filter(isClass); |
| 61 | const keep = new Set(classes.slice(0, N).map(subjectOf).filter(Boolean)); |
| 62 | |
| 63 | // 属性:domainIncludes 落在保留的类里就留。留下指向被切掉的类的属性没有意义 |
| 64 | //——那些 domain 解析不出来,导入时本来就会被跳过 |
| 65 | const props = blocks.filter(isProp).filter((b) => { |
| 66 | const m = b.match(/schema:domainIncludes([^;.]*)/); |
| 67 | if (!m) return false; |
| 68 | return m[1] |
| 69 | .split(",") |
| 70 | .map((x) => x.trim().replace(/[.;]$/, "")) |
| 71 | .some((x) => keep.has(x)); |
| 72 | }); |
| 73 | |
| 74 | const kept = blocks.filter((b) => isClass(b) && keep.has(subjectOf(b))); |
| 75 | process.stdout.write(prefixes + "\n\n" + kept.concat(props).join("\n\n") + "\n"); |
| 76 | process.stderr.write(`保留 ${kept.length} 个类、${props.length} 个属性\n`); |