@chaffjs/lang-ja 0.16.0 → 0.18.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 (151) hide show
  1. package/README.md +1 -1
  2. package/dist/citation.d.ts +6 -3
  3. package/dist/citation.d.ts.map +1 -1
  4. package/dist/citation.js +31 -9
  5. package/dist/citation.js.map +1 -1
  6. package/dist/closing-quote.d.ts +6 -0
  7. package/dist/closing-quote.d.ts.map +1 -0
  8. package/dist/closing-quote.js +50 -0
  9. package/dist/closing-quote.js.map +1 -0
  10. package/dist/english-run.d.ts +8 -0
  11. package/dist/english-run.d.ts.map +1 -0
  12. package/dist/english-run.js +24 -0
  13. package/dist/english-run.js.map +1 -0
  14. package/dist/index.d.ts.map +1 -1
  15. package/dist/index.js +56 -21
  16. package/dist/index.js.map +1 -1
  17. package/dist/inside-brackets.d.ts +5 -0
  18. package/dist/inside-brackets.d.ts.map +1 -0
  19. package/dist/inside-brackets.js +44 -0
  20. package/dist/inside-brackets.js.map +1 -0
  21. package/dist/label-stop.d.ts +13 -0
  22. package/dist/label-stop.d.ts.map +1 -0
  23. package/dist/label-stop.js +14 -0
  24. package/dist/label-stop.js.map +1 -0
  25. package/dist/lexicons.d.ts.map +1 -1
  26. package/dist/lexicons.js +4 -0
  27. package/dist/lexicons.js.map +1 -1
  28. package/dist/long-vowel-form.d.ts +12 -0
  29. package/dist/long-vowel-form.d.ts.map +1 -0
  30. package/dist/long-vowel-form.js +28 -0
  31. package/dist/long-vowel-form.js.map +1 -0
  32. package/dist/number-stop.d.ts +3 -0
  33. package/dist/number-stop.d.ts.map +1 -0
  34. package/dist/number-stop.js +25 -0
  35. package/dist/number-stop.js.map +1 -0
  36. package/dist/pos.d.ts +17 -1
  37. package/dist/pos.d.ts.map +1 -1
  38. package/dist/pos.js +69 -36
  39. package/dist/pos.js.map +1 -1
  40. package/dist/predicate-frame.d.ts +14 -0
  41. package/dist/predicate-frame.d.ts.map +1 -0
  42. package/dist/predicate-frame.js +22 -0
  43. package/dist/predicate-frame.js.map +1 -0
  44. package/dist/quantities.d.ts.map +1 -1
  45. package/dist/quantities.js +11 -1
  46. package/dist/quantities.js.map +1 -1
  47. package/dist/quoted-stop.d.ts +13 -0
  48. package/dist/quoted-stop.d.ts.map +1 -0
  49. package/dist/quoted-stop.js +45 -0
  50. package/dist/quoted-stop.js.map +1 -0
  51. package/dist/ra-dropped.d.ts +12 -0
  52. package/dist/ra-dropped.d.ts.map +1 -0
  53. package/dist/ra-dropped.js +18 -0
  54. package/dist/ra-dropped.js.map +1 -0
  55. package/dist/reduplication.d.ts +17 -5
  56. package/dist/reduplication.d.ts.map +1 -1
  57. package/dist/reduplication.js +85 -6
  58. package/dist/reduplication.js.map +1 -1
  59. package/dist/regexp.d.ts +3 -0
  60. package/dist/regexp.d.ts.map +1 -0
  61. package/dist/regexp.js +4 -0
  62. package/dist/regexp.js.map +1 -0
  63. package/dist/sentence-close.d.ts +2 -0
  64. package/dist/sentence-close.d.ts.map +1 -0
  65. package/dist/sentence-close.js +10 -0
  66. package/dist/sentence-close.js.map +1 -0
  67. package/dist/sentence-split.d.ts +2 -0
  68. package/dist/sentence-split.d.ts.map +1 -1
  69. package/dist/sentence-split.js +3 -1
  70. package/dist/sentence-split.js.map +1 -1
  71. package/dist/structure.d.ts.map +1 -1
  72. package/dist/structure.js +60 -21
  73. package/dist/structure.js.map +1 -1
  74. package/dist/well-formed.d.ts +4 -1
  75. package/dist/well-formed.d.ts.map +1 -1
  76. package/dist/well-formed.js +5 -2
  77. package/dist/well-formed.js.map +1 -1
  78. package/lexicons/abbreviated-label.yaml +16 -0
  79. package/lexicons/ai-tell.yaml +105 -0
  80. package/lexicons/announcing-opener.yaml +11 -0
  81. package/lexicons/assistant-residue.yaml +37 -0
  82. package/lexicons/calendar-era.yaml +10 -0
  83. package/lexicons/closing-cliche.yaml +2 -0
  84. package/lexicons/comparison-marker.yaml +7 -0
  85. package/lexicons/contrast-frame.yaml +9 -0
  86. package/lexicons/contrast-lead.yaml +10 -0
  87. package/lexicons/contrast-turn.yaml +9 -0
  88. package/lexicons/count-anchor.yaml +10 -0
  89. package/lexicons/count-counter.yaml +17 -0
  90. package/lexicons/count-hedge.yaml +61 -0
  91. package/lexicons/count-number.yaml +16 -0
  92. package/lexicons/distributive-noun.yaml +14 -2
  93. package/lexicons/double-negative.yaml +32 -0
  94. package/lexicons/doubled-word.yaml +12 -0
  95. package/lexicons/email-attachment-note.yaml +8 -0
  96. package/lexicons/email-attribution.yaml +10 -0
  97. package/lexicons/email-header-field.yaml +37 -0
  98. package/lexicons/email-written-field.yaml +11 -0
  99. package/lexicons/enumeration-frame.yaml +32 -0
  100. package/lexicons/enumeration-joiner.yaml +15 -0
  101. package/lexicons/enumeration-negation.yaml +10 -0
  102. package/lexicons/figure-label.yaml +11 -0
  103. package/lexicons/item-number.yaml +38 -0
  104. package/lexicons/long-vowel-distinct.yaml +25 -0
  105. package/lexicons/long-vowel-drop-after.yaml +25 -0
  106. package/lexicons/measure-unit.yaml +78 -0
  107. package/lexicons/name-note.yaml +5 -1
  108. package/lexicons/neutral-ending.yaml +10 -0
  109. package/lexicons/nominalization-event-noun.yaml +13 -0
  110. package/lexicons/nominalization-light-verb.yaml +10 -0
  111. package/lexicons/numbered-label.yaml +50 -0
  112. package/lexicons/passive-predicate-frame.yaml +13 -0
  113. package/lexicons/percent-unit.yaml +7 -0
  114. package/lexicons/placeholder-word.yaml +21 -0
  115. package/lexicons/ra-dropped-verb.yaml +7 -0
  116. package/lexicons/range-closer.yaml +5 -0
  117. package/lexicons/range-connector.yaml +11 -0
  118. package/lexicons/range-opener.yaml +7 -0
  119. package/lexicons/requirement-either.yaml +13 -0
  120. package/lexicons/requirement-loophole.yaml +13 -0
  121. package/lexicons/requirement-marker.yaml +15 -0
  122. package/lexicons/requirement-modal-jis-z8301-2019.yaml +14 -0
  123. package/lexicons/requirement-not-marker.yaml +8 -0
  124. package/lexicons/requirement-open-end.yaml +6 -0
  125. package/lexicons/sasete-itadaku.yaml +11 -0
  126. package/lexicons/share-exception.yaml +7 -0
  127. package/lexicons/share-label.yaml +11 -0
  128. package/lexicons/stock-transition.yaml +11 -0
  129. package/lexicons/vague-clause-pointer.yaml +14 -0
  130. package/lexicons/vague-figure-pointer.yaml +23 -0
  131. package/package.json +1 -1
  132. package/src/citation.ts +35 -10
  133. package/src/closing-quote.ts +53 -0
  134. package/src/english-run.ts +25 -0
  135. package/src/index.ts +54 -13
  136. package/src/inside-brackets.ts +45 -0
  137. package/src/label-stop.ts +26 -0
  138. package/src/lexicons.ts +4 -0
  139. package/src/long-vowel-form.ts +38 -0
  140. package/src/number-stop.ts +30 -0
  141. package/src/pos.ts +78 -29
  142. package/src/predicate-frame.ts +28 -0
  143. package/src/quantities.ts +12 -1
  144. package/src/quoted-stop.ts +53 -0
  145. package/src/ra-dropped.ts +29 -0
  146. package/src/reduplication.ts +119 -8
  147. package/src/regexp.ts +3 -0
  148. package/src/sentence-close.ts +10 -0
  149. package/src/sentence-split.ts +3 -1
  150. package/src/structure.ts +62 -19
  151. package/src/well-formed.ts +5 -2
