@chaffjs/lang-ja 0.34.0 → 0.36.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.
Files changed (75) hide show
  1. package/dist/english-list-line.d.ts +6 -0
  2. package/dist/english-list-line.d.ts.map +1 -0
  3. package/dist/english-list-line.js +91 -0
  4. package/dist/english-list-line.js.map +1 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +12 -1
  7. package/dist/index.js.map +1 -1
  8. package/dist/list-line.d.ts +23 -0
  9. package/dist/list-line.d.ts.map +1 -0
  10. package/dist/list-line.js +102 -0
  11. package/dist/list-line.js.map +1 -0
  12. package/lexicons/abbreviation-mention.yaml +9 -0
  13. package/lexicons/addressee-prefix.yaml +6 -0
  14. package/lexicons/addressee-suffix.yaml +8 -0
  15. package/lexicons/addressee-title.yaml +35 -0
  16. package/lexicons/adversative-ga-prefatory.yaml +27 -0
  17. package/lexicons/any-year-marker.yaml +6 -0
  18. package/lexicons/breakdown-partial-word.yaml +7 -0
  19. package/lexicons/cited-title-action.yaml +13 -0
  20. package/lexicons/common-acronym.yaml +13 -0
  21. package/lexicons/company-form-latin.yaml +20 -0
  22. package/lexicons/comparative-baseline.yaml +21 -1
  23. package/lexicons/comparison-marker.yaml +7 -0
  24. package/lexicons/compound-slip-ignored.yaml +13 -0
  25. package/lexicons/count-title.yaml +13 -0
  26. package/lexicons/date-stamp-label.yaml +1 -0
  27. package/lexicons/definition-alternative.yaml +6 -0
  28. package/lexicons/duration-per-period.yaml +17 -0
  29. package/lexicons/figure-scale-joiner.yaml +8 -0
  30. package/lexicons/figure-scale-word.yaml +11 -0
  31. package/lexicons/headline-ending.yaml +9 -0
  32. package/lexicons/known-misspelling.yaml +23 -0
  33. package/lexicons/latin-bound-suffix.yaml +10 -0
  34. package/lexicons/latin-joiner.yaml +10 -0
  35. package/lexicons/line-head-tag.yaml +10 -0
  36. package/lexicons/list-note-lead.yaml +8 -0
  37. package/lexicons/literal-quote-cue.yaml +22 -0
  38. package/lexicons/memo-header-label.yaml +8 -0
  39. package/lexicons/mention-cue.yaml +11 -0
  40. package/lexicons/misconversion.yaml +13 -0
  41. package/lexicons/nado-closed-list.yaml +4 -0
  42. package/lexicons/nakaguro-word.yaml +13 -0
  43. package/lexicons/named-deadline.yaml +34 -0
  44. package/lexicons/negation-word.yaml +1 -1
  45. package/lexicons/notice-header.yaml +10 -0
  46. package/lexicons/notice-phrase.yaml +15 -0
  47. package/lexicons/numbering-counter.yaml +13 -0
  48. package/lexicons/organisation-form.yaml +13 -11
  49. package/lexicons/orthographic-variant-comparative.yaml +8 -0
  50. package/lexicons/orthographic-variant-conjunction.yaml +13 -0
  51. package/lexicons/orthographic-variant-distinct.yaml +3 -0
  52. package/lexicons/orthographic-variant-idiom.yaml +14 -0
  53. package/lexicons/orthographic-variant-judging.yaml +8 -0
  54. package/lexicons/orthographic-variant-whole-word.yaml +17 -0
  55. package/lexicons/particle-run-break.yaml +23 -0
  56. package/lexicons/person-other-sense.yaml +6 -0
  57. package/lexicons/range-kana-unit.yaml +16 -0
  58. package/lexicons/request-condition.yaml +4 -1
  59. package/lexicons/request-owner.yaml +3 -0
  60. package/lexicons/requirement-open-condition.yaml +9 -0
  61. package/lexicons/requirement-open-end.yaml +21 -0
  62. package/lexicons/requirement-open-question.yaml +18 -0
  63. package/lexicons/rounded-amount-unit.yaml +13 -0
  64. package/lexicons/self-prefix.yaml +1 -0
  65. package/lexicons/stated-deadline.yaml +4 -0
  66. package/lexicons/superlative-suffix.yaml +5 -0
  67. package/lexicons/teki-fixed-term.yaml +12 -0
  68. package/lexicons/title-ending.yaml +6 -1
  69. package/lexicons/translationese.yaml +5 -0
  70. package/lexicons/unnumbered-heading.yaml +6 -0
  71. package/lexicons/vague-deadline.yaml +1 -0
  72. package/package.json +1 -1
  73. package/src/english-list-line.ts +106 -0
  74. package/src/index.ts +14 -1
  75. package/src/list-line.ts +130 -0
