book-text-util 0.1.0 → 0.1.2
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 +154 -8
- package/dist/index.cjs +120 -8
- package/dist/index.d.cts +36 -3
- package/dist/index.d.ts +36 -3
- package/dist/index.js +116 -8
- 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;
|
|
@@ -648,12 +680,12 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
648
680
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
649
681
|
wiki = wiki.slice(j0, j1);
|
|
650
682
|
}
|
|
651
|
-
const hunks = diffChars(base, wiki);
|
|
683
|
+
const hunks = diffChars(base, wiki, void 0, input.isVariant);
|
|
652
684
|
if (!hunks) return { ...res, status: "too-different" };
|
|
653
685
|
const structEnd = seg.headEnd - seg.from;
|
|
654
686
|
let changed = 0;
|
|
655
687
|
hunks.forEach((h, n) => {
|
|
656
|
-
const cat = classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
688
|
+
const cat = h.variant ? "glyph" : classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
657
689
|
const ins = h.i1 === h.i2;
|
|
658
690
|
const del = h.j1 === h.j2;
|
|
659
691
|
changed += Math.max(h.i2 - h.i1, h.j2 - h.j1);
|
|
@@ -701,12 +733,88 @@ function buildVariantTest(table) {
|
|
|
701
733
|
return (a, b) => set.has(a + b);
|
|
702
734
|
}
|
|
703
735
|
|
|
736
|
+
// src/compare/report.ts
|
|
737
|
+
var CAT_TITLE = {
|
|
738
|
+
word: "\u7528\u5B57\u4E0D\u540C",
|
|
739
|
+
add: "\u5BF9\u7167\u672C\u591A",
|
|
740
|
+
omit: "\u5BF9\u7167\u672C\u5C11",
|
|
741
|
+
glyph: "\u5F02\u4F53\u5B57",
|
|
742
|
+
struct: "\u7248\u5F0F\u5DEE\u5F02"
|
|
743
|
+
};
|
|
744
|
+
function anchorText(anchor) {
|
|
745
|
+
const m = /^(\d+):(\d+):(-?\d+)/.exec(anchor);
|
|
746
|
+
return m ? `${m[1]}\u9875${m[2]}\u5217${m[3]}\u5B57` : anchor;
|
|
747
|
+
}
|
|
748
|
+
var esc = (s) => s.replace(/\|/g, "\\|");
|
|
749
|
+
function row(it) {
|
|
750
|
+
const b = it.base || "\uFF08\u65E0\uFF09";
|
|
751
|
+
const r = it.reading || "\uFF08\u65E0\uFF09";
|
|
752
|
+
const [b0, b1] = it.ctx.base;
|
|
753
|
+
const [r0, r1] = it.ctx.reading;
|
|
754
|
+
return `| ${anchorText(it.anchor.start)} | ${esc(b0)}**${esc(b)}**${esc(b1)} | ${esc(r0)}**${esc(r)}**${esc(r1)} |`;
|
|
755
|
+
}
|
|
756
|
+
function countByCat(items) {
|
|
757
|
+
const out = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
758
|
+
for (const it of items) out[it.cat]++;
|
|
759
|
+
return out;
|
|
760
|
+
}
|
|
761
|
+
function buildReport(chapters, opts) {
|
|
762
|
+
const list = opts.list ?? ["word", "add", "omit"];
|
|
763
|
+
const L = [];
|
|
764
|
+
L.push(`# ${opts.title}`, "", `\u5E95\u672C\uFF1A${opts.baseLabel}\u3000\u5BF9\u7167\u672C\uFF1A${opts.refLabel}`, "");
|
|
765
|
+
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", "");
|
|
766
|
+
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 |", "|---|---:|---:|---:|---:|---:|---:|---:|");
|
|
767
|
+
const total = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
768
|
+
for (const ch of chapters) {
|
|
769
|
+
const c = countByCat(ch.result.items);
|
|
770
|
+
for (const k of Object.keys(total)) total[k] += c[k];
|
|
771
|
+
const seg = ch.result.segments[0];
|
|
772
|
+
const status = seg && seg.status !== "ok" ? `\uFF08${seg.status}\uFF09` : "";
|
|
773
|
+
const n = seg ? seg.to - seg.from : 0;
|
|
774
|
+
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} |`);
|
|
775
|
+
}
|
|
776
|
+
L.push(`| **\u5408\u8BA1** | | | ${total.word} | ${total.add} | ${total.omit} | ${total.glyph} | ${total.struct} |`, "");
|
|
777
|
+
for (const ch of chapters) {
|
|
778
|
+
L.push(`## \u7B2C ${ch.file} \u518C`, "");
|
|
779
|
+
const seg = ch.result.segments[0];
|
|
780
|
+
if (seg && seg.status !== "ok") {
|
|
781
|
+
L.push(`\u6CA1\u6709\u6BD4\u51FA\u7ED3\u679C\uFF1A${seg.status}\u3002`, "");
|
|
782
|
+
continue;
|
|
783
|
+
}
|
|
784
|
+
for (const cat of list) {
|
|
785
|
+
const its = ch.result.items.filter((i) => i.cat === cat);
|
|
786
|
+
if (!its.length) continue;
|
|
787
|
+
L.push(`### ${CAT_TITLE[cat]}\uFF08${its.length}\uFF09`, "", `| \u4F4D\u7F6E | ${esc(opts.baseLabel)} | ${esc(opts.refLabel)} |`, "|---|---|---|");
|
|
788
|
+
for (const it of its) L.push(row(it));
|
|
789
|
+
L.push("");
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return L.join("\n");
|
|
793
|
+
}
|
|
794
|
+
function parseChapters(spec) {
|
|
795
|
+
const out = [];
|
|
796
|
+
for (const part of spec.split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
797
|
+
const m = /^(\d+)-(\d+)$/.exec(part);
|
|
798
|
+
if (!m) {
|
|
799
|
+
out.push(part);
|
|
800
|
+
continue;
|
|
801
|
+
}
|
|
802
|
+
const w = m[1].length;
|
|
803
|
+
for (let n = +m[1]; n <= +m[2]; n++) out.push(String(n).padStart(w, "0"));
|
|
804
|
+
}
|
|
805
|
+
return out;
|
|
806
|
+
}
|
|
807
|
+
|
|
704
808
|
// src/cli.ts
|
|
809
|
+
import { writeFile as writeFile2 } from "fs/promises";
|
|
705
810
|
var HELP = `\u7528\u6CD5\uFF1A
|
|
706
811
|
book-text-util export --root <\u7248\u672C\u76EE\u5F55> --chapter <\u518C\u53F7> [--format lines|punct|entity] [--links]
|
|
707
812
|
[--glyph original|traditional|simplified] [--title \u4E66\u540D] [--id \u6761\u76EEid] [--out \u76EE\u5F55]
|
|
708
813
|
book-text-util compare --base <\u5E95\u672C.txt> --ref <\u5BF9\u7167\u672C.md> [--variants <glyph-variants.json>] [--json]
|
|
709
814
|
|
|
815
|
+
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>]
|
|
816
|
+
[--title \u6807\u9898] [--base-label \u540D] [--ref-label \u540D] [--list word,add,omit,glyph] [--out \u62A5\u544A.md]
|
|
817
|
+
|
|
710
818
|
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
819
|
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
820
|
function die(msg) {
|
|
@@ -790,7 +898,45 @@ async function cmdCompare(argv) {
|
|
|
790
898
|
`);
|
|
791
899
|
}
|
|
792
900
|
}
|
|
901
|
+
async function cmdReport(argv) {
|
|
902
|
+
const { values: v } = parseArgs({
|
|
903
|
+
args: argv,
|
|
904
|
+
options: {
|
|
905
|
+
"base-root": { type: "string" },
|
|
906
|
+
"ref-root": { type: "string" },
|
|
907
|
+
chapters: { type: "string" },
|
|
908
|
+
variants: { type: "string" },
|
|
909
|
+
title: { type: "string" },
|
|
910
|
+
"base-label": { type: "string", default: "\u5E95\u672C" },
|
|
911
|
+
"ref-label": { type: "string", default: "\u5BF9\u7167\u672C" },
|
|
912
|
+
list: { type: "string" },
|
|
913
|
+
out: { type: "string" }
|
|
914
|
+
}
|
|
915
|
+
});
|
|
916
|
+
if (!v["base-root"] || !v["ref-root"] || !v.chapters) die("\u9700\u8981 --base-root\u3001--ref-root\u3001--chapters\n\n" + HELP);
|
|
917
|
+
const table = v.variants ? JSON.parse(await readFile2(v.variants, "utf-8")) : null;
|
|
918
|
+
const isVariant = table ? buildVariantTest(table) : null;
|
|
919
|
+
const chapters = [];
|
|
920
|
+
for (const file of parseChapters(v.chapters)) {
|
|
921
|
+
const char = JSON.parse(await readFile2(join2(v["base-root"], `${file}.char.json`), "utf-8"));
|
|
922
|
+
const cells = charSlots(char).map(([a, c]) => ({ a, c }));
|
|
923
|
+
const md2 = await readFile2(join2(v["ref-root"], `${file}.md`), "utf-8");
|
|
924
|
+
chapters.push({ file, result: await compareWhole({ cells, md: md2, file, label: file, isVariant }) });
|
|
925
|
+
}
|
|
926
|
+
const md = buildReport(chapters, {
|
|
927
|
+
title: v.title ?? `\u5BF9\u6821\u62A5\u544A ${v.chapters}`,
|
|
928
|
+
baseLabel: v["base-label"],
|
|
929
|
+
refLabel: v["ref-label"],
|
|
930
|
+
...v.list ? { list: v.list.split(",") } : {}
|
|
931
|
+
});
|
|
932
|
+
if (v.out) {
|
|
933
|
+
await writeFile2(v.out, md, "utf-8");
|
|
934
|
+
process.stdout.write(`${v.out}
|
|
935
|
+
`);
|
|
936
|
+
} else process.stdout.write(md + "\n");
|
|
937
|
+
}
|
|
793
938
|
var [cmd, ...rest] = process.argv.slice(2);
|
|
794
939
|
if (cmd === "export") await cmdExport(rest);
|
|
795
940
|
else if (cmd === "compare") await cmdCompare(rest);
|
|
941
|
+
else if (cmd === "report") await cmdReport(rest);
|
|
796
942
|
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,
|
|
@@ -522,7 +526,7 @@ function classify(h, base, wiki, opts) {
|
|
|
522
526
|
|
|
523
527
|
// src/compare/diff.ts
|
|
524
528
|
var MAX_EDIT_DISTANCE = 4e3;
|
|
525
|
-
function myers(a, b, maxD) {
|
|
529
|
+
function myers(a, b, maxD, same) {
|
|
526
530
|
const n = a.length;
|
|
527
531
|
const m = b.length;
|
|
528
532
|
const max = Math.min(n + m, maxD);
|
|
@@ -535,7 +539,7 @@ function myers(a, b, maxD) {
|
|
|
535
539
|
for (let k = -d; k <= d; k += 2) {
|
|
536
540
|
let x2 = k === -d || k !== d && v[off + k - 1] < v[off + k + 1] ? v[off + k + 1] : v[off + k - 1] + 1;
|
|
537
541
|
let y2 = x2 - k;
|
|
538
|
-
while (x2 < n && y2 < m && a[x2]
|
|
542
|
+
while (x2 < n && y2 < m && same(a[x2], b[y2])) {
|
|
539
543
|
x2++;
|
|
540
544
|
y2++;
|
|
541
545
|
}
|
|
@@ -574,19 +578,51 @@ function myers(a, b, maxD) {
|
|
|
574
578
|
}
|
|
575
579
|
return ed.reverse();
|
|
576
580
|
}
|
|
577
|
-
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE) {
|
|
581
|
+
function diffChars(a, b, maxD = MAX_EDIT_DISTANCE, isVariant) {
|
|
582
|
+
const same = isVariant ? (x, y) => x === y || isVariant(x, y) : (x, y) => x === y;
|
|
583
|
+
const hunks = diffRaw(a, b, maxD, same);
|
|
584
|
+
if (!hunks || !isVariant) return hunks;
|
|
585
|
+
const out = [];
|
|
586
|
+
let ai = 0;
|
|
587
|
+
let bi = 0;
|
|
588
|
+
const gap = (to) => {
|
|
589
|
+
let run = null;
|
|
590
|
+
for (let k = 0; ai + k < to; k++) {
|
|
591
|
+
if (a[ai + k] !== b[bi + k]) {
|
|
592
|
+
if (!run) run = { i1: ai + k, i2: ai + k + 1, j1: bi + k, j2: bi + k + 1, variant: true };
|
|
593
|
+
else {
|
|
594
|
+
run.i2++;
|
|
595
|
+
run.j2++;
|
|
596
|
+
}
|
|
597
|
+
} else if (run) {
|
|
598
|
+
out.push(run);
|
|
599
|
+
run = null;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
if (run) out.push(run);
|
|
603
|
+
};
|
|
604
|
+
for (const h of hunks) {
|
|
605
|
+
gap(h.i1);
|
|
606
|
+
out.push(h);
|
|
607
|
+
ai = h.i2;
|
|
608
|
+
bi = h.j2;
|
|
609
|
+
}
|
|
610
|
+
gap(a.length);
|
|
611
|
+
return out;
|
|
612
|
+
}
|
|
613
|
+
function diffRaw(a, b, maxD, same) {
|
|
578
614
|
let lo = 0;
|
|
579
|
-
while (lo < a.length && lo < b.length && a[lo]
|
|
615
|
+
while (lo < a.length && lo < b.length && same(a[lo], b[lo])) lo++;
|
|
580
616
|
let ea = a.length;
|
|
581
617
|
let eb = b.length;
|
|
582
|
-
while (ea > lo && eb > lo && a[ea - 1]
|
|
618
|
+
while (ea > lo && eb > lo && same(a[ea - 1], b[eb - 1])) {
|
|
583
619
|
ea--;
|
|
584
620
|
eb--;
|
|
585
621
|
}
|
|
586
622
|
const sa = a.slice(lo, ea);
|
|
587
623
|
const sb = b.slice(lo, eb);
|
|
588
624
|
if (sa.length === 0 && sb.length === 0) return [];
|
|
589
|
-
const ed = myers(sa, sb, maxD);
|
|
625
|
+
const ed = myers(sa, sb, maxD, same);
|
|
590
626
|
if (!ed) return null;
|
|
591
627
|
const ops = [];
|
|
592
628
|
let cur = null;
|
|
@@ -748,12 +784,12 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
748
784
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
749
785
|
wiki = wiki.slice(j0, j1);
|
|
750
786
|
}
|
|
751
|
-
const hunks = diffChars(base, wiki);
|
|
787
|
+
const hunks = diffChars(base, wiki, void 0, input.isVariant);
|
|
752
788
|
if (!hunks) return { ...res, status: "too-different" };
|
|
753
789
|
const structEnd = seg.headEnd - seg.from;
|
|
754
790
|
let changed = 0;
|
|
755
791
|
hunks.forEach((h, n) => {
|
|
756
|
-
const cat = classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
792
|
+
const cat = h.variant ? "glyph" : classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
757
793
|
const ins = h.i1 === h.i2;
|
|
758
794
|
const del = h.j1 === h.j2;
|
|
759
795
|
changed += Math.max(h.i2 - h.i1, h.j2 - h.j1);
|
|
@@ -891,6 +927,78 @@ function parseCmp(raw) {
|
|
|
891
927
|
function formatCmp(t) {
|
|
892
928
|
return t.key ? `${t.id}-${t.key}` : t.id;
|
|
893
929
|
}
|
|
930
|
+
|
|
931
|
+
// src/compare/report.ts
|
|
932
|
+
var CAT_TITLE = {
|
|
933
|
+
word: "\u7528\u5B57\u4E0D\u540C",
|
|
934
|
+
add: "\u5BF9\u7167\u672C\u591A",
|
|
935
|
+
omit: "\u5BF9\u7167\u672C\u5C11",
|
|
936
|
+
glyph: "\u5F02\u4F53\u5B57",
|
|
937
|
+
struct: "\u7248\u5F0F\u5DEE\u5F02"
|
|
938
|
+
};
|
|
939
|
+
function anchorText(anchor) {
|
|
940
|
+
const m = /^(\d+):(\d+):(-?\d+)/.exec(anchor);
|
|
941
|
+
return m ? `${m[1]}\u9875${m[2]}\u5217${m[3]}\u5B57` : anchor;
|
|
942
|
+
}
|
|
943
|
+
var esc = (s) => s.replace(/\|/g, "\\|");
|
|
944
|
+
function row(it) {
|
|
945
|
+
const b = it.base || "\uFF08\u65E0\uFF09";
|
|
946
|
+
const r = it.reading || "\uFF08\u65E0\uFF09";
|
|
947
|
+
const [b0, b1] = it.ctx.base;
|
|
948
|
+
const [r0, r1] = it.ctx.reading;
|
|
949
|
+
return `| ${anchorText(it.anchor.start)} | ${esc(b0)}**${esc(b)}**${esc(b1)} | ${esc(r0)}**${esc(r)}**${esc(r1)} |`;
|
|
950
|
+
}
|
|
951
|
+
function countByCat(items) {
|
|
952
|
+
const out = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
953
|
+
for (const it of items) out[it.cat]++;
|
|
954
|
+
return out;
|
|
955
|
+
}
|
|
956
|
+
function buildReport(chapters, opts) {
|
|
957
|
+
const list = opts.list ?? ["word", "add", "omit"];
|
|
958
|
+
const L = [];
|
|
959
|
+
L.push(`# ${opts.title}`, "", `\u5E95\u672C\uFF1A${opts.baseLabel}\u3000\u5BF9\u7167\u672C\uFF1A${opts.refLabel}`, "");
|
|
960
|
+
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", "");
|
|
961
|
+
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 |", "|---|---:|---:|---:|---:|---:|---:|---:|");
|
|
962
|
+
const total = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
963
|
+
for (const ch of chapters) {
|
|
964
|
+
const c = countByCat(ch.result.items);
|
|
965
|
+
for (const k of Object.keys(total)) total[k] += c[k];
|
|
966
|
+
const seg = ch.result.segments[0];
|
|
967
|
+
const status = seg && seg.status !== "ok" ? `\uFF08${seg.status}\uFF09` : "";
|
|
968
|
+
const n = seg ? seg.to - seg.from : 0;
|
|
969
|
+
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} |`);
|
|
970
|
+
}
|
|
971
|
+
L.push(`| **\u5408\u8BA1** | | | ${total.word} | ${total.add} | ${total.omit} | ${total.glyph} | ${total.struct} |`, "");
|
|
972
|
+
for (const ch of chapters) {
|
|
973
|
+
L.push(`## \u7B2C ${ch.file} \u518C`, "");
|
|
974
|
+
const seg = ch.result.segments[0];
|
|
975
|
+
if (seg && seg.status !== "ok") {
|
|
976
|
+
L.push(`\u6CA1\u6709\u6BD4\u51FA\u7ED3\u679C\uFF1A${seg.status}\u3002`, "");
|
|
977
|
+
continue;
|
|
978
|
+
}
|
|
979
|
+
for (const cat of list) {
|
|
980
|
+
const its = ch.result.items.filter((i) => i.cat === cat);
|
|
981
|
+
if (!its.length) continue;
|
|
982
|
+
L.push(`### ${CAT_TITLE[cat]}\uFF08${its.length}\uFF09`, "", `| \u4F4D\u7F6E | ${esc(opts.baseLabel)} | ${esc(opts.refLabel)} |`, "|---|---|---|");
|
|
983
|
+
for (const it of its) L.push(row(it));
|
|
984
|
+
L.push("");
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return L.join("\n");
|
|
988
|
+
}
|
|
989
|
+
function parseChapters(spec) {
|
|
990
|
+
const out = [];
|
|
991
|
+
for (const part of spec.split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
992
|
+
const m = /^(\d+)-(\d+)$/.exec(part);
|
|
993
|
+
if (!m) {
|
|
994
|
+
out.push(part);
|
|
995
|
+
continue;
|
|
996
|
+
}
|
|
997
|
+
const w = m[1].length;
|
|
998
|
+
for (let n = +m[1]; n <= +m[2]; n++) out.push(String(n).padStart(w, "0"));
|
|
999
|
+
}
|
|
1000
|
+
return out;
|
|
1001
|
+
}
|
|
894
1002
|
// Annotate the CommonJS export names for ESM import in node:
|
|
895
1003
|
0 && (module.exports = {
|
|
896
1004
|
ALL_CATS,
|
|
@@ -899,9 +1007,11 @@ function formatCmp(t) {
|
|
|
899
1007
|
GujiFormatError,
|
|
900
1008
|
MAX_EDIT_DISTANCE,
|
|
901
1009
|
SOLO_ATTR,
|
|
1010
|
+
anchorText,
|
|
902
1011
|
assertSameLength,
|
|
903
1012
|
availableFormats,
|
|
904
1013
|
buildExport,
|
|
1014
|
+
buildReport,
|
|
905
1015
|
buildVariantTest,
|
|
906
1016
|
cellLane,
|
|
907
1017
|
chapterChars,
|
|
@@ -911,6 +1021,7 @@ function formatCmp(t) {
|
|
|
911
1021
|
codePointLength,
|
|
912
1022
|
compareChapter,
|
|
913
1023
|
compareWhole,
|
|
1024
|
+
countByCat,
|
|
914
1025
|
createGlyphConverters,
|
|
915
1026
|
createToSimplified,
|
|
916
1027
|
diffChars,
|
|
@@ -922,6 +1033,7 @@ function formatCmp(t) {
|
|
|
922
1033
|
loadVariantTest,
|
|
923
1034
|
nextContinues,
|
|
924
1035
|
normalizeVariants,
|
|
1036
|
+
parseChapters,
|
|
925
1037
|
parseCmp,
|
|
926
1038
|
renderLines,
|
|
927
1039
|
renderRich,
|
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';
|
|
@@ -376,4 +385,28 @@ declare function loadVariantTest(url?: string): Promise<((a: string, b: string)
|
|
|
376
385
|
declare function wikiChars(md: string): string[];
|
|
377
386
|
declare function chapterChars(md: string): string[];
|
|
378
387
|
|
|
379
|
-
|
|
388
|
+
/**
|
|
389
|
+
* 对校报告:把若干册的比对结果整理成一份 Markdown,方便人工逐处检查。
|
|
390
|
+
* 纯函数,不读文件;命令行 `book-text-util report` 负责取数。
|
|
391
|
+
*/
|
|
392
|
+
|
|
393
|
+
interface ReportChapter {
|
|
394
|
+
/** 册文件 stem,如 "001" */
|
|
395
|
+
file: string;
|
|
396
|
+
result: CompareResult;
|
|
397
|
+
}
|
|
398
|
+
interface ReportOptions {
|
|
399
|
+
title: string;
|
|
400
|
+
baseLabel: string;
|
|
401
|
+
refLabel: string;
|
|
402
|
+
/** 报告里逐条列出哪些类别;缺省 用字不同、对照本多、对照本少(异体、版式差异只计数) */
|
|
403
|
+
list?: readonly DiffCat[];
|
|
404
|
+
}
|
|
405
|
+
/** `页:列:序[子列]` → 「P页C列K字」;认不出原样返回 */
|
|
406
|
+
declare function anchorText(anchor: string): string;
|
|
407
|
+
declare function countByCat(items: readonly CompareItem[]): Record<DiffCat, number>;
|
|
408
|
+
declare function buildReport(chapters: readonly ReportChapter[], opts: ReportOptions): string;
|
|
409
|
+
/** 「001-005」「001,003,010-012」→ ['001','002',…];保持位数 */
|
|
410
|
+
declare function parseChapters(spec: string): string[];
|
|
411
|
+
|
|
412
|
+
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, 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';
|
|
@@ -376,4 +385,28 @@ declare function loadVariantTest(url?: string): Promise<((a: string, b: string)
|
|
|
376
385
|
declare function wikiChars(md: string): string[];
|
|
377
386
|
declare function chapterChars(md: string): string[];
|
|
378
387
|
|
|
379
|
-
|
|
388
|
+
/**
|
|
389
|
+
* 对校报告:把若干册的比对结果整理成一份 Markdown,方便人工逐处检查。
|
|
390
|
+
* 纯函数,不读文件;命令行 `book-text-util report` 负责取数。
|
|
391
|
+
*/
|
|
392
|
+
|
|
393
|
+
interface ReportChapter {
|
|
394
|
+
/** 册文件 stem,如 "001" */
|
|
395
|
+
file: string;
|
|
396
|
+
result: CompareResult;
|
|
397
|
+
}
|
|
398
|
+
interface ReportOptions {
|
|
399
|
+
title: string;
|
|
400
|
+
baseLabel: string;
|
|
401
|
+
refLabel: string;
|
|
402
|
+
/** 报告里逐条列出哪些类别;缺省 用字不同、对照本多、对照本少(异体、版式差异只计数) */
|
|
403
|
+
list?: readonly DiffCat[];
|
|
404
|
+
}
|
|
405
|
+
/** `页:列:序[子列]` → 「P页C列K字」;认不出原样返回 */
|
|
406
|
+
declare function anchorText(anchor: string): string;
|
|
407
|
+
declare function countByCat(items: readonly CompareItem[]): Record<DiffCat, number>;
|
|
408
|
+
declare function buildReport(chapters: readonly ReportChapter[], opts: ReportOptions): string;
|
|
409
|
+
/** 「001-005」「001,003,010-012」→ ['001','002',…];保持位数 */
|
|
410
|
+
declare function parseChapters(spec: string): string[];
|
|
411
|
+
|
|
412
|
+
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, 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;
|
|
@@ -684,12 +716,12 @@ async function compareSeg(input, seg, out, idStart, whole) {
|
|
|
684
716
|
const [j0, j1] = trimWikiToBase(base, wiki, seg.startsPartial, seg.endsPartial);
|
|
685
717
|
wiki = wiki.slice(j0, j1);
|
|
686
718
|
}
|
|
687
|
-
const hunks = diffChars(base, wiki);
|
|
719
|
+
const hunks = diffChars(base, wiki, void 0, input.isVariant);
|
|
688
720
|
if (!hunks) return { ...res, status: "too-different" };
|
|
689
721
|
const structEnd = seg.headEnd - seg.from;
|
|
690
722
|
let changed = 0;
|
|
691
723
|
hunks.forEach((h, n) => {
|
|
692
|
-
const cat = classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
724
|
+
const cat = h.variant ? "glyph" : classify(h, base, wiki, { isVariant: input.isVariant, structEnd });
|
|
693
725
|
const ins = h.i1 === h.i2;
|
|
694
726
|
const del = h.j1 === h.j2;
|
|
695
727
|
changed += Math.max(h.i2 - h.i1, h.j2 - h.j1);
|
|
@@ -827,6 +859,78 @@ function parseCmp(raw) {
|
|
|
827
859
|
function formatCmp(t) {
|
|
828
860
|
return t.key ? `${t.id}-${t.key}` : t.id;
|
|
829
861
|
}
|
|
862
|
+
|
|
863
|
+
// src/compare/report.ts
|
|
864
|
+
var CAT_TITLE = {
|
|
865
|
+
word: "\u7528\u5B57\u4E0D\u540C",
|
|
866
|
+
add: "\u5BF9\u7167\u672C\u591A",
|
|
867
|
+
omit: "\u5BF9\u7167\u672C\u5C11",
|
|
868
|
+
glyph: "\u5F02\u4F53\u5B57",
|
|
869
|
+
struct: "\u7248\u5F0F\u5DEE\u5F02"
|
|
870
|
+
};
|
|
871
|
+
function anchorText(anchor) {
|
|
872
|
+
const m = /^(\d+):(\d+):(-?\d+)/.exec(anchor);
|
|
873
|
+
return m ? `${m[1]}\u9875${m[2]}\u5217${m[3]}\u5B57` : anchor;
|
|
874
|
+
}
|
|
875
|
+
var esc = (s) => s.replace(/\|/g, "\\|");
|
|
876
|
+
function row(it) {
|
|
877
|
+
const b = it.base || "\uFF08\u65E0\uFF09";
|
|
878
|
+
const r = it.reading || "\uFF08\u65E0\uFF09";
|
|
879
|
+
const [b0, b1] = it.ctx.base;
|
|
880
|
+
const [r0, r1] = it.ctx.reading;
|
|
881
|
+
return `| ${anchorText(it.anchor.start)} | ${esc(b0)}**${esc(b)}**${esc(b1)} | ${esc(r0)}**${esc(r)}**${esc(r1)} |`;
|
|
882
|
+
}
|
|
883
|
+
function countByCat(items) {
|
|
884
|
+
const out = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
885
|
+
for (const it of items) out[it.cat]++;
|
|
886
|
+
return out;
|
|
887
|
+
}
|
|
888
|
+
function buildReport(chapters, opts) {
|
|
889
|
+
const list = opts.list ?? ["word", "add", "omit"];
|
|
890
|
+
const L = [];
|
|
891
|
+
L.push(`# ${opts.title}`, "", `\u5E95\u672C\uFF1A${opts.baseLabel}\u3000\u5BF9\u7167\u672C\uFF1A${opts.refLabel}`, "");
|
|
892
|
+
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", "");
|
|
893
|
+
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 |", "|---|---:|---:|---:|---:|---:|---:|---:|");
|
|
894
|
+
const total = { word: 0, add: 0, omit: 0, glyph: 0, struct: 0 };
|
|
895
|
+
for (const ch of chapters) {
|
|
896
|
+
const c = countByCat(ch.result.items);
|
|
897
|
+
for (const k of Object.keys(total)) total[k] += c[k];
|
|
898
|
+
const seg = ch.result.segments[0];
|
|
899
|
+
const status = seg && seg.status !== "ok" ? `\uFF08${seg.status}\uFF09` : "";
|
|
900
|
+
const n = seg ? seg.to - seg.from : 0;
|
|
901
|
+
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} |`);
|
|
902
|
+
}
|
|
903
|
+
L.push(`| **\u5408\u8BA1** | | | ${total.word} | ${total.add} | ${total.omit} | ${total.glyph} | ${total.struct} |`, "");
|
|
904
|
+
for (const ch of chapters) {
|
|
905
|
+
L.push(`## \u7B2C ${ch.file} \u518C`, "");
|
|
906
|
+
const seg = ch.result.segments[0];
|
|
907
|
+
if (seg && seg.status !== "ok") {
|
|
908
|
+
L.push(`\u6CA1\u6709\u6BD4\u51FA\u7ED3\u679C\uFF1A${seg.status}\u3002`, "");
|
|
909
|
+
continue;
|
|
910
|
+
}
|
|
911
|
+
for (const cat of list) {
|
|
912
|
+
const its = ch.result.items.filter((i) => i.cat === cat);
|
|
913
|
+
if (!its.length) continue;
|
|
914
|
+
L.push(`### ${CAT_TITLE[cat]}\uFF08${its.length}\uFF09`, "", `| \u4F4D\u7F6E | ${esc(opts.baseLabel)} | ${esc(opts.refLabel)} |`, "|---|---|---|");
|
|
915
|
+
for (const it of its) L.push(row(it));
|
|
916
|
+
L.push("");
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
return L.join("\n");
|
|
920
|
+
}
|
|
921
|
+
function parseChapters(spec) {
|
|
922
|
+
const out = [];
|
|
923
|
+
for (const part of spec.split(",").map((x) => x.trim()).filter(Boolean)) {
|
|
924
|
+
const m = /^(\d+)-(\d+)$/.exec(part);
|
|
925
|
+
if (!m) {
|
|
926
|
+
out.push(part);
|
|
927
|
+
continue;
|
|
928
|
+
}
|
|
929
|
+
const w = m[1].length;
|
|
930
|
+
for (let n = +m[1]; n <= +m[2]; n++) out.push(String(n).padStart(w, "0"));
|
|
931
|
+
}
|
|
932
|
+
return out;
|
|
933
|
+
}
|
|
830
934
|
export {
|
|
831
935
|
ALL_CATS,
|
|
832
936
|
FORMAT_LABEL,
|
|
@@ -834,9 +938,11 @@ export {
|
|
|
834
938
|
GujiFormatError,
|
|
835
939
|
MAX_EDIT_DISTANCE,
|
|
836
940
|
SOLO_ATTR,
|
|
941
|
+
anchorText,
|
|
837
942
|
assertSameLength,
|
|
838
943
|
availableFormats,
|
|
839
944
|
buildExport,
|
|
945
|
+
buildReport,
|
|
840
946
|
buildVariantTest,
|
|
841
947
|
cellLane,
|
|
842
948
|
chapterChars,
|
|
@@ -846,6 +952,7 @@ export {
|
|
|
846
952
|
codePointLength,
|
|
847
953
|
compareChapter,
|
|
848
954
|
compareWhole,
|
|
955
|
+
countByCat,
|
|
849
956
|
createGlyphConverters,
|
|
850
957
|
createToSimplified,
|
|
851
958
|
diffChars,
|
|
@@ -857,6 +964,7 @@ export {
|
|
|
857
964
|
loadVariantTest,
|
|
858
965
|
nextContinues,
|
|
859
966
|
normalizeVariants,
|
|
967
|
+
parseChapters,
|
|
860
968
|
parseCmp,
|
|
861
969
|
renderLines,
|
|
862
970
|
renderRich,
|