book-text-util 0.1.1 → 0.1.3
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 +10 -0
- package/dist/cli.js +214 -20
- package/dist/index.cjs +179 -22
- package/dist/index.d.cts +67 -6
- package/dist/index.d.ts +67 -6
- package/dist/index.js +174 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -57,6 +57,7 @@ const { items, segments } = await compareWhole({ cells, md, file: '001', label:
|
|
|
57
57
|
```
|
|
58
58
|
|
|
59
59
|
- `compareChapter`:按卷分段(册↔卷对应);`runCompare`:连取数一起做(传 `transport`,不给 `isVariant` 时按浏览器环境取 `/compare/glyph-variants.json`,Node 请自己传)。
|
|
60
|
+
- **异体字组**:给了 `isVariant` 时,`diffChars` 按「异体=同字」对齐,逐字互为异体的等长一段单独成一处(`hunk.variant`,类别 glyph),不再和旁边真正的用字不同粘成一条 word。判定只认表里的直接配对,不做传递(葢≈蓋、蓋≈盖 不推出 葢≈盖,避免 干/乾/幹 之类并成一组)。
|
|
60
61
|
- 异体字判定用 open-guji-cv 的 `edges.tsv` 抽出的表(`{ pairs: "葢蓋\n畧略…" }`)。
|
|
61
62
|
- 其余:`diffChars`、`classify`、`wikiChars`/`chapterChars`、`parseCmp`/`formatCmp`(`?cmp=` 链接)。
|
|
62
63
|
- 界面相关(标记装饰 `decorate`、偏好存储 `prefs`、React 钩子)留在网站,不在本包。
|
|
@@ -81,3 +82,12 @@ cd npm && npm ci && npm run typecheck && npm test && npm run build
|
|
|
81
82
|
## 发布
|
|
82
83
|
|
|
83
84
|
Actions →「Publish book-text-util」→ Run workflow(`dry_run` 可先试)。走 npm Trusted Publishing,不存 token;发布前把 `npm/package.json` 的 version 升好(默认只升 patch)。首次需在 npmjs.com 的包设置里登记 Trusted Publisher:`open-guji / book-text-util / publish.yml`。
|
|
85
|
+
|
|
86
|
+
## 三、对校报告(命令行)
|
|
87
|
+
|
|
88
|
+
```bash
|
|
89
|
+
book-text-util report --base-root <底本版本目录> --ref-root <对照版本目录> --chapters 001-005 \
|
|
90
|
+
--variants glyph-variants.json --base-label 开源古籍自校本 --ref-label 殆知阁 --out report.md
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
逐册整章对整章,输出 Markdown:汇总表(字数、相似度、各类条数)+每册「用字不同/对照本多/对照本少」逐条(位置、前后文,差异字加粗);`--list word,add,omit,glyph` 可改列哪几类。库内对应 `buildReport`、`parseChapters`。
|
package/dist/cli.js
CHANGED
|
@@ -480,7 +480,7 @@ function classify(h, base, wiki, opts) {
|
|
|
480
480
|
|
|
481
481
|
// src/compare/diff.ts
|
|
482
482
|
var MAX_EDIT_DISTANCE = 4e3;
|
|
483
|
-
function myers(a, b, maxD) {
|
|
483
|
+
function myers(a, b, maxD, same) {
|
|
484
484
|
const n = a.length;
|
|
485
485
|
const m = b.length;
|
|
486
486
|
const max = Math.min(n + m, maxD);
|
|
@@ -493,7 +493,7 @@ function myers(a, b, maxD) {
|
|
|
493
493
|
for (let k = -d; k <= d; k += 2) {
|
|
494
494
|
let x2 = k === -d || k !== d && v[off + k - 1] < v[off + k + 1] ? v[off + k + 1] : v[off + k - 1] + 1;
|
|
495
495
|
let y2 = x2 - k;
|
|
496
|
-
while (x2 < n && y2 < m && a[x2]
|
|
496
|
+
while (x2 < n && y2 < m && same(a[x2], b[y2])) {
|
|
497
497
|
x2++;
|
|
498
498
|
y2++;
|
|
499
499
|
}
|
|
@@ -532,19 +532,51 @@ function myers(a, b, maxD) {
|
|
|
532
532
|
}
|
|
533
533
|
return ed.reverse();
|
|
534
534
|
}
|
|
535
|
-
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE) {
|
|
535
|
+
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE, isVariant) {
|
|
536
|
+
const same = isVariant ? (x, y) => x === y || isVariant(x, y) : (x, y) => x === y;
|
|
537
|
+
const hunks = diffRaw(a, b, maxD, same);
|
|
538
|
+
if (!hunks || !isVariant) return hunks;
|
|
539
|
+
const out = [];
|
|
540
|
+
let ai = 0;
|
|
541
|
+
let bi = 0;
|
|
542
|
+
const gap = (to) => {
|
|
543
|
+
let run = null;
|
|
544
|
+
for (let k = 0; ai + k < to; k++) {
|
|
545
|
+
if (a[ai + k] !== b[bi + k]) {
|
|
546
|
+
if (!run) run = { i1: ai + k, i2: ai + k + 1, j1: bi + k, j2: bi + k + 1, variant: true };
|
|
547
|
+
else {
|
|
548
|
+
run.i2++;
|
|
549
|
+
run.j2++;
|
|
550
|
+
}
|
|
551
|
+
} else if (run) {
|
|
552
|
+
out.push(run);
|
|
553
|
+
run = null;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
if (run) out.push(run);
|
|
557
|
+
};
|
|
558
|
+
for (const h of hunks) {
|
|
559
|
+
gap(h.i1);
|
|
560
|
+
out.push(h);
|
|
561
|
+
ai = h.i2;
|
|
562
|
+
bi = h.j2;
|
|
563
|
+
}
|
|
564
|
+
gap(a.length);
|
|
565
|
+
return out;
|
|
566
|
+
}
|
|
567
|
+
function diffRaw(a, b, maxD, same) {
|
|
536
568
|
let lo = 0;
|
|
537
|
-
while (lo < a.length && lo < b.length && a[lo]
|
|
569
|
+
while (lo < a.length && lo < b.length && same(a[lo], b[lo])) lo++;
|
|
538
570
|
let ea = a.length;
|
|
539
571
|
let eb = b.length;
|
|
540
|
-
while (ea > lo && eb > lo && a[ea - 1]
|
|
572
|
+
while (ea > lo && eb > lo && same(a[ea - 1], b[eb - 1])) {
|
|
541
573
|
ea--;
|
|
542
574
|
eb--;
|
|
543
575
|
}
|
|
544
576
|
const sa = a.slice(lo, ea);
|
|
545
577
|
const sb = b.slice(lo, eb);
|
|
546
578
|
if (sa.length === 0 && sb.length === 0) return [];
|
|
547
|
-
const ed = myers(sa, sb, maxD);
|
|
579
|
+
const ed = myers(sa, sb, maxD, same);
|
|
548
580
|
if (!ed) return null;
|
|
549
581
|
const ops = [];
|
|
550
582
|
let cur = null;
|
|
@@ -575,24 +607,54 @@ function diffChars(a, b, maxD = MAX_EDIT_DISTANCE) {
|
|
|
575
607
|
}
|
|
576
608
|
|
|
577
609
|
// src/compare/volumes.ts
|
|
610
|
+
var DIGIT = { "\u3007": 0, \u96F6: 0, \u4E00: 1, \u4E8C: 2, \u4E09: 3, \u56DB: 4, \u4E94: 5, \u516D: 6, \u4E03: 7, \u516B: 8, \u4E5D: 9 };
|
|
611
|
+
function cnNumber(s) {
|
|
612
|
+
if (!s) return null;
|
|
613
|
+
if (/^\d+$/.test(s)) return Number(s);
|
|
614
|
+
if (![...s].every((ch) => ch in DIGIT || ch === "\u5341" || ch === "\u767E")) return null;
|
|
615
|
+
if (![...s].some((ch) => ch === "\u5341" || ch === "\u767E")) {
|
|
616
|
+
let n = 0;
|
|
617
|
+
for (const ch of s) n = n * 10 + DIGIT[ch];
|
|
618
|
+
return n;
|
|
619
|
+
}
|
|
620
|
+
let total = 0;
|
|
621
|
+
let cur = 0;
|
|
622
|
+
for (const ch of s) {
|
|
623
|
+
if (ch === "\u767E") {
|
|
624
|
+
total += (cur || 1) * 100;
|
|
625
|
+
cur = 0;
|
|
626
|
+
} else if (ch === "\u5341") {
|
|
627
|
+
total += (cur || 1) * 10;
|
|
628
|
+
cur = 0;
|
|
629
|
+
} else cur = DIGIT[ch];
|
|
630
|
+
}
|
|
631
|
+
return total + cur;
|
|
632
|
+
}
|
|
633
|
+
function volumeNo(label) {
|
|
634
|
+
const m = /^卷([\d〇零一二三四五六七八九十百]+)/.exec(label.trim());
|
|
635
|
+
return m ? cnNumber(m[1]) : null;
|
|
636
|
+
}
|
|
578
637
|
function wikiFileOf(no, chapters) {
|
|
579
638
|
const want = `\u5377${String(no).padStart(3, "0")}`;
|
|
580
|
-
|
|
639
|
+
const exact = chapters.find((c) => c.title.trim() === want);
|
|
640
|
+
if (exact) return exact.file;
|
|
641
|
+
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
581
642
|
}
|
|
582
643
|
|
|
583
644
|
// src/compare/wikitext.ts
|
|
584
645
|
var STATUS_LINE = /^(有标点|无标点|已修正|好$|簡繁|简繁)/;
|
|
585
646
|
var VOLUME_LINE = /^#+\s*卷[\d〇零一二三四五六七八九十百]+\s*$/;
|
|
586
647
|
var STRIP = /[\p{P}\p{Z}\s]/gu;
|
|
648
|
+
var ZI = /:zi\[[^\]\n]*\]/g;
|
|
649
|
+
var NOTE_MARK = /[<>|]/g;
|
|
587
650
|
function wikiChars(md) {
|
|
588
651
|
const lines = md.split("\n").map((s) => s.trim()).filter((s) => s && !VOLUME_LINE.test(s) && !STATUS_LINE.test(s)).map((s) => s.replace(/^#+\s*/, ""));
|
|
589
|
-
return Array.from(lines.join("").replace(STRIP, ""));
|
|
652
|
+
return Array.from(lines.join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
590
653
|
}
|
|
591
|
-
var NOTE_MARK = /[<>|]/g;
|
|
592
654
|
function chapterChars(md) {
|
|
593
655
|
const lines = md.split("\n").map((s) => s.trim()).filter(Boolean);
|
|
594
656
|
if (lines.length && /^#{1,2}\s/.test(lines[0])) lines.shift();
|
|
595
|
-
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
657
|
+
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
596
658
|
}
|
|
597
659
|
|
|
598
660
|
// src/compare/compare.ts
|
|
@@ -635,25 +697,43 @@ function trimWikiToBase(base, wiki, start, end) {
|
|
|
635
697
|
}
|
|
636
698
|
return j0 < j1 ? [j0, j1] : [0, wiki.length];
|
|
637
699
|
}
|
|
638
|
-
async function compareSeg(input, seg, out, idStart, whole) {
|
|
700
|
+
async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
639
701
|
const res = { no: seg.no, label: seg.label, from: seg.from, to: seg.to, status: "ok", wikiFile: null };
|
|
640
|
-
|
|
641
|
-
if (
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
if (
|
|
702
|
+
let wiki;
|
|
703
|
+
if (whole) {
|
|
704
|
+
res.wikiFile = whole.file;
|
|
705
|
+
wiki = chapterChars(whole.md);
|
|
706
|
+
} else if (range) {
|
|
707
|
+
const files = [];
|
|
708
|
+
for (let n = range[0]; n <= range[1]; n++) {
|
|
709
|
+
const f = wikiFileOf(n, input.wikiChapters);
|
|
710
|
+
if (!f) return { ...res, wikiFile: files[0] ?? null, wikiFiles: files, status: "no-wiki" };
|
|
711
|
+
files.push(f);
|
|
712
|
+
}
|
|
713
|
+
res.wikiFile = files[0];
|
|
714
|
+
res.wikiFiles = files;
|
|
715
|
+
const mds = await Promise.all(files.map((f) => input.getWikiMd(f)));
|
|
716
|
+
if (mds.some((m) => m === null)) return { ...res, status: "fetch-failed" };
|
|
717
|
+
wiki = mds.flatMap((m) => wikiChars(m));
|
|
718
|
+
} else {
|
|
719
|
+
const file = seg.no === null ? null : wikiFileOf(seg.no, input.wikiChapters);
|
|
720
|
+
if (!file) return { ...res, status: "no-wiki" };
|
|
721
|
+
res.wikiFile = file;
|
|
722
|
+
const md = await input.getWikiMd(file);
|
|
723
|
+
if (md === null) return { ...res, status: "fetch-failed" };
|
|
724
|
+
wiki = wikiChars(md);
|
|
725
|
+
}
|
|
645
726
|
const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
|
|
646
|
-
|
|
647
|
-
if (!whole) {
|
|
727
|
+
if (!whole && !range) {
|
|
648
728
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
649
729
|
wiki = wiki.slice(j0, j1);
|
|
650
730
|
}
|
|
651
|
-
const hunks = diffChars(base, wiki);
|
|
731
|
+
const hunks = diffChars(base, wiki, void 0, input.isVariant);
|
|
652
732
|
if (!hunks) return { ...res, status: "too-different" };
|
|
653
733
|
const structEnd = seg.headEnd - seg.from;
|
|
654
734
|
let changed = 0;
|
|
655
735
|
hunks.forEach((h, n) => {
|
|
656
|
-
const cat = classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
736
|
+
const cat = h.variant ? "glyph" : classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
657
737
|
const ins = h.i1 === h.i2;
|
|
658
738
|
const del = h.j1 === h.j2;
|
|
659
739
|
changed += Math.max(h.i2 - h.i1, h.j2 - h.j1);
|
|
@@ -701,12 +781,88 @@ function buildVariantTest(table) {
|
|
|
701
781
|
return (a, b) => set.has(a + b);
|
|
702
782
|
}
|
|
703
783
|
|
|
784
|
+
// src/compare/report.ts
|
|
785
|
+
var CAT_TITLE = {
|
|
786
|
+
word: "\u7528\u5B57\u4E0D\u540C",
|
|
787
|
+
add: "\u5BF9\u7167\u672C\u591A",
|
|
788
|
+
omit: "\u5BF9\u7167\u672C\u5C11",
|
|
789
|
+
glyph: "\u5F02\u4F53\u5B57",
|
|
790
|
+
struct: "\u7248\u5F0F\u5DEE\u5F02"
|
|
791
|
+
};
|
|
792
|
+
function anchorText(anchor) {
|
|
793
|
+
const m = /^(\d+):(\d+):(-?\d+)/.exec(anchor);
|
|
794
|
+
return m ? `${m[1]}\u9875${m[2]}\u5217${m[3]}\u5B57` : anchor;
|
|
795
|
+
}
|
|
796
|
+
var esc = (s) => s.replace(/\|/g, "\\|");
|
|
797
|
+
function row(it) {
|
|
798
|
+
const b = it.base || "\uFF08\u65E0\uFF09";
|
|
799
|
+
const r = it.reading || "\uFF08\u65E0\uFF09";
|
|
800
|
+
const [b0, b1] = it.ctx.base;
|
|
801
|
+
const [r0, r1] = it.ctx.reading;
|
|
802
|
+
return `| ${anchorText(it.anchor.start)} | ${esc(b0)}**${esc(b)}**${esc(b1)} | ${esc(r0)}**${esc(r)}**${esc(r1)} |`;
|
|
803
|
+
}
|
|
804
|
+
function countByCat(items) {
|
|
805
|
+
const out = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
806
|
+
for (const it of items) out[it.cat]++;
|
|
807
|
+
return out;
|
|
808
|
+
}
|
|
809
|
+
function buildReport(chapters, opts) {
|
|
810
|
+
const list = opts.list ?? ["word", "add", "omit"];
|
|
811
|
+
const L = [];
|
|
812
|
+
L.push(`# ${opts.title}`, "", `\u5E95\u672C\uFF1A${opts.baseLabel}\u3000\u5BF9\u7167\u672C\uFF1A${opts.refLabel}`, "");
|
|
813
|
+
L.push("\u7C7B\u522B\uFF1A**\u7528\u5B57\u4E0D\u540C**\uFF1D\u540C\u4F4D\u7F6E\u4E24\u7248\u5B57\u4E0D\u540C\uFF08\u542B\u901A\u5047\uFF09\uFF1B**\u5BF9\u7167\u672C\u591A**\uFF1D\u5BF9\u7167\u672C\u6709\u3001\u5E95\u672C\u6CA1\u6709\uFF1B**\u5BF9\u7167\u672C\u5C11**\uFF1D\u5E95\u672C\u6709\u3001\u5BF9\u7167\u672C\u6CA1\u6709\uFF1B\u5F02\u4F53\u5B57\uFF08\u5F62\u8FD1\u5F02\u4F53\uFF0C\u5982 \u8462\uFF0F\u84CB\uFF09\u4E0E\u7248\u5F0F\u5DEE\u5F02\u53EA\u8BA1\u6570\uFF0C\u4E0D\u9010\u6761\u5217\u3002", "");
|
|
814
|
+
L.push("## \u6C47\u603B", "", "| \u518C | \u5E95\u672C\u5B57\u6570 | \u76F8\u4F3C\u5EA6 | \u7528\u5B57\u4E0D\u540C | \u5BF9\u7167\u672C\u591A | \u5BF9\u7167\u672C\u5C11 | \u5F02\u4F53\u5B57 | \u7248\u5F0F |", "|---|---:|---:|---:|---:|---:|---:|---:|");
|
|
815
|
+
const total = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
816
|
+
for (const ch of chapters) {
|
|
817
|
+
const c = countByCat(ch.result.items);
|
|
818
|
+
for (const k of Object.keys(total)) total[k] += c[k];
|
|
819
|
+
const seg = ch.result.segments[0];
|
|
820
|
+
const status = seg && seg.status !== "ok" ? `\uFF08${seg.status}\uFF09` : "";
|
|
821
|
+
const n = seg ? seg.to - seg.from : 0;
|
|
822
|
+
L.push(`| ${ch.file} | ${n} | ${seg?.similarity !== void 0 ? (seg.similarity * 100).toFixed(2) + "%" : "-"}${status} | ${c.word} | ${c.add} | ${c.omit} | ${c.glyph} | ${c.struct} |`);
|
|
823
|
+
}
|
|
824
|
+
L.push(`| **\u5408\u8BA1** | | | ${total.word} | ${total.add} | ${total.omit} | ${total.glyph} | ${total.struct} |`, "");
|
|
825
|
+
for (const ch of chapters) {
|
|
826
|
+
L.push(`## \u7B2C ${ch.file} \u518C`, "");
|
|
827
|
+
const seg = ch.result.segments[0];
|
|
828
|
+
if (seg && seg.status !== "ok") {
|
|
829
|
+
L.push(`\u6CA1\u6709\u6BD4\u51FA\u7ED3\u679C\uFF1A${seg.status}\u3002`, "");
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
for (const cat of list) {
|
|
833
|
+
const its = ch.result.items.filter((i) => i.cat === cat);
|
|
834
|
+
if (!its.length) continue;
|
|
835
|
+
L.push(`### ${CAT_TITLE[cat]}\uFF08${its.length}\uFF09`, "", `| \u4F4D\u7F6E | ${esc(opts.baseLabel)} | ${esc(opts.refLabel)} |`, "|---|---|---|");
|
|
836
|
+
for (const it of its) L.push(row(it));
|
|
837
|
+
L.push("");
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
return L.join("\n");
|
|
841
|
+
}
|
|
842
|
+
function parseChapters(spec) {
|
|
843
|
+
const out = [];
|
|
844
|
+
for (const part of spec.split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
845
|
+
const m = /^(\d+)-(\d+)$/.exec(part);
|
|
846
|
+
if (!m) {
|
|
847
|
+
out.push(part);
|
|
848
|
+
continue;
|
|
849
|
+
}
|
|
850
|
+
const w = m[1].length;
|
|
851
|
+
for (let n = +m[1]; n <= +m[2]; n++) out.push(String(n).padStart(w, "0"));
|
|
852
|
+
}
|
|
853
|
+
return out;
|
|
854
|
+
}
|
|
855
|
+
|
|
704
856
|
// src/cli.ts
|
|
857
|
+
import { writeFile as writeFile2 } from "fs/promises";
|
|
705
858
|
var HELP = `\u7528\u6CD5\uFF1A
|
|
706
859
|
book-text-util export --root <\u7248\u672C\u76EE\u5F55> --chapter <\u518C\u53F7> [--format lines|punct|entity] [--links]
|
|
707
860
|
[--glyph original|traditional|simplified] [--title \u4E66\u540D] [--id \u6761\u76EEid] [--out \u76EE\u5F55]
|
|
708
861
|
book-text-util compare --base <\u5E95\u672C.txt> --ref <\u5BF9\u7167\u672C.md> [--variants <glyph-variants.json>] [--json]
|
|
709
862
|
|
|
863
|
+
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
|
+
[--title \u6807\u9898] [--base-label \u540D] [--ref-label \u540D] [--list word,add,omit,glyph] [--out \u62A5\u544A.md]
|
|
865
|
+
|
|
710
866
|
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
|
|
711
867
|
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`;
|
|
712
868
|
function die(msg) {
|
|
@@ -790,7 +946,45 @@ async function cmdCompare(argv) {
|
|
|
790
946
|
`);
|
|
791
947
|
}
|
|
792
948
|
}
|
|
949
|
+
async function cmdReport(argv) {
|
|
950
|
+
const { values: v } = parseArgs({
|
|
951
|
+
args: argv,
|
|
952
|
+
options: {
|
|
953
|
+
"base-root": { type: "string" },
|
|
954
|
+
"ref-root": { type: "string" },
|
|
955
|
+
chapters: { type: "string" },
|
|
956
|
+
variants: { type: "string" },
|
|
957
|
+
title: { type: "string" },
|
|
958
|
+
"base-label": { type: "string", default: "\u5E95\u672C" },
|
|
959
|
+
"ref-label": { type: "string", default: "\u5BF9\u7167\u672C" },
|
|
960
|
+
list: { type: "string" },
|
|
961
|
+
out: { type: "string" }
|
|
962
|
+
}
|
|
963
|
+
});
|
|
964
|
+
if (!v["base-root"] || !v["ref-root"] || !v.chapters) die("\u9700\u8981 --base-root\u3001--ref-root\u3001--chapters\n\n" + HELP);
|
|
965
|
+
const table = v.variants ? JSON.parse(await readFile2(v.variants, "utf-8")) : null;
|
|
966
|
+
const isVariant = table ? buildVariantTest(table) : null;
|
|
967
|
+
const chapters = [];
|
|
968
|
+
for (const file of parseChapters(v.chapters)) {
|
|
969
|
+
const char = JSON.parse(await readFile2(join2(v["base-root"], `${file}.char.json`), "utf-8"));
|
|
970
|
+
const cells = charSlots(char).map(([a, c]) => ({ a, c }));
|
|
971
|
+
const md2 = await readFile2(join2(v["ref-root"], `${file}.md`), "utf-8");
|
|
972
|
+
chapters.push({ file, result: await compareWhole({ cells, md: md2, file, label: file, isVariant }) });
|
|
973
|
+
}
|
|
974
|
+
const md = buildReport(chapters, {
|
|
975
|
+
title: v.title ?? `\u5BF9\u6821\u62A5\u544A ${v.chapters}`,
|
|
976
|
+
baseLabel: v["base-label"],
|
|
977
|
+
refLabel: v["ref-label"],
|
|
978
|
+
...v.list ? { list: v.list.split(",") } : {}
|
|
979
|
+
});
|
|
980
|
+
if (v.out) {
|
|
981
|
+
await writeFile2(v.out, md, "utf-8");
|
|
982
|
+
process.stdout.write(`${v.out}
|
|
983
|
+
`);
|
|
984
|
+
} else process.stdout.write(md + "\n");
|
|
985
|
+
}
|
|
793
986
|
var [cmd, ...rest] = process.argv.slice(2);
|
|
794
987
|
if (cmd === "export") await cmdExport(rest);
|
|
795
988
|
else if (cmd === "compare") await cmdCompare(rest);
|
|
989
|
+
else if (cmd === "report") await cmdReport(rest);
|
|
796
990
|
else process.stdout.write(HELP + "\n");
|
package/dist/index.cjs
CHANGED
|
@@ -26,9 +26,11 @@ __export(index_exports, {
|
|
|
26
26
|
GujiFormatError: () => GujiFormatError,
|
|
27
27
|
MAX_EDIT_DISTANCE: () => MAX_EDIT_DISTANCE,
|
|
28
28
|
SOLO_ATTR: () => SOLO_ATTR,
|
|
29
|
+
anchorText: () => anchorText,
|
|
29
30
|
assertSameLength: () => assertSameLength,
|
|
30
31
|
availableFormats: () => availableFormats,
|
|
31
32
|
buildExport: () => buildExport,
|
|
33
|
+
buildReport: () => buildReport,
|
|
32
34
|
buildVariantTest: () => buildVariantTest,
|
|
33
35
|
cellLane: () => cellLane,
|
|
34
36
|
chapterChars: () => chapterChars,
|
|
@@ -38,6 +40,7 @@ __export(index_exports, {
|
|
|
38
40
|
codePointLength: () => codePointLength,
|
|
39
41
|
compareChapter: () => compareChapter,
|
|
40
42
|
compareWhole: () => compareWhole,
|
|
43
|
+
countByCat: () => countByCat,
|
|
41
44
|
createGlyphConverters: () => createGlyphConverters,
|
|
42
45
|
createToSimplified: () => createToSimplified,
|
|
43
46
|
diffChars: () => diffChars,
|
|
@@ -49,6 +52,7 @@ __export(index_exports, {
|
|
|
49
52
|
loadVariantTest: () => loadVariantTest,
|
|
50
53
|
nextContinues: () => nextContinues,
|
|
51
54
|
normalizeVariants: () => normalizeVariants,
|
|
55
|
+
parseChapters: () => parseChapters,
|
|
52
56
|
parseCmp: () => parseCmp,
|
|
53
57
|
renderLines: () => renderLines,
|
|
54
58
|
renderRich: () => renderRich,
|
|
@@ -57,6 +61,7 @@ __export(index_exports, {
|
|
|
57
61
|
splitVolumes: () => splitVolumes,
|
|
58
62
|
trimWikiToBase: () => trimWikiToBase,
|
|
59
63
|
volumeNo: () => volumeNo,
|
|
64
|
+
volumeRangeOf: () => volumeRangeOf,
|
|
60
65
|
wikiChars: () => wikiChars,
|
|
61
66
|
wikiFileOf: () => wikiFileOf
|
|
62
67
|
});
|
|
@@ -522,7 +527,7 @@ function classify(h, base, wiki, opts) {
|
|
|
522
527
|
|
|
523
528
|
// src/compare/diff.ts
|
|
524
529
|
var MAX_EDIT_DISTANCE = 4e3;
|
|
525
|
-
function myers(a, b, maxD) {
|
|
530
|
+
function myers(a, b, maxD, same) {
|
|
526
531
|
const n = a.length;
|
|
527
532
|
const m = b.length;
|
|
528
533
|
const max = Math.min(n + m, maxD);
|
|
@@ -535,7 +540,7 @@ function myers(a, b, maxD) {
|
|
|
535
540
|
for (let k = -d; k <= d; k += 2) {
|
|
536
541
|
let x2 = k === -d || k !== d && v[off + k - 1] < v[off + k + 1] ? v[off + k + 1] : v[off + k - 1] + 1;
|
|
537
542
|
let y2 = x2 - k;
|
|
538
|
-
while (x2 < n && y2 < m && a[x2]
|
|
543
|
+
while (x2 < n && y2 < m && same(a[x2], b[y2])) {
|
|
539
544
|
x2++;
|
|
540
545
|
y2++;
|
|
541
546
|
}
|
|
@@ -574,19 +579,51 @@ function myers(a, b, maxD) {
|
|
|
574
579
|
}
|
|
575
580
|
return ed.reverse();
|
|
576
581
|
}
|
|
577
|
-
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE) {
|
|
582
|
+
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE, isVariant) {
|
|
583
|
+
const same = isVariant ? (x, y) => x === y || isVariant(x, y) : (x, y) => x === y;
|
|
584
|
+
const hunks = diffRaw(a, b, maxD, same);
|
|
585
|
+
if (!hunks || !isVariant) return hunks;
|
|
586
|
+
const out = [];
|
|
587
|
+
let ai = 0;
|
|
588
|
+
let bi = 0;
|
|
589
|
+
const gap = (to) => {
|
|
590
|
+
let run = null;
|
|
591
|
+
for (let k = 0; ai + k < to; k++) {
|
|
592
|
+
if (a[ai + k] !== b[bi + k]) {
|
|
593
|
+
if (!run) run = { i1: ai + k, i2: ai + k + 1, j1: bi + k, j2: bi + k + 1, variant: true };
|
|
594
|
+
else {
|
|
595
|
+
run.i2++;
|
|
596
|
+
run.j2++;
|
|
597
|
+
}
|
|
598
|
+
} else if (run) {
|
|
599
|
+
out.push(run);
|
|
600
|
+
run = null;
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
if (run) out.push(run);
|
|
604
|
+
};
|
|
605
|
+
for (const h of hunks) {
|
|
606
|
+
gap(h.i1);
|
|
607
|
+
out.push(h);
|
|
608
|
+
ai = h.i2;
|
|
609
|
+
bi = h.j2;
|
|
610
|
+
}
|
|
611
|
+
gap(a.length);
|
|
612
|
+
return out;
|
|
613
|
+
}
|
|
614
|
+
function diffRaw(a, b, maxD, same) {
|
|
578
615
|
let lo = 0;
|
|
579
|
-
while (lo < a.length && lo < b.length && a[lo]
|
|
616
|
+
while (lo < a.length && lo < b.length && same(a[lo], b[lo])) lo++;
|
|
580
617
|
let ea = a.length;
|
|
581
618
|
let eb = b.length;
|
|
582
|
-
while (ea > lo && eb > lo && a[ea - 1]
|
|
619
|
+
while (ea > lo && eb > lo && same(a[ea - 1], b[eb - 1])) {
|
|
583
620
|
ea--;
|
|
584
621
|
eb--;
|
|
585
622
|
}
|
|
586
623
|
const sa = a.slice(lo, ea);
|
|
587
624
|
const sb = b.slice(lo, eb);
|
|
588
625
|
if (sa.length === 0 && sb.length === 0) return [];
|
|
589
|
-
const ed = myers(sa, sb, maxD);
|
|
626
|
+
const ed = myers(sa, sb, maxD, same);
|
|
590
627
|
if (!ed) return null;
|
|
591
628
|
const ops = [];
|
|
592
629
|
let cur = null;
|
|
@@ -646,7 +683,17 @@ function volumeNo(label) {
|
|
|
646
683
|
}
|
|
647
684
|
function wikiFileOf(no, chapters) {
|
|
648
685
|
const want = `\u5377${String(no).padStart(3, "0")}`;
|
|
649
|
-
|
|
686
|
+
const exact = chapters.find((c) => c.title.trim() === want);
|
|
687
|
+
if (exact) return exact.file;
|
|
688
|
+
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
689
|
+
}
|
|
690
|
+
function volumeRangeOf(title) {
|
|
691
|
+
const m = /^卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
|
|
692
|
+
if (!m) return null;
|
|
693
|
+
const a = cnNumber(m[1]);
|
|
694
|
+
const b = m[2] ? cnNumber(m[2]) : a;
|
|
695
|
+
if (a === null || b === null || b < a || b - a > 200) return null;
|
|
696
|
+
return [a, b];
|
|
650
697
|
}
|
|
651
698
|
var anchorKey = (a) => a;
|
|
652
699
|
function splitVolumes(cells, headings, prevVolNo, lastContinues) {
|
|
@@ -684,15 +731,16 @@ function nextContinues(nextHeadings) {
|
|
|
684
731
|
var STATUS_LINE = /^(有标点|无标点|已修正|好$|簡繁|简繁)/;
|
|
685
732
|
var VOLUME_LINE = /^#+\s*卷[\d〇零一二三四五六七八九十百]+\s*$/;
|
|
686
733
|
var STRIP = /[\p{P}\p{Z}\s]/gu;
|
|
734
|
+
var ZI = /:zi\[[^\]\n]*\]/g;
|
|
735
|
+
var NOTE_MARK = /[<>|]/g;
|
|
687
736
|
function wikiChars(md) {
|
|
688
737
|
const lines = md.split("\n").map((s) => s.trim()).filter((s) => s && !VOLUME_LINE.test(s) && !STATUS_LINE.test(s)).map((s) => s.replace(/^#+\s*/, ""));
|
|
689
|
-
return Array.from(lines.join("").replace(STRIP, ""));
|
|
738
|
+
return Array.from(lines.join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
690
739
|
}
|
|
691
|
-
var NOTE_MARK = /[<>|]/g;
|
|
692
740
|
function chapterChars(md) {
|
|
693
741
|
const lines = md.split("\n").map((s) => s.trim()).filter(Boolean);
|
|
694
742
|
if (lines.length && /^#{1,2}\s/.test(lines[0])) lines.shift();
|
|
695
|
-
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
743
|
+
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
696
744
|
}
|
|
697
745
|
|
|
698
746
|
// src/compare/compare.ts
|
|
@@ -735,25 +783,43 @@ function trimWikiToBase(base, wiki, start, end) {
|
|
|
735
783
|
}
|
|
736
784
|
return j0 < j1 ? [j0, j1] : [0, wiki.length];
|
|
737
785
|
}
|
|
738
|
-
async function compareSeg(input, seg, out, idStart, whole) {
|
|
786
|
+
async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
739
787
|
const res = { no: seg.no, label: seg.label, from: seg.from, to: seg.to, status: "ok", wikiFile: null };
|
|
740
|
-
|
|
741
|
-
if (
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
if (
|
|
788
|
+
let wiki;
|
|
789
|
+
if (whole) {
|
|
790
|
+
res.wikiFile = whole.file;
|
|
791
|
+
wiki = chapterChars(whole.md);
|
|
792
|
+
} else if (range) {
|
|
793
|
+
const files = [];
|
|
794
|
+
for (let n = range[0]; n <= range[1]; n++) {
|
|
795
|
+
const f = wikiFileOf(n, input.wikiChapters);
|
|
796
|
+
if (!f) return { ...res, wikiFile: files[0] ?? null, wikiFiles: files, status: "no-wiki" };
|
|
797
|
+
files.push(f);
|
|
798
|
+
}
|
|
799
|
+
res.wikiFile = files[0];
|
|
800
|
+
res.wikiFiles = files;
|
|
801
|
+
const mds = await Promise.all(files.map((f) => input.getWikiMd(f)));
|
|
802
|
+
if (mds.some((m) => m === null)) return { ...res, status: "fetch-failed" };
|
|
803
|
+
wiki = mds.flatMap((m) => wikiChars(m));
|
|
804
|
+
} else {
|
|
805
|
+
const file = seg.no === null ? null : wikiFileOf(seg.no, input.wikiChapters);
|
|
806
|
+
if (!file) return { ...res, status: "no-wiki" };
|
|
807
|
+
res.wikiFile = file;
|
|
808
|
+
const md = await input.getWikiMd(file);
|
|
809
|
+
if (md === null) return { ...res, status: "fetch-failed" };
|
|
810
|
+
wiki = wikiChars(md);
|
|
811
|
+
}
|
|
745
812
|
const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
|
|
746
|
-
|
|
747
|
-
if (!whole) {
|
|
813
|
+
if (!whole && !range) {
|
|
748
814
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
749
815
|
wiki = wiki.slice(j0, j1);
|
|
750
816
|
}
|
|
751
|
-
const hunks = diffChars(base, wiki);
|
|
817
|
+
const hunks = diffChars(base, wiki, void 0, input.isVariant);
|
|
752
818
|
if (!hunks) return { ...res, status: "too-different" };
|
|
753
819
|
const structEnd = seg.headEnd - seg.from;
|
|
754
820
|
let changed = 0;
|
|
755
821
|
hunks.forEach((h, n) => {
|
|
756
|
-
const cat = classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
822
|
+
const cat = h.variant ? "glyph" : classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
757
823
|
const ins = h.i1 === h.i2;
|
|
758
824
|
const del = h.j1 === h.j2;
|
|
759
825
|
changed += Math.max(h.i2 - h.i1, h.j2 - h.j1);
|
|
@@ -781,6 +847,15 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
781
847
|
return res;
|
|
782
848
|
}
|
|
783
849
|
async function compareChapter(input) {
|
|
850
|
+
const range = input.volumeRange;
|
|
851
|
+
if (range && !input.headings.some((h) => h.level === 1)) {
|
|
852
|
+
const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
|
|
853
|
+
const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
|
|
854
|
+
const items2 = [];
|
|
855
|
+
const res = await compareSeg(input, seg, items2, 0, void 0, range);
|
|
856
|
+
items2.sort((a, b) => a.i1 - b.i1 || a.i2 - b.i2);
|
|
857
|
+
return { items: items2, segments: [res] };
|
|
858
|
+
}
|
|
784
859
|
const segs = splitVolumes(input.cells, input.headings, input.prevVolNo, input.lastContinues);
|
|
785
860
|
const items = [];
|
|
786
861
|
const segments = [];
|
|
@@ -826,7 +901,9 @@ async function resolveTargetKey(transport, target) {
|
|
|
826
901
|
const m = await transport.getTextManifest?.(target.id);
|
|
827
902
|
const vs = m?.versions;
|
|
828
903
|
if (!Array.isArray(vs) || vs.length === 0) return null;
|
|
829
|
-
|
|
904
|
+
const main = vs.find((v) => v.key === "default") ?? vs[0];
|
|
905
|
+
if (main.kind !== "collated") return main.key;
|
|
906
|
+
return vs.find((v) => v.kind !== "collated")?.key ?? main.key;
|
|
830
907
|
}
|
|
831
908
|
async function runCompare(input) {
|
|
832
909
|
const { base, target, cells } = input;
|
|
@@ -870,7 +947,10 @@ async function runCompare(input) {
|
|
|
870
947
|
lastContinues: nextContinues(at >= 0 && at + 1 < chapters.length ? headingsOf(chapters[at + 1]) : void 0),
|
|
871
948
|
wikiChapters,
|
|
872
949
|
getWikiMd,
|
|
873
|
-
isVariant
|
|
950
|
+
isVariant,
|
|
951
|
+
// 册无卷标题(如全唐文自校本一册一章、章名「卷七至卷十」):按章名取对照本的几卷
|
|
952
|
+
volumeRange: typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null,
|
|
953
|
+
volumeLabel: typeof chapters[at]?.title === "string" ? chapters[at].title : void 0
|
|
874
954
|
});
|
|
875
955
|
return { ...result, targetKey, wikiChapters };
|
|
876
956
|
}
|
|
@@ -891,6 +971,78 @@ function parseCmp(raw) {
|
|
|
891
971
|
function formatCmp(t) {
|
|
892
972
|
return t.key ? `${t.id}-${t.key}` : t.id;
|
|
893
973
|
}
|
|
974
|
+
|
|
975
|
+
// src/compare/report.ts
|
|
976
|
+
var CAT_TITLE = {
|
|
977
|
+
word: "\u7528\u5B57\u4E0D\u540C",
|
|
978
|
+
add: "\u5BF9\u7167\u672C\u591A",
|
|
979
|
+
omit: "\u5BF9\u7167\u672C\u5C11",
|
|
980
|
+
glyph: "\u5F02\u4F53\u5B57",
|
|
981
|
+
struct: "\u7248\u5F0F\u5DEE\u5F02"
|
|
982
|
+
};
|
|
983
|
+
function anchorText(anchor) {
|
|
984
|
+
const m = /^(\d+):(\d+):(-?\d+)/.exec(anchor);
|
|
985
|
+
return m ? `${m[1]}\u9875${m[2]}\u5217${m[3]}\u5B57` : anchor;
|
|
986
|
+
}
|
|
987
|
+
var esc = (s) => s.replace(/\|/g, "\\|");
|
|
988
|
+
function row(it) {
|
|
989
|
+
const b = it.base || "\uFF08\u65E0\uFF09";
|
|
990
|
+
const r = it.reading || "\uFF08\u65E0\uFF09";
|
|
991
|
+
const [b0, b1] = it.ctx.base;
|
|
992
|
+
const [r0, r1] = it.ctx.reading;
|
|
993
|
+
return `| ${anchorText(it.anchor.start)} | ${esc(b0)}**${esc(b)}**${esc(b1)} | ${esc(r0)}**${esc(r)}**${esc(r1)} |`;
|
|
994
|
+
}
|
|
995
|
+
function countByCat(items) {
|
|
996
|
+
const out = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
997
|
+
for (const it of items) out[it.cat]++;
|
|
998
|
+
return out;
|
|
999
|
+
}
|
|
1000
|
+
function buildReport(chapters, opts) {
|
|
1001
|
+
const list = opts.list ?? ["word", "add", "omit"];
|
|
1002
|
+
const L = [];
|
|
1003
|
+
L.push(`# ${opts.title}`, "", `\u5E95\u672C\uFF1A${opts.baseLabel}\u3000\u5BF9\u7167\u672C\uFF1A${opts.refLabel}`, "");
|
|
1004
|
+
L.push("\u7C7B\u522B\uFF1A**\u7528\u5B57\u4E0D\u540C**\uFF1D\u540C\u4F4D\u7F6E\u4E24\u7248\u5B57\u4E0D\u540C\uFF08\u542B\u901A\u5047\uFF09\uFF1B**\u5BF9\u7167\u672C\u591A**\uFF1D\u5BF9\u7167\u672C\u6709\u3001\u5E95\u672C\u6CA1\u6709\uFF1B**\u5BF9\u7167\u672C\u5C11**\uFF1D\u5E95\u672C\u6709\u3001\u5BF9\u7167\u672C\u6CA1\u6709\uFF1B\u5F02\u4F53\u5B57\uFF08\u5F62\u8FD1\u5F02\u4F53\uFF0C\u5982 \u8462\uFF0F\u84CB\uFF09\u4E0E\u7248\u5F0F\u5DEE\u5F02\u53EA\u8BA1\u6570\uFF0C\u4E0D\u9010\u6761\u5217\u3002", "");
|
|
1005
|
+
L.push("## \u6C47\u603B", "", "| \u518C | \u5E95\u672C\u5B57\u6570 | \u76F8\u4F3C\u5EA6 | \u7528\u5B57\u4E0D\u540C | \u5BF9\u7167\u672C\u591A | \u5BF9\u7167\u672C\u5C11 | \u5F02\u4F53\u5B57 | \u7248\u5F0F |", "|---|---:|---:|---:|---:|---:|---:|---:|");
|
|
1006
|
+
const total = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
1007
|
+
for (const ch of chapters) {
|
|
1008
|
+
const c = countByCat(ch.result.items);
|
|
1009
|
+
for (const k of Object.keys(total)) total[k] += c[k];
|
|
1010
|
+
const seg = ch.result.segments[0];
|
|
1011
|
+
const status = seg && seg.status !== "ok" ? `\uFF08${seg.status}\uFF09` : "";
|
|
1012
|
+
const n = seg ? seg.to - seg.from : 0;
|
|
1013
|
+
L.push(`| ${ch.file} | ${n} | ${seg?.similarity !== void 0 ? (seg.similarity * 100).toFixed(2) + "%" : "-"}${status} | ${c.word} | ${c.add} | ${c.omit} | ${c.glyph} | ${c.struct} |`);
|
|
1014
|
+
}
|
|
1015
|
+
L.push(`| **\u5408\u8BA1** | | | ${total.word} | ${total.add} | ${total.omit} | ${total.glyph} | ${total.struct} |`, "");
|
|
1016
|
+
for (const ch of chapters) {
|
|
1017
|
+
L.push(`## \u7B2C ${ch.file} \u518C`, "");
|
|
1018
|
+
const seg = ch.result.segments[0];
|
|
1019
|
+
if (seg && seg.status !== "ok") {
|
|
1020
|
+
L.push(`\u6CA1\u6709\u6BD4\u51FA\u7ED3\u679C\uFF1A${seg.status}\u3002`, "");
|
|
1021
|
+
continue;
|
|
1022
|
+
}
|
|
1023
|
+
for (const cat of list) {
|
|
1024
|
+
const its = ch.result.items.filter((i) => i.cat === cat);
|
|
1025
|
+
if (!its.length) continue;
|
|
1026
|
+
L.push(`### ${CAT_TITLE[cat]}\uFF08${its.length}\uFF09`, "", `| \u4F4D\u7F6E | ${esc(opts.baseLabel)} | ${esc(opts.refLabel)} |`, "|---|---|---|");
|
|
1027
|
+
for (const it of its) L.push(row(it));
|
|
1028
|
+
L.push("");
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
return L.join("\n");
|
|
1032
|
+
}
|
|
1033
|
+
function parseChapters(spec) {
|
|
1034
|
+
const out = [];
|
|
1035
|
+
for (const part of spec.split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
1036
|
+
const m = /^(\d+)-(\d+)$/.exec(part);
|
|
1037
|
+
if (!m) {
|
|
1038
|
+
out.push(part);
|
|
1039
|
+
continue;
|
|
1040
|
+
}
|
|
1041
|
+
const w = m[1].length;
|
|
1042
|
+
for (let n = +m[1]; n <= +m[2]; n++) out.push(String(n).padStart(w, "0"));
|
|
1043
|
+
}
|
|
1044
|
+
return out;
|
|
1045
|
+
}
|
|
894
1046
|
// Annotate the CommonJS export names for ESM import in node:
|
|
895
1047
|
0 && (module.exports = {
|
|
896
1048
|
ALL_CATS,
|
|
@@ -899,9 +1051,11 @@ function formatCmp(t) {
|
|
|
899
1051
|
GujiFormatError,
|
|
900
1052
|
MAX_EDIT_DISTANCE,
|
|
901
1053
|
SOLO_ATTR,
|
|
1054
|
+
anchorText,
|
|
902
1055
|
assertSameLength,
|
|
903
1056
|
availableFormats,
|
|
904
1057
|
buildExport,
|
|
1058
|
+
buildReport,
|
|
905
1059
|
buildVariantTest,
|
|
906
1060
|
cellLane,
|
|
907
1061
|
chapterChars,
|
|
@@ -911,6 +1065,7 @@ function formatCmp(t) {
|
|
|
911
1065
|
codePointLength,
|
|
912
1066
|
compareChapter,
|
|
913
1067
|
compareWhole,
|
|
1068
|
+
countByCat,
|
|
914
1069
|
createGlyphConverters,
|
|
915
1070
|
createToSimplified,
|
|
916
1071
|
diffChars,
|
|
@@ -922,6 +1077,7 @@ function formatCmp(t) {
|
|
|
922
1077
|
loadVariantTest,
|
|
923
1078
|
nextContinues,
|
|
924
1079
|
normalizeVariants,
|
|
1080
|
+
parseChapters,
|
|
925
1081
|
parseCmp,
|
|
926
1082
|
renderLines,
|
|
927
1083
|
renderRich,
|
|
@@ -930,6 +1086,7 @@ function formatCmp(t) {
|
|
|
930
1086
|
splitVolumes,
|
|
931
1087
|
trimWikiToBase,
|
|
932
1088
|
volumeNo,
|
|
1089
|
+
volumeRangeOf,
|
|
933
1090
|
wikiChars,
|
|
934
1091
|
wikiFileOf
|
|
935
1092
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -147,11 +147,20 @@ interface Hunk {
|
|
|
147
147
|
i2: number;
|
|
148
148
|
j1: number;
|
|
149
149
|
j2: number;
|
|
150
|
+
/** 仅 `diffChars` 带 `isVariant` 时出现:这一处是「逐字都是异体字」的等长替换(两侧按异体视为同字对上了,但字形不同) */
|
|
151
|
+
variant?: true;
|
|
150
152
|
}
|
|
153
|
+
/** 判两字等价(异体字等);相同的字不会传进来 */
|
|
154
|
+
type CharEq = (a: string, b: string) => boolean;
|
|
151
155
|
/** 兜底上限:D 太大时 trace 占 O(D²) 内存,也说明两份文本根本对不上 */
|
|
152
156
|
declare const MAX_EDIT_DISTANCE = 4000;
|
|
153
|
-
/**
|
|
154
|
-
|
|
157
|
+
/**
|
|
158
|
+
* 比对两串字,返回合并后的差异;差异太大返回 null。
|
|
159
|
+
*
|
|
160
|
+
* 给了 `isVariant` 时,异体字组(逐字互为异体的等长一段)按「同字」对齐,不再把旁边真正的用字不同拖进同一处:
|
|
161
|
+
* 例如「葢天按」对「蓋天案」得到两处——[葢→蓋](variant: true)与 [按→案],而不是一处 3 字的替换。
|
|
162
|
+
*/
|
|
163
|
+
declare function diffChars(a: readonly string[], b: readonly string[], maxD?: number, isVariant?: CharEq | null): Hunk[] | null;
|
|
155
164
|
|
|
156
165
|
/** 差异类别:glyph 异体字(默认不显示)、word 用字不同、add 对照本多出、omit 底本有而对照本没有、struct 题名/卷端署题等版式差异(默认不显示) */
|
|
157
166
|
type DiffCat = 'glyph' | 'word' | 'add' | 'omit' | 'struct';
|
|
@@ -193,8 +202,16 @@ interface WikiChapter {
|
|
|
193
202
|
declare function cnNumber(s: string): number | null;
|
|
194
203
|
/** 一级标题「卷一 經部一」→ 1;不是「卷+数」开头的返回 null */
|
|
195
204
|
declare function volumeNo(label: string): number | null;
|
|
196
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* 卷序号 → 对照本章文件;没有返回 null。
|
|
207
|
+
* 认两种章名:维基文庫的「卷001」(数字补零到三位),和 Kanripo 等的「卷七」「卷二十三」(汉字数字,后面可带篇名)。
|
|
208
|
+
*/
|
|
197
209
|
declare function wikiFileOf(no: number, chapters: readonly WikiChapter[]): string | null;
|
|
210
|
+
/**
|
|
211
|
+
* 章名里的卷范围:「卷七至卷十」→ [7, 10],「卷七」→ [7, 7];认不出返回 null。
|
|
212
|
+
* 用于底本册没有一级标题(卷)时,按册名决定取对照本哪几卷(如全唐文自校本一册一章、章名写「卷七至卷十」)。
|
|
213
|
+
*/
|
|
214
|
+
declare function volumeRangeOf(title: string): [number, number] | null;
|
|
198
215
|
interface VolumeSeg {
|
|
199
216
|
/** 卷序号;认不出(卷首、序等)为 null */
|
|
200
217
|
no: number | null;
|
|
@@ -255,8 +272,10 @@ interface SegResult {
|
|
|
255
272
|
from: number;
|
|
256
273
|
to: number;
|
|
257
274
|
status: SegStatus;
|
|
258
|
-
/**
|
|
275
|
+
/** 对照本章文件(多卷拼成一段时为第一卷,全部见 wikiFiles) */
|
|
259
276
|
wikiFile: string | null;
|
|
277
|
+
/** 多卷拼成一段比对时的各卷章文件 */
|
|
278
|
+
wikiFiles?: string[];
|
|
260
279
|
/** 相似度 0..1(ok 时有) */
|
|
261
280
|
similarity?: number;
|
|
262
281
|
}
|
|
@@ -279,6 +298,13 @@ interface CompareInput {
|
|
|
279
298
|
/** 取对照本某章的 md 文本;取不到返回 null */
|
|
280
299
|
getWikiMd: (file: string) => Promise<string | null>;
|
|
281
300
|
isVariant: VariantTest;
|
|
301
|
+
/**
|
|
302
|
+
* 本册没有一级标题(卷)时,按册名(章 title)给出的卷范围,如「卷七至卷十」→ [7, 10]:
|
|
303
|
+
* 对照本这几卷拼起来与整册比对。有一级标题时不用。
|
|
304
|
+
*/
|
|
305
|
+
volumeRange?: [number, number] | null;
|
|
306
|
+
/** 那一段的名称(一般就是册名);不给按卷号写 */
|
|
307
|
+
volumeLabel?: string;
|
|
282
308
|
}
|
|
283
309
|
/** 比对一册:按卷分段,各卷分别取对照本、分别比对(不跨卷错位),结果按底本字序排列 */
|
|
284
310
|
declare function compareChapter(input: CompareInput): Promise<CompareResult>;
|
|
@@ -316,6 +342,7 @@ interface TextTransport {
|
|
|
316
342
|
getTextManifest?(id: string): Promise<{
|
|
317
343
|
versions: {
|
|
318
344
|
key: string;
|
|
345
|
+
kind?: string;
|
|
319
346
|
label?: string;
|
|
320
347
|
name?: string;
|
|
321
348
|
}[];
|
|
@@ -329,7 +356,11 @@ interface TextTransport {
|
|
|
329
356
|
md: string | null;
|
|
330
357
|
} | null>;
|
|
331
358
|
}
|
|
332
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* 对照本没指明版本时取哪一份:有 default 取它,否则第一份(与阅读页同规则);
|
|
361
|
+
* 但整理本(kind=collated)的 md 只有篇目、正文在 json 里,拿来逐字比没有意义,
|
|
362
|
+
* 这时改取第一份不是整理本的版本(如四庫總目维基条目的 wikisource)。都是整理本就仍取 default。
|
|
363
|
+
*/
|
|
333
364
|
declare function resolveTargetKey(transport: TextTransport, target: CompareTarget): Promise<string | null>;
|
|
334
365
|
interface RunInput {
|
|
335
366
|
/** 阅读页的传输层(IndexStorage);只用到 getTextManifest/getTextIndex/getChapter */
|
|
@@ -374,6 +405,36 @@ declare function loadVariantTest(url?: string): Promise<((a: string, b: string)
|
|
|
374
405
|
* 其余(含各篇标题文字)保留,因为底本的标题也是字层里的字。
|
|
375
406
|
*/
|
|
376
407
|
declare function wikiChars(md: string): string[];
|
|
408
|
+
/**
|
|
409
|
+
* 同一本书的另一个版本(如光盘本)的章 md → 参与比对的字序列。
|
|
410
|
+
* 与维基文庫那份不同:章文件与底本一一对应(同一个文件 stem),第一行 `## 章标题` 是章名、不是正文,去掉;
|
|
411
|
+
* 其余照常去掉标点与空白(这类版本多数没有标点,保持原分行)。
|
|
412
|
+
* 双行小注的标记 `<…|…>`(`<` `>` 起止,`|` 换列)不是字,去掉(它们属符号类 Sm,不在 STRIP 里)。
|
|
413
|
+
*/
|
|
377
414
|
declare function chapterChars(md: string): string[];
|
|
378
415
|
|
|
379
|
-
|
|
416
|
+
/**
|
|
417
|
+
* 对校报告:把若干册的比对结果整理成一份 Markdown,方便人工逐处检查。
|
|
418
|
+
* 纯函数,不读文件;命令行 `book-text-util report` 负责取数。
|
|
419
|
+
*/
|
|
420
|
+
|
|
421
|
+
interface ReportChapter {
|
|
422
|
+
/** 册文件 stem,如 "001" */
|
|
423
|
+
file: string;
|
|
424
|
+
result: CompareResult;
|
|
425
|
+
}
|
|
426
|
+
interface ReportOptions {
|
|
427
|
+
title: string;
|
|
428
|
+
baseLabel: string;
|
|
429
|
+
refLabel: string;
|
|
430
|
+
/** 报告里逐条列出哪些类别;缺省 用字不同、对照本多、对照本少(异体、版式差异只计数) */
|
|
431
|
+
list?: readonly DiffCat[];
|
|
432
|
+
}
|
|
433
|
+
/** `页:列:序[子列]` → 「P页C列K字」;认不出原样返回 */
|
|
434
|
+
declare function anchorText(anchor: string): string;
|
|
435
|
+
declare function countByCat(items: readonly CompareItem[]): Record<DiffCat, number>;
|
|
436
|
+
declare function buildReport(chapters: readonly ReportChapter[], opts: ReportOptions): string;
|
|
437
|
+
/** 「001-005」「001,003,010-012」→ ['001','002',…];保持位数 */
|
|
438
|
+
declare function parseChapters(spec: string): string[];
|
|
439
|
+
|
|
440
|
+
export { ALL_CATS, type BaseCell, 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 VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
|
package/dist/index.d.ts
CHANGED
|
@@ -147,11 +147,20 @@ interface Hunk {
|
|
|
147
147
|
i2: number;
|
|
148
148
|
j1: number;
|
|
149
149
|
j2: number;
|
|
150
|
+
/** 仅 `diffChars` 带 `isVariant` 时出现:这一处是「逐字都是异体字」的等长替换(两侧按异体视为同字对上了,但字形不同) */
|
|
151
|
+
variant?: true;
|
|
150
152
|
}
|
|
153
|
+
/** 判两字等价(异体字等);相同的字不会传进来 */
|
|
154
|
+
type CharEq = (a: string, b: string) => boolean;
|
|
151
155
|
/** 兜底上限:D 太大时 trace 占 O(D²) 内存,也说明两份文本根本对不上 */
|
|
152
156
|
declare const MAX_EDIT_DISTANCE = 4000;
|
|
153
|
-
/**
|
|
154
|
-
|
|
157
|
+
/**
|
|
158
|
+
* 比对两串字,返回合并后的差异;差异太大返回 null。
|
|
159
|
+
*
|
|
160
|
+
* 给了 `isVariant` 时,异体字组(逐字互为异体的等长一段)按「同字」对齐,不再把旁边真正的用字不同拖进同一处:
|
|
161
|
+
* 例如「葢天按」对「蓋天案」得到两处——[葢→蓋](variant: true)与 [按→案],而不是一处 3 字的替换。
|
|
162
|
+
*/
|
|
163
|
+
declare function diffChars(a: readonly string[], b: readonly string[], maxD?: number, isVariant?: CharEq | null): Hunk[] | null;
|
|
155
164
|
|
|
156
165
|
/** 差异类别:glyph 异体字(默认不显示)、word 用字不同、add 对照本多出、omit 底本有而对照本没有、struct 题名/卷端署题等版式差异(默认不显示) */
|
|
157
166
|
type DiffCat = 'glyph' | 'word' | 'add' | 'omit' | 'struct';
|
|
@@ -193,8 +202,16 @@ interface WikiChapter {
|
|
|
193
202
|
declare function cnNumber(s: string): number | null;
|
|
194
203
|
/** 一级标题「卷一 經部一」→ 1;不是「卷+数」开头的返回 null */
|
|
195
204
|
declare function volumeNo(label: string): number | null;
|
|
196
|
-
/**
|
|
205
|
+
/**
|
|
206
|
+
* 卷序号 → 对照本章文件;没有返回 null。
|
|
207
|
+
* 认两种章名:维基文庫的「卷001」(数字补零到三位),和 Kanripo 等的「卷七」「卷二十三」(汉字数字,后面可带篇名)。
|
|
208
|
+
*/
|
|
197
209
|
declare function wikiFileOf(no: number, chapters: readonly WikiChapter[]): string | null;
|
|
210
|
+
/**
|
|
211
|
+
* 章名里的卷范围:「卷七至卷十」→ [7, 10],「卷七」→ [7, 7];认不出返回 null。
|
|
212
|
+
* 用于底本册没有一级标题(卷)时,按册名决定取对照本哪几卷(如全唐文自校本一册一章、章名写「卷七至卷十」)。
|
|
213
|
+
*/
|
|
214
|
+
declare function volumeRangeOf(title: string): [number, number] | null;
|
|
198
215
|
interface VolumeSeg {
|
|
199
216
|
/** 卷序号;认不出(卷首、序等)为 null */
|
|
200
217
|
no: number | null;
|
|
@@ -255,8 +272,10 @@ interface SegResult {
|
|
|
255
272
|
from: number;
|
|
256
273
|
to: number;
|
|
257
274
|
status: SegStatus;
|
|
258
|
-
/**
|
|
275
|
+
/** 对照本章文件(多卷拼成一段时为第一卷,全部见 wikiFiles) */
|
|
259
276
|
wikiFile: string | null;
|
|
277
|
+
/** 多卷拼成一段比对时的各卷章文件 */
|
|
278
|
+
wikiFiles?: string[];
|
|
260
279
|
/** 相似度 0..1(ok 时有) */
|
|
261
280
|
similarity?: number;
|
|
262
281
|
}
|
|
@@ -279,6 +298,13 @@ interface CompareInput {
|
|
|
279
298
|
/** 取对照本某章的 md 文本;取不到返回 null */
|
|
280
299
|
getWikiMd: (file: string) => Promise<string | null>;
|
|
281
300
|
isVariant: VariantTest;
|
|
301
|
+
/**
|
|
302
|
+
* 本册没有一级标题(卷)时,按册名(章 title)给出的卷范围,如「卷七至卷十」→ [7, 10]:
|
|
303
|
+
* 对照本这几卷拼起来与整册比对。有一级标题时不用。
|
|
304
|
+
*/
|
|
305
|
+
volumeRange?: [number, number] | null;
|
|
306
|
+
/** 那一段的名称(一般就是册名);不给按卷号写 */
|
|
307
|
+
volumeLabel?: string;
|
|
282
308
|
}
|
|
283
309
|
/** 比对一册:按卷分段,各卷分别取对照本、分别比对(不跨卷错位),结果按底本字序排列 */
|
|
284
310
|
declare function compareChapter(input: CompareInput): Promise<CompareResult>;
|
|
@@ -316,6 +342,7 @@ interface TextTransport {
|
|
|
316
342
|
getTextManifest?(id: string): Promise<{
|
|
317
343
|
versions: {
|
|
318
344
|
key: string;
|
|
345
|
+
kind?: string;
|
|
319
346
|
label?: string;
|
|
320
347
|
name?: string;
|
|
321
348
|
}[];
|
|
@@ -329,7 +356,11 @@ interface TextTransport {
|
|
|
329
356
|
md: string | null;
|
|
330
357
|
} | null>;
|
|
331
358
|
}
|
|
332
|
-
/**
|
|
359
|
+
/**
|
|
360
|
+
* 对照本没指明版本时取哪一份:有 default 取它,否则第一份(与阅读页同规则);
|
|
361
|
+
* 但整理本(kind=collated)的 md 只有篇目、正文在 json 里,拿来逐字比没有意义,
|
|
362
|
+
* 这时改取第一份不是整理本的版本(如四庫總目维基条目的 wikisource)。都是整理本就仍取 default。
|
|
363
|
+
*/
|
|
333
364
|
declare function resolveTargetKey(transport: TextTransport, target: CompareTarget): Promise<string | null>;
|
|
334
365
|
interface RunInput {
|
|
335
366
|
/** 阅读页的传输层(IndexStorage);只用到 getTextManifest/getTextIndex/getChapter */
|
|
@@ -374,6 +405,36 @@ declare function loadVariantTest(url?: string): Promise<((a: string, b: string)
|
|
|
374
405
|
* 其余(含各篇标题文字)保留,因为底本的标题也是字层里的字。
|
|
375
406
|
*/
|
|
376
407
|
declare function wikiChars(md: string): string[];
|
|
408
|
+
/**
|
|
409
|
+
* 同一本书的另一个版本(如光盘本)的章 md → 参与比对的字序列。
|
|
410
|
+
* 与维基文庫那份不同:章文件与底本一一对应(同一个文件 stem),第一行 `## 章标题` 是章名、不是正文,去掉;
|
|
411
|
+
* 其余照常去掉标点与空白(这类版本多数没有标点,保持原分行)。
|
|
412
|
+
* 双行小注的标记 `<…|…>`(`<` `>` 起止,`|` 换列)不是字,去掉(它们属符号类 Sm,不在 STRIP 里)。
|
|
413
|
+
*/
|
|
377
414
|
declare function chapterChars(md: string): string[];
|
|
378
415
|
|
|
379
|
-
|
|
416
|
+
/**
|
|
417
|
+
* 对校报告:把若干册的比对结果整理成一份 Markdown,方便人工逐处检查。
|
|
418
|
+
* 纯函数,不读文件;命令行 `book-text-util report` 负责取数。
|
|
419
|
+
*/
|
|
420
|
+
|
|
421
|
+
interface ReportChapter {
|
|
422
|
+
/** 册文件 stem,如 "001" */
|
|
423
|
+
file: string;
|
|
424
|
+
result: CompareResult;
|
|
425
|
+
}
|
|
426
|
+
interface ReportOptions {
|
|
427
|
+
title: string;
|
|
428
|
+
baseLabel: string;
|
|
429
|
+
refLabel: string;
|
|
430
|
+
/** 报告里逐条列出哪些类别;缺省 用字不同、对照本多、对照本少(异体、版式差异只计数) */
|
|
431
|
+
list?: readonly DiffCat[];
|
|
432
|
+
}
|
|
433
|
+
/** `页:列:序[子列]` → 「P页C列K字」;认不出原样返回 */
|
|
434
|
+
declare function anchorText(anchor: string): string;
|
|
435
|
+
declare function countByCat(items: readonly CompareItem[]): Record<DiffCat, number>;
|
|
436
|
+
declare function buildReport(chapters: readonly ReportChapter[], opts: ReportOptions): string;
|
|
437
|
+
/** 「001-005」「001,003,010-012」→ ['001','002',…];保持位数 */
|
|
438
|
+
declare function parseChapters(spec: string): string[];
|
|
439
|
+
|
|
440
|
+
export { ALL_CATS, type BaseCell, 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 VariantTable, type VariantTest, type VolumeSeg, type WikiChapter, anchorText, assertSameLength, buildReport, buildVariantTest, cellLane, chapterChars, charSlots, classify, cnNumber, codePointLength, compareChapter, compareWhole, countByCat, createGlyphConverters, createToSimplified, diffChars, entityHref, extractType, figAlts, formatCmp, frontMatter, loadVariantTest, nextContinues, normalizeVariants, parseChapters, parseCmp, renderLines, renderRich, resolveTargetKey, runCompare, splitVolumes, trimWikiToBase, volumeNo, volumeRangeOf, wikiChars, wikiFileOf };
|
package/dist/index.js
CHANGED
|
@@ -458,7 +458,7 @@ function classify(h, base, wiki, opts) {
|
|
|
458
458
|
|
|
459
459
|
// src/compare/diff.ts
|
|
460
460
|
var MAX_EDIT_DISTANCE = 4e3;
|
|
461
|
-
function myers(a, b, maxD) {
|
|
461
|
+
function myers(a, b, maxD, same) {
|
|
462
462
|
const n = a.length;
|
|
463
463
|
const m = b.length;
|
|
464
464
|
const max = Math.min(n + m, maxD);
|
|
@@ -471,7 +471,7 @@ function myers(a, b, maxD) {
|
|
|
471
471
|
for (let k = -d; k <= d; k += 2) {
|
|
472
472
|
let x2 = k === -d || k !== d && v[off + k - 1] < v[off + k + 1] ? v[off + k + 1] : v[off + k - 1] + 1;
|
|
473
473
|
let y2 = x2 - k;
|
|
474
|
-
while (x2 < n && y2 < m && a[x2]
|
|
474
|
+
while (x2 < n && y2 < m && same(a[x2], b[y2])) {
|
|
475
475
|
x2++;
|
|
476
476
|
y2++;
|
|
477
477
|
}
|
|
@@ -510,19 +510,51 @@ function myers(a, b, maxD) {
|
|
|
510
510
|
}
|
|
511
511
|
return ed.reverse();
|
|
512
512
|
}
|
|
513
|
-
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE) {
|
|
513
|
+
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE, isVariant) {
|
|
514
|
+
const same = isVariant ? (x, y) => x === y || isVariant(x, y) : (x, y) => x === y;
|
|
515
|
+
const hunks = diffRaw(a, b, maxD, same);
|
|
516
|
+
if (!hunks || !isVariant) return hunks;
|
|
517
|
+
const out = [];
|
|
518
|
+
let ai = 0;
|
|
519
|
+
let bi = 0;
|
|
520
|
+
const gap = (to) => {
|
|
521
|
+
let run = null;
|
|
522
|
+
for (let k = 0; ai + k < to; k++) {
|
|
523
|
+
if (a[ai + k] !== b[bi + k]) {
|
|
524
|
+
if (!run) run = { i1: ai + k, i2: ai + k + 1, j1: bi + k, j2: bi + k + 1, variant: true };
|
|
525
|
+
else {
|
|
526
|
+
run.i2++;
|
|
527
|
+
run.j2++;
|
|
528
|
+
}
|
|
529
|
+
} else if (run) {
|
|
530
|
+
out.push(run);
|
|
531
|
+
run = null;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (run) out.push(run);
|
|
535
|
+
};
|
|
536
|
+
for (const h of hunks) {
|
|
537
|
+
gap(h.i1);
|
|
538
|
+
out.push(h);
|
|
539
|
+
ai = h.i2;
|
|
540
|
+
bi = h.j2;
|
|
541
|
+
}
|
|
542
|
+
gap(a.length);
|
|
543
|
+
return out;
|
|
544
|
+
}
|
|
545
|
+
function diffRaw(a, b, maxD, same) {
|
|
514
546
|
let lo = 0;
|
|
515
|
-
while (lo < a.length && lo < b.length && a[lo]
|
|
547
|
+
while (lo < a.length && lo < b.length && same(a[lo], b[lo])) lo++;
|
|
516
548
|
let ea = a.length;
|
|
517
549
|
let eb = b.length;
|
|
518
|
-
while (ea > lo && eb > lo && a[ea - 1]
|
|
550
|
+
while (ea > lo && eb > lo && same(a[ea - 1], b[eb - 1])) {
|
|
519
551
|
ea--;
|
|
520
552
|
eb--;
|
|
521
553
|
}
|
|
522
554
|
const sa = a.slice(lo, ea);
|
|
523
555
|
const sb = b.slice(lo, eb);
|
|
524
556
|
if (sa.length === 0 && sb.length === 0) return [];
|
|
525
|
-
const ed = myers(sa, sb, maxD);
|
|
557
|
+
const ed = myers(sa, sb, maxD, same);
|
|
526
558
|
if (!ed) return null;
|
|
527
559
|
const ops = [];
|
|
528
560
|
let cur = null;
|
|
@@ -582,7 +614,17 @@ function volumeNo(label) {
|
|
|
582
614
|
}
|
|
583
615
|
function wikiFileOf(no, chapters) {
|
|
584
616
|
const want = `\u5377${String(no).padStart(3, "0")}`;
|
|
585
|
-
|
|
617
|
+
const exact = chapters.find((c) => c.title.trim() === want);
|
|
618
|
+
if (exact) return exact.file;
|
|
619
|
+
return chapters.find((c) => volumeNo(c.title) === no)?.file ?? null;
|
|
620
|
+
}
|
|
621
|
+
function volumeRangeOf(title) {
|
|
622
|
+
const m = /^卷([\d〇零一二三四五六七八九十百]+)(?:\s*[至到—–-]\s*卷?([\d〇零一二三四五六七八九十百]+))?/.exec(title.trim());
|
|
623
|
+
if (!m) return null;
|
|
624
|
+
const a = cnNumber(m[1]);
|
|
625
|
+
const b = m[2] ? cnNumber(m[2]) : a;
|
|
626
|
+
if (a === null || b === null || b < a || b - a > 200) return null;
|
|
627
|
+
return [a, b];
|
|
586
628
|
}
|
|
587
629
|
var anchorKey = (a) => a;
|
|
588
630
|
function splitVolumes(cells, headings, prevVolNo, lastContinues) {
|
|
@@ -620,15 +662,16 @@ function nextContinues(nextHeadings) {
|
|
|
620
662
|
var STATUS_LINE = /^(有标点|无标点|已修正|好$|簡繁|简繁)/;
|
|
621
663
|
var VOLUME_LINE = /^#+\s*卷[\d〇零一二三四五六七八九十百]+\s*$/;
|
|
622
664
|
var STRIP = /[\p{P}\p{Z}\s]/gu;
|
|
665
|
+
var ZI = /:zi\[[^\]\n]*\]/g;
|
|
666
|
+
var NOTE_MARK = /[<>|]/g;
|
|
623
667
|
function wikiChars(md) {
|
|
624
668
|
const lines = md.split("\n").map((s) => s.trim()).filter((s) => s && !VOLUME_LINE.test(s) && !STATUS_LINE.test(s)).map((s) => s.replace(/^#+\s*/, ""));
|
|
625
|
-
return Array.from(lines.join("").replace(STRIP, ""));
|
|
669
|
+
return Array.from(lines.join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
626
670
|
}
|
|
627
|
-
var NOTE_MARK = /[<>|]/g;
|
|
628
671
|
function chapterChars(md) {
|
|
629
672
|
const lines = md.split("\n").map((s) => s.trim()).filter(Boolean);
|
|
630
673
|
if (lines.length && /^#{1,2}\s/.test(lines[0])) lines.shift();
|
|
631
|
-
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
674
|
+
return Array.from(lines.map((s) => s.replace(/^#+\s*/, "")).join("").replace(ZI, "\u25A1").replace(NOTE_MARK, "").replace(STRIP, ""));
|
|
632
675
|
}
|
|
633
676
|
|
|
634
677
|
// src/compare/compare.ts
|
|
@@ -671,25 +714,43 @@ function trimWikiToBase(base, wiki, start, end) {
|
|
|
671
714
|
}
|
|
672
715
|
return j0 < j1 ? [j0, j1] : [0, wiki.length];
|
|
673
716
|
}
|
|
674
|
-
async function compareSeg(input, seg, out, idStart, whole) {
|
|
717
|
+
async function compareSeg(input, seg, out, idStart, whole, range) {
|
|
675
718
|
const res = { no: seg.no, label: seg.label, from: seg.from, to: seg.to, status: "ok", wikiFile: null };
|
|
676
|
-
|
|
677
|
-
if (
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
if (
|
|
719
|
+
let wiki;
|
|
720
|
+
if (whole) {
|
|
721
|
+
res.wikiFile = whole.file;
|
|
722
|
+
wiki = chapterChars(whole.md);
|
|
723
|
+
} else if (range) {
|
|
724
|
+
const files = [];
|
|
725
|
+
for (let n = range[0]; n <= range[1]; n++) {
|
|
726
|
+
const f = wikiFileOf(n, input.wikiChapters);
|
|
727
|
+
if (!f) return { ...res, wikiFile: files[0] ?? null, wikiFiles: files, status: "no-wiki" };
|
|
728
|
+
files.push(f);
|
|
729
|
+
}
|
|
730
|
+
res.wikiFile = files[0];
|
|
731
|
+
res.wikiFiles = files;
|
|
732
|
+
const mds = await Promise.all(files.map((f) => input.getWikiMd(f)));
|
|
733
|
+
if (mds.some((m) => m === null)) return { ...res, status: "fetch-failed" };
|
|
734
|
+
wiki = mds.flatMap((m) => wikiChars(m));
|
|
735
|
+
} else {
|
|
736
|
+
const file = seg.no === null ? null : wikiFileOf(seg.no, input.wikiChapters);
|
|
737
|
+
if (!file) return { ...res, status: "no-wiki" };
|
|
738
|
+
res.wikiFile = file;
|
|
739
|
+
const md = await input.getWikiMd(file);
|
|
740
|
+
if (md === null) return { ...res, status: "fetch-failed" };
|
|
741
|
+
wiki = wikiChars(md);
|
|
742
|
+
}
|
|
681
743
|
const base = input.cells.slice(seg.from, seg.to).map((c) => c.c);
|
|
682
|
-
|
|
683
|
-
if (!whole) {
|
|
744
|
+
if (!whole && !range) {
|
|
684
745
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
685
746
|
wiki = wiki.slice(j0, j1);
|
|
686
747
|
}
|
|
687
|
-
const hunks = diffChars(base, wiki);
|
|
748
|
+
const hunks = diffChars(base, wiki, void 0, input.isVariant);
|
|
688
749
|
if (!hunks) return { ...res, status: "too-different" };
|
|
689
750
|
const structEnd = seg.headEnd - seg.from;
|
|
690
751
|
let changed = 0;
|
|
691
752
|
hunks.forEach((h, n) => {
|
|
692
|
-
const cat = classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
753
|
+
const cat = h.variant ? "glyph" : classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
693
754
|
const ins = h.i1 === h.i2;
|
|
694
755
|
const del = h.j1 === h.j2;
|
|
695
756
|
changed += Math.max(h.i2 - h.i1, h.j2 - h.j1);
|
|
@@ -717,6 +778,15 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
717
778
|
return res;
|
|
718
779
|
}
|
|
719
780
|
async function compareChapter(input) {
|
|
781
|
+
const range = input.volumeRange;
|
|
782
|
+
if (range && !input.headings.some((h) => h.level === 1)) {
|
|
783
|
+
const label = input.volumeLabel ?? (range[0] === range[1] ? `\u5377${range[0]}` : `\u5377${range[0]}\u81F3\u5377${range[1]}`);
|
|
784
|
+
const seg = { no: range[0], label, from: 0, to: input.cells.length, startsPartial: false, endsPartial: false, headEnd: 0 };
|
|
785
|
+
const items2 = [];
|
|
786
|
+
const res = await compareSeg(input, seg, items2, 0, void 0, range);
|
|
787
|
+
items2.sort((a, b) => a.i1 - b.i1 || a.i2 - b.i2);
|
|
788
|
+
return { items: items2, segments: [res] };
|
|
789
|
+
}
|
|
720
790
|
const segs = splitVolumes(input.cells, input.headings, input.prevVolNo, input.lastContinues);
|
|
721
791
|
const items = [];
|
|
722
792
|
const segments = [];
|
|
@@ -762,7 +832,9 @@ async function resolveTargetKey(transport, target) {
|
|
|
762
832
|
const m = await transport.getTextManifest?.(target.id);
|
|
763
833
|
const vs = m?.versions;
|
|
764
834
|
if (!Array.isArray(vs) || vs.length === 0) return null;
|
|
765
|
-
|
|
835
|
+
const main = vs.find((v) => v.key === "default") ?? vs[0];
|
|
836
|
+
if (main.kind !== "collated") return main.key;
|
|
837
|
+
return vs.find((v) => v.kind !== "collated")?.key ?? main.key;
|
|
766
838
|
}
|
|
767
839
|
async function runCompare(input) {
|
|
768
840
|
const { base, target, cells } = input;
|
|
@@ -806,7 +878,10 @@ async function runCompare(input) {
|
|
|
806
878
|
lastContinues: nextContinues(at >= 0 && at + 1 < chapters.length ? headingsOf(chapters[at + 1]) : void 0),
|
|
807
879
|
wikiChapters,
|
|
808
880
|
getWikiMd,
|
|
809
|
-
isVariant
|
|
881
|
+
isVariant,
|
|
882
|
+
// 册无卷标题(如全唐文自校本一册一章、章名「卷七至卷十」):按章名取对照本的几卷
|
|
883
|
+
volumeRange: typeof chapters[at]?.title === "string" ? volumeRangeOf(chapters[at].title) : null,
|
|
884
|
+
volumeLabel: typeof chapters[at]?.title === "string" ? chapters[at].title : void 0
|
|
810
885
|
});
|
|
811
886
|
return { ...result, targetKey, wikiChapters };
|
|
812
887
|
}
|
|
@@ -827,6 +902,78 @@ function parseCmp(raw) {
|
|
|
827
902
|
function formatCmp(t) {
|
|
828
903
|
return t.key ? `${t.id}-${t.key}` : t.id;
|
|
829
904
|
}
|
|
905
|
+
|
|
906
|
+
// src/compare/report.ts
|
|
907
|
+
var CAT_TITLE = {
|
|
908
|
+
word: "\u7528\u5B57\u4E0D\u540C",
|
|
909
|
+
add: "\u5BF9\u7167\u672C\u591A",
|
|
910
|
+
omit: "\u5BF9\u7167\u672C\u5C11",
|
|
911
|
+
glyph: "\u5F02\u4F53\u5B57",
|
|
912
|
+
struct: "\u7248\u5F0F\u5DEE\u5F02"
|
|
913
|
+
};
|
|
914
|
+
function anchorText(anchor) {
|
|
915
|
+
const m = /^(\d+):(\d+):(-?\d+)/.exec(anchor);
|
|
916
|
+
return m ? `${m[1]}\u9875${m[2]}\u5217${m[3]}\u5B57` : anchor;
|
|
917
|
+
}
|
|
918
|
+
var esc = (s) => s.replace(/\|/g, "\\|");
|
|
919
|
+
function row(it) {
|
|
920
|
+
const b = it.base || "\uFF08\u65E0\uFF09";
|
|
921
|
+
const r = it.reading || "\uFF08\u65E0\uFF09";
|
|
922
|
+
const [b0, b1] = it.ctx.base;
|
|
923
|
+
const [r0, r1] = it.ctx.reading;
|
|
924
|
+
return `| ${anchorText(it.anchor.start)} | ${esc(b0)}**${esc(b)}**${esc(b1)} | ${esc(r0)}**${esc(r)}**${esc(r1)} |`;
|
|
925
|
+
}
|
|
926
|
+
function countByCat(items) {
|
|
927
|
+
const out = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
928
|
+
for (const it of items) out[it.cat]++;
|
|
929
|
+
return out;
|
|
930
|
+
}
|
|
931
|
+
function buildReport(chapters, opts) {
|
|
932
|
+
const list = opts.list ?? ["word", "add", "omit"];
|
|
933
|
+
const L = [];
|
|
934
|
+
L.push(`# ${opts.title}`, "", `\u5E95\u672C\uFF1A${opts.baseLabel}\u3000\u5BF9\u7167\u672C\uFF1A${opts.refLabel}`, "");
|
|
935
|
+
L.push("\u7C7B\u522B\uFF1A**\u7528\u5B57\u4E0D\u540C**\uFF1D\u540C\u4F4D\u7F6E\u4E24\u7248\u5B57\u4E0D\u540C\uFF08\u542B\u901A\u5047\uFF09\uFF1B**\u5BF9\u7167\u672C\u591A**\uFF1D\u5BF9\u7167\u672C\u6709\u3001\u5E95\u672C\u6CA1\u6709\uFF1B**\u5BF9\u7167\u672C\u5C11**\uFF1D\u5E95\u672C\u6709\u3001\u5BF9\u7167\u672C\u6CA1\u6709\uFF1B\u5F02\u4F53\u5B57\uFF08\u5F62\u8FD1\u5F02\u4F53\uFF0C\u5982 \u8462\uFF0F\u84CB\uFF09\u4E0E\u7248\u5F0F\u5DEE\u5F02\u53EA\u8BA1\u6570\uFF0C\u4E0D\u9010\u6761\u5217\u3002", "");
|
|
936
|
+
L.push("## \u6C47\u603B", "", "| \u518C | \u5E95\u672C\u5B57\u6570 | \u76F8\u4F3C\u5EA6 | \u7528\u5B57\u4E0D\u540C | \u5BF9\u7167\u672C\u591A | \u5BF9\u7167\u672C\u5C11 | \u5F02\u4F53\u5B57 | \u7248\u5F0F |", "|---|---:|---:|---:|---:|---:|---:|---:|");
|
|
937
|
+
const total = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
938
|
+
for (const ch of chapters) {
|
|
939
|
+
const c = countByCat(ch.result.items);
|
|
940
|
+
for (const k of Object.keys(total)) total[k] += c[k];
|
|
941
|
+
const seg = ch.result.segments[0];
|
|
942
|
+
const status = seg && seg.status !== "ok" ? `\uFF08${seg.status}\uFF09` : "";
|
|
943
|
+
const n = seg ? seg.to - seg.from : 0;
|
|
944
|
+
L.push(`| ${ch.file} | ${n} | ${seg?.similarity !== void 0 ? (seg.similarity * 100).toFixed(2) + "%" : "-"}${status} | ${c.word} | ${c.add} | ${c.omit} | ${c.glyph} | ${c.struct} |`);
|
|
945
|
+
}
|
|
946
|
+
L.push(`| **\u5408\u8BA1** | | | ${total.word} | ${total.add} | ${total.omit} | ${total.glyph} | ${total.struct} |`, "");
|
|
947
|
+
for (const ch of chapters) {
|
|
948
|
+
L.push(`## \u7B2C ${ch.file} \u518C`, "");
|
|
949
|
+
const seg = ch.result.segments[0];
|
|
950
|
+
if (seg && seg.status !== "ok") {
|
|
951
|
+
L.push(`\u6CA1\u6709\u6BD4\u51FA\u7ED3\u679C\uFF1A${seg.status}\u3002`, "");
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
for (const cat of list) {
|
|
955
|
+
const its = ch.result.items.filter((i) => i.cat === cat);
|
|
956
|
+
if (!its.length) continue;
|
|
957
|
+
L.push(`### ${CAT_TITLE[cat]}\uFF08${its.length}\uFF09`, "", `| \u4F4D\u7F6E | ${esc(opts.baseLabel)} | ${esc(opts.refLabel)} |`, "|---|---|---|");
|
|
958
|
+
for (const it of its) L.push(row(it));
|
|
959
|
+
L.push("");
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
return L.join("\n");
|
|
963
|
+
}
|
|
964
|
+
function parseChapters(spec) {
|
|
965
|
+
const out = [];
|
|
966
|
+
for (const part of spec.split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
967
|
+
const m = /^(\d+)-(\d+)$/.exec(part);
|
|
968
|
+
if (!m) {
|
|
969
|
+
out.push(part);
|
|
970
|
+
continue;
|
|
971
|
+
}
|
|
972
|
+
const w = m[1].length;
|
|
973
|
+
for (let n = +m[1]; n <= +m[2]; n++) out.push(String(n).padStart(w, "0"));
|
|
974
|
+
}
|
|
975
|
+
return out;
|
|
976
|
+
}
|
|
830
977
|
export {
|
|
831
978
|
ALL_CATS,
|
|
832
979
|
FORMAT_LABEL,
|
|
@@ -834,9 +981,11 @@ export {
|
|
|
834
981
|
GujiFormatError,
|
|
835
982
|
MAX_EDIT_DISTANCE,
|
|
836
983
|
SOLO_ATTR,
|
|
984
|
+
anchorText,
|
|
837
985
|
assertSameLength,
|
|
838
986
|
availableFormats,
|
|
839
987
|
buildExport,
|
|
988
|
+
buildReport,
|
|
840
989
|
buildVariantTest,
|
|
841
990
|
cellLane,
|
|
842
991
|
chapterChars,
|
|
@@ -846,6 +995,7 @@ export {
|
|
|
846
995
|
codePointLength,
|
|
847
996
|
compareChapter,
|
|
848
997
|
compareWhole,
|
|
998
|
+
countByCat,
|
|
849
999
|
createGlyphConverters,
|
|
850
1000
|
createToSimplified,
|
|
851
1001
|
diffChars,
|
|
@@ -857,6 +1007,7 @@ export {
|
|
|
857
1007
|
loadVariantTest,
|
|
858
1008
|
nextContinues,
|
|
859
1009
|
normalizeVariants,
|
|
1010
|
+
parseChapters,
|
|
860
1011
|
parseCmp,
|
|
861
1012
|
renderLines,
|
|
862
1013
|
renderRich,
|
|
@@ -865,6 +1016,7 @@ export {
|
|
|
865
1016
|
splitVolumes,
|
|
866
1017
|
trimWikiToBase,
|
|
867
1018
|
volumeNo,
|
|
1019
|
+
volumeRangeOf,
|
|
868
1020
|
wikiChars,
|
|
869
1021
|
wikiFileOf
|
|
870
1022
|
};
|