@@ -1,4 +1,4 @@
1
- # 題の付いたもの(要綱・法令・事業・制度・試験・計画・補助金)の名前を閉じる語。漢字の連なりがこれで終わり、
1
+ # 題の付いたもの(要綱・法令・事業・制度・試験・計画・補助金・届け出の書式)の名前を閉じる語。漢字の連なりがこれで終わり、
2
2
  # 題の置き方(括弧の中身を閉じる、すぐ後ろに補いの括弧が付く、文がその連なりだけ、見出しにある)をしていれば、
3
3
  # 書き手が選んだ複合語ではなく、変えられない名前(大阪市鶴見区役所公募型比較見積実施要綱、国家公務員中途採用者選考試験)。
4
4
  # 閉じる語は語の切れ目で始まり、前に 2 字以上の語が要る(解決方法の「法」、本事業は当たらない)。
@@ -27,3 +27,8 @@ entries:
27
27
  - pattern: 奨励金
28
28
  - pattern: 給付金
29
29
  - pattern: 案件
30
+ - pattern: 申込書
31
+ - pattern: 申請書
32
+ - pattern: 届出書
33
+ - pattern: 報告書
34
+ - pattern: 証明書
@@ -3,6 +3,8 @@
3
3
  # 人の書いた技術記事でもふつうに使う言い回し(〜を提供する、〜に焦点を当てる、〜することができる)は載せない。
4
4
  # 一つの文に二つの語が当たると二つと数えるので、ほかの語を含む長い形(することを可能にする)は載せない。
5
5
  # 動詞は原形で書けば活用した形(可能にします、果たしている)にも当たる。rewrite は日本語として素直な言い方の例。
6
+ # group address は読み手への呼びかけ(あなたの)。行動規範や案内のように読み手に語りかける文書では書き手の選んだ書き方なので、
7
+ # ほかの言い回しと一緒のときだけ数える。
6
8
  id: translationese
7
9
  language: ja
8
10
  entries:
@@ -29,8 +31,10 @@ entries:
29
31
  - pattern: という利点を持つ
30
32
  rewrite: 〜という利点がある
31
33
  - pattern: あなたは
34
+ group: address
32
35
  rewrite: 主語を省く、または「読者は」「利用者は」
33
36
  - pattern: あなたの
37
+ group: address
34
38
  rewrite: 主語を省く、または「自分の」
35
39
  - pattern: 彼女は
36
40
  rewrite: 名前で呼ぶ、または主語を省く
@@ -45,6 +49,7 @@ entries:
45
49
  - pattern: 驚くべきことに
46
50
  rewrite: 省く、または何が意外なのかを書く
47
51
  - pattern: あなた自身
52
+ group: address
48
53
  rewrite: 自分、または主語を省く
49
54
  - pattern: 最後になりましたが
50
55
  rewrite: 最後に
@@ -33,6 +33,12 @@ entries:
33
33
  - pattern: お問い合わせ先
34
34
  - pattern: 問合せ先
35
35
  - pattern: お問合せ先
36
+ - pattern: 問合わせ先
37
+ - pattern: お問合わせ先
38
+ - pattern: 問い合わせ窓口
39
+ - pattern: お問い合わせ窓口
40
+ # 官公庁の報道発表が使う「照会先」。
41
+ - pattern: 照会先
36
42
  - pattern: はしがき
37
43
  - pattern: あとがき
38
44
  - pattern: 序文
@@ -7,6 +7,7 @@ entries:
7
7
  - pattern: すみやかに
