| 1 | #!/usr/bin/env node |
| 2 | // 从一份语料里挑出几个条目,做成一份新语料。 |
| 3 | // |
| 4 | // 存在的理由是 `wiki-history` 跑不完:219 篇快照约 7000 块,按实测速率要六到七 |
| 5 | // 小时,还会撞上端点的每分钟 token 配额。而**它回答的两个问题对语料的要求不一样**: |
| 6 | // |
| 7 | // - 「违反率归零站不站得住」是统计题。60 块出 149 条可校验事实,零违反的 |
| 8 | // 置信上界按三倍律是 `3/n`,几百块就已经结论性了,再多跑不增加信息。 |
| 9 | // - 「图会不会真的改主意」是结构题,统计帮不上忙。它要的是**整条快照链按 |
| 10 | // `doc_time` 顺序进去**,因为 `supersedes` 只在同一条目的相邻快照之间发生。 |
| 11 | // |
| 12 | // 所以切法是**整条目取,不切块**:随机抽块能满足第一个问题,会把第二个问题 |
| 13 | // 直接废掉。挑几个互相咬合的条目,比多跑几千块有用。 |
| 14 | // |
| 15 | // 输出按 `doc_time` 升序排列。灌入顺序就是认知生长的顺序,乱序进去的图 |
| 16 | // 在溯源时态那根轴上是没有意义的。 |
| 17 | // |
| 18 | // 用法:node scripts/bench/subset-corpus.mjs <语料.json> <条目,条目,…> > 新语料.json |
| 19 | // |
| 20 | // 例(2023 年 11 月 OpenAI 那场风波,七个共享实体的条目): |
| 21 | // node scripts/bench/subset-corpus.mjs scripts/bench/corpora/wiki-history.json \ |
| 22 | // openai,removal-of-sam-altman-from-openai,sam-altman,ilya-sutskever,\ |
| 23 | // mira-murati,greg-brockman,emmett-shear \ |
| 24 | // > scripts/bench/corpora/wiki-nov2023.json |
| 25 | // |
| 26 | // 不带条目参数就只列出源语料有哪些条目、各多少快照,不输出语料。 |
| 27 | |
| 28 | import fs from "node:fs"; |
| 29 | |
| 30 | const [, , src, titlesRaw] = process.argv; |
| 31 | if (!src) { |
| 32 | console.error("用法: subset-corpus.mjs <语料.json> [条目,条目,…]"); |
| 33 | console.error(" 不给条目则只列出源语料的条目清单"); |
| 34 | process.exit(2); |
| 35 | } |
| 36 | |
| 37 | const corpus = JSON.parse(fs.readFileSync(src, "utf8")); |
| 38 | if (!Array.isArray(corpus.docs)) { |
| 39 | console.error(`${src} 里没有 docs 数组,不像是一份语料`); |
| 40 | process.exit(2); |
| 41 | } |
| 42 | |
| 43 | /// 文件名形如 `openai@2023-11-19.txt`,`@` 之前是条目。 |
| 44 | /// 当前版语料(fetch-ai-timeline)没有 `@`,整个文件名就是条目。 |
| 45 | const titleOf = (filename) => filename.replace(/@.*$/, "").replace(/\.txt$/, ""); |
| 46 | |
| 47 | // 条目清单:快照数与体量。给挑之前看用 |
| 48 | const groups = new Map(); |
| 49 | for (const [filename, text] of corpus.docs) { |
| 50 | const t = titleOf(filename); |
| 51 | const g = groups.get(t) || { n: 0, chars: 0 }; |
| 52 | g.n += 1; |
| 53 | g.chars += text.length; |
| 54 | groups.set(t, g); |
| 55 | } |
| 56 | |
| 57 | if (!titlesRaw) { |
| 58 | const rows = [...groups].sort((a, b) => b[1].chars - a[1].chars); |
| 59 | const total = rows.reduce((s, [, g]) => s + g.chars, 0); |
| 60 | for (const [t, g] of rows) { |
| 61 | const pct = ((100 * g.chars) / total).toFixed(1).padStart(5); |
| 62 | console.error( |
| 63 | `${String(g.n).padStart(4)} 张 ${String(Math.round(g.chars / 1000)).padStart(6)}k ${pct}% ${t}`, |
| 64 | ); |
| 65 | } |
| 66 | console.error(`\n共 ${rows.length} 个条目,${corpus.docs.length} 张快照,${(total / 1e6).toFixed(2)}M 字符`); |
| 67 | process.exit(0); |
| 68 | } |
| 69 | |
| 70 | const wanted = new Set(titlesRaw.split(",").map((t) => t.trim()).filter(Boolean)); |
| 71 | |
| 72 | // **认不出的条目名要报错,不能静默产出一份小语料。** 打错一个字就少一个条目, |
| 73 | // 而少了的那个条目正是共享实体的来源,图会散成互不相连的星团—— |
| 74 | // 而这在结果里看起来只是"效果没那么好",查不到根上。 |
| 75 | const unknown = [...wanted].filter((t) => !groups.has(t)); |
| 76 | if (unknown.length) { |
| 77 | console.error(`源语料里没有这些条目:${unknown.join(", ")}`); |
| 78 | console.error(`不带条目参数重跑一次可以看到全部条目名。`); |
| 79 | process.exit(2); |
| 80 | } |
| 81 | |
| 82 | const docs = corpus.docs |
| 83 | .filter(([filename]) => wanted.has(titleOf(filename))) |
| 84 | // 按 doc_time 升序。第三个元素缺席时(当前版语料)退回文件名排序, |
| 85 | // 至少是确定的 |
| 86 | .sort((a, b) => String(a[2] ?? a[0]).localeCompare(String(b[2] ?? b[0]))); |
| 87 | |
| 88 | const chars = docs.reduce((s, d) => s + d[1].length, 0); |
| 89 | |
| 90 | process.stdout.write( |
| 91 | JSON.stringify({ |
| 92 | name: `${corpus.name}-subset`, |
| 93 | note: |
| 94 | `${corpus.name} 的子集,条目:${[...wanted].join("、")}。` + |
| 95 | `整条目取并按 doc_time 升序,因为 supersedes 只在同一条目的相邻快照之间发生。` + |
| 96 | (corpus.note ? ` 源语料说明:${corpus.note}` : ""), |
| 97 | source: corpus.source, |
| 98 | license: corpus.license, |
| 99 | sampling: corpus.sampling, |
| 100 | subset_of: corpus.name, |
| 101 | docs, |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | // 统计走 stderr,这样 stdout 可以直接重定向成语料文件 |
| 106 | console.error( |
| 107 | `${docs.length} 张快照,${Math.round(chars / 1000)}k 字符,` + |
| 108 | // 1200 字符预算、150 重叠,见 utopia-ingest 的 chunk_text |
| 109 | `约 ${Math.round(chars / 1050)} 块`, |
| 110 | ); |