book-text-util 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/cli.js +145 -22
- package/dist/index.cjs +124 -28
- package/dist/index.d.cts +41 -7
- package/dist/index.d.ts +41 -7
- package/dist/index.js +119 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -59,6 +59,11 @@ const { items, segments } = await compareWhole({ cells, md, file: '001', label:
|
|
|
59
59
|
- `compareChapter`:按卷分段(册↔卷对应);`runCompare`:连取数一起做(传 `transport`,不给 `isVariant` 时按浏览器环境取 `/compare/glyph-variants.json`,Node 请自己传)。
|
|
60
60
|
- **异体字组**:给了 `isVariant` 时,`diffChars` 按「异体=同字」对齐,逐字互为异体的等长一段单独成一处(`hunk.variant`,类别 glyph),不再和旁边真正的用字不同粘成一条 word。判定只认表里的直接配对,不做传递(葢≈蓋、蓋≈盖 不推出 葢≈盖,避免 干/乾/幹 之类并成一组)。
|
|
61
61
|
- 异体字判定用 open-guji-cv 的 `edges.tsv` 抽出的表(`{ pairs: "葢蓋\n畧略…" }`)。
|
|
62
|
+
- **异体字表怎么生成**(0.1.4 起在本包):`book-text-util variants --edges <open-guji-cv/config/hanzi/core/edges.tsv> [--mode collate|trusted|open] --out glyph-variants.json`,或在代码里 `buildVariantTable(edgesTsv, { mode, toSimplified })`。
|
|
63
|
+
- `trusted`/`open`:与网站原 `scripts/build-glyph-variants.mjs` 一样:variant/variant_partial/simp/z 边,来源限可信(open=Unihan、本项目、yi-bai;trusted 再加 yitizi、twedu、dypytz、hydzd、cjkvi-simplified),有 distinct 或 borrow(通假)边的不算。
|
|
64
|
+
- `collate`(默认,2026-11-14 殿本第1–5册参校查漏用、用户认可):在 trusted 上再加三条——① Unihan 语义/Z 异体或教育部异体字字典认定的,压过通假边(辨/辯、奕/弈、筭/算),distinct 仍否决;② 两字有一个是扩展区字(扩展 A 或 BMP 以外)时,任何来源的 variant 边都算(𠉀/候、𭧂/暴);常用字之间只靠宽松来源的不算(令/命、壁/璧、宇/字 仍是「用字不同」);③ 转简后同形的字对另放 `simp` 栏(範/范、乾/幹、閒/閑),`buildVariantTest(table, { simp: false })` 可以不并——对照本是简繁转换来的(维基文库)就该并。
|
|
65
|
+
- 分类另有两条对照本整理痕迹算 `struct`:底本条目起首的「一」被换成序号(一→十七,前一字是 卷/第/年 等的不算)、对照本写「無法顯示」占位。
|
|
66
|
+
- 第1–5册 911 处非异体差异上,collate 与人工核过的分类 910 处一致。
|
|
62
67
|
- 其余:`diffChars`、`classify`、`wikiChars`/`chapterChars`、`parseCmp`/`formatCmp`(`?cmp=` 链接)。
|
|
63
68
|
- 界面相关(标记装饰 `decorate`、偏好存储 `prefs`、React 钩子)留在网站,不在本包。
|
|
64
69
|
|
package/dist/cli.js
CHANGED
|
@@ -167,9 +167,9 @@ function cmpKey(a, b) {
|
|
|
167
167
|
for (let i = 1; i < 5; i++) if (a[i] !== b[i]) return a[i] ? 1 : -1;
|
|
168
168
|
return 0;
|
|
169
169
|
}
|
|
170
|
-
function resolveOffsets(pos, items,
|
|
170
|
+
function resolveOffsets(pos, items, key2) {
|
|
171
171
|
return items.map((it, i) => {
|
|
172
|
-
const a =
|
|
172
|
+
const a = key2(it);
|
|
173
173
|
const off = a === void 0 ? void 0 : pos.get(a);
|
|
174
174
|
if (off === void 0) throw new GujiFormatError(`\u7B2C ${i} \u6761\u7684\u683C\u4F4D ${String(a)} \u4E0D\u5728 char \u91CC`);
|
|
175
175
|
return off;
|
|
@@ -312,21 +312,21 @@ function availableFormats(ch) {
|
|
|
312
312
|
function safeFile(v) {
|
|
313
313
|
return typeof v === "string" && /^[\w.-]+$/.test(v) ? v : null;
|
|
314
314
|
}
|
|
315
|
-
async function getJson(source, id,
|
|
315
|
+
async function getJson(source, id, key2, file) {
|
|
316
316
|
try {
|
|
317
|
-
return await source.getTextFile(id,
|
|
317
|
+
return await source.getTextFile(id, key2, file) ?? null;
|
|
318
318
|
} catch {
|
|
319
319
|
return null;
|
|
320
320
|
}
|
|
321
321
|
}
|
|
322
|
-
async function getPreferred(source, id,
|
|
322
|
+
async function getPreferred(source, id, key2, slim, original) {
|
|
323
323
|
const s = safeFile(slim);
|
|
324
324
|
if (s) {
|
|
325
|
-
const v = await getJson(source, id,
|
|
325
|
+
const v = await getJson(source, id, key2, s);
|
|
326
326
|
if (v != null) return v;
|
|
327
327
|
}
|
|
328
328
|
const o = safeFile(original);
|
|
329
|
-
return o ? getJson(source, id,
|
|
329
|
+
return o ? getJson(source, id, key2, o) : null;
|
|
330
330
|
}
|
|
331
331
|
function glyphConverter(glyph2, converters) {
|
|
332
332
|
if (glyph2 === "original") return (s) => s;
|
|
@@ -335,14 +335,14 @@ function glyphConverter(glyph2, converters) {
|
|
|
335
335
|
}
|
|
336
336
|
async function buildExport(req, source, converters) {
|
|
337
337
|
const ch = req.chapter;
|
|
338
|
-
const { id, versionKey:
|
|
338
|
+
const { id, versionKey: key2 } = req;
|
|
339
339
|
const needPunct = req.format !== "lines";
|
|
340
340
|
const [char, punct, entity, fig] = await Promise.all([
|
|
341
|
-
getPreferred(source, id,
|
|
342
|
-
needPunct ? getPreferred(source, id,
|
|
341
|
+
getPreferred(source, id, key2, ch.reader_char_file, ch.char_file),
|
|
342
|
+
needPunct ? getPreferred(source, id, key2, ch.reader_punct_file, ch.punct_file) : null,
|
|
343
343
|
// 专名层取原文件:瘦身副本有 entity_id 时去掉了 href,而下载要原样写仓里的 book-index:// 链接
|
|
344
|
-
needPunct && safeFile(ch.entity_file) ? getJson(source, id,
|
|
345
|
-
safeFile(ch.fig_file) ? getJson(source, id,
|
|
344
|
+
needPunct && safeFile(ch.entity_file) ? getJson(source, id, key2, ch.entity_file) : null,
|
|
345
|
+
safeFile(ch.fig_file) ? getJson(source, id, key2, ch.fig_file) : null
|
|
346
346
|
]);
|
|
347
347
|
if (!char) throw new Error("\u672C\u518A\u6587\u5B57\u53D6\u4E0D\u5230");
|
|
348
348
|
if (needPunct && !punct) throw new Error("\u672C\u518A\u6A19\u9EDE\u53D6\u4E0D\u5230");
|
|
@@ -441,8 +441,8 @@ import { readFile, writeFile, mkdir } from "fs/promises";
|
|
|
441
441
|
import { join } from "path";
|
|
442
442
|
function dirSource(root, layout = "flat") {
|
|
443
443
|
return {
|
|
444
|
-
async getTextFile(id,
|
|
445
|
-
const p = layout === "flat" ? join(root, file) : join(root, id,
|
|
444
|
+
async getTextFile(id, key2, file) {
|
|
445
|
+
const p = layout === "flat" ? join(root, file) : join(root, id, key2, file);
|
|
446
446
|
try {
|
|
447
447
|
return JSON.parse(await readFile(p, "utf-8"));
|
|
448
448
|
} catch {
|
|
@@ -459,9 +459,15 @@ async function writeExport(dir, r) {
|
|
|
459
459
|
}
|
|
460
460
|
|
|
461
461
|
// src/compare/classify.ts
|
|
462
|
+
var NUMERAL = /^[一二三四五六七八九十百]+$/;
|
|
463
|
+
var PLACEHOLDER = /無法顯示|无法显示/;
|
|
464
|
+
var NUM_CTX = /* @__PURE__ */ new Set([..."\u5377\u7B2C\u5E74\u6708\u65E5\u6B72\u5C81\u4E4B\u5341\u767E\u5343\u842C\u4E07\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D"]);
|
|
462
465
|
function classify(h, base, wiki, opts) {
|
|
463
466
|
if (h.i1 === h.i2) return "add";
|
|
464
467
|
if (h.j1 === h.j2) return opts.structEnd !== void 0 && h.i2 <= opts.structEnd ? "struct" : "omit";
|
|
468
|
+
const b = base.slice(h.i1, h.i2).join("");
|
|
469
|
+
const r = wiki.slice(h.j1, h.j2).join("");
|
|
470
|
+
if (b === "\u4E00" && r !== b && NUMERAL.test(r) && !NUM_CTX.has(base[h.i1 - 1] ?? "") || PLACEHOLDER.test(r)) return "struct";
|
|
465
471
|
const len = h.i2 - h.i1;
|
|
466
472
|
if (len === h.j2 - h.j1 && opts.isVariant) {
|
|
467
473
|
let all = true;
|
|
@@ -631,7 +637,7 @@ function cnNumber(s) {
|
|
|
631
637
|
return total + cur;
|
|
632
638
|
}
|
|
633
639
|
function volumeNo(label) {
|
|
634
|
-
const m =
|
|
640
|
+
const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
|
|
635
641
|
return m ? cnNumber(m[1]) : null;
|
|
636
642
|
}
|
|
637
643
|
function wikiFileOf(no, chapters) {
|
|
@@ -770,17 +776,103 @@ async function compareWhole(input) {
|
|
|
770
776
|
}
|
|
771
777
|
|
|
772
778
|
// src/compare/variants.ts
|
|
773
|
-
function buildVariantTest(table) {
|
|
779
|
+
function buildVariantTest(table, opts = {}) {
|
|
774
780
|
const set = /* @__PURE__ */ new Set();
|
|
775
|
-
|
|
776
|
-
const
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
+
const add = (text) => {
|
|
782
|
+
for (const line of text.split("\n")) {
|
|
783
|
+
const cp = Array.from(line);
|
|
784
|
+
if (cp.length !== 2) continue;
|
|
785
|
+
set.add(cp[0] + cp[1]);
|
|
786
|
+
set.add(cp[1] + cp[0]);
|
|
787
|
+
}
|
|
788
|
+
};
|
|
789
|
+
add(table.pairs);
|
|
790
|
+
if (table.simp && opts.simp !== false) add(table.simp);
|
|
781
791
|
return (a, b) => set.has(a + b);
|
|
782
792
|
}
|
|
783
793
|
|
|
794
|
+
// src/compare/variant-build.ts
|
|
795
|
+
var VARIANT_REL = /* @__PURE__ */ new Set(["variant", "variant_partial", "simp", "z"]);
|
|
796
|
+
var isOpen = (src) => src.startsWith("unihan") || src.startsWith("local:keben") || src.startsWith("yibai");
|
|
797
|
+
var TRUSTED_EXTRA = /* @__PURE__ */ new Set(["yitizi", "twedu", "dypytz", "hydzd", "cjkvi-simplified"]);
|
|
798
|
+
var isTrusted = (src) => isOpen(src) || TRUSTED_EXTRA.has(src);
|
|
799
|
+
var isStrong = (rel, src) => rel === "z" || src === "unihan:kSemanticVariant" || src === "unihan:kZVariant" || src === "twedu";
|
|
800
|
+
function isRareHan(ch) {
|
|
801
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
802
|
+
return cp > 65535 || cp >= 13312 && cp <= 19903;
|
|
803
|
+
}
|
|
804
|
+
var key = (a, b) => a < b ? a + b : b + a;
|
|
805
|
+
function buildVariantTable(edgesTsv, opts = {}) {
|
|
806
|
+
const mode = opts.mode ?? "collate";
|
|
807
|
+
const pickSrc = mode === "open" ? isOpen : isTrusted;
|
|
808
|
+
const yes = /* @__PURE__ */ new Set();
|
|
809
|
+
const strong = /* @__PURE__ */ new Set();
|
|
810
|
+
const loose = /* @__PURE__ */ new Set();
|
|
811
|
+
const no = /* @__PURE__ */ new Set();
|
|
812
|
+
const borrow = /* @__PURE__ */ new Set();
|
|
813
|
+
const used = /* @__PURE__ */ new Set();
|
|
814
|
+
const chars = /* @__PURE__ */ new Set();
|
|
815
|
+
for (const line of edgesTsv.split("\n")) {
|
|
816
|
+
if (!line) continue;
|
|
817
|
+
const [a, b, rel, , src = ""] = line.split(" ");
|
|
818
|
+
if (!a || !b || a === b || [...a].length !== 1 || [...b].length !== 1) continue;
|
|
819
|
+
const k = key(a, b);
|
|
820
|
+
chars.add(a);
|
|
821
|
+
chars.add(b);
|
|
822
|
+
if (rel === "distinct") no.add(k);
|
|
823
|
+
else if (rel === "borrow") borrow.add(k);
|
|
824
|
+
else if (VARIANT_REL.has(rel)) {
|
|
825
|
+
if (pickSrc(src)) {
|
|
826
|
+
yes.add(k);
|
|
827
|
+
used.add(src);
|
|
828
|
+
}
|
|
829
|
+
if (isStrong(rel, src)) strong.add(k);
|
|
830
|
+
if (rel !== "simp") loose.add(k);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
const out = /* @__PURE__ */ new Set();
|
|
834
|
+
for (const k of yes) if (!no.has(k) && !borrow.has(k)) out.add(k);
|
|
835
|
+
let simp;
|
|
836
|
+
if (mode === "collate") {
|
|
837
|
+
for (const k of strong) if (!no.has(k)) out.add(k);
|
|
838
|
+
for (const k of loose) {
|
|
839
|
+
if (no.has(k)) continue;
|
|
840
|
+
const [a, b] = [...k];
|
|
841
|
+
if (isRareHan(a) || isRareHan(b)) out.add(k);
|
|
842
|
+
}
|
|
843
|
+
if (opts.toSimplified) {
|
|
844
|
+
const groups = /* @__PURE__ */ new Map();
|
|
845
|
+
for (let cp = 19968; cp <= 40959; cp++) chars.add(String.fromCodePoint(cp));
|
|
846
|
+
for (const c of chars) {
|
|
847
|
+
const s = opts.toSimplified(c);
|
|
848
|
+
if ([...s].length !== 1) continue;
|
|
849
|
+
const g = groups.get(s) ?? [];
|
|
850
|
+
g.push(c);
|
|
851
|
+
groups.set(s, g);
|
|
852
|
+
}
|
|
853
|
+
const sp = /* @__PURE__ */ new Set();
|
|
854
|
+
for (const [s, g] of groups) {
|
|
855
|
+
const all = g.includes(s) ? g : [...g, s];
|
|
856
|
+
if (all.length < 2 || all.length > 12) continue;
|
|
857
|
+
for (let i = 0; i < all.length; i++)
|
|
858
|
+
for (let j = i + 1; j < all.length; j++) {
|
|
859
|
+
const k = key(all[i], all[j]);
|
|
860
|
+
if (!no.has(k) && !out.has(k)) sp.add(k);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
simp = [...sp].sort();
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
const table = {
|
|
867
|
+
version: opts.version ?? (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
|
|
868
|
+
mode,
|
|
869
|
+
sources: [...used].sort(),
|
|
870
|
+
pairs: [...out].sort().join("\n")
|
|
871
|
+
};
|
|
872
|
+
if (simp) table.simp = simp.join("\n");
|
|
873
|
+
return table;
|
|
874
|
+
}
|
|
875
|
+
|
|
784
876
|
// src/compare/report.ts
|
|
785
877
|
var CAT_TITLE = {
|
|
786
878
|
word: "\u7528\u5B57\u4E0D\u540C",
|
|
@@ -860,10 +952,14 @@ var HELP = `\u7528\u6CD5\uFF1A
|
|
|
860
952
|
[--glyph original|traditional|simplified] [--title \u4E66\u540D] [--id \u6761\u76EEid] [--out \u76EE\u5F55]
|
|
861
953
|
book-text-util compare --base <\u5E95\u672C.txt> --ref <\u5BF9\u7167\u672C.md> [--variants <glyph-variants.json>] [--json]
|
|
862
954
|
|
|
955
|
+
book-text-util variants --edges <open-guji-cv/config/hanzi/core/edges.tsv> [--mode collate|trusted|open] [--no-simp]
|
|
956
|
+
[--out glyph-variants.json]
|
|
957
|
+
|
|
863
958
|
book-text-util report --base-root <\u7248\u672C\u76EE\u5F55> --ref-root <\u5BF9\u7167\u7248\u672C\u76EE\u5F55> --chapters 001-005 [--variants <glyph-variants.json>]
|
|
864
959
|
[--title \u6807\u9898] [--base-label \u540D] [--ref-label \u540D] [--list word,add,omit,glyph] [--out \u62A5\u544A.md]
|
|
865
960
|
|
|
866
961
|
export\uFF1A\u8BFB\u7248\u672C\u76EE\u5F55\u91CC\u7684 index.json \u4E0E <\u518C>.char.json \u7B49\uFF0C\u751F\u6210 guji-markdown\uFF1B--glyph \u975E original \u9700\u8981\u5B89\u88C5 opencc-js \u4E0E book-index-ui\u3002
|
|
962
|
+
variants\uFF1A\u751F\u6210\u5BF9\u6821\u7528\u7684\u5F02\u4F53\u5B57\u8868\uFF08\u5224\u6CD5\u89C1 src/compare/variant-build.ts\uFF1Bcollate \u7684\u7E41\u7B80\u540C\u5F62\u4E00\u680F\u8981 opencc-js\uFF09\u3002
|
|
867
963
|
compare\uFF1A\u628A\u4E24\u4EFD\u6587\u672C\u53BB\u6807\u70B9\u540E\u505A\u5B57\u7EA7\u6BD4\u5BF9\uFF0C\u5217\u51FA\u6BCF\u5904\u5DEE\u5F02\u53CA\u5206\u7C7B\u3002`;
|
|
868
964
|
function die(msg) {
|
|
869
965
|
process.stderr.write(msg + "\n");
|
|
@@ -983,8 +1079,35 @@ async function cmdReport(argv) {
|
|
|
983
1079
|
`);
|
|
984
1080
|
} else process.stdout.write(md + "\n");
|
|
985
1081
|
}
|
|
1082
|
+
async function cmdVariants(argv) {
|
|
1083
|
+
const { values: v } = parseArgs({
|
|
1084
|
+
args: argv,
|
|
1085
|
+
options: { edges: { type: "string" }, mode: { type: "string", default: "collate" }, "no-simp": { type: "boolean", default: false }, out: { type: "string", default: "glyph-variants.json" } }
|
|
1086
|
+
});
|
|
1087
|
+
if (!v.edges) die("\u9700\u8981 --edges\n\n" + HELP);
|
|
1088
|
+
const mode = v.mode;
|
|
1089
|
+
if (!["open", "trusted", "collate"].includes(mode)) die(`--mode \u53EA\u8BA4 collate|trusted|open\uFF0C\u6536\u5230 ${mode}`);
|
|
1090
|
+
let toSimplified;
|
|
1091
|
+
if (mode === "collate" && !v["no-simp"]) {
|
|
1092
|
+
try {
|
|
1093
|
+
const { Converter } = await import(
|
|
1094
|
+
/* @vite-ignore */
|
|
1095
|
+
"opencc-js/t2cn"
|
|
1096
|
+
);
|
|
1097
|
+
toSimplified = Converter({ from: "t", to: "cn" });
|
|
1098
|
+
} catch (e) {
|
|
1099
|
+
die(`\u7E41\u7B80\u540C\u5F62\u4E00\u680F\u9700\u8981 opencc-js\uFF08${e.message}\uFF09\uFF1B\u4E0D\u8981\u8FD9\u4E00\u680F\u5C31\u52A0 --no-simp`);
|
|
1100
|
+
}
|
|
1101
|
+
}
|
|
1102
|
+
const table = buildVariantTable(await readFile2(v.edges, "utf-8"), { mode, toSimplified });
|
|
1103
|
+
await writeFile2(v.out, JSON.stringify(table));
|
|
1104
|
+
const n = (s) => s ? s.split("\n").length : 0;
|
|
1105
|
+
process.stdout.write(`${n(table.pairs)} \u5BF9\u5F02\u4F53\u3001${n(table.simp)} \u5BF9\u7E41\u7B80\u540C\u5F62 \u2192 ${v.out}\uFF08mode ${mode}\uFF0C\u6765\u6E90 ${table.sources.join("\u3001")}\uFF09
|
|
1106
|
+
`);
|
|
1107
|
+
}
|
|
986
1108
|
var [cmd, ...rest] = process.argv.slice(2);
|
|
987
1109
|
if (cmd === "export") await cmdExport(rest);
|
|
988
1110
|
else if (cmd === "compare") await cmdCompare(rest);
|
|
1111
|
+
else if (cmd === "variants") await cmdVariants(rest);
|
|
989
1112
|
else if (cmd === "report") await cmdReport(rest);
|
|
990
1113
|
else process.stdout.write(HELP + "\n");
|
package/dist/index.cjs
CHANGED
|
@@ -9,9 +9,9 @@ var __export = (target, all) => {
|
|
|
9
9
|
};
|
|
10
10
|
var __copyProps = (to, from, except, desc) => {
|
|
11
11
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let
|
|
13
|
-
if (!__hasOwnProp.call(to,
|
|
14
|
-
__defProp(to,
|
|
12
|
+
for (let key2 of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key2) && key2 !== except)
|
|
14
|
+
__defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable });
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
@@ -31,6 +31,7 @@ __export(index_exports, {
|
|
|
31
31
|
availableFormats: () => availableFormats,
|
|
32
32
|
buildExport: () => buildExport,
|
|
33
33
|
buildReport: () => buildReport,
|
|
34
|
+
buildVariantTable: () => buildVariantTable,
|
|
34
35
|
buildVariantTest: () => buildVariantTest,
|
|
35
36
|
cellLane: () => cellLane,
|
|
36
37
|
chapterChars: () => chapterChars,
|
|
@@ -49,6 +50,7 @@ __export(index_exports, {
|
|
|
49
50
|
figAlts: () => figAlts,
|
|
50
51
|
formatCmp: () => formatCmp,
|
|
51
52
|
frontMatter: () => frontMatter,
|
|
53
|
+
isRareHan: () => isRareHan,
|
|
52
54
|
loadVariantTest: () => loadVariantTest,
|
|
53
55
|
nextContinues: () => nextContinues,
|
|
54
56
|
normalizeVariants: () => normalizeVariants,
|
|
@@ -229,9 +231,9 @@ function cmpKey(a, b) {
|
|
|
229
231
|
for (let i = 1; i < 5; i++) if (a[i] !== b[i]) return a[i] ? 1 : -1;
|
|
230
232
|
return 0;
|
|
231
233
|
}
|
|
232
|
-
function resolveOffsets(pos, items,
|
|
234
|
+
function resolveOffsets(pos, items, key2) {
|
|
233
235
|
return items.map((it, i) => {
|
|
234
|
-
const a =
|
|
236
|
+
const a = key2(it);
|
|
235
237
|
const off = a === void 0 ? void 0 : pos.get(a);
|
|
236
238
|
if (off === void 0) throw new GujiFormatError(`\u7B2C ${i} \u6761\u7684\u683C\u4F4D ${String(a)} \u4E0D\u5728 char \u91CC`);
|
|
237
239
|
return off;
|
|
@@ -374,21 +376,21 @@ function availableFormats(ch) {
|
|
|
374
376
|
function safeFile(v) {
|
|
375
377
|
return typeof v === "string" && /^[\w.-]+$/.test(v) ? v : null;
|
|
376
378
|
}
|
|
377
|
-
async function getJson(source, id,
|
|
379
|
+
async function getJson(source, id, key2, file) {
|
|
378
380
|
try {
|
|
379
|
-
return await source.getTextFile(id,
|
|
381
|
+
return await source.getTextFile(id, key2, file) ?? null;
|
|
380
382
|
} catch {
|
|
381
383
|
return null;
|
|
382
384
|
}
|
|
383
385
|
}
|
|
384
|
-
async function getPreferred(source, id,
|
|
386
|
+
async function getPreferred(source, id, key2, slim, original) {
|
|
385
387
|
const s = safeFile(slim);
|
|
386
388
|
if (s) {
|
|
387
|
-
const v = await getJson(source, id,
|
|
389
|
+
const v = await getJson(source, id, key2, s);
|
|
388
390
|
if (v != null) return v;
|
|
389
391
|
}
|
|
390
392
|
const o = safeFile(original);
|
|
391
|
-
return o ? getJson(source, id,
|
|
393
|
+
return o ? getJson(source, id, key2, o) : null;
|
|
392
394
|
}
|
|
393
395
|
function glyphConverter(glyph2, converters) {
|
|
394
396
|
if (glyph2 === "original") return (s) => s;
|
|
@@ -397,14 +399,14 @@ function glyphConverter(glyph2, converters) {
|
|
|
397
399
|
}
|
|
398
400
|
async function buildExport(req, source, converters) {
|
|
399
401
|
const ch = req.chapter;
|
|
400
|
-
const { id, versionKey:
|
|
402
|
+
const { id, versionKey: key2 } = req;
|
|
401
403
|
const needPunct = req.format !== "lines";
|
|
402
404
|
const [char, punct, entity, fig] = await Promise.all([
|
|
403
|
-
getPreferred(source, id,
|
|
404
|
-
needPunct ? getPreferred(source, id,
|
|
405
|
+
getPreferred(source, id, key2, ch.reader_char_file, ch.char_file),
|
|
406
|
+
needPunct ? getPreferred(source, id, key2, ch.reader_punct_file, ch.punct_file) : null,
|
|
405
407
|
// 专名层取原文件:瘦身副本有 entity_id 时去掉了 href,而下载要原样写仓里的 book-index:// 链接
|
|
406
|
-
needPunct && safeFile(ch.entity_file) ? getJson(source, id,
|
|
407
|
-
safeFile(ch.fig_file) ? getJson(source, id,
|
|
408
|
+
needPunct && safeFile(ch.entity_file) ? getJson(source, id, key2, ch.entity_file) : null,
|
|
409
|
+
safeFile(ch.fig_file) ? getJson(source, id, key2, ch.fig_file) : null
|
|
408
410
|
]);
|
|
409
411
|
if (!char) throw new Error("\u672C\u518A\u6587\u5B57\u53D6\u4E0D\u5230");
|
|
410
412
|
if (needPunct && !punct) throw new Error("\u672C\u518A\u6A19\u9EDE\u53D6\u4E0D\u5230");
|
|
@@ -506,9 +508,15 @@ function createGlyphConverters(opts) {
|
|
|
506
508
|
|
|
507
509
|
// src/compare/classify.ts
|
|
508
510
|
var ALL_CATS = ["word", "add", "omit", "glyph", "struct"];
|
|
511
|
+
var NUMERAL = /^[一二三四五六七八九十百]+$/;
|
|
512
|
+
var PLACEHOLDER = /無法顯示|无法显示/;
|
|
513
|
+
var NUM_CTX = /* @__PURE__ */ new Set([..."\u5377\u7B2C\u5E74\u6708\u65E5\u6B72\u5C81\u4E4B\u5341\u767E\u5343\u842C\u4E07\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D"]);
|
|
509
514
|
function classify(h, base, wiki, opts) {
|
|
510
515
|
if (h.i1 === h.i2) return "add";
|
|
511
516
|
if (h.j1 === h.j2) return opts.structEnd !== void 0 && h.i2 <= opts.structEnd ? "struct" : "omit";
|
|
517
|
+
const b = base.slice(h.i1, h.i2).join("");
|
|
518
|
+
const r = wiki.slice(h.j1, h.j2).join("");
|
|
519
|
+
if (b === "\u4E00" && r !== b && NUMERAL.test(r) && !NUM_CTX.has(base[h.i1 - 1] ?? "") || PLACEHOLDER.test(r)) return "struct";
|
|
512
520
|
const len = h.i2 - h.i1;
|
|
513
521
|
if (len === h.j2 - h.j1 && opts.isVariant) {
|
|
514
522
|
let all = true;
|
|
@@ -678,7 +686,7 @@ function cnNumber(s) {
|
|
|
678
686
|
return total + cur;
|
|
679
687
|
}
|
|
680
688
|
function volumeNo(label) {
|
|
681
|
-
const m =
|
|
689
|
+
const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
|
|
682
690
|
return m ? cnNumber(m[1]) : null;
|
|
683
691
|
}
|
|
684
692
|
function wikiFileOf(no, chapters) {
|
|
@@ -688,7 +696,7 @@ function wikiFileOf(no, chapters) {
|
|
|
688
696
|
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
689
697
|
}
|
|
690
698
|
function volumeRangeOf(title) {
|
|
691
|
-
const m =
|
|
699
|
+
const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
|
|
692
700
|
if (!m) return null;
|
|
693
701
|
const a = cnNumber(m[1]);
|
|
694
702
|
const b = m[2] ? cnNumber(m[2]) : a;
|
|
@@ -848,7 +856,7 @@ async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
|
848
856
|
}
|
|
849
857
|
async function compareChapter(input) {
|
|
850
858
|
const range = input.volumeRange;
|
|
851
|
-
if (range && !input.headings.some((h) => h.level === 1)) {
|
|
859
|
+
if (range && !input.headings.some((h) => h.level === 1 && volumeNo(h.label) !== null)) {
|
|
852
860
|
const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
|
|
853
861
|
const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
|
|
854
862
|
const items2 = [];
|
|
@@ -873,14 +881,18 @@ async function compareWhole(input) {
|
|
|
873
881
|
}
|
|
874
882
|
|
|
875
883
|
// src/compare/variants.ts
|
|
876
|
-
function buildVariantTest(table) {
|
|
884
|
+
function buildVariantTest(table, opts = {}) {
|
|
877
885
|
const set = /* @__PURE__ */ new Set();
|
|
878
|
-
|
|
879
|
-
const
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
886
|
+
const add = (text) => {
|
|
887
|
+
for (const line of text.split("\n")) {
|
|
888
|
+
const cp = Array.from(line);
|
|
889
|
+
if (cp.length !== 2) continue;
|
|
890
|
+
set.add(cp[0] + cp[1]);
|
|
891
|
+
set.add(cp[1] + cp[0]);
|
|
892
|
+
}
|
|
893
|
+
};
|
|
894
|
+
add(table.pairs);
|
|
895
|
+
if (table.simp && opts.simp !== false) add(table.simp);
|
|
884
896
|
return (a, b) => set.has(a + b);
|
|
885
897
|
}
|
|
886
898
|
var loading = null;
|
|
@@ -963,10 +975,10 @@ function parseCmp(raw) {
|
|
|
963
975
|
const first = raw.split(",")[0].trim();
|
|
964
976
|
const dash = first.indexOf("-");
|
|
965
977
|
const id = dash < 0 ? first : first.slice(0, dash);
|
|
966
|
-
const
|
|
978
|
+
const key2 = dash < 0 ? void 0 : first.slice(dash + 1);
|
|
967
979
|
if (!ID_RE.test(id)) return null;
|
|
968
|
-
if (
|
|
969
|
-
return
|
|
980
|
+
if (key2 !== void 0 && !KEY_RE.test(key2)) return null;
|
|
981
|
+
return key2 ? { id, key: key2 } : { id };
|
|
970
982
|
}
|
|
971
983
|
function formatCmp(t) {
|
|
972
984
|
return t.key ? `${t.id}-${t.key}` : t.id;
|
|
@@ -1043,6 +1055,88 @@ function parseChapters(spec) {
|
|
|
1043
1055
|
}
|
|
1044
1056
|
return out;
|
|
1045
1057
|
}
|
|
1058
|
+
|
|
1059
|
+
// src/compare/variant-build.ts
|
|
1060
|
+
var VARIANT_REL = /* @__PURE__ */ new Set(["variant", "variant_partial", "simp", "z"]);
|
|
1061
|
+
var isOpen = (src) => src.startsWith("unihan") || src.startsWith("local:keben") || src.startsWith("yibai");
|
|
1062
|
+
var TRUSTED_EXTRA = /* @__PURE__ */ new Set(["yitizi", "twedu", "dypytz", "hydzd", "cjkvi-simplified"]);
|
|
1063
|
+
var isTrusted = (src) => isOpen(src) || TRUSTED_EXTRA.has(src);
|
|
1064
|
+
var isStrong = (rel, src) => rel === "z" || src === "unihan:kSemanticVariant" || src === "unihan:kZVariant" || src === "twedu";
|
|
1065
|
+
function isRareHan(ch) {
|
|
1066
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
1067
|
+
return cp > 65535 || cp >= 13312 && cp <= 19903;
|
|
1068
|
+
}
|
|
1069
|
+
var key = (a, b) => a < b ? a + b : b + a;
|
|
1070
|
+
function buildVariantTable(edgesTsv, opts = {}) {
|
|
1071
|
+
const mode = opts.mode ?? "collate";
|
|
1072
|
+
const pickSrc = mode === "open" ? isOpen : isTrusted;
|
|
1073
|
+
const yes = /* @__PURE__ */ new Set();
|
|
1074
|
+
const strong = /* @__PURE__ */ new Set();
|
|
1075
|
+
const loose = /* @__PURE__ */ new Set();
|
|
1076
|
+
const no = /* @__PURE__ */ new Set();
|
|
1077
|
+
const borrow = /* @__PURE__ */ new Set();
|
|
1078
|
+
const used = /* @__PURE__ */ new Set();
|
|
1079
|
+
const chars = /* @__PURE__ */ new Set();
|
|
1080
|
+
for (const line of edgesTsv.split("\n")) {
|
|
1081
|
+
if (!line) continue;
|
|
1082
|
+
const [a, b, rel, , src = ""] = line.split(" ");
|
|
1083
|
+
if (!a || !b || a === b || [...a].length !== 1 || [...b].length !== 1) continue;
|
|
1084
|
+
const k = key(a, b);
|
|
1085
|
+
chars.add(a);
|
|
1086
|
+
chars.add(b);
|
|
1087
|
+
if (rel === "distinct") no.add(k);
|
|
1088
|
+
else if (rel === "borrow") borrow.add(k);
|
|
1089
|
+
else if (VARIANT_REL.has(rel)) {
|
|
1090
|
+
if (pickSrc(src)) {
|
|
1091
|
+
yes.add(k);
|
|
1092
|
+
used.add(src);
|
|
1093
|
+
}
|
|
1094
|
+
if (isStrong(rel, src)) strong.add(k);
|
|
1095
|
+
if (rel !== "simp") loose.add(k);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
const out = /* @__PURE__ */ new Set();
|
|
1099
|
+
for (const k of yes) if (!no.has(k) && !borrow.has(k)) out.add(k);
|
|
1100
|
+
let simp;
|
|
1101
|
+
if (mode === "collate") {
|
|
1102
|
+
for (const k of strong) if (!no.has(k)) out.add(k);
|
|
1103
|
+
for (const k of loose) {
|
|
1104
|
+
if (no.has(k)) continue;
|
|
1105
|
+
const [a, b] = [...k];
|
|
1106
|
+
if (isRareHan(a) || isRareHan(b)) out.add(k);
|
|
1107
|
+
}
|
|
1108
|
+
if (opts.toSimplified) {
|
|
1109
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1110
|
+
for (let cp = 19968; cp <= 40959; cp++) chars.add(String.fromCodePoint(cp));
|
|
1111
|
+
for (const c of chars) {
|
|
1112
|
+
const s = opts.toSimplified(c);
|
|
1113
|
+
if ([...s].length !== 1) continue;
|
|
1114
|
+
const g = groups.get(s) ?? [];
|
|
1115
|
+
g.push(c);
|
|
1116
|
+
groups.set(s, g);
|
|
1117
|
+
}
|
|
1118
|
+
const sp = /* @__PURE__ */ new Set();
|
|
1119
|
+
for (const [s, g] of groups) {
|
|
1120
|
+
const all = g.includes(s) ? g : [...g, s];
|
|
1121
|
+
if (all.length < 2 || all.length > 12) continue;
|
|
1122
|
+
for (let i = 0; i < all.length; i++)
|
|
1123
|
+
for (let j = i + 1; j < all.length; j++) {
|
|
1124
|
+
const k = key(all[i], all[j]);
|
|
1125
|
+
if (!no.has(k) && !out.has(k)) sp.add(k);
|
|
1126
|
+
}
|
|
1127
|
+
}
|
|
1128
|
+
simp = [...sp].sort();
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
const table = {
|
|
1132
|
+
version: opts.version ?? (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
|
|
1133
|
+
mode,
|
|
1134
|
+
sources: [...used].sort(),
|
|
1135
|
+
pairs: [...out].sort().join("\n")
|
|
1136
|
+
};
|
|
1137
|
+
if (simp) table.simp = simp.join("\n");
|
|
1138
|
+
return table;
|
|
1139
|
+
}
|
|
1046
1140
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1047
1141
|
0 && (module.exports = {
|
|
1048
1142
|
ALL_CATS,
|
|
@@ -1056,6 +1150,7 @@ function parseChapters(spec) {
|
|
|
1056
1150
|
availableFormats,
|
|
1057
1151
|
buildExport,
|
|
1058
1152
|
buildReport,
|
|
1153
|
+
buildVariantTable,
|
|
1059
1154
|
buildVariantTest,
|
|
1060
1155
|
cellLane,
|
|
1061
1156
|
chapterChars,
|
|
@@ -1074,6 +1169,7 @@ function parseChapters(spec) {
|
|
|
1074
1169
|
figAlts,
|
|
1075
1170
|
formatCmp,
|
|
1076
1171
|
frontMatter,
|
|
1172
|
+
isRareHan,
|
|
1077
1173
|
loadVariantTest,
|
|
1078
1174
|
nextContinues,
|
|
1079
1175
|
normalizeVariants,
|
package/dist/index.d.cts
CHANGED
|
@@ -167,11 +167,6 @@ type DiffCat = 'glyph' | 'word' | 'add' | 'omit' | 'struct';
|
|
|
167
167
|
declare const ALL_CATS: readonly DiffCat[];
|
|
168
168
|
/** 判定两个字是不是异体字关系:由 glyph-variants.json 加载后给出(见 variants.ts);null=表还没载入,按「不是」算 */
|
|
169
169
|
type VariantTest = ((a: string, b: string) => boolean) | null;
|
|
170
|
-
/**
|
|
171
|
-
* 给一处差异定类别。
|
|
172
|
-
* - 纯增:add;纯删:落在「卷端署题区」(structEnd 之前)算 struct,否则 omit;
|
|
173
|
-
* - 等长替换且逐字都是异体:glyph;其余(含通假,按/案)word。
|
|
174
|
-
*/
|
|
175
170
|
declare function classify(h: Hunk, base: readonly string[], wiki: readonly string[], opts: {
|
|
176
171
|
isVariant: VariantTest;
|
|
177
172
|
structEnd?: number;
|
|
@@ -393,9 +388,14 @@ interface VariantTable {
|
|
|
393
388
|
version: string;
|
|
394
389
|
sources: string[];
|
|
395
390
|
pairs: string;
|
|
391
|
+
/** 仅「转简后相同」的字对(collate 表才有,见 variant-build.ts);默认也当异体 */
|
|
392
|
+
simp?: string;
|
|
393
|
+
mode?: string;
|
|
396
394
|
}
|
|
397
395
|
/** 由表建判定函数(双向);两字相同视为同字,不在这里处理 */
|
|
398
|
-
declare function buildVariantTest(table: Pick<VariantTable, 'pairs'
|
|
396
|
+
declare function buildVariantTest(table: Pick<VariantTable, 'pairs' | 'simp'>, opts?: {
|
|
397
|
+
simp?: boolean;
|
|
398
|
+
}): (a: string, b: string) => boolean;
|
|
399
399
|
/** 取表并建判定函数;取不到返回 null(对校照常,只是异体字都归入「用字不同」)。只取一次 */
|
|
400
400
|
declare function loadVariantTest(url?: string): Promise<((a: string, b: string) => boolean) | null>;
|
|
401
401
|
|
|
@@ -437,4 +437,38 @@ declare function buildReport(chapters: readonly ReportChapter[], opts: ReportOpt
|
|
|
437
437
|
/** 「001-005」「001,003,010-012」→ ['001','002',…];保持位数 */
|
|
438
438
|
declare function parseChapters(spec: string): string[];
|
|
439
439
|
|
|
440
|
-
|
|
440
|
+
/**
|
|
441
|
+
* 从 open-guji-cv 的 config/hanzi/core/edges.tsv 生成对校用的异体字表(glyph-variants.json)。
|
|
442
|
+
* 纯函数,不读文件:调用方把 edges.tsv 的文本和(可选的)繁→简转换函数传进来。命令行见 `book-text-util variants`。
|
|
443
|
+
*
|
|
444
|
+
* 判法分三种 mode:
|
|
445
|
+
* - open/trusted:与网站原先的 scripts/build-glyph-variants.mjs 相同。两字之间有 variant/variant_partial/simp/z 边,
|
|
446
|
+
* 而且来源在 open(Unihan、本项目自有、yi-bai)或 trusted(再加 yitizi、twedu、dypytz、hydzd、cjkvi-simplified)之内;
|
|
447
|
+
* 有 distinct(人审定永不并)或 borrow(通假)边的不算。
|
|
448
|
+
* - collate(默认,2026-11-14 殿本参校查漏用过、用户认可):在 trusted 之上再加三条,
|
|
449
|
+
* 1. 强证据压过通假:Unihan kSemanticVariant/kZVariant 或教育部异体字字典(twedu)认定是异体的,即使另有 borrow 边也算异体
|
|
450
|
+
* (辨/辯、奕/弈、筭/算、寖/浸);distinct 仍然一票否决。
|
|
451
|
+
* 2. 僻字放宽:两字中有一个是扩展区字(CJK 扩展 A 或 BMP 以外),任何来源的 variant 边都算(𠉀/候、𭧂/暴、𨒪/速)。
|
|
452
|
+
* 常用字之间只靠宽松来源(zi:link、charcomp 等)的不算,如 令/命、壁/璧、宇/字 都留作「用字不同」。
|
|
453
|
+
* 3. 繁简同形:给了 toSimplified 时,转简后相同的两个字也算(範/范、乾/幹、閒/閑、捜/蒐),放在表的 `simp` 栏,
|
|
454
|
+
* 用的时候可以选择要不要并进异体(对照本是简繁转换来的,比如维基文库,就该并)。
|
|
455
|
+
*/
|
|
456
|
+
type VariantMode = 'open' | 'trusted' | 'collate';
|
|
457
|
+
interface BuiltVariantTable {
|
|
458
|
+
version: string;
|
|
459
|
+
mode: VariantMode;
|
|
460
|
+
sources: string[];
|
|
461
|
+
/** 每行一对,两个码点;判为异体 */
|
|
462
|
+
pairs: string;
|
|
463
|
+
/** 每行一对;仅「转简后相同」(collate 且给了 toSimplified 时才有) */
|
|
464
|
+
simp?: string;
|
|
465
|
+
}
|
|
466
|
+
/** 扩展区字:CJK 扩展 A(U+3400–4DBF)或 BMP 以外 */
|
|
467
|
+
declare function isRareHan(ch: string): boolean;
|
|
468
|
+
declare function buildVariantTable(edgesTsv: string, opts?: {
|
|
469
|
+
mode?: VariantMode;
|
|
470
|
+
toSimplified?: (s: string) => string;
|
|
471
|
+
version?: string;
|
|
472
|
+
}): BuiltVariantTable;
|
|
473
|
+
|
|
474
|
+
export { ALL_CATS, type BaseCell, type BuiltVariantTable, type CharCell, type CharColumn, type CharDoc, type CharEq, type CharPage, type CompareInput, type CompareItem, type CompareResult, type CompareTarget, type DiffCat, type EntityDoc, type EntityItem, type EntityMode, type FigDoc, GlyphConverters, GujiFormatError, type Heading, type Hunk, type IndexType, MAX_EDIT_DISTANCE, type PunctDoc, type PunctItem, type ReportChapter, type ReportOptions, type RunError, type RunInput, SOLO_ATTR, type SegResult, type SegStatus, type VariantMode, type VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTable, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, isRareHan, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
|
package/dist/index.d.ts
CHANGED
|
@@ -167,11 +167,6 @@ type DiffCat = 'glyph' | 'word' | 'add' | 'omit' | 'struct';
|
|
|
167
167
|
declare const ALL_CATS: readonly DiffCat[];
|
|
168
168
|
/** 判定两个字是不是异体字关系:由 glyph-variants.json 加载后给出(见 variants.ts);null=表还没载入,按「不是」算 */
|
|
169
169
|
type VariantTest = ((a: string, b: string) => boolean) | null;
|
|
170
|
-
/**
|
|
171
|
-
* 给一处差异定类别。
|
|
172
|
-
* - 纯增:add;纯删:落在「卷端署题区」(structEnd 之前)算 struct,否则 omit;
|
|
173
|
-
* - 等长替换且逐字都是异体:glyph;其余(含通假,按/案)word。
|
|
174
|
-
*/
|
|
175
170
|
declare function classify(h: Hunk, base: readonly string[], wiki: readonly string[], opts: {
|
|
176
171
|
isVariant: VariantTest;
|
|
177
172
|
structEnd?: number;
|
|
@@ -393,9 +388,14 @@ interface VariantTable {
|
|
|
393
388
|
version: string;
|
|
394
389
|
sources: string[];
|
|
395
390
|
pairs: string;
|
|
391
|
+
/** 仅「转简后相同」的字对(collate 表才有,见 variant-build.ts);默认也当异体 */
|
|
392
|
+
simp?: string;
|
|
393
|
+
mode?: string;
|
|
396
394
|
}
|
|
397
395
|
/** 由表建判定函数(双向);两字相同视为同字,不在这里处理 */
|
|
398
|
-
declare function buildVariantTest(table: Pick<VariantTable, 'pairs'
|
|
396
|
+
declare function buildVariantTest(table: Pick<VariantTable, 'pairs' | 'simp'>, opts?: {
|
|
397
|
+
simp?: boolean;
|
|
398
|
+
}): (a: string, b: string) => boolean;
|
|
399
399
|
/** 取表并建判定函数;取不到返回 null(对校照常,只是异体字都归入「用字不同」)。只取一次 */
|
|
400
400
|
declare function loadVariantTest(url?: string): Promise<((a: string, b: string) => boolean) | null>;
|
|
401
401
|
|
|
@@ -437,4 +437,38 @@ declare function buildReport(chapters: readonly ReportChapter[], opts: ReportOpt
|
|
|
437
437
|
/** 「001-005」「001,003,010-012」→ ['001','002',…];保持位数 */
|
|
438
438
|
declare function parseChapters(spec: string): string[];
|
|
439
439
|
|
|
440
|
-
|
|
440
|
+
/**
|
|
441
|
+
* 从 open-guji-cv 的 config/hanzi/core/edges.tsv 生成对校用的异体字表(glyph-variants.json)。
|
|
442
|
+
* 纯函数,不读文件:调用方把 edges.tsv 的文本和(可选的)繁→简转换函数传进来。命令行见 `book-text-util variants`。
|
|
443
|
+
*
|
|
444
|
+
* 判法分三种 mode:
|
|
445
|
+
* - open/trusted:与网站原先的 scripts/build-glyph-variants.mjs 相同。两字之间有 variant/variant_partial/simp/z 边,
|
|
446
|
+
* 而且来源在 open(Unihan、本项目自有、yi-bai)或 trusted(再加 yitizi、twedu、dypytz、hydzd、cjkvi-simplified)之内;
|
|
447
|
+
* 有 distinct(人审定永不并)或 borrow(通假)边的不算。
|
|
448
|
+
* - collate(默认,2026-11-14 殿本参校查漏用过、用户认可):在 trusted 之上再加三条,
|
|
449
|
+
* 1. 强证据压过通假:Unihan kSemanticVariant/kZVariant 或教育部异体字字典(twedu)认定是异体的,即使另有 borrow 边也算异体
|
|
450
|
+
* (辨/辯、奕/弈、筭/算、寖/浸);distinct 仍然一票否决。
|
|
451
|
+
* 2. 僻字放宽:两字中有一个是扩展区字(CJK 扩展 A 或 BMP 以外),任何来源的 variant 边都算(𠉀/候、𭧂/暴、𨒪/速)。
|
|
452
|
+
* 常用字之间只靠宽松来源(zi:link、charcomp 等)的不算,如 令/命、壁/璧、宇/字 都留作「用字不同」。
|
|
453
|
+
* 3. 繁简同形:给了 toSimplified 时,转简后相同的两个字也算(範/范、乾/幹、閒/閑、捜/蒐),放在表的 `simp` 栏,
|
|
454
|
+
* 用的时候可以选择要不要并进异体(对照本是简繁转换来的,比如维基文库,就该并)。
|
|
455
|
+
*/
|
|
456
|
+
type VariantMode = 'open' | 'trusted' | 'collate';
|
|
457
|
+
interface BuiltVariantTable {
|
|
458
|
+
version: string;
|
|
459
|
+
mode: VariantMode;
|
|
460
|
+
sources: string[];
|
|
461
|
+
/** 每行一对,两个码点;判为异体 */
|
|
462
|
+
pairs: string;
|
|
463
|
+
/** 每行一对;仅「转简后相同」(collate 且给了 toSimplified 时才有) */
|
|
464
|
+
simp?: string;
|
|
465
|
+
}
|
|
466
|
+
/** 扩展区字:CJK 扩展 A(U+3400–4DBF)或 BMP 以外 */
|
|
467
|
+
declare function isRareHan(ch: string): boolean;
|
|
468
|
+
declare function buildVariantTable(edgesTsv: string, opts?: {
|
|
469
|
+
mode?: VariantMode;
|
|
470
|
+
toSimplified?: (s: string) => string;
|
|
471
|
+
version?: string;
|
|
472
|
+
}): BuiltVariantTable;
|
|
473
|
+
|
|
474
|
+
export { ALL_CATS, type BaseCell, type BuiltVariantTable, type CharCell, type CharColumn, type CharDoc, type CharEq, type CharPage, type CompareInput, type CompareItem, type CompareResult, type CompareTarget, type DiffCat, type EntityDoc, type EntityItem, type EntityMode, type FigDoc, GlyphConverters, GujiFormatError, type Heading, type Hunk, type IndexType, MAX_EDIT_DISTANCE, type PunctDoc, type PunctItem, type ReportChapter, type ReportOptions, type RunError, type RunInput, SOLO_ATTR, type SegResult, type SegStatus, type VariantMode, type VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTable, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, isRareHan, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
|
package/dist/index.js
CHANGED
|
@@ -160,9 +160,9 @@ function cmpKey(a, b) {
|
|
|
160
160
|
for (let i = 1; i < 5; i++) if (a[i] !== b[i]) return a[i] ? 1 : -1;
|
|
161
161
|
return 0;
|
|
162
162
|
}
|
|
163
|
-
function resolveOffsets(pos, items,
|
|
163
|
+
function resolveOffsets(pos, items, key2) {
|
|
164
164
|
return items.map((it, i) => {
|
|
165
|
-
const a =
|
|
165
|
+
const a = key2(it);
|
|
166
166
|
const off = a === void 0 ? void 0 : pos.get(a);
|
|
167
167
|
if (off === void 0) throw new GujiFormatError(`\u7B2C ${i} \u6761\u7684\u683C\u4F4D ${String(a)} \u4E0D\u5728 char \u91CC`);
|
|
168
168
|
return off;
|
|
@@ -305,21 +305,21 @@ function availableFormats(ch) {
|
|
|
305
305
|
function safeFile(v) {
|
|
306
306
|
return typeof v === "string" && /^[\w.-]+$/.test(v) ? v : null;
|
|
307
307
|
}
|
|
308
|
-
async function getJson(source, id,
|
|
308
|
+
async function getJson(source, id, key2, file) {
|
|
309
309
|
try {
|
|
310
|
-
return await source.getTextFile(id,
|
|
310
|
+
return await source.getTextFile(id, key2, file) ?? null;
|
|
311
311
|
} catch {
|
|
312
312
|
return null;
|
|
313
313
|
}
|
|
314
314
|
}
|
|
315
|
-
async function getPreferred(source, id,
|
|
315
|
+
async function getPreferred(source, id, key2, slim, original) {
|
|
316
316
|
const s = safeFile(slim);
|
|
317
317
|
if (s) {
|
|
318
|
-
const v = await getJson(source, id,
|
|
318
|
+
const v = await getJson(source, id, key2, s);
|
|
319
319
|
if (v != null) return v;
|
|
320
320
|
}
|
|
321
321
|
const o = safeFile(original);
|
|
322
|
-
return o ? getJson(source, id,
|
|
322
|
+
return o ? getJson(source, id, key2, o) : null;
|
|
323
323
|
}
|
|
324
324
|
function glyphConverter(glyph2, converters) {
|
|
325
325
|
if (glyph2 === "original") return (s) => s;
|
|
@@ -328,14 +328,14 @@ function glyphConverter(glyph2, converters) {
|
|
|
328
328
|
}
|
|
329
329
|
async function buildExport(req, source, converters) {
|
|
330
330
|
const ch = req.chapter;
|
|
331
|
-
const { id, versionKey:
|
|
331
|
+
const { id, versionKey: key2 } = req;
|
|
332
332
|
const needPunct = req.format !== "lines";
|
|
333
333
|
const [char, punct, entity, fig] = await Promise.all([
|
|
334
|
-
getPreferred(source, id,
|
|
335
|
-
needPunct ? getPreferred(source, id,
|
|
334
|
+
getPreferred(source, id, key2, ch.reader_char_file, ch.char_file),
|
|
335
|
+
needPunct ? getPreferred(source, id, key2, ch.reader_punct_file, ch.punct_file) : null,
|
|
336
336
|
// 专名层取原文件:瘦身副本有 entity_id 时去掉了 href,而下载要原样写仓里的 book-index:// 链接
|
|
337
|
-
needPunct && safeFile(ch.entity_file) ? getJson(source, id,
|
|
338
|
-
safeFile(ch.fig_file) ? getJson(source, id,
|
|
337
|
+
needPunct && safeFile(ch.entity_file) ? getJson(source, id, key2, ch.entity_file) : null,
|
|
338
|
+
safeFile(ch.fig_file) ? getJson(source, id, key2, ch.fig_file) : null
|
|
339
339
|
]);
|
|
340
340
|
if (!char) throw new Error("\u672C\u518A\u6587\u5B57\u53D6\u4E0D\u5230");
|
|
341
341
|
if (needPunct && !punct) throw new Error("\u672C\u518A\u6A19\u9EDE\u53D6\u4E0D\u5230");
|
|
@@ -437,9 +437,15 @@ function createGlyphConverters(opts) {
|
|
|
437
437
|
|
|
438
438
|
// src/compare/classify.ts
|
|
439
439
|
var ALL_CATS = ["word", "add", "omit", "glyph", "struct"];
|
|
440
|
+
var NUMERAL = /^[一二三四五六七八九十百]+$/;
|
|
441
|
+
var PLACEHOLDER = /無法顯示|无法显示/;
|
|
442
|
+
var NUM_CTX = /* @__PURE__ */ new Set([..."\u5377\u7B2C\u5E74\u6708\u65E5\u6B72\u5C81\u4E4B\u5341\u767E\u5343\u842C\u4E07\u4E8C\u4E09\u56DB\u4E94\u516D\u4E03\u516B\u4E5D"]);
|
|
440
443
|
function classify(h, base, wiki, opts) {
|
|
441
444
|
if (h.i1 === h.i2) return "add";
|
|
442
445
|
if (h.j1 === h.j2) return opts.structEnd !== void 0 && h.i2 <= opts.structEnd ? "struct" : "omit";
|
|
446
|
+
const b = base.slice(h.i1, h.i2).join("");
|
|
447
|
+
const r = wiki.slice(h.j1, h.j2).join("");
|
|
448
|
+
if (b === "\u4E00" && r !== b && NUMERAL.test(r) && !NUM_CTX.has(base[h.i1 - 1] ?? "") || PLACEHOLDER.test(r)) return "struct";
|
|
443
449
|
const len = h.i2 - h.i1;
|
|
444
450
|
if (len === h.j2 - h.j1 && opts.isVariant) {
|
|
445
451
|
let all = true;
|
|
@@ -609,7 +615,7 @@ function cnNumber(s) {
|
|
|
609
615
|
return total + cur;
|
|
610
616
|
}
|
|
611
617
|
function volumeNo(label) {
|
|
612
|
-
const m =
|
|
618
|
+
const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
|
|
613
619
|
return m ? cnNumber(m[1]) : null;
|
|
614
620
|
}
|
|
615
621
|
function wikiFileOf(no, chapters) {
|
|
@@ -619,7 +625,7 @@ function wikiFileOf(no, chapters) {
|
|
|
619
625
|
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
620
626
|
}
|
|
621
627
|
function volumeRangeOf(title) {
|
|
622
|
-
const m =
|
|
628
|
+
const m = /(?:^|[、,,;;\s])卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
|
|
623
629
|
if (!m) return null;
|
|
624
630
|
const a = cnNumber(m[1]);
|
|
625
631
|
const b = m[2] ? cnNumber(m[2]) : a;
|
|
@@ -779,7 +785,7 @@ async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
|
779
785
|
}
|
|
780
786
|
async function compareChapter(input) {
|
|
781
787
|
const range = input.volumeRange;
|
|
782
|
-
if (range && !input.headings.some((h) => h.level === 1)) {
|
|
788
|
+
if (range && !input.headings.some((h) => h.level === 1 && volumeNo(h.label) !== null)) {
|
|
783
789
|
const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
|
|
784
790
|
const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
|
|
785
791
|
const items2 = [];
|
|
@@ -804,14 +810,18 @@ async function compareWhole(input) {
|
|
|
804
810
|
}
|
|
805
811
|
|
|
806
812
|
// src/compare/variants.ts
|
|
807
|
-
function buildVariantTest(table) {
|
|
813
|
+
function buildVariantTest(table, opts = {}) {
|
|
808
814
|
const set = /* @__PURE__ */ new Set();
|
|
809
|
-
|
|
810
|
-
const
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
+
const add = (text) => {
|
|
816
|
+
for (const line of text.split("\n")) {
|
|
817
|
+
const cp = Array.from(line);
|
|
818
|
+
if (cp.length !== 2) continue;
|
|
819
|
+
set.add(cp[0] + cp[1]);
|
|
820
|
+
set.add(cp[1] + cp[0]);
|
|
821
|
+
}
|
|
822
|
+
};
|
|
823
|
+
add(table.pairs);
|
|
824
|
+
if (table.simp && opts.simp !== false) add(table.simp);
|
|
815
825
|
return (a, b) => set.has(a + b);
|
|
816
826
|
}
|
|
817
827
|
var loading = null;
|
|
@@ -894,10 +904,10 @@ function parseCmp(raw) {
|
|
|
894
904
|
const first = raw.split(",")[0].trim();
|
|
895
905
|
const dash = first.indexOf("-");
|
|
896
906
|
const id = dash < 0 ? first : first.slice(0, dash);
|
|
897
|
-
const
|
|
907
|
+
const key2 = dash < 0 ? void 0 : first.slice(dash + 1);
|
|
898
908
|
if (!ID_RE.test(id)) return null;
|
|
899
|
-
if (
|
|
900
|
-
return
|
|
909
|
+
if (key2 !== void 0 && !KEY_RE.test(key2)) return null;
|
|
910
|
+
return key2 ? { id, key: key2 } : { id };
|
|
901
911
|
}
|
|
902
912
|
function formatCmp(t) {
|
|
903
913
|
return t.key ? `${t.id}-${t.key}` : t.id;
|
|
@@ -974,6 +984,88 @@ function parseChapters(spec) {
|
|
|
974
984
|
}
|
|
975
985
|
return out;
|
|
976
986
|
}
|
|
987
|
+
|
|
988
|
+
// src/compare/variant-build.ts
|
|
989
|
+
var VARIANT_REL = /* @__PURE__ */ new Set(["variant", "variant_partial", "simp", "z"]);
|
|
990
|
+
var isOpen = (src) => src.startsWith("unihan") || src.startsWith("local:keben") || src.startsWith("yibai");
|
|
991
|
+
var TRUSTED_EXTRA = /* @__PURE__ */ new Set(["yitizi", "twedu", "dypytz", "hydzd", "cjkvi-simplified"]);
|
|
992
|
+
var isTrusted = (src) => isOpen(src) || TRUSTED_EXTRA.has(src);
|
|
993
|
+
var isStrong = (rel, src) => rel === "z" || src === "unihan:kSemanticVariant" || src === "unihan:kZVariant" || src === "twedu";
|
|
994
|
+
function isRareHan(ch) {
|
|
995
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
996
|
+
return cp > 65535 || cp >= 13312 && cp <= 19903;
|
|
997
|
+
}
|
|
998
|
+
var key = (a, b) => a < b ? a + b : b + a;
|
|
999
|
+
function buildVariantTable(edgesTsv, opts = {}) {
|
|
1000
|
+
const mode = opts.mode ?? "collate";
|
|
1001
|
+
const pickSrc = mode === "open" ? isOpen : isTrusted;
|
|
1002
|
+
const yes = /* @__PURE__ */ new Set();
|
|
1003
|
+
const strong = /* @__PURE__ */ new Set();
|
|
1004
|
+
const loose = /* @__PURE__ */ new Set();
|
|
1005
|
+
const no = /* @__PURE__ */ new Set();
|
|
1006
|
+
const borrow = /* @__PURE__ */ new Set();
|
|
1007
|
+
const used = /* @__PURE__ */ new Set();
|
|
1008
|
+
const chars = /* @__PURE__ */ new Set();
|
|
1009
|
+
for (const line of edgesTsv.split("\n")) {
|
|
1010
|
+
if (!line) continue;
|
|
1011
|
+
const [a, b, rel, , src = ""] = line.split(" ");
|
|
1012
|
+
if (!a || !b || a === b || [...a].length !== 1 || [...b].length !== 1) continue;
|
|
1013
|
+
const k = key(a, b);
|
|
1014
|
+
chars.add(a);
|
|
1015
|
+
chars.add(b);
|
|
1016
|
+
if (rel === "distinct") no.add(k);
|
|
1017
|
+
else if (rel === "borrow") borrow.add(k);
|
|
1018
|
+
else if (VARIANT_REL.has(rel)) {
|
|
1019
|
+
if (pickSrc(src)) {
|
|
1020
|
+
yes.add(k);
|
|
1021
|
+
used.add(src);
|
|
1022
|
+
}
|
|
1023
|
+
if (isStrong(rel, src)) strong.add(k);
|
|
1024
|
+
if (rel !== "simp") loose.add(k);
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
const out = /* @__PURE__ */ new Set();
|
|
1028
|
+
for (const k of yes) if (!no.has(k) && !borrow.has(k)) out.add(k);
|
|
1029
|
+
let simp;
|
|
1030
|
+
if (mode === "collate") {
|
|
1031
|
+
for (const k of strong) if (!no.has(k)) out.add(k);
|
|
1032
|
+
for (const k of loose) {
|
|
1033
|
+
if (no.has(k)) continue;
|
|
1034
|
+
const [a, b] = [...k];
|
|
1035
|
+
if (isRareHan(a) || isRareHan(b)) out.add(k);
|
|
1036
|
+
}
|
|
1037
|
+
if (opts.toSimplified) {
|
|
1038
|
+
const groups = /* @__PURE__ */ new Map();
|
|
1039
|
+
for (let cp = 19968; cp <= 40959; cp++) chars.add(String.fromCodePoint(cp));
|
|
1040
|
+
for (const c of chars) {
|
|
1041
|
+
const s = opts.toSimplified(c);
|
|
1042
|
+
if ([...s].length !== 1) continue;
|
|
1043
|
+
const g = groups.get(s) ?? [];
|
|
1044
|
+
g.push(c);
|
|
1045
|
+
groups.set(s, g);
|
|
1046
|
+
}
|
|
1047
|
+
const sp = /* @__PURE__ */ new Set();
|
|
1048
|
+
for (const [s, g] of groups) {
|
|
1049
|
+
const all = g.includes(s) ? g : [...g, s];
|
|
1050
|
+
if (all.length < 2 || all.length > 12) continue;
|
|
1051
|
+
for (let i = 0; i < all.length; i++)
|
|
1052
|
+
for (let j = i + 1; j < all.length; j++) {
|
|
1053
|
+
const k = key(all[i], all[j]);
|
|
1054
|
+
if (!no.has(k) && !out.has(k)) sp.add(k);
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
simp = [...sp].sort();
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
const table = {
|
|
1061
|
+
version: opts.version ?? (/* @__PURE__ */ new Date()).toISOString().slice(0, 10),
|
|
1062
|
+
mode,
|
|
1063
|
+
sources: [...used].sort(),
|
|
1064
|
+
pairs: [...out].sort().join("\n")
|
|
1065
|
+
};
|
|
1066
|
+
if (simp) table.simp = simp.join("\n");
|
|
1067
|
+
return table;
|
|
1068
|
+
}
|
|
977
1069
|
export {
|
|
978
1070
|
ALL_CATS,
|
|
979
1071
|
FORMAT_LABEL,
|
|
@@ -986,6 +1078,7 @@ export {
|
|
|
986
1078
|
availableFormats,
|
|
987
1079
|
buildExport,
|
|
988
1080
|
buildReport,
|
|
1081
|
+
buildVariantTable,
|
|
989
1082
|
buildVariantTest,
|
|
990
1083
|
cellLane,
|
|
991
1084
|
chapterChars,
|
|
@@ -1004,6 +1097,7 @@ export {
|
|
|
1004
1097
|
figAlts,
|
|
1005
1098
|
formatCmp,
|
|
1006
1099
|
frontMatter,
|
|
1100
|
+
isRareHan,
|
|
1007
1101
|
loadVariantTest,
|
|
1008
1102
|
nextContinues,
|
|
1009
1103
|
normalizeVariants,
|