@@ -1,8 +1,9 @@
1
1
  import type { Lexicon, Token } from "chaffjs/plugin";
2
2
 
3
3
  /**
4
- * 名詞を重ねて「それぞれの」を言う形(会社会社で、部署部署の)。重ねられるのは、まとまりや場合を指す名詞だけで、
5
- * 後ろに助詞が続く。二つ目の名詞に UD の Echo=Rdp を付ける。「資料資料の」「確認確認する」は書き損じのまま。
4
+ * 名詞を重ねて「それぞれの」を言う形(会社会社で、チームチームの)。重ねられるのは、まとまりや場合を指す名詞だけで、
5
+ * 後ろに助詞が続く。二つ目の名詞に UD の Echo=Rdp を付ける。二字以上の和語・漢語の重なりは isWholeWordEcho も拾うので、
6
+ * ここが要るのは外来語(チーム)と一字の語(国)。
6
7
  */
7
8
  export type Distributive = { readonly nouns: ReadonlySet<string>; readonly particles: ReadonlySet<string> };
8
9
 
@@ -153,8 +154,8 @@ const FREE_WORD = new Set(["名詞", "動詞", "形容詞"]);
153
154
  /** 接尾語が付く語。 */
154
155
  const HOST = new Set(["名詞", "動詞", "形容詞", "助動詞"]);
