@chaffjs/lang-ja 0.8.0 → 0.10.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 +17 -1
- package/dist/citation.d.ts.map +1 -1
- package/dist/citation.js +22 -49
- package/dist/citation.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -5
- package/dist/index.js.map +1 -1
- package/dist/pos.d.ts.map +1 -1
- package/dist/pos.js +48 -12
- package/dist/pos.js.map +1 -1
- package/dist/quantities.d.ts +8 -0
- package/dist/quantities.d.ts.map +1 -1
- package/dist/quantities.js +53 -6
- package/dist/quantities.js.map +1 -1
- package/dist/section-reference.d.ts +14 -0
- package/dist/section-reference.d.ts.map +1 -0
- package/dist/section-reference.js +37 -0
- package/dist/section-reference.js.map +1 -0
- package/dist/sentence-split.d.ts +6 -0
- package/dist/sentence-split.d.ts.map +1 -0
- package/dist/sentence-split.js +114 -0
- package/dist/sentence-split.js.map +1 -0
- package/dist/structure.d.ts.map +1 -1
- package/dist/structure.js +45 -57
- package/dist/structure.js.map +1 -1
- package/dist/surface-starts.d.ts +6 -0
- package/dist/surface-starts.d.ts.map +1 -0
- package/dist/surface-starts.js +15 -0
- package/dist/surface-starts.js.map +1 -0
- package/lexicons/ai-tell.yaml +3 -3
- package/lexicons/closing-cliche.yaml +1 -1
- package/lexicons/document-kind.yaml +26 -0
- package/lexicons/double-keigo.yaml +10 -10
- package/lexicons/kana-title-kind.yaml +5 -0
- package/lexicons/name-note.yaml +7 -0
- package/lexicons/not-section-after.yaml +19 -0
- package/lexicons/not-section-before.yaml +11 -0
- package/lexicons/sasete-itadaku.yaml +3 -3
- package/lexicons/self-prefix.yaml +6 -0
- package/lexicons/total-label.yaml +9 -0
- package/lexicons/weekday.yaml +11 -0
- package/package.json +1 -1
- package/src/citation.ts +37 -52
- package/src/index.ts +2 -7
- package/src/pos.ts +51 -11
- package/src/quantities.ts +60 -6
- package/src/section-reference.ts +56 -0
- package/src/sentence-split.ts +131 -0
- package/src/structure.ts +51 -60
- package/src/surface-starts.ts +13 -0
package/src/structure.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { Mention, NumberedLine, NumberingContext, StructurePatterns } from "chaffjs/plugin";
|
|
2
2
|
import { parseJapaneseNumber, toHalfWidth } from "./numbers.ts";
|
|
3
|
-
import { citedDocument } from "./citation.ts";
|
|
3
|
+
import { citationVocabulary, citedDocument } from "./citation.ts";
|
|
4
|
+
import { loadLexicons } from "./lexicons.ts";
|
|
4
5
|
import { countedAfter, dates, quantities } from "./quantities.ts";
|
|
6
|
+
import { sectionReferences, sectionVocabulary } from "./section-reference.ts";
|
|
5
7
|
|
|
6
8
|
// 契約書・規程・法令の番号の書き方。core は番号の書き方を知らず、ここで読んだものを入れ子にする。
|
|
7
9
|
|
|
@@ -167,28 +169,8 @@ const chapter = (line: string): NumberedLine | undefined => {
|
|
|
167
169
|
};
|
|
168
170
|
};
|
|
169
171
|
|
|
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
|
-
// 条の行そのものを見る。前の行から付いた見出しは、条の行の書き方を変えない。
|
|
182
|
-
const article = [...context.open].reverse().find((open) => open.kind === "article");
|
|
183
|
-
if (article === undefined || headingOf(article.rest, true) !== "" || article.rest === "") return undefined;
|
|
184
|
-
const previous = [...context.open].reverse().find((open) => open.depth === PARAGRAPH_DEPTH);
|
|
185
|
-
if (previous !== undefined && previous.label !== "") return undefined;
|
|
186
|
-
const number = String((previous === undefined ? 1 : Number(previous.number)) + 1);
|
|
187
|
-
return { kind: "item", depth: PARAGRAPH_DEPTH, number, absolute: false, label: "", heading: "", rest: rest.trim(), ordinal: Number(number) };
|
|
188
|
-
};
|
|
189
|
-
|
|
190
172
|
const numbered = (line: string, context: NumberingContext): NumberedLine | undefined =>
|
|
191
|
-
chapter(line) ?? articleRange(line) ?? article(line) ?? item(line, context)
|
|
173
|
+
chapter(line) ?? articleRange(line) ?? article(line) ?? item(line, context);
|
|
192
174
|
|
|
193
175
|
/** 正規表現の一致を Mention にする。g フラグ付きのものだけを渡す。 */
|
|
194
176
|
const mentions = (
|
|
@@ -238,35 +220,16 @@ const PARENTHESES = /([^()]*)/gu;
|
|
|
238
220
|
/** 間の文字列が、接続の語・読点・項や号の断片・括弧書きだけでできているか。 */
|
|
239
221
|
const isContinuation = (gap: string): boolean => gap.replace(PARENTHESES, "").replace(FRAGMENT, "").replace(CONNECTORS, "") === "";
|
|
240
222
|
|
|
241
|
-
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
/** 行を一度だけ読んで、位置ごとの括弧の深さと、読み替えの「」の中かどうかを出す。参照ごとに行を読み直さない。 */
|
|
245
|
-
type LineMap = { readonly depth: Int32Array; readonly substituted: Uint8Array };
|
|
246
|
-
|
|
247
|
-
/**
|
|
248
|
-
* 読み替え(「『X』とあるのは『Y』と読み替える」)の「」の中の番地は、読み替える先の法令のもの。
|
|
249
|
-
* ほかの「」(定義した語、引用)はこの文書の言葉なので、その中の参照もこの文書の参照として扱う。
|
|
250
|
-
*/
|
|
251
|
-
const isSubstitution = (text: string, start: number, end: number): boolean =>
|
|
252
|
-
text.startsWith(SUBSTITUTION, end + 1) || text.slice(Math.max(0, start - SUBSTITUTION.length), start) === SUBSTITUTION;
|
|
253
|
-
|
|
254
|
-
const lineMapOf = (text: string): LineMap => {
|
|
223
|
+
/** 行を一度だけ読んで、位置ごとの括弧の深さを出す。参照ごとに行を読み直さない。 */
|
|
224
|
+
const depthsOf = (text: string): Int32Array => {
|
|
255
225
|
const depth = new Int32Array(text.length + 1);
|
|
256
|
-
const substituted = new Uint8Array(text.length + 1);
|
|
257
|
-
const opens: number[] = [];
|
|
258
226
|
const open = { parentheses: 0 };
|
|
259
227
|
text.split("").forEach((char, at) => {
|
|
260
228
|
depth[at] = open.parentheses;
|
|
261
|
-
if (char === "「") opens.push(at);
|
|
262
|
-
if (char === "」") {
|
|
263
|
-
const start = opens.pop();
|
|
264
|
-
if (start !== undefined && opens.length === 0 && isSubstitution(text, start, at)) substituted.fill(1, start, at + 1);
|
|
265
|
-
}
|
|
266
229
|
if (char === "(") open.parentheses += 1;
|
|
267
230
|
if (char === ")") open.parentheses = Math.max(0, open.parentheses - 1);
|
|
268
231
|
});
|
|
269
|
-
return
|
|
232
|
+
return depth;
|
|
270
233
|
};
|
|
271
234
|
|
|
272
235
|
/**
|
|
@@ -299,25 +262,44 @@ const addressOfReference = (
|
|
|
299
262
|
return { target, fallback: paragraph === "1" && item === undefined ? article : undefined };
|
|
300
263
|
};
|
|
301
264
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
265
|
+
/** 他の文書の名前を読む語と、章・節の番地でない言い方。語彙表から一度だけ作る。 */
|
|
266
|
+
const LEXICONS = loadLexicons();
|
|
267
|
+
const CITATION = citationVocabulary(LEXICONS);
|
|
268
|
+
const SECTIONS = sectionVocabulary(LEXICONS);
|
|
269
|
+
|
|
270
|
+
const sectionsOf = (text: string): Mention[] => sectionReferences(text, SECTIONS);
|
|
271
|
+
|
|
272
|
+
const articlesOf = (text: string): Mention[] =>
|
|
273
|
+
[...text.matchAll(REFERENCE)].flatMap((match) => {
|
|
274
|
+
const address = addressOfReference(match.groups ?? {});
|
|
309
275
|
if (address === undefined) return [];
|
|
310
276
|
const { target, fallback } = address;
|
|
311
|
-
const
|
|
312
|
-
const previous = chains.get(depth);
|
|
313
|
-
const inherited =
|
|
314
|
-
previous?.document !== undefined && isContinuation(withoutClosedParentheses(text.slice(previous.end, match.index))) ? previous.document : undefined;
|
|
315
|
-
const inQuote = map.substituted[match.index] === 1 ? SUBSTITUTED : undefined;
|
|
316
|
-
const document = citedDocument(text, match.index) ?? inherited ?? inQuote;
|
|
317
|
-
chains.set(depth, { end: match.index + match[0].length, document });
|
|
318
|
-
const attrs = { target, label: match[0], ...(fallback === undefined ? {} : { fallback }), ...(document === undefined ? {} : { document }) };
|
|
277
|
+
const attrs = { target, label: match[0], ...(fallback === undefined ? {} : { fallback }) };
|
|
319
278
|
return [{ start: match.index, end: match.index + match[0].length, attrs }];
|
|
320
279
|
});
|
|
280
|
+
|
|
281
|
+
/** 条の参照と章・節の参照を文書の順に並べ、他の文書の名前を付ける。「民法第3章及び第4章」の第4章も民法の章。 */
|
|
282
|
+
const references = (text: string): Mention[] => {
|
|
283
|
+
const depths = depthsOf(text);
|
|
284
|
+
// 括弧書きの中の参照(「(同法第五十九条において準用する場合を含む。)」)は、外の並びを切らない。並びは括弧の深さごとに持つ。
|
|
285
|
+
const chains = new Map<number, { readonly end: number; readonly document: string | undefined }>();
|
|
286
|
+
return [...articlesOf(text), ...sectionsOf(text)]
|
|
287
|
+
.sort((left, right) => left.start - right.start)
|
|
288
|
+
.map((mention) => {
|
|
289
|
+
const depth = depths[mention.start] ?? 0;
|
|
290
|
+
const previous = chains.get(depth);
|
|
291
|
+
const inherited =
|
|
292
|
+
previous?.document !== undefined && isContinuation(withoutClosedParentheses(text.slice(previous.end, mention.start))) ? previous.document : undefined;
|
|
293
|
+
const document = citedDocument(text, mention.start, CITATION) ?? inherited;
|
|
294
|
+
chains.set(depth, { end: mention.end, document });
|
|
295
|
+
return document === undefined ? mention : { ...mention, attrs: { ...mention.attrs, document } };
|
|
296
|
+
});
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/** 「3.2節」の 3.2 は節の番地で、節の数ではない。 */
|
|
300
|
+
const quantitiesOutsideSections = (text: string): Mention[] => {
|
|
301
|
+
const sections = sectionsOf(text);
|
|
302
|
+
return quantities(text).filter((quantity) => !sections.some((section) => quantity.start < section.end && section.start < quantity.end));
|
|
321
303
|
};
|
|
322
304
|
|
|
323
305
|
/** 長いものから探し、重なったら先に見つけたものを残す。「しなければならない」を「なければならない」と二重に数えない。 */
|
|
@@ -366,4 +348,13 @@ const obligations = (text: string): Mention[] => {
|
|
|
366
348
|
/** 「二十二」「2」を数にする。相対の参照(前二項・前条第二項)を core が読むときに使う。 */
|
|
367
349
|
const number = (text: string): number | undefined => parseJapaneseNumber(toHalfWidth(text));
|
|
368
350
|
|
|
369
|
-
export const structure: StructurePatterns = {
|
|
351
|
+
export const structure: StructurePatterns = {
|
|
352
|
+
numbered,
|
|
353
|
+
definitions,
|
|
354
|
+
references,
|
|
355
|
+
obligations,
|
|
356
|
+
quantities: quantitiesOutsideSections,
|
|
357
|
+
dates,
|
|
358
|
+
countedAfter,
|
|
359
|
+
number,
|
|
360
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 語の文字を前から順に本文と照らし、それぞれの始まり(UTF-16 の位置)を返す。見つからない語は undefined。
|
|
3
|
+
* kuromoji の word_position は、記号のまとまり(「)、」「**、」)や絵文字の後ろで本文とずれる。語をつなげた文字は本文と同じなので、照らせば正しい位置が出る。
|
|
4
|
+
*/
|
|
5
|
+
export const surfaceStarts = (text: string, surfaces: readonly string[]): (number | undefined)[] => {
|
|
6
|
+
const scan = { cursor: 0 };
|
|
7
|
+
return surfaces.map((surface) => {
|
|
8
|
+
const at = surface === "" ? -1 : text.indexOf(surface, scan.cursor);
|
|
9
|
+
if (at === -1) return undefined;
|
|
10
|
+
scan.cursor = at + surface.length;
|
|
11
|
+
return at;
|
|
12
|
+
});
|
|
13
|
+
};
|