@chaffjs/lang-ja 0.5.0 → 0.7.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/README.md +1 -1
- package/dist/citation.d.ts +2 -0
- package/dist/citation.d.ts.map +1 -0
- package/dist/citation.js +62 -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/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 +17 -0
- 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 +319 -0
- package/dist/structure.js.map +1 -0
- package/package.json +1 -1
- package/src/citation.ts +68 -0
- package/src/index.ts +2 -0
- package/src/numbers.ts +35 -0
- package/src/pos.ts +30 -0
- package/src/quantities.ts +183 -0
- package/src/structure.ts +365 -0
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type { Mention } from "chaffjs/plugin";
|
|
2
|
+
import { parseJapaneseNumber, toHalfWidth } from "./numbers.ts";
|
|
3
|
+
import { isReady, morphemes, type Morph } from "./pos.ts";
|
|
4
|
+
|
|
5
|
+
// 数量と日付。形態素が使えれば品詞で読み、使えなければ単位の表で読む。
|
|
6
|
+
|
|
7
|
+
type Counted = { readonly start: number; readonly end: number; readonly value: number; readonly unit: string };
|
|
8
|
+
|
|
9
|
+
const PERCENT = new Set(["%", "%"]);
|
|
10
|
+
/** 形態素が無いときの単位の表。長いものから当てる。形態素があれば、助数詞はすべて読める。 */
|
|
11
|
+
const UNITS = [
|
|
12
|
+
"営業日",
|
|
13
|
+
"パーセント",
|
|
14
|
+
"か月",
|
|
15
|
+
"ヶ月",
|
|
16
|
+
"カ月",
|
|
17
|
+
"箇月",
|
|
18
|
+
"週間",
|
|
19
|
+
"時間",
|
|
20
|
+
"万円",
|
|
21
|
+
"日",
|
|
22
|
+
"年",
|
|
23
|
+
"月",
|
|
24
|
+
"分",
|
|
25
|
+
"円",
|
|
26
|
+
"件",
|
|
27
|
+
"回",
|
|
28
|
+
"人",
|
|
29
|
+
"名",
|
|
30
|
+
"倍",
|
|
31
|
+
"個",
|
|
32
|
+
"%",
|
|
33
|
+
"%",
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
const isNumeral = (morph: Morph | undefined): boolean => morph !== undefined && morph.pos === "名詞" && morph.detail1 === "数";
|
|
37
|
+
|
|
38
|
+
/** IPADIC は「4月」の「月」を助数詞でなく普通の名詞と読む。数の直後にあるときだけ、暦の月として数える。 */
|
|
39
|
+
const COMMON_NOUN_COUNTERS = new Set(["月"]);
|
|
40
|
+
|
|
41
|
+
const isCounter = (morph: Morph | undefined): boolean =>
|
|
42
|
+
morph !== undefined && (morph.detail2 === "助数詞" || PERCENT.has(morph.surface) || COMMON_NOUN_COUNTERS.has(morph.surface));
|
|
43
|
+
|
|
44
|
+
/** 「1.5」は 1 . 5 と切られる。数に挟まれた点とカンマは数の一部として読む。 */
|
|
45
|
+
const isJoiner = (morphs: readonly Morph[], index: number): boolean => {
|
|
46
|
+
const morph = morphs[index];
|
|
47
|
+
return morph !== undefined && [".", ",", ".", ","].includes(morph.surface) && isNumeral(morphs[index - 1]) && isNumeral(morphs[index + 1]);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** index から始まる数の並びの終わり(含まない)。 */
|
|
51
|
+
const runEnd = (morphs: readonly Morph[], index: number): number => {
|
|
52
|
+
let end = index;
|
|
53
|
+
while (isNumeral(morphs[end]) || isJoiner(morphs, end)) end += 1;
|
|
54
|
+
return end;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** 「第3条」の 3 は番号で、数量ではない。 */
|
|
58
|
+
const isOrdinal = (morph: Morph | undefined): boolean => morph?.surface === "第";
|
|
59
|
+
|
|
60
|
+
/** 数と単位のあいだに置かれうる空白 1 文字。構造の型(structure.ts の SPACE)と同じく、全角空白とタブも含む。 */
|
|
61
|
+
const GAP = new Set([" ", "\t", "\u3000"]);
|
|
62
|
+
|
|
63
|
+
const isSpace = (morph: Morph | undefined): morph is Morph => morph !== undefined && GAP.has(morph.surface);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 「1.5 倍」の「倍」は、数とのあいだに空白があると解析器が普通の名詞と読む。
|
|
67
|
+
* 空白を詰めて読み直し、数の直後に来る語が助数詞ならその語を返す。
|
|
68
|
+
*/
|
|
69
|
+
const unitAfterSpace = (number: string, rest: string): string | undefined => {
|
|
70
|
+
const next = morphemes(toHalfWidth(number + rest.slice(0, 8)))?.find((morph) => morph.start >= number.length);
|
|
71
|
+
return next !== undefined && isCounter(next) ? next.surface : undefined;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
type Unit = { readonly unit: string; readonly end: number };
|
|
75
|
+
|
|
76
|
+
const counterAt = (text: string, morphs: readonly Morph[], number: string, end: number): Unit | undefined => {
|
|
77
|
+
const next = morphs[end];
|
|
78
|
+
if (next !== undefined && isCounter(next)) return { unit: next.surface, end: next.end };
|
|
79
|
+
if (!isSpace(next)) return undefined;
|
|
80
|
+
const unit = unitAfterSpace(number, text.slice(next.end));
|
|
81
|
+
return unit === undefined ? undefined : { unit, end: next.end + unit.length };
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const countedByMorphemes = (text: string, morphs: readonly Morph[]): Counted[] => {
|
|
85
|
+
const found: Counted[] = [];
|
|
86
|
+
let index = 0;
|
|
87
|
+
while (index < morphs.length) {
|
|
88
|
+
const end = runEnd(morphs, index);
|
|
89
|
+
const first = morphs[index];
|
|
90
|
+
if (first !== undefined && end > index && !isOrdinal(morphs[index - 1])) {
|
|
91
|
+
const number = text.slice(first.start, morphs[end - 1]?.end ?? first.end);
|
|
92
|
+
const value = parseJapaneseNumber(number);
|
|
93
|
+
const counter = counterAt(text, morphs, number, end);
|
|
94
|
+
if (value !== undefined && counter !== undefined) found.push({ start: first.start, end: counter.end, value, unit: counter.unit });
|
|
95
|
+
}
|
|
96
|
+
index = Math.max(end, index + 1);
|
|
97
|
+
}
|
|
98
|
+
return found;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const NUMBER_RUN = /[0-90-9][0-90-9.,]{0,15}|[〇一二三四五六七八九十百千万億]{1,12}/gu;
|
|
102
|
+
|
|
103
|
+
const countedByTable = (text: string): Counted[] =>
|
|
104
|
+
[...text.matchAll(NUMBER_RUN)].flatMap((match) => {
|
|
105
|
+
// 形態素の経路と同じく、数と単位のあいだの空白 1 つは詰めて読む。
|
|
106
|
+
const after = match.index + match[0].length + (GAP.has(text[match.index + match[0].length] ?? "") ? 1 : 0);
|
|
107
|
+
const unit = UNITS.find((candidate) => text.startsWith(candidate, after));
|
|
108
|
+
const value = parseJapaneseNumber(toHalfWidth(match[0]));
|
|
109
|
+
const ordinal = text[match.index - 1] === "第";
|
|
110
|
+
return unit === undefined || value === undefined || ordinal ? [] : [{ start: match.index, end: after + unit.length, value, unit }];
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
/** quantities と dates は同じ行を続けて読む。直前の 1 行だけ覚えて、同じ行を二度解析しない。 */
|
|
114
|
+
const last: { text: string | undefined; ready: boolean; found: readonly Counted[] } = { text: undefined, ready: false, found: [] };
|
|
115
|
+
|
|
116
|
+
const counted = (text: string): readonly Counted[] => {
|
|
117
|
+
if (last.text === text && last.ready === isReady()) return last.found;
|
|
118
|
+
// IPADIC は全角の「4月」を一語の名詞と読む。全角数字は 1 文字ずつ半角にしてから読むので、位置は変わらない。
|
|
119
|
+
const half = toHalfWidth(text);
|
|
120
|
+
const morphs = morphemes(half);
|
|
121
|
+
const found = morphs === undefined ? countedByTable(text) : countedByMorphemes(half, morphs);
|
|
122
|
+
Object.assign(last, { text, ready: isReady(), found });
|
|
123
|
+
return found;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const pad = (value: number): string => String(value).padStart(2, "0");
|
|
127
|
+
|
|
128
|
+
const YEAR_FLOOR = 1000;
|
|
129
|
+
|
|
130
|
+
type DateMatch = { readonly date: Mention; readonly used: number };
|
|
131
|
+
|
|
132
|
+
/** right が left の直後に続く、単位 unit の数量か。「2024年 4月」のように間が空けば日付にしない。 */
|
|
133
|
+
const follows = (left: Counted | undefined, right: Counted | undefined, unit: string): right is Counted =>
|
|
134
|
+
left !== undefined && right?.unit === unit && right.start === left.end;
|
|
135
|
+
|
|
136
|
+
const dateOf = (parts: readonly Counted[], value: string): DateMatch => ({
|
|
137
|
+
date: { start: parts[0]?.start ?? 0, end: parts.at(-1)?.end ?? 0, attrs: { value } },
|
|
138
|
+
used: parts.length,
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
/** 「2024年4月1日」「2024年4月」。 */
|
|
142
|
+
const yearMonth = (year: Counted | undefined, month: Counted | undefined, day: Counted | undefined): DateMatch | undefined => {
|
|
143
|
+
if (year?.unit !== "年" || !follows(year, month, "月")) return undefined;
|
|
144
|
+
const base = `${String(year.value)}-${pad(month.value)}`;
|
|
145
|
+
return follows(month, day, "日") ? dateOf([year, month, day], `${base}-${pad(day.value)}`) : dateOf([year, month], base);
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/** 「4月1日」。年の無い日付。 */
|
|
149
|
+
const monthDay = (month: Counted | undefined, day: Counted | undefined): DateMatch | undefined =>
|
|
150
|
+
month?.unit === "月" && follows(month, day, "日") ? dateOf([month, day], `${pad(month.value)}-${pad(day.value)}`) : undefined;
|
|
151
|
+
|
|
152
|
+
/** 年だけのときは 1000 以上なら年(2024年)、それより小さければ期間(3年)。 */
|
|
153
|
+
const yearOnly = (year: Counted | undefined): DateMatch | undefined =>
|
|
154
|
+
year?.unit === "年" && year.value >= YEAR_FLOOR ? dateOf([year], String(year.value)) : undefined;
|
|
155
|
+
|
|
156
|
+
/** 隣り合った「年」「月」「日」を 1 つの日付にする。「2024年4月1日」は 3 つの数量ではなく 2024-04-01。 */
|
|
157
|
+
const toDates = (items: readonly Counted[]): { readonly dates: Mention[]; readonly rest: Counted[] } => {
|
|
158
|
+
const dates: Mention[] = [];
|
|
159
|
+
const rest: Counted[] = [];
|
|
160
|
+
let index = 0;
|
|
161
|
+
while (index < items.length) {
|
|
162
|
+
const [first, second, third] = [items[index], items[index + 1], items[index + 2]];
|
|
163
|
+
const found = yearMonth(first, second, third) ?? monthDay(first, second) ?? yearOnly(first);
|
|
164
|
+
if (found !== undefined) dates.push(found.date);
|
|
165
|
+
else if (first !== undefined) rest.push(first);
|
|
166
|
+
index += found?.used ?? 1;
|
|
167
|
+
}
|
|
168
|
+
return { dates, rest };
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export const quantities = (text: string): Mention[] =>
|
|
172
|
+
toDates(counted(text)).rest.map((item) => ({ start: item.start, end: item.end, attrs: { value: item.value, unit: item.unit } }));
|
|
173
|
+
|
|
174
|
+
export const dates = (text: string): Mention[] => toDates(counted(text)).dates;
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 「1.5 倍になった。」の 1.5 は章番号ではない。番号と続く語のあいだの空白を詰めて読み直し、
|
|
178
|
+
* 続く語が助数詞なら数量の一部と分かる。空白のあるままだと、解析器は「倍」を普通の名詞と読む。
|
|
179
|
+
*/
|
|
180
|
+
export const countedAfter = (number: string, rest: string): boolean => {
|
|
181
|
+
if (!isReady()) return UNITS.some((unit) => rest.startsWith(unit));
|
|
182
|
+
return unitAfterSpace(number, rest) !== undefined;
|
|
183
|
+
};
|
package/src/structure.ts
ADDED
|
@@ -0,0 +1,365 @@
|
|
|
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
|
+
/** 「第四十三条から第五十五条まで 削除」「第五百十六条及び第五百十七条 削除」。削られた条を一行でまとめる法令の書き方。 */
|
|
14
|
+
const ARTICLE_RANGE = new RegExp(
|
|
15
|
+
`^${SPACE}*第(?<n>${NUMBER})条(?:の(?<sub>${NUMBER}))?(?:から第(?<m>${NUMBER})条(?:の(?<msub>${NUMBER}))?まで|及び第(?<m2>${NUMBER})条(?:の(?<msub2>${NUMBER}))?)(?<rest>(?:${SPACE}|(|\\().*|)$`,
|
|
16
|
+
"u",
|
|
17
|
+
);
|
|
18
|
+
const PARAGRAPH = new RegExp(`^${SPACE}*第(?<n>${NUMBER})項(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
|
|
19
|
+
const ITEM = new RegExp(`^${SPACE}*第(?<n>${NUMBER})号(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
|
|
20
|
+
/**
|
|
21
|
+
* 法令の項と号は、番号だけを行頭に置き、全角空白で本文と区切る(「2」「一」)。条の中でだけ読む。
|
|
22
|
+
* 半角数字や漢数字に半角空白が続くだけなら本文として扱う。「3 人で」「一 人で」を項や号にしない。
|
|
23
|
+
*/
|
|
24
|
+
const BARE_PARAGRAPH = new RegExp(`^${SPACE}*(?<n>[0-9]{1,3})\\u3000+(?<rest>\\S.*)$`, "u");
|
|
25
|
+
const BARE_FULLWIDTH_PARAGRAPH = new RegExp(`^${SPACE}*(?<n>[0-9]{1,3})${SPACE}+(?<rest>\\S.*)$`, "u");
|
|
26
|
+
const BARE_ITEM = new RegExp(`^${SPACE}*(?<n>[一二三四五六七八九十]{1,3})\\u3000+(?<rest>\\S.*)$`, "u");
|
|
27
|
+
const PAREN_ITEM = new RegExp(`^${SPACE}*[((](?<n>[0-90-9]{1,3})[))]${SPACE}*(?<rest>\\S.*)$`, "u");
|
|
28
|
+
|
|
29
|
+
/** 「第3条(支払)」の括弧の中が見出し。括弧が無ければ、条は後ろ全部を、項・号は本文なので空を見出しにする。 */
|
|
30
|
+
const headingOf = (rest: string, whole: boolean): string => {
|
|
31
|
+
const trimmed = rest.trim();
|
|
32
|
+
const bracketed = /^[((](?<title>[^))]{1,40})[))]\s*$/u.exec(trimmed)?.groups?.["title"];
|
|
33
|
+
// 法令の「第三条 事業者は、…。」は見出しの無い条で、後ろは本文。文を見出しにしない。
|
|
34
|
+
return bracketed ?? (whole && !trimmed.includes("。") ? trimmed : "");
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const numberOf = (text: string | undefined): string | undefined => {
|
|
38
|
+
const value = text === undefined ? undefined : parseJapaneseNumber(text);
|
|
39
|
+
return value === undefined ? undefined : String(value);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const insideArticle = (context: NumberingContext): boolean => context.open.some((open) => open.kind === "article");
|
|
43
|
+
|
|
44
|
+
/** 「(2)」は開いている「(1)」の兄弟。無ければ、開いているものより一段深い。 */
|
|
45
|
+
const PAREN_LABEL = /^(\d+)$/u;
|
|
46
|
+
const parenDepth = (context: NumberingContext): number =>
|
|
47
|
+
[...context.open].reverse().find((open) => PAREN_LABEL.test(open.label))?.depth ?? (context.open.at(-1)?.depth ?? 0) + 1;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 条の範囲の行は、最初の条の番地を持ち、並びの位置は範囲の最後まで進める。中の条は番地を持たない。
|
|
51
|
+
* 後ろ(「削除」)だけを読むので、範囲の両端を参照として拾わない。
|
|
52
|
+
*/
|
|
53
|
+
const articleRange = (line: string): NumberedLine | undefined => {
|
|
54
|
+
const groups = ARTICLE_RANGE.exec(line)?.groups;
|
|
55
|
+
const first = numberOf(groups?.["n"]);
|
|
56
|
+
const last = numberOf(groups?.["m"] ?? groups?.["m2"]);
|
|
57
|
+
if (groups === undefined || first === undefined || last === undefined) return undefined;
|
|
58
|
+
const sub = numberOf(groups["sub"]);
|
|
59
|
+
const branched = groups["sub"] !== undefined || (groups["msub"] ?? groups["msub2"]) !== undefined;
|
|
60
|
+
const rest = (groups["rest"] ?? "").trim();
|
|
61
|
+
const label = line
|
|
62
|
+
.trim()
|
|
63
|
+
.slice(0, line.trim().length - rest.length)
|
|
64
|
+
.trim();
|
|
65
|
+
return {
|
|
66
|
+
kind: "article",
|
|
67
|
+
depth: 1,
|
|
68
|
+
number: sub === undefined ? first : `${first}-${sub}`,
|
|
69
|
+
absolute: true,
|
|
70
|
+
label,
|
|
71
|
+
heading: headingOf(rest, true),
|
|
72
|
+
rest,
|
|
73
|
+
ordinal: branched ? undefined : Number(first),
|
|
74
|
+
ordinalTo: branched ? undefined : Number(last),
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const article = (line: string): NumberedLine | undefined => {
|
|
79
|
+
const groups = ARTICLE.exec(line)?.groups;
|
|
80
|
+
const main = numberOf(groups?.["n"]);
|
|
81
|
+
if (groups === undefined || main === undefined) return undefined;
|
|
82
|
+
const sub = numberOf(groups["sub"]);
|
|
83
|
+
const branch = groups["sub"] === undefined ? "" : "の" + groups["sub"];
|
|
84
|
+
const label = `第${groups["n"] ?? ""}条${branch}`;
|
|
85
|
+
const rest = (groups["rest"] ?? "").trim();
|
|
86
|
+
// 枝番号の条(第3条の2)は並びの外。第3条の次が第4条であることを乱さない。
|
|
87
|
+
const ordinal = sub === undefined ? Number(main) : undefined;
|
|
88
|
+
return {
|
|
89
|
+
kind: "article",
|
|
90
|
+
depth: 1,
|
|
91
|
+
number: sub === undefined ? main : `${main}-${sub}`,
|
|
92
|
+
absolute: true,
|
|
93
|
+
label,
|
|
94
|
+
heading: headingOf(rest, true),
|
|
95
|
+
rest,
|
|
96
|
+
ordinal,
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
/** 項と号の深さ。法令の第 1 項は番号を持たないので、条の直下の号は第 1 項の号。 */
|
|
101
|
+
const PARAGRAPH_DEPTH = 2;
|
|
102
|
+
const ITEM_DEPTH = 3;
|
|
103
|
+
|
|
104
|
+
type ItemShape = {
|
|
105
|
+
readonly pattern: RegExp;
|
|
106
|
+
readonly depth: (context: NumberingContext) => number;
|
|
107
|
+
readonly label: (n: string) => string;
|
|
108
|
+
readonly needsArticle: boolean;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const ITEM_SHAPES: readonly ItemShape[] = [
|
|
112
|
+
{ pattern: PARAGRAPH, depth: () => PARAGRAPH_DEPTH, label: (n) => `第${n}項`, needsArticle: false },
|
|
113
|
+
{ pattern: ITEM, depth: () => ITEM_DEPTH, label: (n) => `第${n}号`, needsArticle: false },
|
|
114
|
+
{ pattern: BARE_PARAGRAPH, depth: () => PARAGRAPH_DEPTH, label: (n) => n, needsArticle: true },
|
|
115
|
+
{ pattern: BARE_FULLWIDTH_PARAGRAPH, depth: () => PARAGRAPH_DEPTH, label: (n) => n, needsArticle: true },
|
|
116
|
+
{ pattern: BARE_ITEM, depth: () => ITEM_DEPTH, label: (n) => n, needsArticle: true },
|
|
117
|
+
{ pattern: PAREN_ITEM, depth: parenDepth, label: (n) => `(${toHalfWidth(n)})`, needsArticle: false },
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 号が条の直下に来たら、番号の無い第 1 項の中にある。番地を「条.1.号」にして、「第十二条第一項第一号」(12.1.1)と
|
|
122
|
+
* 「第十二条第二項」(12.2)がぶつからないようにする。
|
|
123
|
+
*/
|
|
124
|
+
const inFirstParagraph = (context: NumberingContext, depth: number): boolean =>
|
|
125
|
+
depth === ITEM_DEPTH && [...context.open].reverse().find((open) => open.depth < ITEM_DEPTH)?.kind === "article";
|
|
126
|
+
|
|
127
|
+
const item = (line: string, context: NumberingContext): NumberedLine | undefined =>
|
|
128
|
+
ITEM_SHAPES.reduce<NumberedLine | undefined>((found, shape) => {
|
|
129
|
+
if (found !== undefined || (shape.needsArticle && !insideArticle(context))) return found;
|
|
130
|
+
const groups = shape.pattern.exec(line)?.groups;
|
|
131
|
+
const number = numberOf(groups?.["n"]);
|
|
132
|
+
if (groups === undefined || number === undefined) return undefined;
|
|
133
|
+
const rest = (groups["rest"] ?? "").trim();
|
|
134
|
+
const label = shape.label(groups["n"] ?? "");
|
|
135
|
+
const depth = shape.depth(context);
|
|
136
|
+
const address = inFirstParagraph(context, depth) ? `1.${number}` : number;
|
|
137
|
+
return { kind: "item", depth, number: address, absolute: false, label, heading: headingOf(rest, false), rest, ordinal: Number(number) };
|
|
138
|
+
}, undefined);
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 条より外のまとまり。法令は章の番号を編ごとに、節の番号を章ごとに振り直すので、
|
|
142
|
+
* 章と節は親の番地に続ける(第2編第1章 → pt2.ch1、その第3節 → pt2.ch1.3)。条は通し番号のまま。
|
|
143
|
+
*/
|
|
144
|
+
const CHAPTER = new RegExp(`^${SPACE}*第(?<n>${NUMBER})(?<unit>[編章節])(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
|
|
145
|
+
const CHAPTER_SHAPES: Readonly<Record<string, { readonly depth: number; readonly prefix: string }>> = {
|
|
146
|
+
編: { depth: -2, prefix: "pt" },
|
|
147
|
+
章: { depth: -1, prefix: "ch" },
|
|
148
|
+
節: { depth: 0, prefix: "" },
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const chapter = (line: string): NumberedLine | undefined => {
|
|
152
|
+
const groups = CHAPTER.exec(line)?.groups;
|
|
153
|
+
const number = numberOf(groups?.["n"]);
|
|
154
|
+
const shape = CHAPTER_SHAPES[groups?.["unit"] ?? ""];
|
|
155
|
+
if (groups === undefined || number === undefined || shape === undefined) return undefined;
|
|
156
|
+
const rest = (groups["rest"] ?? "").trim();
|
|
157
|
+
const label = `第${groups["n"] ?? ""}${groups["unit"] ?? ""}`;
|
|
158
|
+
return {
|
|
159
|
+
kind: "chapter",
|
|
160
|
+
depth: shape.depth,
|
|
161
|
+
number: shape.prefix + number,
|
|
162
|
+
absolute: false,
|
|
163
|
+
label,
|
|
164
|
+
heading: headingOf(rest, true),
|
|
165
|
+
rest,
|
|
166
|
+
ordinal: Number(number),
|
|
167
|
+
};
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 古い法令は第 2 項以降にも番号を振らず、全角空白で字下げした行を新しい項にする。条の行に本文が続く条の中で、号でもない字下げの行を、
|
|
172
|
+
* 開いている項の次の項(無ければ第 2 項)として読む。番号付きの項(「2」)がある条では、字下げは使われない。
|
|
173
|
+
*/
|
|
174
|
+
const OLD_STYLE_PARAGRAPH = /^\u3000(?<rest>\S.*)$/u;
|
|
175
|
+
|
|
176
|
+
const unnumberedParagraph = (line: string, context: NumberingContext): NumberedLine | undefined => {
|
|
177
|
+
if (!insideArticle(context)) return undefined;
|
|
178
|
+
const rest = OLD_STYLE_PARAGRAPH.exec(line)?.groups?.["rest"];
|
|
179
|
+
if (rest === undefined) return undefined;
|
|
180
|
+
// 条の行に本文が続く書き方(「第三十四条 使用者は、…。」)のときだけ。契約書の「第1条(目的)」の次の字下げの行は、第 1 項の本文。
|
|
181
|
+
const article = [...context.open].reverse().find((open) => open.kind === "article");
|
|
182
|
+
if (article === undefined || article.heading !== "" || article.rest === "") return undefined;
|
|
183
|
+
const previous = [...context.open].reverse().find((open) => open.depth === PARAGRAPH_DEPTH);
|
|
184
|
+
if (previous !== undefined && previous.label !== "") return undefined;
|
|
185
|
+
const number = String((previous === undefined ? 1 : Number(previous.number)) + 1);
|
|
186
|
+
return { kind: "item", depth: PARAGRAPH_DEPTH, number, absolute: false, label: "", heading: "", rest: rest.trim(), ordinal: Number(number) };
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
const numbered = (line: string, context: NumberingContext): NumberedLine | undefined =>
|
|
190
|
+
chapter(line) ?? articleRange(line) ?? article(line) ?? item(line, context) ?? unnumberedParagraph(line, context);
|
|
191
|
+
|
|
192
|
+
/** 正規表現の一致を Mention にする。g フラグ付きのものだけを渡す。 */
|
|
193
|
+
const mentions = (
|
|
194
|
+
pattern: RegExp,
|
|
195
|
+
text: string,
|
|
196
|
+
attrs: (groups: Readonly<Record<string, string | undefined>>, whole: string, start: number) => Mention["attrs"] | undefined,
|
|
197
|
+
): Mention[] =>
|
|
198
|
+
[...text.matchAll(pattern)].flatMap((match) => {
|
|
199
|
+
const found = attrs(match.groups ?? {}, match[0], match.index);
|
|
200
|
+
return found === undefined ? [] : [{ start: match.index, end: match.index + match[0].length, attrs: found }];
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const DEFINITIONS = [/以下「(?<term>[^「」\n]{1,40})」という/gu, /「(?<term>[^「」\n]{1,40})」とは/gu];
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* 範囲を限った定義。「この条において『X』とは」「前項に規定する『X』とは」は、その条や項の中だけの定義で、
|
|
207
|
+
* 別の条で同じ語を定義し直すのは誤りではない。scope: "local" を付け、重なりの rule に比べさせない。
|
|
208
|
+
*/
|
|
209
|
+
const THIS_PART = /この[条項号款目節章編](?:及び[^、。]{1,20})?において$/u;
|
|
210
|
+
const NEARBY_PART = /[前次同](?:各)?[項条号]に規定する$/u;
|
|
211
|
+
const scopedLocally = (before: string): boolean => THIS_PART.test(before) || NEARBY_PART.test(before);
|
|
212
|
+
|
|
213
|
+
const definitions = (text: string): Mention[] =>
|
|
214
|
+
DEFINITIONS.flatMap((pattern) =>
|
|
215
|
+
mentions(pattern, text, (groups, _whole, start) => {
|
|
216
|
+
if (groups["term"] === undefined) return undefined;
|
|
217
|
+
const local = scopedLocally(text.slice(Math.max(0, start - 30), start));
|
|
218
|
+
return { term: groups["term"], ...(local ? { scope: "local" } : {}) };
|
|
219
|
+
}),
|
|
220
|
+
);
|
|
221
|
+
|
|
222
|
+
const REFERENCE = new RegExp(`第(?<a>${NUMBER})条(?:の(?<s>${NUMBER}))?(?:第(?<p>${NUMBER})項)?(?:第(?<i>${NUMBER})号)?`, "gu");
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 「第12条第1項」→ 12.1、「第3条の2」→ 3-2。番地の付け方は木と同じにする。
|
|
226
|
+
* 法令は第 1 項に番号を振らないので、「第4条第1項」は番号付きの 4.1 が無ければ第4条そのものを指す。その行き先を fallback に入れる。
|
|
227
|
+
* 「民法第709条」のように他の文書の名前が前にあれば、その名前を document に入れる。この文書の木では引かない。
|
|
228
|
+
*/
|
|
229
|
+
/**
|
|
230
|
+
* 他の文書の参照に続く並び(「民事訴訟法第百条第一項、第百一条、第百二条の二」)は、同じ文書の条を指す。
|
|
231
|
+
* 間が読点・接続の語・号や項の断片・括弧書きだけなら、前の参照の文書を引き継ぐ。
|
|
232
|
+
*/
|
|
233
|
+
const CONNECTORS = /及び|並びに|若しくは|又は|から|まで|ただし書|前段|後段|[、,\s]/gu;
|
|
234
|
+
const FRAGMENT = new RegExp(`第${NUMBER}[項号](?:の${NUMBER})*`, "gu");
|
|
235
|
+
const PARENTHESES = /([^()]*)/gu;
|
|
236
|
+
|
|
237
|
+
/** 間の文字列が、接続の語・読点・項や号の断片・括弧書きだけでできているか。 */
|
|
238
|
+
const isContinuation = (gap: string): boolean => gap.replace(PARENTHESES, "").replace(FRAGMENT, "").replace(CONNECTORS, "") === "";
|
|
239
|
+
|
|
240
|
+
const SUBSTITUTION = "とあるのは";
|
|
241
|
+
const SUBSTITUTED = "(読み替えの中)";
|
|
242
|
+
|
|
243
|
+
/** 行を一度だけ読んで、位置ごとの括弧の深さと、読み替えの「」の中かどうかを出す。参照ごとに行を読み直さない。 */
|
|
244
|
+
type LineMap = { readonly depth: Int32Array; readonly substituted: Uint8Array };
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 読み替え(「『X』とあるのは『Y』と読み替える」)の「」の中の番地は、読み替える先の法令のもの。
|
|
248
|
+
* ほかの「」(定義した語、引用)はこの文書の言葉なので、その中の参照もこの文書の参照として扱う。
|
|
249
|
+
*/
|
|
250
|
+
const isSubstitution = (text: string, start: number, end: number): boolean =>
|
|
251
|
+
text.startsWith(SUBSTITUTION, end + 1) || text.slice(Math.max(0, start - SUBSTITUTION.length), start) === SUBSTITUTION;
|
|
252
|
+
|
|
253
|
+
const lineMapOf = (text: string): LineMap => {
|
|
254
|
+
const depth = new Int32Array(text.length + 1);
|
|
255
|
+
const substituted = new Uint8Array(text.length + 1);
|
|
256
|
+
const opens: number[] = [];
|
|
257
|
+
const open = { parentheses: 0 };
|
|
258
|
+
text.split("").forEach((char, at) => {
|
|
259
|
+
depth[at] = open.parentheses;
|
|
260
|
+
if (char === "「") opens.push(at);
|
|
261
|
+
if (char === "」") {
|
|
262
|
+
const start = opens.pop();
|
|
263
|
+
if (start !== undefined && opens.length === 0 && isSubstitution(text, start, at)) substituted.fill(1, start, at + 1);
|
|
264
|
+
}
|
|
265
|
+
if (char === "(") open.parentheses += 1;
|
|
266
|
+
if (char === ")") open.parentheses = Math.max(0, open.parentheses - 1);
|
|
267
|
+
});
|
|
268
|
+
return { depth, substituted };
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* 「第12条第1項」→ 12.1、「第3条の2」→ 3-2。番地の付け方は木と同じにする。
|
|
273
|
+
* 法令は第 1 項に番号を振らないので、「第4条第1項」は番号付きの 4.1 が無ければ第4条そのものを指す。その行き先を fallback に入れる。
|
|
274
|
+
* 「民法第709条」のように他の文書の名前が前にあれば、その名前を document に入れる。この文書の木では引かない。
|
|
275
|
+
*/
|
|
276
|
+
/** at より前で開いたまま閉じていない括弧の数。 */
|
|
277
|
+
/** 間に挟まった括弧書き(中に参照があっても)を外す。入れ子は内側から外す。 */
|
|
278
|
+
const withoutClosedParentheses = (gap: string): string => {
|
|
279
|
+
const once = gap.replace(PARENTHESES, "");
|
|
280
|
+
return once === gap ? gap : withoutClosedParentheses(once);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* 「第12条第1項」→ 12.1、「第3条の2」→ 3-2。番地の付け方は木と同じにする。
|
|
285
|
+
* 法令は第 1 項に番号を振らないので、「第4条第1項」は番号付きの 4.1 が無ければ第4条そのものを指す。その行き先を fallback に入れる。
|
|
286
|
+
*/
|
|
287
|
+
const addressOfReference = (
|
|
288
|
+
groups: Readonly<Record<string, string | undefined>>,
|
|
289
|
+
): { readonly target: string; readonly fallback: string | undefined } | undefined => {
|
|
290
|
+
const main = numberOf(groups["a"]);
|
|
291
|
+
if (main === undefined) return undefined;
|
|
292
|
+
const sub = numberOf(groups["s"]);
|
|
293
|
+
const article = sub === undefined ? main : `${main}-${sub}`;
|
|
294
|
+
const item = numberOf(groups["i"]);
|
|
295
|
+
// 「第二条第一号」は第 1 項の号。項を書かずに号を指すのは、項が一つしかない条。
|
|
296
|
+
const paragraph = numberOf(groups["p"]) ?? (item === undefined ? undefined : "1");
|
|
297
|
+
const target = [article, paragraph, item].filter((part) => part !== undefined).join(".");
|
|
298
|
+
return { target, fallback: paragraph === "1" && item === undefined ? article : undefined };
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const references = (text: string): Mention[] => {
|
|
302
|
+
const map = lineMapOf(text);
|
|
303
|
+
// 括弧書きの中の参照(「(同法第五十九条において準用する場合を含む。)」)は、外の並びを切らない。並びは括弧の深さごとに持つ。
|
|
304
|
+
const chains = new Map<number, { readonly end: number; readonly document: string | undefined }>();
|
|
305
|
+
return [...text.matchAll(REFERENCE)].flatMap((match) => {
|
|
306
|
+
const groups = match.groups ?? {};
|
|
307
|
+
const address = addressOfReference(groups);
|
|
308
|
+
if (address === undefined) return [];
|
|
309
|
+
const { target, fallback } = address;
|
|
310
|
+
const depth = map.depth[match.index] ?? 0;
|
|
311
|
+
const previous = chains.get(depth);
|
|
312
|
+
const inherited =
|
|
313
|
+
previous?.document !== undefined && isContinuation(withoutClosedParentheses(text.slice(previous.end, match.index))) ? previous.document : undefined;
|
|
314
|
+
const inQuote = map.substituted[match.index] === 1 ? SUBSTITUTED : undefined;
|
|
315
|
+
const document = citedDocument(text, match.index) ?? inherited ?? inQuote;
|
|
316
|
+
chains.set(depth, { end: match.index + match[0].length, document });
|
|
317
|
+
const attrs = { target, label: match[0], ...(fallback === undefined ? {} : { fallback }), ...(document === undefined ? {} : { document }) };
|
|
318
|
+
return [{ start: match.index, end: match.index + match[0].length, attrs }];
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/** 長いものから探し、重なったら先に見つけたものを残す。「しなければならない」を「なければならない」と二重に数えない。 */
|
|
323
|
+
const MARKERS: readonly (readonly [string, "must" | "must-not" | "may"])[] = [
|
|
324
|
+
["しなければならない", "must"],
|
|
325
|
+
["なければならない", "must"],
|
|
326
|
+
["してはならない", "must-not"],
|
|
327
|
+
["てはならない", "must-not"],
|
|
328
|
+
["なければなりません", "must"],
|
|
329
|
+
["てはなりません", "must-not"],
|
|
330
|
+
["てはいけません", "must-not"],
|
|
331
|
+
["てはいけない", "must-not"],
|
|
332
|
+
["義務を負う", "must"],
|
|
333
|
+
["ものとする", "must"],
|
|
334
|
+
["禁止する", "must-not"],
|
|
335
|
+
["することができる", "may"],
|
|
336
|
+
["ことができる", "may"],
|
|
337
|
+
["ことができます", "may"],
|
|
338
|
+
];
|
|
339
|
+
|
|
340
|
+
/** 語が現れる位置を全部。再帰や配列の広げ直しをしないので、長い文書でも線形で終わる。 */
|
|
341
|
+
const occurrences = (text: string, word: string): number[] => {
|
|
342
|
+
const found: number[] = [];
|
|
343
|
+
for (let at = text.indexOf(word); at !== -1; at = text.indexOf(word, at + word.length)) found.push(at);
|
|
344
|
+
return found;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* 長い語から当て、すでに取った文字に重なるものは捨てる。重なりは文字ごとの印で見る。
|
|
349
|
+
* 見つけたもの同士を総当たりで比べると、同じ語が何万もある文書で二乗に遅くなる。
|
|
350
|
+
*/
|
|
351
|
+
const obligations = (text: string): Mention[] => {
|
|
352
|
+
const taken = new Uint8Array(text.length);
|
|
353
|
+
const kept: Mention[] = [];
|
|
354
|
+
MARKERS.forEach(([marker, type]) => {
|
|
355
|
+
occurrences(text, marker).forEach((start) => {
|
|
356
|
+
const end = start + marker.length;
|
|
357
|
+
if (taken.subarray(start, end).some((mark) => mark === 1)) return;
|
|
358
|
+
taken.fill(1, start, end);
|
|
359
|
+
kept.push({ start, end, attrs: { marker, type } });
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
return kept.sort((left, right) => left.start - right.start);
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
export const structure: StructurePatterns = { numbered, definitions, references, obligations, quantities, dates, countedAfter };
|