155
156
 
156
- /** 拍に数えない小さい仮名(ちょ・しゃ)。 */
157
- const SMALL_KANA = /[ぁぃぅぇぉゃゅょゎ]/gu;
157
+ /** 拍に数えない小さい仮名(ちょ・しゃ・ティ)。 */
158
+ const SMALL_KANA = /[ぁぃぅぇぉゃゅょゎァィゥェォャュョヮ]/gu;
158
159
 
159
160
  /** 擬音・擬態語の根は二拍(すう・きし・ちょん)。動詞に読まれた重なりは二拍までが擬音で、長い動詞の重なり(できるできる)は書き損じ。 */
160
161
  const MIMETIC_ROOT_MORAE = 2;
@@ -177,7 +178,7 @@ const isAttachedSuffix = (before: Inflection | undefined, word: Inflection): boo
177
178
 
178
179
  /**
179
180
  * 擬音・擬態語は副詞として立つ。後ろに「と」「に」が続くか、行が終わる(歌の行末。解析器は改行を「記号,空白」と読む)。
180
- * 句読点の前は、名詞の書き損じ(まとめまとめ、)と見分けられない。
181
+ * 句読点の前は、動詞の書き損じ(できるできる、)と見分けられない。
181
182
  */
182
183
  const ADVERB_MARK = new Set(["と", "に"]);
183
184
 
@@ -187,7 +188,7 @@ const endsAsAdverb = (second: Inflection, next: Inflection | undefined): boolean
187
188
  /**
188
189
  * 平仮名だけの語を丸ごと重ねた形(きしきし・ちょんちょん・すうすう・しだいしだい・あはれあはれ)は擬音・擬態語か感動詞。
189
190
  * 解析器は辞書に無い擬音を、表層の合う名詞や動詞に切って読む(すう = 吸う)ので、品詞ではなく平仮名であることと、副詞の位置に立つことで見分ける。
190
- * 前の語に付いた接尾語(田中さんさん)と、副詞の位置に立たない重なり(まとめまとめを・できるできるように)は書き損じのまま。
191
+ * 前の語に付いた接尾語(田中さんさん)と、副詞の位置に立たない動詞の重なり(できるできるように)は書き損じのまま。名詞の重なりは isWholeWordEcho が見る。
191
192
  */
192
193
  export const isKanaEcho = (words: readonly Inflection[], index: number): boolean => {
193
194
  const [before, first, second] = [words[index - 2], words[index - 1], words[index]];
@@ -201,5 +202,115 @@ export const isKanaEcho = (words: readonly Inflection[], index: number): boolean
201
202
  );
202
203
  };
203
204
 
