World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
#!/usr/bin/env node
// 原始材料 + 映射表 → 答案卷(0016 的 C1)。
//
// `fetch-wikidata-truth.mjs` 抓下来的是 Wikidata 说的话(QID、P31、类落在哪些锚下),
// `p31-to-classes.json` 是我们说的话(一个锚对应本体里哪些类)。这里把两者合成
// `truth/<corpus>.wikidata.json`,格式与手填的答案卷一致,`run.mjs --truth` 直接吃。
//
// 三种结果:
// - 有答案:P31 的类落在至少一个映射到本体类的锚下 → 可接受类的并集;
// - 留空:落到的锚都映射为空(概念、学科、历史时期)→ `[]`,正确行为是不动它;
// - 不出现:一个锚都没落到(消歧页、歌曲、桥……)→ 不写进答案卷。不知道就不猜。
//
// 用法:node scripts/bench/make-truth.mjs --corpus ai-timeline
// [--mapping scripts/bench/p31-to-classes.json] [--out truth/ai-timeline.wikidata.json]
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const args = process.argv.slice(2).reduce((acc, cur, i, arr) => {
if (cur.startsWith("--")) {
const next = arr[i + 1];
acc[cur.slice(2)] = next && !next.startsWith("--") ? next : true;
}
return acc;
}, {});
if (!args.corpus) {
console.error("usage: make-truth.mjs --corpus <name> [--mapping file] [--out file]");
process.exit(2);
}
const rawPath = path.join(HERE, "truth", args.corpus + ".wikidata.raw.json");
const raw = JSON.parse(fs.readFileSync(rawPath, "utf8"));
const mapping = JSON.parse(
fs.readFileSync(args.mapping ?? path.join(HERE, "p31-to-classes.json"), "utf8"),
);
const outPath = args.out ?? path.join(HERE, "truth", args.corpus + ".wikidata.json");
const expect = {};
const stats = { answered: 0, untouched: 0, skipped: 0, unanchored: 0 };
const unanchoredClasses = new Map();
for (const [name, e] of Object.entries(raw.entities)) {
if (e.p31.some((c) => c in mapping.skip)) {
stats.skipped += 1;
continue;
}
// 类本身或它落到的锚,凡在映射表里的都算;再按家族规则剔掉串家族的
//(市县合一 ⊂ 地方政府 ⊂ 组织:San Francisco 不该接受 corporation)
let anchors = new Set();
for (const c of e.p31) {
for (const a of [c, ...(raw.classes[c]?.anchors ?? [])]) {
if (a in mapping.classes) anchors.add(a);
}
}
const familyOf = (a) =>
Object.entries(mapping.families).find(([, f]) => f.anchors.includes(a))?.[0];
const present = new Set([...anchors].map(familyOf).filter(Boolean));
const excluded = new Set(
[...present].flatMap((fam) => mapping.families[fam].excludes.filter((x) => present.has(x))),
);
anchors = new Set([...anchors].filter((a) => !excluded.has(familyOf(a))));
const hits = new Set([...anchors].flatMap((a) => mapping.classes[a]));
if (anchors.size === 0) {
stats.unanchored += 1;
for (const c of e.p31) {
const label = raw.classes[c]?.label ?? c;
unanchoredClasses.set(label, (unanchoredClasses.get(label) ?? 0) + 1);
}
continue;
}
expect[name] = [...hits].sort();
if (hits.size) stats.answered += 1;
else stats.untouched += 1;
}
const out = {
note:
"Generated by make-truth.mjs from " +
path.basename(rawPath) +
" and p31-to-classes.json; do not edit by hand — fix the mapping or refetch.",
source: raw.source,
generated_at: new Date().toISOString(),
vocabulary: mapping.vocabulary,
// 生成的答案卷按**全名精确**匹配。手填的卷用子串(名字每次略有出入),而这里的
// key 是维基百科条目名:子串会把 "OpenAI" 的答案套到 "OpenAI Foundation" 头上
match: "exact",
expect: Object.fromEntries(Object.keys(expect).sort((a, b) => a.localeCompare(b)).map((k) => [k, expect[k]])),
};
fs.writeFileSync(outPath, JSON.stringify(out, null, 2) + "\n");
const top = [...unanchoredClasses.entries()].sort((a, b) => b[1] - a[1]).slice(0, 12);
process.stderr.write(
`wrote ${path.relative(process.cwd(), outPath)}: ${stats.answered} answered, ${stats.untouched} to leave alone, ` +
`${stats.skipped} skipped, ${stats.unanchored} without an anchor (omitted)\n`,
);
if (top.length) {
process.stderr.write(
"most common classes among the omitted (candidates for the mapping): " +
top.map(([l, n]) => `${l} ×${n}`).join(", ") +
"\n",
);
}