8
8
  - pattern: 可及的速やかに
9
9
  - pattern: 遅滞なく
10
+ - pattern: 遅延なく
10
11
  - pattern: 早急に
11
12
  - pattern: 相当の期間内に
12
13
  - pattern: 相当期間内に
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaffjs/lang-ja",
3
- "version": "0.34.0",
3
+ "version": "0.36.0",
4
4
  "description": "Japanese language adapter for chaff",
5
5
  "license": "MIT",
6
6
  "author": "isamu",
@@ -0,0 +1,106 @@
1
+ import type { Span } from "chaffjs/plugin";
2
+
3
+ // 英語のアダプタの list-line.ts の写し(英文だけの段落を英語のアダプタと同じに切るため)。アダプタは互いに依存しない。
4
+
5
+ /**
6
+ * Items written one per line with no blank line between them ("(a) …;\n(b) …", plain-text "1." or "-" lines, lines that
7
+ * were `<br>`). An item seldom ends in a full stop, so the splitter reads the whole run as one sentence.
8
+ *
9
+ * English is hard-wrapped, and a wrapped line can start like an item ("in Section 2\n(a) of this Agreement", "in
10
+ * RFC\n793. It"). So a break before an item is cut only on a cue: the line before ends a lead-in or an item, the next
11
+ * marker continues the list ("-" after "-", "(b)" after "(a)"), both lines are items and the next starts with a capital,
12
+ * or a bracketed marker is followed by a capital. A break after
13
+ * an item into a line with no marker is never cut: that line continues the item as often as it starts new text.
14
+ */
15
+
16
+ const PAREN = String.raw`\((?:\d{1,3}[A-Z]?|[a-z]{1,2}|[A-Z]|[ivxlc]{2,6}|[IVXLC]{2,6})\)`;
17
+ const NUMBER = String.raw`\d{1,3}(?:\.\d{1,3})*[.)]|\d{1,3}(?:\.\d{1,3})+`;
18
+ const LETTER = String.raw`[a-z][.)]`;
19
+ /** An appendix's numbered part ("A.1.", "B.2.3"), as an RFC writes it in the body and in its contents. */
20
+ const APPENDIX = String.raw`[A-Z](?:\.\d{1,3})+\.?`;
21
+ const BULLET = "[-–•●▪◦‣*·]";
22
+
23
+ /** The marker, then the first character of the item's text. A marker other than a bracketed one needs a space after it. */
24
+ const MARKER = new RegExp(String.raw`^[ \t]*(?:(${PAREN})[ \t]*|(${NUMBER}|${APPENDIX}|${LETTER}|${BULLET})[ \t]+)(\S)?`, "u");
25
+
26
+ /** marker: as written ("(a)", "2.", "-"). capital: the item's text starts with a capital or a digit. */
27
+ type Item = { readonly marker: string; readonly bracketed: boolean; readonly capital: boolean };
28
+
29
+ const itemOf = (line: string): Item | undefined => {
30
+ const match = MARKER.exec(line);
31
+ if (match === null) return undefined;
32
+ return { marker: match[1] ?? match[2] ?? "", bracketed: match[1] !== undefined, capital: /[\p{Lu}\d]/u.test(match[3] ?? "") };
33
+ };
34
+
35
+ const BULLET_ONLY = new RegExp(`^${BULLET}$`, "u");
36
+
37
+ /** A number or a single letter between the same brackets or before the same stop ("(a)", "2.", "b)"). */
38
+ const SEQUENCED = /^(\(?)(\d{1,3}|[a-zA-Z])([.)]?)$/u;
39
+
40
+ const nextLabel = (label: string): string => (/\d/u.test(label) ? String(Number(label) + 1) : String.fromCharCode(label.charCodeAt(0) + 1));
41
+
42
+ /** The second marker continues the first: the same bullet ("-", "-"), or the next number or letter written alike ("(a)", "(b)"). */
43
+ const continuesList = (first: string, second: string): boolean => {
44
+ if (BULLET_ONLY.test(first)) return first === second;
45
+ const [, open, label, close] = SEQUENCED.exec(first) ?? [];
46
+ if (label === undefined) return false;
47
+ return second === `${open ?? ""}${nextLabel(label)}${close ?? ""}`;
48
+ };
49
+
50
+ /** Whether the line starts with a list marker. */
51
+ const startsWithListMarker = (line: string): boolean => itemOf(line) !== undefined;
52
+
53
+ /** The end of a line, without a Markdown hard break ("\" or trailing spaces). */
54
+ const endOf = (line: string): string => {
55
+ const trimmed = line.trimEnd();
56
+ return trimmed.endsWith("\\") ? trimmed.slice(0, -1).trimEnd() : trimmed;
57
+ };
58
+
59
+ /** A lead-in or an item that ends here: the next item is a new sentence whatever it starts with. */
60
+ const LEAD_IN_END = /(?:[:;—–]|[;,:][ \t]*(?:and|or|and\/or|but|then))$/u;
61
+ /** Ends the line's own item: a comma or a conjunction after it, when the line is an item too. */
62
+ const ITEM_END = /(?:,|\b(?:and|or))$/u;
63
+ /** A full stop the splitter may have read as an abbreviation ("Inc.", "U.S."). */
64
+ const STOP_END = /[.?!]["”’)]?$/u;
65
+
66
+ const isCut = (previous: string, next: string): boolean => {
67
+ const item = itemOf(next);
68
+ if (item === undefined) return false;
69
+ const end = endOf(previous);
70
+ const before = itemOf(previous);
71
+ const previousIsItem = before !== undefined;
72
+ if (LEAD_IN_END.test(end)) return true;
73
+ if (before !== undefined && continuesList(before.marker, item.marker)) return true;
74
+ if (previousIsItem && ITEM_END.test(end)) return true;
75
+ if (!item.capital) return false;
76
+ return previousIsItem || item.bracketed || STOP_END.test(end);
77
+ };
78
+
79
+ type Line = { readonly start: number; readonly end: number; readonly text: string };
80
+
81
+ const NEWLINE = /\r?\n/gu;
82
+
83
+ const linesOf = (text: string): Line[] => {
84
+ const breaks = [...text.matchAll(NEWLINE)];
85
+ const starts = [0, ...breaks.map((match) => match.index + match[0].length)];
86
+ return starts.map((start, index) => {
87
+ const end = breaks[index]?.index ?? text.length;
88
+ return { start, end, text: text.slice(start, end) };
89
+ });
90
+ };
91
+
92
+ const indentOf = (line: Line): number => line.text.length - line.text.trimStart().length;
93
+
94
+ /** The line breaks that end a sentence: from the end of the line before to the first character of the item. */
95
+ export const listLineBreaks = (text: string): Span[] => {
96
+ const lines = linesOf(text);
97
+ return lines.slice(1).flatMap((next, index) => {
98
+ const previous = lines[index];
99
+ if (previous === undefined || previous.text.trim() === "" || !isCut(previous.text, next.text)) return [];
100
+ return [{ start: previous.end, end: next.start + indentOf(next) }];
101
+ });
102
+ };
103
+
104
+ /** Each item's head: from the start of its line to the marker's first character. A sentence starting here starts an item. */
105
+ export const listItemHeads = (text: string): Span[] =>
106
+ linesOf(text).flatMap((line) => (startsWithListMarker(line.text) ? [{ start: line.start, end: line.start + indentOf(line) + 1 }] : []));
package/src/index.ts CHANGED
@@ -9,6 +9,8 @@ import { splitAtQuotedStops } from "./quoted-stop.ts";
9
9
  import { reattachClosingQuotes } from "./closing-quote.ts";
10
10
  import { insideBrackets, startsWithCloser } from "./inside-brackets.ts";
11
11
  import { structure } from "./structure.ts";
12
+ import { cutAtBreaks, lineVocabulary, listItemHeads, listLineBreaks, startsListItem } from "./list-line.ts";
13
+ import { listItemHeads as englishListItemHeads, listLineBreaks as englishListLineBreaks } from "./english-list-line.ts";
12
14
  import { isReady, predicateOnly, prepare, readsAsCounter, readsAsOneAdverb, readsAsOneWord, tokenize } from "./pos.ts";
13
15
  import { markSpacedCounters } from "./spaced-counter.ts";
14
16
  import { tokensWithin } from "./tokens-within.ts";
@@ -45,6 +47,8 @@ const LINE_BREAK = /[\n\r]/u;
45
47
 
46
48
  const ENGLISH: EmbeddedLanguage = { id: "en", lengthUnit: "word" };
47
49
 
50
+ const asListItem = (sentence: Sentence): Sentence => ({ ...sentence, listItem: true });
51
+
48
52
  const withLanguage = (text: string, span: Span): Sentence => (isEnglishRun(text) ? { span, text, embeddedLanguage: ENGLISH } : { span, text });
49
53
 
50
54
  /**
@@ -53,6 +57,10 @@ const withLanguage = (text: string, span: Span): Sentence => (isEnglishRun(text)
53
57
  */
54
58
  const merge = (source: string, spans: readonly Span[]): Sentence[] => {
55
59
  const open = insideBrackets(source);
60
+ // 英文だけの段落は英語のアダプタと同じに切る(項目の印も英語の読み方で)。行頭の「J.」は人名の頭文字で、項目の印ではない。
61
+ const english = isEnglishRun(source);
62
+ const listBreaks = english ? englishListLineBreaks(source) : listLineBreaks(source, LINE_VOCABULARY);
63
+ const itemHeads = english ? englishListItemHeads(source) : listItemHeads(source);
56
64
  return spans
57
65
  .reduce<Span[]>((acc, span) => {
58
66
  const last = acc.at(-1);
@@ -65,7 +73,9 @@ const merge = (source: string, spans: readonly Span[]): Sentence[] => {
65
73
  else acc.push(span);
66
74
  return acc;
67
75
  }, [])
68
- .map((span) => withLanguage(source.slice(span.start, span.end), span));
76
+ .flatMap((span) => cutAtBreaks(span, listBreaks))
77
+ .map((span) => withLanguage(source.slice(span.start, span.end), span))
78
+ .map((sentence) => (startsListItem(itemHeads, sentence.span.start) ? asListItem(sentence) : sentence));
69
79
  };
70
80
 
71
81
  /**
@@ -91,6 +101,9 @@ const withTokens = (source: string, sentences: readonly Sentence[]): Sentence[]
91
101
  }));
92
102
  };
93
103
 
104
+ const patternsOf = (id: string): string[] => (LEXICONS[id] ?? []).map((entry) => entry.pattern);
105
+ const LINE_VOCABULARY = lineVocabulary(patternsOf("numbering-counter"), patternsOf("line-head-tag"));
106
+
94
107
  const LABEL_STOPS = labelStops((LEXICONS["abbreviated-label"] ?? []).map((entry) => entry.pattern));
95
108
 
96
109
  /**
@@ -0,0 +1,130 @@
1
+ import type { Span } from "chaffjs/plugin";
2
+ import { escapeRegExp } from "./regexp.ts";
3
+
4
+ /**
5
+ * 空行を挟まず 1 行に 1 項目を並べた箇条書き(「①誓約書」「・〜に関する業務」「(1)学歴:…」、`<br>` で改めた行)。
6
+ * 項目は句点で終わらないので、分割器は項目の並び全体を 1 文に読む。行頭の印が、そこで文が改まる印。
7
+ *
8
+ * 切るのは、印で始まる行の前と後ろの改行。ただし前の行が読点で終わるか、括弧を開いたまま終わるなら、文は次の行へ続いている。
9
+ * 印の無い行の折り返しは切らない。
10
+ *
11
+ * 番号だけの札の行(「第1」「第3条」)と、行頭の括弧で囲んだ札(「(別添)…」)も、印で始まる行と同じに切る。札の語は辞書から取る。
12
+ */
13
+
14
+ const SPACE = "[ \\t\\u3000]";
15
+ /** 記号の印。後ろに何が続いても項目の頭。「○」「◎」は議事録の発言者の印でもあるので含めない。 */
16
+ const BULLET = "[・•●■□◆◇▪▫★☆▶►➢✓]";
17
+ /** 番号の印。囲み数字(①〜㊿・⑴〜⒇)、括弧の番号・英字・仮名((1)(イ))、数字と点・閉じ括弧(1. 1.2))。 */
18
+ const NUMBERED = [
19
+ "[\\u2460-\\u2473\\u3251-\\u325F\\u32B1-\\u32BF\\u2474-\\u2487]",
20
+ "[((][0-90-9]{1,3}[))]",
21
+ "[((](?:[a-zA-Za-zA-Z]|[ivx]{2,4}|[IVX]{2,4})[))]",
22
+ "[((][\\p{Script=Katakana}\\p{Script=Hiragana}][))]",
23
+ "[0-90-9]{1,3}[..))](?![0-90-9])",
24
+ ].join("|");
25
+ /**
26
+ * 英字や片仮名 1 字の印は、空白で本文と区切るときだけ(「a. 教育機関」、片仮名 1 字と全角空白)。「イベント」は語。
27
+ * 法令の号の漢数字は、全角空白で本文と区切るときだけ。半角空白なら「一 人で」のような本文。
28
+ */
29
+ const SPACED = "(?:(?:[a-z][.)]|[ア-ンア-ン])(?=[ \\t\\u3000])|[一二三四五六七八九十]{1,3}(?=\\u3000))";
30
+ /** 番号の後ろが助詞なら、番号は文の中の語(「①に掲げる」「(1)の規定」)。 */
31
+ const PARTICLE_AFTER = "(?![ \\t\\u3000]*(?:の|に|を|が|は|と|で|へ|も|から|まで|より|及び|又は|並びに|若しくは))";
32
+
33
+ const MARKER = new RegExp(`^${SPACE}*(?:${BULLET}|(?:${NUMBERED}|${SPACED})${PARTICLE_AFTER})`, "u");
34
+
35
+ /** 行が箇条書きの印で始まるか。 */
36
+ export const startsWithListMarker = (line: string): boolean => MARKER.test(line);
37
+
38
+ /** 行の終わりで、文が次の行へ続いている: 読点で終わるか、開いた括弧を閉じずに終わる。 */
39
+ const COMMA_END = /[、,,][ \t\u3000]*$/u;
40
+ const OPENERS = /[((「『【[[{{〔〈《]/gu;
41
+ const CLOSERS = /[))」』】]\]}}〕〉》]/gu;
42
+ const leavesBracketOpen = (line: string): boolean => (line.match(OPENERS)?.length ?? 0) > (line.match(CLOSERS)?.length ?? 0);
43
+ const continuesToNextLine = (line: string): boolean => COMMA_END.test(line) || leavesBracketOpen(line);
44
+
45
+ type Line = { readonly start: number; readonly end: number; readonly text: string };
46
+
47
+ const NEWLINE = /\r?\n/gu;
48
+
49
+ const linesOf = (text: string): Line[] => {
50
+ const breaks = [...text.matchAll(NEWLINE)];
51
+ const starts = [0, ...breaks.map((match) => match.index + match[0].length)];
52
+ return starts.map((start, index) => {
53
+ const end = breaks[index]?.index ?? text.length;
54
+ return { start, end, text: text.slice(start, end) };
55
+ });
56
+ };
57
+
58
+ /**
59
+ * 注記の印(「※」)。前の行に添える注記で、前の行の文を終えるとは限らない(「ファックス番号:…\n※送ったらご連絡ください。」)。
60
+ * 注記は折り返して次の行へ続くこともあるので、注記の行の前と後ろでは文を改めない。ただし注記の行が並べば、その間で改める。
61
+ * そこで文が始まっていれば項目と同じく段落を改める。
62
+ */
63
+ const NOTE = /^[ \t\u3000]*※/u;
64
+
65
+ /**
66
+ * 印のほかに、行を項目と同じに切る札。
67
+ * label は番号だけの行(「第1」「第3条」「第二章の二」)。数える語は辞書 numbering-counter から。
68
+ * tag は行頭の括弧で囲んだ札(「(別添)」「【参考】」)。札の語は辞書 line-head-tag から。後ろが助詞なら文の中の語(「(別添)の資料」)。
69
+ */
70
+ export type LineVocabulary = { readonly label: RegExp; readonly tag: RegExp | undefined };
71
+
72
+ const NUMERAL = "[0-90-9〇一二三四五六七八九十百千]+";
73
+ const TAG_OPENER = "[((【〔[]";
74
+ const TAG_CLOSER = "[))】〕]]";
75
+ const alternativesOf = (words: readonly string[]): string => words.map(escapeRegExp).join("|");
76
+
77
+ /** counters は番号を数える語(条、章)、tags は括弧で囲む札の語(別添、参考)。 */
78
+ export const lineVocabulary = (counters: readonly string[], tags: readonly string[]): LineVocabulary => {
79
+ const counter = counters.length === 0 ? "" : `(?:${alternativesOf(counters)})?`;
80
+ return {
81
+ label: new RegExp(`^${SPACE}*第${NUMERAL}${counter}(?:の${NUMERAL})*${SPACE}*$`, "u"),
82
+ tag: tags.length === 0 ? undefined : new RegExp(`^${SPACE}*${TAG_OPENER}(?:${alternativesOf(tags)})${TAG_CLOSER}${PARTICLE_AFTER}`, "u"),
83
+ };
84
+ };
85
+
86
+ /** 項目の行: 箇条書きの印、番号だけの札、行頭の括弧の札で始まる。その前でも後ろでも文が改まる。 */
87
+ const isListLine = (line: string, vocabulary: LineVocabulary): boolean =>
88
+ startsWithListMarker(line) || vocabulary.label.test(line) || (vocabulary.tag?.test(line) ?? false);
89
+
90
+ /** 前の行と後ろの行の間で文が改まるか: どちらかが項目の行か、注記の行が続き、前の行が次へ続いていない。 */
91
+ const isCut = (previous: Line, next: Line, vocabulary: LineVocabulary): boolean =>
92
+ next.text.trim().length > 0 &&
93
+ !continuesToNextLine(previous.text) &&
94
+ (isListLine(next.text, vocabulary) || isListLine(previous.text, vocabulary) || (NOTE.test(previous.text) && NOTE.test(next.text)));
95
+
96
+ /** 文を改める改行の位置: 前の行の終わり(end)と、次の行の最初の文字(start)。 */
97
+ export const listLineBreaks = (text: string, vocabulary: LineVocabulary): Span[] => {
98
+ const lines = linesOf(text);
99
+ return lines.slice(1).flatMap((next, index) => {
100
+ const previous = lines[index];
101
+ if (previous === undefined || !isCut(previous, next, vocabulary)) return [];
102
+ return [{ start: previous.end, end: next.start + (next.text.length - next.text.trimStart().length) }];
103
+ });
104
+ };
105
+
106
+ /** 箇条書きの項目か注記の行の頭: 行の始まりから印の最初の文字まで。項目を始める文は、ここで始まる。 */
107
+ export const listItemHeads = (text: string): Span[] =>
108
+ linesOf(text).flatMap((line) =>
109
+ startsWithListMarker(line.text) || NOTE.test(line.text)
110
+ ? [{ start: line.start, end: line.start + line.text.length - line.text.trimStart().length + 1 }]
111
+ : [],
112
+ );
113
+
114
+ /** offset が、項目の頭(heads、並び順で重ならない)のどれかの中にあるか。 */
115
+ export const startsListItem = (lines: readonly Span[], offset: number): boolean => {
116
+ const search = (low: number, high: number): Span | undefined => {
117
+ if (low >= high) return lines[low - 1];
118
+ const middle = (low + high) >> 1;
119
+ return (lines[middle]?.start ?? Number.POSITIVE_INFINITY) > offset ? search(low, middle) : search(middle + 1, high);
120
+ };
121
+ const line = search(0, lines.length);
122
+ return line !== undefined && offset < line.end;
123
+ };
124
+
125
+ /** span を、中にある改行(breaks)で割る。割った前の片は改行の手前で終わり、後ろの片は次の行の最初の文字から始まる。 */
126
+ export const cutAtBreaks = (span: Span, breaks: readonly Span[]): Span[] => {
127
+ const inside = breaks.filter((part) => part.start > span.start && part.end < span.end);
128
+ const starts = [span.start, ...inside.map((part) => part.end)];
129
+ return starts.map((start, index) => ({ start, end: inside[index]?.start ?? span.end }));
130
+ };