204
- /** 活用した語の重なりか、仮名の語の重なり。pos.ts が二つ目に Echo=Rdp を付ける。 */
205
- export const isEchoAt = (words: readonly Inflection[], index: number): boolean => isInflectedEcho(words, index) || isKanaEcho(words, index);
205
+ /** 名詞の細分類のうち、それだけでは語にならないもの(こと・の、さん・的、数)。 */
206
+ const DEPENDENT_NOUN = new Set(["非自立", "接尾", "数"]);
207
+
208
+ const isContentNoun = (word: Inflection): boolean => word.pos === "名詞" && !DEPENDENT_NOUN.has(word.detail);
209
+
210
+ /** 言い切りの形の形容詞(えらい・若い)。続く形(長く長く)は isInflectedEcho が見る。 */
211
+ const isPlainAdjective = (word: Inflection): boolean => word.pos === "形容詞" && word.detail === "自立" && word.form === "基本形";
212
+
213
+ /** 漢字だけで書いた名詞。漢語(確認・資料)の重なりはたいてい書き損じ。 */
214
+ const KANJI_ONLY = /^\p{Script=Han}+$/u;
215
+
216
+ /** 漢字の名詞でも重ねて言うもの。時を言う副詞的な名詞(毎年・各自・一瞬)と、形容動詞の語幹(駄目・大変)。 */
217
+ const REDUPLICATING_DETAIL = new Set(["副詞可能", "形容動詞語幹"]);
218
+
219
+ /**
220
+ * 重ねて言える名詞か。漢字だけの名詞は、人・単位・時・場所のまとまりを言うもの(個人・一行・時代・地域。語彙表 distributive-noun)と
221
+ * REDUPLICATING_DETAIL の細分類だけ。仮名や送り仮名のある名詞(それ・もちもち・早め)は丸ごと重ねれば畳語か強め。
222
+ */
223
+ const reduplicates = (noun: Inflection, kanjiNouns: ReadonlySet<string>): boolean =>
224
+ !KANJI_ONLY.test(noun.surface) || REDUPLICATING_DETAIL.has(noun.detail) || kanjiNouns.has(noun.surface);
225
+
226
+ /** 二つ目の名詞は、解析器が前の名詞に付く接尾語と読むことがある(好き好き・嫌い嫌い の二つ目)。 */
227
+ const isWholeWordPair = (first: Inflection, second: Inflection, kanjiNouns: ReadonlySet<string>): boolean =>
228
+ (isContentNoun(first) && second.pos === "名詞" && reduplicates(first, kanjiNouns)) || (isPlainAdjective(first) && isPlainAdjective(second));
229
+
230
+ const KATAKANA = /^[\p{Script=Katakana}ー]+$/u;
231
+
232
+ /** 擬音・擬態語の根の拍の数(ムク・ブイ・ゴロン・ブー)。 */
233
+ const MIMETIC_UNIT = { fewest: 2, most: 3 } as const;
234
+
235
+ /** 伸ばす音が根の途中にある語(データ)は外来語。擬音の伸ばす音は根の終わりに来る(ブーブー)。 */
236
+ const INNER_LONG_VOWEL = /ー./u;
237
+
238
+ const isMimeticUnit = (surface: string): boolean => {
239
+ const morae = moraeOf(surface);
240
+ return morae >= MIMETIC_UNIT.fewest && morae <= MIMETIC_UNIT.most && !INNER_LONG_VOWEL.test(surface);
241
+ };
242
+
243
+ /** 「する」は名詞を動詞にするだけで、擬音の後ろの動詞(ブイブイ言わせる)とは違い、外来語の重なり(テストテストする)にも付く。 */
244
+ const LIGHT_VERB = "する";
245
+
246
+ /** 擬音は副詞として立つ。後ろに「と」「に」が続くか、行が終わるか(endsAsAdverb)、動詞に直接かかる(ブイブイ言わせる)。 */
247
+ const standsAsMimetic = (second: Inflection, next: Inflection | undefined): boolean =>
248
+ endsAsAdverb(second, next) || (next !== undefined && next.pos === "動詞" && next.detail === "自立" && next.surface !== LIGHT_VERB && touches(second, next));
249
+
250
+ /**
251
+ * 片仮名の語はたいてい外来語(ユーザー・テスト・メモ)で、重ねれば書き損じ。擬音(ムクムクと・ブイブイ言わせる)は、根が擬音の形で、
252
+ * 副詞の位置に立つものだけ。解析器は擬音の根も名詞(椋・ブイ)と読むので、品詞では見分けられない。
253
+ */
254
+ const isLoanwordPair = (second: Inflection, next: Inflection | undefined): boolean =>
255
+ KATAKANA.test(second.surface) && !(isMimeticUnit(second.surface) && standsAsMimetic(second, next));
256
+
257
+ const sameAndTouching = (first: Inflection | undefined, second: Inflection | undefined): boolean =>
258
+ first !== undefined && second !== undefined && first.surface === second.surface && touches(first, second);
259
+
260
+ /**
261
+ * 内容語(名詞・形容詞)を丸ごと重ねた形は畳語か強め(個人個人・一行一行・駄目駄目・えらいえらい・それそれ・もちもち)。
262
+ * 漢語の名詞は reduplicates が認めるものだけで、ほかの重なり(確認確認・資料資料)は書き損じ。kanjiNouns は pos.ts が語彙表から渡す。
263
+ * 一字の漢字(法法・金金)は isIteratedKanji、外来語(ユーザーユーザー)と三つ続く重なり(早め早め早め)は書き損じのまま。
264
+ */
265
+ export const isWholeWordEcho = (words: readonly Inflection[], index: number, kanjiNouns: ReadonlySet<string>): boolean => {
266
+ const [before, first, second] = [words[index - 2], words[index - 1], words[index]];
267
+ return (
268
+ first !== undefined &&
269
+ second !== undefined &&
270
+ isWholeWordPair(first, second, kanjiNouns) &&
271
+ sameAndTouching(first, second) &&
272
+ first.surface.length >= MIN_ECHO_LENGTH &&
273
+ !isLoanwordPair(second, words[index + 1]) &&
274
+ !sameAndTouching(before, first)
275
+ );
276
+ };
277
+
278
+ /**
279
+ * 重ねて笑い声や擬音になる一字の仮名。平仮名は笑い声の行(ははは・ふふふ・へへへ)だけで、助詞(ををを・ににに・よよよ)は書き損じ。
280
+ * 片仮名の一字(ババババ・ドドド)は擬音で、片仮名の助詞は書かない。
281
+ */
282
+ const REPEATED_KANA = /^[はひふへほ\p{Script=Katakana}]$/u;
283
+
284
+ /** 前の語に付かずに立つ並びの、前に来てよい語。記号(「・、)と、笑い声の頭の「あ」(フィラー・感動詞)。 */
285
+ const FREE_BEFORE = new Set(["記号", "フィラー", "感動詞"]);
286
+
287
+ /** index の語を含む、同じ語が接して続く並びの最初の位置。 */
288
+ const runStart = (words: readonly Inflection[], index: number): number => {
289
+ const before = words.slice(0, index + 1).findLastIndex((word, at) => !sameAndTouching(words[at - 1], word));
290
+ return Math.max(before, 0);
291
+ };
292
+
293
+ /** 並びが前の語に付いていない(文の頭、空白の後ろ、記号やフィラーの後ろ)。付いていれば助詞・助動詞の書き損じ(資料ををを・行ったたた)。 */
294
+ const standsFree = (words: readonly Inflection[], start: number): boolean => {
295
+ const [before, first] = [words[start - 1], words[start]];
296
+ return before === undefined || first === undefined || !touches(before, first) || FREE_BEFORE.has(before.pos);
297
+ };
298
+
299
+ /**
300
+ * 笑い声・擬音の一字の仮名が三つ以上続き、前の語に付かずに立つ並び(ははは・あははは・ふふふ・ドドド)は笑い声か擬音。解析器は一字ずつ助詞などに切る。
301
+ * 二つだけの重なり(をを・たた・よよ・かか)、助詞の仮名の並び(ををを)、前の語に付いた並び(私ははは)は書き損じのまま。
302
+ */
303
+ export const isKanaRepeat = (words: readonly Inflection[], index: number): boolean => {
304
+ const [before, first, second, after] = [words[index - 2], words[index - 1], words[index], words[index + 1]];
305
+ return (
306
+ second !== undefined &&
307
+ REPEATED_KANA.test(second.surface) &&
308
+ sameAndTouching(first, second) &&
309
+ (sameAndTouching(before, first) || sameAndTouching(second, after)) &&
310
+ standsFree(words, runStart(words, index))
311
+ );
312
+ };
313
+
314
+ /** 活用した語・仮名の語・内容語の重なりか、一字の仮名の三つ以上の並び。pos.ts が二つ目に Echo=Rdp を付ける。 */
315
+ export const isEchoAt = (words: readonly Inflection[], index: number, kanjiNouns: ReadonlySet<string>): boolean =>
316
+ isInflectedEcho(words, index) || isKanaEcho(words, index) || isWholeWordEcho(words, index, kanjiNouns) || isKanaRepeat(words, index);
package/src/regexp.ts ADDED
@@ -0,0 +1,3 @@
1
+ // lang-en/src/regexp.ts の写し。アダプタは互いに依存しないので、和文の中の英文に同じ手当てをするために置く。元とのずれは test_mixed_language_stops.ts で比べる。
2
+ /** A word from a lexicon, matched as written: a sign in it is that character, not a pattern. */
3
+ export const escapeRegExp = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/gu, String.raw`\$&`);
@@ -0,0 +1,10 @@
1
+ /**
2
+ * 文が閉じているか。句点の後に閉じ括弧と空白が続いてよい。
3
+ *
4
+ * 「.」は、論文などが「,.」で書くときの句点。仮名・漢字か閉じ括弧の後だけ句点と読む。
5
+ * 数字の後の「.」は番号か小数点なので(1.はじめに、3.5%)、文を閉じない。
6
+ */
7
+ const CLOSED = /[。!?!?]["))」』]*\s*$/u;
8
+ const FULL_STOP_AFTER_WORD = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}ー))」』].["))」』]*\s*$/u;
9
+
10
+ export const closesSentence = (text: string): boolean => CLOSED.test(text) || FULL_STOP_AFTER_WORD.test(text);
@@ -45,6 +45,8 @@ const { language } = DefaultAbbrMarkerOptions;
45
45
  const ABBREVIATIONS = new Set(
46
46
  [...language.ABBREVIATIONS, ...language.PREPOSITIVE_ABBREVIATIONS, ...language.EXCLAMATION_WORDS].map((word) => word.toLowerCase()),
47
47
  );
48
+ /** 分割器が略語と読む語(ピリオドまで含めて渡す)。「Dr.」「U.S.」「Jan.」。 */
49
+ export const isAbbreviation = (word: string): boolean => ABBREVIATIONS.has(word.toLowerCase());
48
50
  // 「J. Smith」のような語は前の語を見て略語か決まる。前の語が切れ目の向こうにあると判定が変わる。
49
51
  const CAPITAL_DOT = /\p{Lu}\./gu;
50
52
 
@@ -76,7 +78,7 @@ const indexesOf = (text: string, pattern: RegExp): number[] => [...text.matchAll
76
78
  /** 句点の並びの直後。和文は直前が仮名・漢字の句点、英文は略語でない語のピリオド。 */
77
79
  const stopEnds = (text: string): number[] => {
78
80
  const japanese = [...text.matchAll(JAPANESE_STOPS)].filter((match) => isCJK(text[match.index - 1]));
79
- const english = [...text.matchAll(ENGLISH_STOP)].filter((match) => !ABBREVIATIONS.has(match[0].toLowerCase()));
81
+ const english = [...text.matchAll(ENGLISH_STOP)].filter((match) => !isAbbreviation(match[0]));
80
82
  return [...japanese, ...english].map((match) => match.index + match[0].length).toSorted((a, b) => a - b);
81
83
  };
82
84
 
package/src/structure.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Mention, NumberedLine, NumberingContext, StructurePatterns } from "chaffjs/plugin";
2
2
  import { parseJapaneseNumber, toHalfWidth } from "./numbers.ts";
3
- import { citationVocabulary, citedDocument } from "./citation.ts";
3
+ import { citationVocabulary, citedDocument, namedDocument } from "./citation.ts";
4
4
  import { loadLexicons } from "./lexicons.ts";
5
5
  import { countedAfter, dates, quantities } from "./quantities.ts";
6
6
  import { sectionReferences, sectionVocabulary } from "./section-reference.ts";
@@ -13,6 +13,12 @@ const SPACE = "[ \\t\\u3000]";
13
13
 
14
14
  /** 見出しとして読めるのは、番号の直後が空白・括弧・行末のとき。「第3条に定める」は本文。 */
15
15
  const ARTICLE = new RegExp(`^${SPACE}*第(?<n>${NUMBER})条(?:の(?<sub>${NUMBER}))?(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
16
+ /**
17
+ * 見出しの行では、題を詰めて書いた「第7条委託」「第2章概要」も読む。後ろが平仮名(「第3条に定める」「第2章では」)、
18
+ * 次の番号(「第2章第1節」)、並べる語(「第4条及び第5条」「第2条若しくは第3条」「第2条乃至第5条」)なら、番号は題の札でないので読まない。
19
+ */
20
+ const TIGHT_TITLE = "(?<rest>[^\\s\\p{Script=Hiragana}0-90-9第及又並若乃].*)";
21
+ const ARTICLE_TIGHT = new RegExp(`^${SPACE}*第(?<n>${NUMBER})条(?:の(?<sub>${NUMBER}))?${TIGHT_TITLE}$`, "u");
16
22
  /** 「第四十三条から第五十五条まで 削除」「第五百十六条及び第五百十七条 削除」。削られた条を一行でまとめる法令の書き方。 */
17
23
  const ARTICLE_RANGE = new RegExp(
18
24
  `^${SPACE}*第(?<n>${NUMBER})条(?:の(?<sub>${NUMBER}))?(?:から第(?<m>${NUMBER})条(?:の(?<msub>${NUMBER}))?まで|及び第(?<m2>${NUMBER})条(?:の(?<msub2>${NUMBER}))?)(?<rest>(?:${SPACE}|(|\\().*|)$`,
@@ -37,6 +43,9 @@ const headingOf = (rest: string, whole: boolean): string => {
37
43
  return bracketed ?? (whole && !trimmed.includes("。") ? trimmed : "");
38
44
  };
39
45
 
46
+ const headingGroups = (pattern: RegExp, line: string, context: NumberingContext): Readonly<Record<string, string | undefined>> | undefined =>
47
+ context.isHeading ? pattern.exec(line)?.groups : undefined;
48
+
40
49
  const numberOf = (text: string | undefined): string | undefined => {
41
50
  const value = text === undefined ? undefined : parseJapaneseNumber(text);
42
51
  return value === undefined ? undefined : String(value);
@@ -88,8 +97,8 @@ const articleRange = (line: string): NumberedLine | undefined => {
88
97
  };
89
98
  };
90
99
 
91
- const article = (line: string): NumberedLine | undefined => {
92
- const groups = ARTICLE.exec(line)?.groups;
100
+ const article = (line: string, context: NumberingContext): NumberedLine | undefined => {
101
+ const groups = ARTICLE.exec(line)?.groups ?? headingGroups(ARTICLE_TIGHT, line, context);
93
102
  const main = numberOf(groups?.["n"]);
94
103
  if (groups === undefined || main === undefined) return undefined;
95
104
  const sub = numberOf(groups["sub"]);
@@ -155,14 +164,15 @@ const item = (line: string, context: NumberingContext): NumberedLine | undefined
155
164
  * 章と節は親の番地に続ける(第2編第1章 → pt2.ch1、その第3節 → pt2.ch1.3)。条は通し番号のまま。
156
165
  */
157
166
  const CHAPTER = new RegExp(`^${SPACE}*第(?<n>${NUMBER})(?<unit>[編章節])(?<rest>(?:${SPACE}|(|\\().*|)$`, "u");
167
+ const CHAPTER_TIGHT = new RegExp(`^${SPACE}*第(?<n>${NUMBER})(?<unit>[編章節])${TIGHT_TITLE}$`, "u");
158
168
  const CHAPTER_SHAPES: Readonly<Record<string, { readonly depth: number; readonly prefix: string }>> = {
159
169
  編: { depth: -2, prefix: "pt" },
160
170
  章: { depth: -1, prefix: "ch" },
161
171
  節: { depth: 0, prefix: "" },
162
172
  };
163
173
 
164
- const chapter = (line: string): NumberedLine | undefined => {
165
- const groups = CHAPTER.exec(line)?.groups;
174
+ const chapter = (line: string, context: NumberingContext): NumberedLine | undefined => {
175
+ const groups = CHAPTER.exec(line)?.groups ?? headingGroups(CHAPTER_TIGHT, line, context);
166
176
  const number = numberOf(groups?.["n"]);
167
177
  const shape = CHAPTER_SHAPES[groups?.["unit"] ?? ""];
168
178
  if (groups === undefined || number === undefined || shape === undefined) return undefined;
@@ -181,7 +191,7 @@ const chapter = (line: string): NumberedLine | undefined => {
181
191
  };
182
192
 
183
193
  const numbered = (line: string, context: NumberingContext): NumberedLine | undefined =>
184
- chapter(line) ?? articleRange(line) ?? article(line) ?? item(line, context);
194
+ chapter(line, context) ?? articleRange(line) ?? article(line, context) ?? item(line, context);
185
195
 
186
196
  /** 正規表現の一致を Mention にする。g フラグ付きのものだけを渡す。 */
187
197
  const mentions = (
@@ -208,10 +218,11 @@ const scopedLocally = (before: string): boolean => THIS_PART.test(before) || NEA
208
218
 
209
219
  const definitions = (text: string): Mention[] =>
210
220
  DEFINITIONS.flatMap((pattern) =>
211
- mentions(pattern, text, (groups, _whole, start) => {
221
+ mentions(pattern, text, (groups, whole, start) => {
212
222
  if (groups["term"] === undefined) return undefined;
213
223
  const local = scopedLocally(text.slice(Math.max(0, start - 30), start));
214
- return { term: groups["term"], ...(local ? { scope: "local" } : {}) };
224
+ const inline = whole.startsWith("以下");
225
+ return { term: groups["term"], ...(local ? { scope: "local" } : {}), ...(inline ? { placement: "inline" } : {}) };
215
226
  }),
216
227
  );
217
228
 
@@ -227,11 +238,20 @@ const REFERENCE = new RegExp(`第(?<a>${NUMBER})条(?:の(?<s>${NUMBER}))?(?:第
227
238
  */
228
239
  /**
229
240
  * 他の文書の参照に続く並び(「民事訴訟法第百条第一項、第百一条、第百二条の二」)は、同じ文書の条を指す。
230
- * 間が読点・接続の語・号や項の断片・括弧書きだけなら、前の参照の文書を引き継ぐ。
241
+ * 間が読点・接続の語・号や項の断片・括弧書きだけなら、前の参照の文書を引き継ぐ。接続の語は、規約や案内が仮名で書く形(および)も同じ。
231
242
  */
232
- const CONNECTORS = /及び|並びに|若しくは|又は|から|まで|ただし書|前段|後段|[、,\s]/gu;
243
+ const CONNECTORS = /及び|並びに|若しくは|又は|および|ならびに|もしくは|または|から|まで|ただし書|前段|後段|[、,\s]/gu;
233
244
  const FRAGMENT = new RegExp(`第${NUMBER}[項号](?:の${NUMBER})*`, "gu");
234
- const PARENTHESES = /([^()]*)/gu;
245
+ /** 括弧書きの括弧。全角でも半角でもよい(厚生労働省法令等データベースは半角で書く)が、開きと閉じは同じ幅で組む。 */
246
+ const OPENER_OF: ReadonlyMap<string, string> = new Map([
247
+ [")", "("],
248
+ [")", "("],
249
+ ]);
250
+ const isOpening = (char: string): boolean => char === "(" || char === "(";
251
+ const PARENTHESES = /([^()()]*)|\([^()()]*\)/gu;
252
+
253
+ /** 閉じの括弧が、開いたままの最後の括弧と組になるか。 */
254
+ const closes = (char: string, opened: string | undefined): boolean => opened !== undefined && OPENER_OF.get(char) === opened;
235
255
 
236
256
  /** 間の文字列が、接続の語・読点・項や号の断片・括弧書きだけでできているか。 */
237
257
  const isContinuation = (gap: string): boolean => gap.replace(PARENTHESES, "").replace(FRAGMENT, "").replace(CONNECTORS, "") === "";
@@ -239,11 +259,11 @@ const isContinuation = (gap: string): boolean => gap.replace(PARENTHESES, "").re
239
259
  /** 行を一度だけ読んで、位置ごとの括弧の深さを出す。参照ごとに行を読み直さない。 */
240
260
  const depthsOf = (text: string): Int32Array => {
241
261
  const depth = new Int32Array(text.length + 1);
242
- const open = { parentheses: 0 };
262
+ const opened: string[] = [];
243
263
  text.split("").forEach((char, at) => {
244
- depth[at] = open.parentheses;
245
- if (char === "(") open.parentheses += 1;
246
- if (char === ")") open.parentheses = Math.max(0, open.parentheses - 1);
264
+ depth[at] = opened.length;
265
+ if (isOpening(char)) opened.push(char);
266
+ else if (closes(char, opened.at(-1))) opened.pop();
247
267
  });
248
268
  return depth;
249
269
  };
@@ -254,10 +274,24 @@ const depthsOf = (text: string): Int32Array => {
254
274
  * 「民法第709条」のように他の文書の名前が前にあれば、その名前を document に入れる。この文書の木では引かない。
255
275
  */
256
276
  /** at より前で開いたまま閉じていない括弧の数。 */
257
- /** 間に挟まった括弧書き(中に参照があっても)を外す。入れ子は内側から外す。 */
277
+ /**
278
+ * 間に挟まった括弧書き(中に参照があっても)を外す。入れ子は内側から外れる。閉じない「(」と開かない「)」は残す。
279
+ * 内側を外すのを繰り返すと深さの数だけ読み直すので、組ごと 1 度で外す。
280
+ */
258
281
  const withoutClosedParentheses = (gap: string): string => {
259
- const once = gap.replace(PARENTHESES, "");
260
- return once === gap ? gap : withoutClosedParentheses(once);
282
+ const kept: string[] = [];
283
+ const opens: number[] = [];
284
+ for (const char of gap) {
285
+ const last = opens.at(-1);
286
+ if (last !== undefined && closes(char, kept[last])) {
287
+ opens.pop();
288
+ kept.splice(last);
289
+ } else {
290
+ if (isOpening(char)) opens.push(kept.length);
291
+ kept.push(char);
292
+ }
293
+ }
294
+ return kept.join("");
261
295
  };
262
296
 
263
297
  /**
@@ -283,9 +317,17 @@ const LEXICONS = loadLexicons();
283
317
  const CITATION = citationVocabulary(LEXICONS);
284
318
  const SECTIONS = sectionVocabulary(LEXICONS);
285
319
  const NOT_MAGNITUDE = (LEXICONS["not-magnitude"] ?? []).map((entry) => entry.pattern);
320
+ const MEASURE_UNITS = (LEXICONS["measure-unit"] ?? []).map((entry) => entry.pattern);
321
+
322
+ /** 記号のすぐ後ろにラテン文字・数字・ハイフンが続けば、単位ではなく語の頭(「2.1 mmap」)。 */
323
+ const CONTINUES_WORD = /^[\p{Script=Latin}\p{Nd}_-]/u;
324
+
325
+ /** 「1.5 mM の塩化マグネシウム」「37.5 ℃で」: 解析器が助数詞と読まない単位の記号。 */
326
+ const startsWithMeasureUnit = (rest: string): boolean => MEASURE_UNITS.some((unit) => rest.startsWith(unit) && !CONTINUES_WORD.test(rest.slice(unit.length)));
286
327
 
287
328
  /** 辞書が桁の語と助数詞に切る一語(「1.5 万葉の世界」)は数量の続きではないので、行頭の番号は節のまま。 */
288
- const countedAfterNumber = (number: string, rest: string): boolean => !NOT_MAGNITUDE.some((word) => rest.startsWith(word)) && countedAfter(number, rest);
329
+ const countedAfterNumber = (number: string, rest: string): boolean =>
330
+ startsWithMeasureUnit(rest) || (!NOT_MAGNITUDE.some((word) => rest.startsWith(word)) && countedAfter(number, rest));
289
331
 
290
332
  const sectionsOf = (text: string): Mention[] => sectionReferences(text, SECTIONS);
291
333
 
@@ -378,4 +420,5 @@ export const structure: StructurePatterns = {
378
420
  countedAfter: countedAfterNumber,
379
421
  continuesSentence: startsWithParticle,
380
422
  number,
423
+ namedDocument: (text, at) => namedDocument(text, at, CITATION),
381
424
  };
@@ -1,2 +1,5 @@
1
- /** 対になっていないサロゲートを U+FFFD に置き換える。長さを変えないので、位置はそのまま同じ文字を指す。 */
2
- export const wellFormed = (text: string): string => text.replace(/\p{Surrogate}/gu, "\uFFFD");
1
+ /**
2
+ * 解析器(kuromoji)が例外を投げる文字を U+FFFD に置き換える。対になっていないサロゲートと NUL(U+0000)。
3
+ * 長さを変えないので、位置はそのまま同じ文字を指す。BMP の全文字を一つずつ渡して、投げたのはこの二つだけだった。
4
+ */
5
+ export const wellFormed = (text: string): string => text.replace(/[\p{Surrogate}\0]/gu, "�");