@yanqirenshi/hefti 2.0.0 → 2.1.1

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.
Files changed (42) hide show
  1. package/README.md +165 -2
  2. package/examples/valid/big_table_book/001_s1.json +27 -27
  3. package/examples/valid/big_table_book/002_s1.1.json +216 -216
  4. package/examples/valid/big_table_book/003_s1.2.json +27 -27
  5. package/examples/valid/big_table_book/004_s2.json +25 -25
  6. package/examples/valid/big_table_book/index.json +28 -28
  7. package/examples/valid/deep_book/001_s1.json +27 -27
  8. package/examples/valid/deep_book/002_s1.1.json +29 -29
  9. package/examples/valid/deep_book/003_s1.1.1.json +30 -30
  10. package/examples/valid/deep_book/004_s1.1.1.1.json +31 -31
  11. package/examples/valid/deep_book/005_s1.1.1.1.1.json +32 -32
  12. package/examples/valid/deep_book/006_s1.1.1.1.1.1.json +33 -33
  13. package/examples/valid/deep_book/007_s1.1.1.1.1.1.1.json +33 -33
  14. package/examples/valid/deep_book/index.json +69 -69
  15. package/examples/valid/empty_book/index.json +9 -9
  16. package/examples/valid/merged_table_book/001_s1.json +38 -38
  17. package/examples/valid/merged_table_book/index.json +15 -15
  18. package/examples/valid/no_heading_book/001_front.json +39 -39
  19. package/examples/valid/no_heading_book/index.json +14 -14
  20. package/examples/valid/rich_book/001_front.json +17 -17
  21. package/examples/valid/rich_book/002_s1.json +123 -123
  22. package/examples/valid/rich_book/003_s2.json +25 -25
  23. package/examples/valid/rich_book/index.json +45 -45
  24. package/examples/valid/rich_split_book/001_front.json +17 -17
  25. package/examples/valid/rich_split_book/002_s1.json +28 -28
  26. package/examples/valid/rich_split_book/003_s1.1.json +90 -90
  27. package/examples/valid/rich_split_book/004_s1.1.1.json +28 -28
  28. package/examples/valid/rich_split_book/005_s2.json +25 -25
  29. package/examples/valid/rich_split_book/index.json +47 -47
  30. package/examples/valid/sized_book/001_front.json +17 -17
  31. package/examples/valid/sized_book/002_s1.json +51 -51
  32. package/examples/valid/sized_book/003_s1.1.json +43 -43
  33. package/examples/valid/sized_book/004_s1.1.1.json +42 -42
  34. package/examples/valid/sized_book/005_s1.2.json +28 -28
  35. package/examples/valid/sized_book/006_s2.json +64 -64
  36. package/examples/valid/sized_book/index.json +39 -39
  37. package/index.d.ts +26 -0
  38. package/index.js +2 -2
  39. package/package.json +22 -7
  40. package/schema/structure-index.schema.json +1 -1
  41. package/schema/structure-part.schema.json +1 -1
  42. package/types.d.ts +420 -0
package/README.md CHANGED
@@ -8,9 +8,98 @@ npm パッケージ `@yanqirenshi/hefti` として配ります。
8
8
 
9
9
  ## 形式
10
10
 
11
- `structure/<版>/` は、1 冊の本を「セクション(入れ子)→ パラグラフ → センテンス」で持つフォルダです。
11
+ `structure/<版>/` は、1 冊の本を「セクション(入れ子)→ パラグラフ → センテンス」で持ち、セクション単位のファイルに分けたフォルダです。
12
12
  `structure/` の下には版が並びます(OCR の結果から作る原本の `original`、それを編集した版など)。
13
13
  どの版も、同じ形式を使います。
