@chaffjs/lang-ja 0.3.0 → 0.6.0
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/dist/citation.d.ts +2 -0
- package/dist/citation.d.ts.map +1 -0
- package/dist/citation.js +52 -0
- package/dist/citation.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/lexicons.d.ts.map +1 -1
- package/dist/lexicons.js +6 -1
- package/dist/lexicons.js.map +1 -1
- package/dist/numbers.d.ts +8 -0
- package/dist/numbers.d.ts.map +1 -0
- package/dist/numbers.js +34 -0
- package/dist/numbers.js.map +1 -0
- package/dist/pos.d.ts +14 -0
- package/dist/pos.d.ts.map +1 -1
- package/dist/pos.js +25 -1
- package/dist/pos.js.map +1 -1
- package/dist/quantities.d.ts +9 -0
- package/dist/quantities.d.ts.map +1 -0
- package/dist/quantities.js +151 -0
- package/dist/quantities.js.map +1 -0
- package/dist/structure.d.ts +3 -0
- package/dist/structure.d.ts.map +1 -0
- package/dist/structure.js +176 -0
- package/dist/structure.js.map +1 -0
- package/package.json +1 -1
- package/src/citation.ts +56 -0
- package/src/index.ts +2 -0
- package/src/lexicons.ts +6 -1
- package/src/numbers.ts +35 -0
- package/src/pos.ts +41 -1
- package/src/quantities.ts +183 -0
- package/src/structure.ts +207 -0
package/src/structure.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import type { Mention, NumberedLine, NumberingContext, StructurePatterns } from "chaffjs/plugin";
|
|
2
|
+
import { parseJapaneseNumber, toHalfWidth } from "./numbers.ts";
|
|
3
|
+
import { citedDocument } from "./citation.ts";
|
|
4
|
+
import { countedAfter, dates, quantities } from "./quantities.ts";
|
|
5
|
+
|
|
6
|
+
// 契約書・規程・法令の番号の書き方。core は番号の書き方を知らず、ここで読んだものを入れ子にする。
|
|
7
|
+
|
|
8
|
+
const NUMBER = "[0-90-9〇一二三四五六七八九十百千]{1,6}";
|
|
9
|
+
const SPACE = "[ \\t\\u3000]";
|
|
10
|
+
|
|
11
|
+
/** 見出しとして読めるのは、番号の直後が空白・括弧・行末のとき。「第3条に定める」は本文。 */
|
|
12
|
+
const ARTICLE = new RegExp(`^${SPACE}*第(?<n>${NUMBER})条(?:の(?<sub>${NUMBER}))?(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
|
|
13
|
+
const PARAGRAPH = new RegExp(`^${SPACE}*第(?<n>${NUMBER})項(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
|
|
14
|
+
const ITEM = new RegExp(`^${SPACE}*第(?<n>${NUMBER})号(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
|
|
15
|
+
/**
|
|
16
|
+
* 法令の項と号は、番号だけを行頭に置き、全角空白で本文と区切る(「2」「一」)。条の中でだけ読む。
|
|
17
|
+
* 半角数字や漢数字に半角空白が続くだけなら本文として扱う。「3 人で」「一 人で」を項や号にしない。
|
|
18
|
+
*/
|
|
19
|
+
const BARE_PARAGRAPH = new RegExp(`^${SPACE}*(?<n>[0-9]{1,3})\\u3000+(?<rest>\\S.*)$`, "u");
|
|
20
|
+
const BARE_FULLWIDTH_PARAGRAPH = new RegExp(`^${SPACE}*(?<n>[0-9]{1,3})${SPACE}+(?<rest>\\S.*)$`, "u");
|
|
21
|
+
const BARE_ITEM = new RegExp(`^${SPACE}*(?<n>[一二三四五六七八九十]{1,3})\\u3000+(?<rest>\\S.*)$`, "u");
|
|
22
|
+
const PAREN_ITEM = new RegExp(`^${SPACE}*[((](?<n>[0-90-9]{1,3})[))]${SPACE}*(?<rest>\\S.*)$`, "u");
|
|
23
|
+
|
|
24
|
+
/** 「第3条(支払)」の括弧の中が見出し。括弧が無ければ、条は後ろ全部を、項・号は本文なので空を見出しにする。 */
|
|
25
|
+
const headingOf = (rest: string, whole: boolean): string => {
|
|
26
|
+
const trimmed = rest.trim();
|
|
27
|
+
const bracketed = /^[((](?<title>[^))]{1,40})[))]\s*$/u.exec(trimmed)?.groups?.["title"];
|
|
28
|
+
// 法令の「第三条 事業者は、…。」は見出しの無い条で、後ろは本文。文を見出しにしない。
|
|
29
|
+
return bracketed ?? (whole && !trimmed.includes("。") ? trimmed : "");
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const numberOf = (text: string | undefined): string | undefined => {
|
|
33
|
+
const value = text === undefined ? undefined : parseJapaneseNumber(text);
|
|
34
|
+
return value === undefined ? undefined : String(value);
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const insideArticle = (context: NumberingContext): boolean => context.open.some((open) => open.kind === "article");
|
|
38
|
+
|
|
39
|
+
/** 「(2)」は開いている「(1)」の兄弟。無ければ、開いているものより一段深い。 */
|
|
40
|
+
const PAREN_LABEL = /^(\d+)$/u;
|
|
41
|
+
const parenDepth = (context: NumberingContext): number =>
|
|
42
|
+
[...context.open].reverse().find((open) => PAREN_LABEL.test(open.label))?.depth ?? (context.open.at(-1)?.depth ?? 0) + 1;
|
|
43
|
+
|
|
44
|
+
const article = (line: string): NumberedLine | undefined => {
|
|
45
|
+
const groups = ARTICLE.exec(line)?.groups;
|
|
46
|
+
const main = numberOf(groups?.["n"]);
|
|
47
|
+
if (groups === undefined || main === undefined) return undefined;
|
|
48
|
+
const sub = numberOf(groups["sub"]);
|
|
49
|
+
const branch = groups["sub"] === undefined ? "" : "の" + groups["sub"];
|
|
50
|
+
const label = `第${groups["n"] ?? ""}条${branch}`;
|
|
51
|
+
const rest = (groups["rest"] ?? "").trim();
|
|
52
|
+
// 枝番号の条(第3条の2)は並びの外。第3条の次が第4条であることを乱さない。
|
|
53
|
+
const ordinal = sub === undefined ? Number(main) : undefined;
|
|
54
|
+
return {
|
|
55
|
+
kind: "article",
|
|
56
|
+
depth: 1,
|
|
57
|
+
number: sub === undefined ? main : `${main}-${sub}`,
|
|
58
|
+
absolute: true,
|
|
59
|
+
label,
|
|
60
|
+
heading: headingOf(rest, true),
|
|
61
|
+
rest,
|
|
62
|
+
ordinal,
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
type ItemShape = {
|
|
67
|
+
readonly pattern: RegExp;
|
|
68
|
+
readonly depth: (context: NumberingContext) => number;
|
|
69
|
+
readonly label: (n: string) => string;
|
|
70
|
+
readonly needsArticle: boolean;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const ITEM_SHAPES: readonly ItemShape[] = [
|
|
74
|
+
{ pattern: PARAGRAPH, depth: () => 2, label: (n) => `第${n}項`, needsArticle: false },
|
|
75
|
+
{ pattern: ITEM, depth: () => 3, label: (n) => `第${n}号`, needsArticle: false },
|
|
76
|
+
{ pattern: BARE_PARAGRAPH, depth: () => 2, label: (n) => n, needsArticle: true },
|
|
77
|
+
{ pattern: BARE_FULLWIDTH_PARAGRAPH, depth: () => 2, label: (n) => n, needsArticle: true },
|
|
78
|
+
{ pattern: BARE_ITEM, depth: () => 3, label: (n) => n, needsArticle: true },
|
|
79
|
+
{ pattern: PAREN_ITEM, depth: parenDepth, label: (n) => `(${toHalfWidth(n)})`, needsArticle: false },
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const item = (line: string, context: NumberingContext): NumberedLine | undefined =>
|
|
83
|
+
ITEM_SHAPES.reduce<NumberedLine | undefined>((found, shape) => {
|
|
84
|
+
if (found !== undefined || (shape.needsArticle && !insideArticle(context))) return found;
|
|
85
|
+
const groups = shape.pattern.exec(line)?.groups;
|
|
86
|
+
const number = numberOf(groups?.["n"]);
|
|
87
|
+
if (groups === undefined || number === undefined) return undefined;
|
|
88
|
+
const rest = (groups["rest"] ?? "").trim();
|
|
89
|
+
const label = shape.label(groups["n"] ?? "");
|
|
90
|
+
return { kind: "item", depth: shape.depth(context), number, absolute: false, label, heading: headingOf(rest, false), rest, ordinal: Number(number) };
|
|
91
|
+
}, undefined);
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* 条より外のまとまり。法令は章の番号を編ごとに、節の番号を章ごとに振り直すので、
|
|
95
|
+
* 章と節は親の番地に続ける(第2編第1章 → pt2.ch1、その第3節 → pt2.ch1.3)。条は通し番号のまま。
|
|
96
|
+
*/
|
|
97
|
+
const CHAPTER = new RegExp(`^${SPACE}*第(?<n>${NUMBER})(?<unit>[編章節])(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
|
|
98
|
+
const CHAPTER_SHAPES: Readonly<Record<string, { readonly depth: number; readonly prefix: string }>> = {
|
|
99
|
+
編: { depth: -2, prefix: "pt" },
|
|
100
|
+
章: { depth: -1, prefix: "ch" },
|
|
101
|
+
節: { depth: 0, prefix: "" },
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const chapter = (line: string): NumberedLine | undefined => {
|
|
105
|
+
const groups = CHAPTER.exec(line)?.groups;
|
|
106
|
+
const number = numberOf(groups?.["n"]);
|
|
107
|
+
const shape = CHAPTER_SHAPES[groups?.["unit"] ?? ""];
|
|
108
|
+
if (groups === undefined || number === undefined || shape === undefined) return undefined;
|
|
109
|
+
const rest = (groups["rest"] ?? "").trim();
|
|
110
|
+
const label = `第${groups["n"] ?? ""}${groups["unit"] ?? ""}`;
|
|
111
|
+
return {
|
|
112
|
+
kind: "chapter",
|
|
113
|
+
depth: shape.depth,
|
|
114
|
+
number: shape.prefix + number,
|
|
115
|
+
absolute: false,
|
|
116
|
+
label,
|
|
117
|
+
heading: headingOf(rest, true),
|
|
118
|
+
rest,
|
|
119
|
+
ordinal: Number(number),
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const numbered = (line: string, context: NumberingContext): NumberedLine | undefined => chapter(line) ?? article(line) ?? item(line, context);
|
|
124
|
+
|
|
125
|
+
/** 正規表現の一致を Mention にする。g フラグ付きのものだけを渡す。 */
|
|
126
|
+
const mentions = (
|
|
127
|
+
pattern: RegExp,
|
|
128
|
+
text: string,
|
|
129
|
+
attrs: (groups: Readonly<Record<string, string | undefined>>, whole: string) => Mention["attrs"] | undefined,
|
|
130
|
+
): Mention[] =>
|
|
131
|
+
[...text.matchAll(pattern)].flatMap((match) => {
|
|
132
|
+
const found = attrs(match.groups ?? {}, match[0]);
|
|
133
|
+
return found === undefined ? [] : [{ start: match.index, end: match.index + match[0].length, attrs: found }];
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const DEFINITIONS = [/以下「(?<term>[^「」\n]{1,40})」という/gu, /「(?<term>[^「」\n]{1,40})」とは/gu];
|
|
137
|
+
|
|
138
|
+
const definitions = (text: string): Mention[] =>
|
|
139
|
+
DEFINITIONS.flatMap((pattern) => mentions(pattern, text, (groups) => (groups["term"] === undefined ? undefined : { term: groups["term"] })));
|
|
140
|
+
|
|
141
|
+
const REFERENCE = new RegExp(`第(?<a>${NUMBER})条(?:の(?<s>${NUMBER}))?(?:第(?<p>${NUMBER})項)?(?:第(?<i>${NUMBER})号)?`, "gu");
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 「第12条第1項」→ 12.1、「第3条の2」→ 3-2。番地の付け方は木と同じにする。
|
|
145
|
+
* 法令は第 1 項に番号を振らないので、「第4条第1項」は番号付きの 4.1 が無ければ第4条そのものを指す。その行き先を fallback に入れる。
|
|
146
|
+
* 「民法第709条」のように他の文書の名前が前にあれば、その名前を document に入れる。この文書の木では引かない。
|
|
147
|
+
*/
|
|
148
|
+
const references = (text: string): Mention[] =>
|
|
149
|
+
[...text.matchAll(REFERENCE)].flatMap((match) => {
|
|
150
|
+
const groups = match.groups ?? {};
|
|
151
|
+
const main = numberOf(groups["a"]);
|
|
152
|
+
if (main === undefined) return [];
|
|
153
|
+
const sub = numberOf(groups["s"]);
|
|
154
|
+
const article = sub === undefined ? main : `${main}-${sub}`;
|
|
155
|
+
const paragraph = numberOf(groups["p"]);
|
|
156
|
+
const item = numberOf(groups["i"]);
|
|
157
|
+
const target = [article, paragraph, item].filter((part) => part !== undefined).join(".");
|
|
158
|
+
const fallback = paragraph === "1" ? [article, item].filter((part) => part !== undefined).join(".") : undefined;
|
|
159
|
+
const document = citedDocument(text, match.index);
|
|
160
|
+
const attrs = { target, label: match[0], ...(fallback === undefined ? {} : { fallback }), ...(document === undefined ? {} : { document }) };
|
|
161
|
+
return [{ start: match.index, end: match.index + match[0].length, attrs }];
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
/** 長いものから探し、重なったら先に見つけたものを残す。「しなければならない」を「なければならない」と二重に数えない。 */
|
|
165
|
+
const MARKERS: readonly (readonly [string, "must" | "must-not" | "may"])[] = [
|
|
166
|
+
["しなければならない", "must"],
|
|
167
|
+
["なければならない", "must"],
|
|
168
|
+
["してはならない", "must-not"],
|
|
169
|
+
["てはならない", "must-not"],
|
|
170
|
+
["なければなりません", "must"],
|
|
171
|
+
["てはなりません", "must-not"],
|
|
172
|
+
["てはいけません", "must-not"],
|
|
173
|
+
["てはいけない", "must-not"],
|
|
174
|
+
["義務を負う", "must"],
|
|
175
|
+
["ものとする", "must"],
|
|
176
|
+
["禁止する", "must-not"],
|
|
177
|
+
["することができる", "may"],
|
|
178
|
+
["ことができる", "may"],
|
|
179
|
+
["ことができます", "may"],
|
|
180
|
+
];
|
|
181
|
+
|
|
182
|
+
/** 語が現れる位置を全部。再帰や配列の広げ直しをしないので、長い文書でも線形で終わる。 */
|
|
183
|
+
const occurrences = (text: string, word: string): number[] => {
|
|
184
|
+
const found: number[] = [];
|
|
185
|
+
for (let at = text.indexOf(word); at !== -1; at = text.indexOf(word, at + word.length)) found.push(at);
|
|
186
|
+
return found;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 長い語から当て、すでに取った文字に重なるものは捨てる。重なりは文字ごとの印で見る。
|
|
191
|
+
* 見つけたもの同士を総当たりで比べると、同じ語が何万もある文書で二乗に遅くなる。
|
|
192
|
+
*/
|
|
193
|
+
const obligations = (text: string): Mention[] => {
|
|
194
|
+
const taken = new Uint8Array(text.length);
|
|
195
|
+
const kept: Mention[] = [];
|
|
196
|
+
MARKERS.forEach(([marker, type]) => {
|
|
197
|
+
occurrences(text, marker).forEach((start) => {
|
|
198
|
+
const end = start + marker.length;
|
|
199
|
+
if (taken.subarray(start, end).some((mark) => mark === 1)) return;
|
|
200
|
+
taken.fill(1, start, end);
|
|
201
|
+
kept.push({ start, end, attrs: { marker, type } });
|
|
202
|
+
});
|
|
203
|
+
});
|
|
204
|
+
return kept.sort((left, right) => left.start - right.start);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export const structure: StructurePatterns = { numbered, definitions, references, obligations, quantities, dates, countedAfter };
|