14
+ 各要素に ID と元ページへの参照(`source`)があり、文章のチェックや校正を ID で指して行えます。
15
+
16
+ この形式を使うリポジトリは、次のとおりです。
17
+
18
+ | リポジトリ | 役割 |
19
+ |---|---|
20
+ | [Tyggja](https://github.com/yanqirenshi/Tyggja) | 原本(`structure/original/`)を作る |
21
+ | Melta | 原本を元に、校正・編集した版(`structure/<別の名前>/`)を作る |
22
+ | Frasoga | 版を表示・出版する |
23
+ | Bokasafn | 版を読む |
24
+
25
+ ### フォルダの形
26
+
27
+ ```
28
+ structure/<版>/
29
+ index.json 全体の情報と目次
30
+ 001_front.json 最初の見出しより前の本文(あるときだけ)
31
+ 002_s1.json 読み順の連番 + セクション ID
32
+ 014_s10.1.1.json
33
+ ```
34
+
35
+ 校正・編集のエージェントが、1 ファイルずつ読んで別の版を作れる大きさに分けてあります(64 ページの本で 70 ファイルほど)。
36
+ ファイル名は、読み順の 3 桁の連番とセクション ID です。
37
+
38
+ ### index.json
39
+
40
+ ```json
41
+ {
42
+ "version": 2,
43
+ "title": "本のタイトル",
44
+ "source": { "book": "book.json", "sha256": "…" },
45
+ "split_chars": 12000,
46
+ "pages": [ { "id": "p006_1", "status": "ok", "decorations": [ { "type": "header", "text": "柱の文字" } ] } ],
47
+ "files": [ { "file": "001_front.json", "id": null, "chars": 372, "json_chars": 2210 }, { "file": "002_s1.json", "id": "s1", "chars": 1012, "json_chars": 5873 } ],
48
+ "toc": [
49
+ { "id": "s10", "level": 1, "title": "2 …", "file": "014_s10.json",
50
+ "children": [ { "id": "s10.1", "level": 2, "title": "2.1 …", "file": "015_s10.1.json", "children": [] } ] }
51
+ ]
52
+ }
53
+ ```
54
+
55
+ - `source.sha256`:原本の元になったデータ(`source.book` のファイル)のハッシュです。
56
+ - `split_chars`:分けるときに使った上限(1 ファイルの JSON の文字数)です。
57
+ - `pages`:柱・ノンブル(header / footer / index)は本文に入れず、`pages[].decorations` に残します。`status` は `ok` または `ocr_failed` です。
58
+ - `files`:ファイルの一覧で、読み順です。`id` はファイルの先頭のセクションの ID(001_front.json は `null`)、`chars` は本文の文字数(空白を除く)、`json_chars` はそのファイルの JSON の文字数です。
59
+ - `toc`:すべてのセクションの木です。`file` は、そのセクションの見出しが入っているファイルです(分けられずに親のファイルに入っているセクションは、親と同じファイル名)。
60
+
61
+ ### 分けたファイル
62
+
63
+ ```json
64
+ {
65
+ "version": 2,
66
+ "file": "015_s10.1.json",
67
+ "id": "s10.1",
68
+ "ancestors": [ { "id": "s10", "level": 1, "title": "2 …" } ],
69
+ "body": [
70
+ { "type": "section", "id": "s10.1", "level": 2, "title": "2.1 …", "source": ["p021_1#1"],
71
+ "children": [
72
+ { "type": "paragraph", "id": "p120", "source": ["p021_1#2"], "sentences": [ { "id": "p120.1", "text": "…。" } ] },
73
+ { "type": "list", "id": "l1", "source": ["p007_1#3"],
74
+ "items": [ { "id": "l1.1", "source": ["p007_1#3"], "sentences": [ { "id": "l1.1.1", "text": "…" } ] } ] },
75
+ { "type": "figure", "id": "f1", "src": "figures/p007_1_0.png", "text": ["図の中の文字"], "source": ["p007_1#5"] },
76
+ { "type": "caption", "id": "c1", "text": "…", "source": ["p007_1#6"] },
77
+ { "type": "table", "id": "t1", "n_row": 2, "n_col": 2, "cells": [ { "row": 0, "col": 0, "row_span": 1, "col_span": 1, "text": "…" } ], "source": ["p007_1#7"] },
78
+ { "type": "section_ref", "id": "s10.1.1", "level": 3, "title": "2.1.1 …", "file": "016_s10.1.1.json" }
79
+ ] }
80
+ ]
81
+ }
82
+ ```
83
+
84
+ - `id` は、そのファイルの先頭のセクションの ID です。`ancestors` は、そのセクションより上の見出し(外側から順)です。
85
+ - `body` の要素は、`section`・`paragraph`・`list`・`figure`・`caption`・`table` です(`list` の項目は `items`、表のセルは `cells`)。
86
+ - `section_ref`:子セクションが別のファイルに分けてあることを示す印です。`file` の先のファイルの `body` の先頭が、その子セクションです。
87
+ - `001_front.json` は `id` が `null`、`ancestors` が空で、`body` に本文の要素が直接並びます。
88
+ - `figures` の画像:`figure` の `src`(例: `figures/p008_1_0.png`)は、`<出力フォルダ>` からの相対パスです。`structure/original/` から見ると `../../figures/p008_1_0.png` になります。
89
+ - `source`:`<ページID>#<そのページの book.json の blocks の番号>`。元の本のどこから来たかを辿れます。ページをまたぐ段落は複数になります。
90
+ - パラグラフに `text` はありません。センテンスが唯一の正本で、HTML などにするときはセンテンスを連結して段落にします。
91
+ - センテンスの `text` を連結すると、元の段落になります(区切りの後ろの改行や空白は、前のセンテンスの末尾に含めます)。
92
+ - ID は出現順の連番です(`s1` / `s1.1` / `p12` / `p12.3` / `l1.1.1` / `f1` / `t1` / `c1`)。ID は原本を作り直すと変わります。
93
+
94
+ ### JSON の書き方
95
+
96
+ エージェントが読むので、行数を抑えています。
97
+ センテンス 1 つ、表のセル 1 つ、`source` の配列は、それぞれ 1 行に書きます。短い入れ子(目次の項目など)も 1 行です。それ以外は字下げします。
98
+ 手で直すときも、この書き方でなくてかまいません(JSON として読めればよい)。
99
+
100
+ ### スキーマ
101
+
102
+ 形式は、JSON Schema(draft 2020-12)で定めてあります。
14
103
 
15
104
  | ファイル | 検証するもの |
16
105
  |---|---|
@@ -21,13 +110,31 @@ npm パッケージ `@yanqirenshi/hefti` として配ります。
21
110
  - 各プロパティに日本語の `description` があります。スキーマだけ読んでも意味が分かります。
22
111
  - 知らないプロパティは許します。後工程が要素に情報を足して別の JSON にできるようにするためです。
23
112
  - ID の形(`s1.2`・`p12`・`p12.3`・`l1.2.3`・`f1`・`t1`・`c1`)と `source`(`ページID#ブロック番号`)は、`pattern` で締めてあります。
24
- - **スキーマでは確かめられないこと**:ID が `structure/<版>/` 全体で重複しない、`section_ref` の先のファイルがある、`index.json` の `files` と実際のファイルが合う、など。これらは、読み込む側のプログラムが確かめます。
113
+
114
+ ### スキーマでは確かめられないこと
115
+
116
+ 次のことは、読み込む側のプログラムが確かめます。
117
+
118
+ - `index.json` の `files` にあるファイルが無い
119
+ - `section_ref` の先が無い、先の中身と合わない、2 か所から参照されている
120
+ - どのファイルからも辿れないファイルがある
121
+ - ID が `structure/<版>/` 全体で重複している
122
+
123
+ ### 版を編集するときの ID の約束
124
+
125
+ original を元に版(`structure/<別の名前>/`)を作る後工程(Melta など)が、守る約束です。ID が動くと、指摘と本文の対応が取れなくなります。
126
+
127
+ 1. ID は原本を作ったときに決まる名前で、位置を表す番号ではない。版を編集して文を足したり消したりしても、他の要素の ID は振り直さない。
128
+ 2. 文を分けたり足したりするときは、そのパラグラフでまだ使っていない番号を付ける(p12 に 3 文あれば、足す文は p12.4)。並び順は配列の順で決まり、ID の大小は関係ない。
129
+ 3. 消した ID は使い回さない。
130
+ 4. 後工程は ID の形や順序に頼らない(ID は名前で、位置を表す番号ではない)。
25
131
 
26
132
  ## 版番号
27
133
 
28
134
  パッケージの版番号のメジャーは、データ形式の版(JSON の `version`)と同じです。
29
135
  いまの形式は `version: 2` なので、`2.x.y` が対応します。
30
136
  形式を壊す変更をしたときは、`version` とメジャーを一緒に上げます。
137
+ メジャーはデータ形式の版です。型(`types.d.ts`)だけの変更は、マイナーで扱います。
31
138
 
32
139
  ## 使い方(npm)
33
140
 
@@ -59,6 +166,57 @@ import index from "@yanqirenshi/hefti/schema/structure-index.schema.json" with {
59
166
  > 補足:ajv の `strict: true`(`strictTypes` を含む)では、`structure-part.schema.json` の `then` の中の `maxItems` に「`type` が無い」と警告されます。
60
167
  > 検証の結果は変わらないので、`new Ajv2020({ strictTypes: false })` にするか、既定の設定で使ってください。
61
168
 
169
+ ## TypeScript の型
170
+
171
+ スキーマから生成した型(`types.d.ts`)を、パッケージに入れています。
172
+ 型は `import type` で読めます。
173
+
174
+ ```ts
175
+ import type { StructureIndex, StructurePart, BodyNode } from "@yanqirenshi/hefti";
176
+ ```
177
+
178
+ 主な型は、次のとおりです。
179
+
180
+ | 型 | 内容 |
181
+ |---|---|
182
+ | `StructureIndex` | `index.json`。`pages`(`PageInfo`)・`files`(`FileEntry`)・`toc`(`TocNode`)を持つ |
183
+ | `StructurePart` | 分けたファイル。`FrontPart`(`id` が `null`、`body` は本文の要素)か `SectionPart`(`body` はセクション 1 つ) |
184
+ | `BodyNode` | セクションの `children` に入る要素。`Section`・`SectionRef`・`Paragraph`・`List`・`Figure`・`Caption`・`Table` の共用体 |
185
+ | `FrontNode` | `001_front.json` の `body` に入る要素(`BodyNode` からセクションと `SectionRef` を除いたもの) |
186
+
187
+ 要素は `type` で絞り込めます(判別共用体)。
188
+ `default` で `never` になるので、種類が増えたときに型検査で気づけます。
189
+
190
+ ```ts
191
+ function text(node: BodyNode): string {
192
+ switch (node.type) {
193
+ case "section": return node.title;
194
+ case "section_ref": return node.title;
195
+ case "paragraph": return node.sentences.map((s) => s.text).join("");
196
+ case "list": return node.items.map((i) => i.sentences.map((s) => s.text).join("")).join("
197
+ ");
198
+ case "figure": return node.text.join("");
199
+ case "caption": return node.text;
200
+ case "table": return node.cells.map((c) => c.text).join(" ");
201
+ default: {
202
+ const unreachable: never = node;
203
+ return unreachable;
204
+ }
205
+ }
206
+ }
207
+
208
+ function elements(part: StructurePart) {
209
+ return part.id === null ? part.body : part.body[0].children; // FrontPart か SectionPart かで決まる
210
+ }
211
+ ```
212
+
213
+ - 知らないプロパティは許す形式なので、どの型にも `[k: string]: unknown` が付いています。後工程が項目を足した JSON も、同じ型で扱えます。
214
+ - 件数(`minItems` / `maxItems`)は型に出ません。`source` は `string[]` です。件数はスキーマの検証で確かめてください。
215
+ - 型で表せない約束があります。ID の書式(`pattern`)、`sha256` の書式、`minimum` などです。たとえば `SectionId` は `string` で、`"s1.2"` も `"abc"` も型検査を通ります。
216
+ これらは型検査では見つからず、スキーマの検証でだけ見つかります。
217
+ 外から受け取ったデータ(ファイルや通信で読んだ JSON)は、型を付けるだけでなく、スキーマで検証してください。
218
+ - 日本語の `description` は、JSDoc として残してあります。
219
+
62
220
  ## 使い方(npm を使わない言語)
63
221
 
64
222
  Python や Rust など、npm を使わない言語では、レジストリの tarball から `schema/` を取り出して使います。
@@ -126,9 +284,14 @@ Rust では、`include_str!` でスキーマをバイナリに埋め込み、`js
126
284
  npm install
127
285
  npm test # スキーマ自体・見本・index.js のテスト(test/)
128
286
  node scripts/build-index.mjs # schema/ から index.js を作り直す
287
+ node scripts/build-types.mjs # schema/ から types.d.ts を作り直す(npm run build は両方)
129
288
  node scripts/build-invalid.mjs # examples/valid/sized_book から examples/invalid/ を作り直す
130
289
  ```
131
290
 
291
+ `index.js` と `types.d.ts` も、生成したものをコミットしています。
292
+ スキーマを直したときは、`npm run build` で作り直してください。
293
+ `types.d.ts` は、`json-schema-to-typescript` と `typescript` の版を固定して作るので、同じスキーマからは同じ内容になります。
294
+
132
295
  `examples/invalid/` は生成したものをコミットしています。
133
296
  壊し方を変えるときは `scripts/build-invalid.mjs` を直して作り直してください(作り直し忘れは `npm test` が見つけます)。
134
297
 
@@ -1,27 +1,27 @@
1
- {
2
- "version": 2,
3
- "file": "001_s1.json",
4
- "id": "s1",
5
- "ancestors": [],
6
- "body": [
7
- {
8
- "type": "section",
9
- "id": "s1",
10
- "level": 1,
11
- "title": "第1章 表の章",
12
- "source": ["p0001_0#0"],
13
- "children": [
14
- {
15
- "type": "paragraph",
16
- "id": "p1",
17
- "source": ["p0001_0#1"],
18
- "sentences": [
19
- {"id": "p1.1", "text": "短い。"}
20
- ]
21
- },
22
- {"type": "section_ref", "id": "s1.1", "level": 2, "title": "1.1 大きな表の節", "file": "002_s1.1.json"},
23
- {"type": "section_ref", "id": "s1.2", "level": 2, "title": "1.2 短い節", "file": "003_s1.2.json"}
24
- ]
25
- }
26
- ]
27
- }
1
+ {
2
+ "version": 2,
3
+ "file": "001_s1.json",
4
+ "id": "s1",
5
+ "ancestors": [],
6
+ "body": [
7
+ {
8
+ "type": "section",
9
+ "id": "s1",
10
+ "level": 1,
11
+ "title": "第1章 表の章",
12
+ "source": ["p0001_0#0"],
13
+ "children": [
14
+ {
15
+ "type": "paragraph",
16
+ "id": "p1",
17
+ "source": ["p0001_0#1"],
18
+ "sentences": [
19
+ {"id": "p1.1", "text": "短い。"}
20
+ ]
21
+ },
22
+ {"type": "section_ref", "id": "s1.1", "level": 2, "title": "1.1 大きな表の節", "file": "002_s1.1.json"},
23
+ {"type": "section_ref", "id": "s1.2", "level": 2, "title": "1.2 短い節", "file": "003_s1.2.json"}
24
+ ]
25
+ }
26
+ ]
27
+ }