chaffjs 0.39.0 → 0.40.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 (321) hide show
  1. package/README.ja.md +3 -2
  2. package/README.md +2 -1
  3. package/dist/ai-score/human-shares.d.ts.map +1 -1
  4. package/dist/ai-score/human-shares.js +1 -0
  5. package/dist/ai-score/human-shares.js.map +1 -1
  6. package/dist/alternative-words.d.ts +6 -0
  7. package/dist/alternative-words.d.ts.map +1 -1
  8. package/dist/alternative-words.js +10 -0
  9. package/dist/alternative-words.js.map +1 -1
  10. package/dist/closing-code.d.ts +8 -0
  11. package/dist/closing-code.d.ts.map +1 -0
  12. package/dist/closing-code.js +18 -0
  13. package/dist/closing-code.js.map +1 -0
  14. package/dist/date-format.d.ts +19 -1
  15. package/dist/date-format.d.ts.map +1 -1
  16. package/dist/date-format.js +41 -5
  17. package/dist/date-format.js.map +1 -1
  18. package/dist/detectors/acronym-context.d.ts +1 -1
  19. package/dist/detectors/acronym-context.d.ts.map +1 -1
  20. package/dist/detectors/acronym-context.js +3 -2
  21. package/dist/detectors/acronym-context.js.map +1 -1
  22. package/dist/detectors/acronym-mention.d.ts +5 -0
  23. package/dist/detectors/acronym-mention.d.ts.map +1 -1
  24. package/dist/detectors/acronym-mention.js +16 -0
  25. package/dist/detectors/acronym-mention.js.map +1 -1
  26. package/dist/detectors/acronym-name.d.ts +5 -0
  27. package/dist/detectors/acronym-name.d.ts.map +1 -1
  28. package/dist/detectors/acronym-name.js +15 -0
  29. package/dist/detectors/acronym-name.js.map +1 -1
  30. package/dist/detectors/blank-runs.d.ts +12 -0
  31. package/dist/detectors/blank-runs.d.ts.map +1 -0
  32. package/dist/detectors/blank-runs.js +27 -0
  33. package/dist/detectors/blank-runs.js.map +1 -0
  34. package/dist/detectors/bold-label.d.ts +3 -1
  35. package/dist/detectors/bold-label.d.ts.map +1 -1
  36. package/dist/detectors/bold-label.js +16 -6
  37. package/dist/detectors/bold-label.js.map +1 -1
  38. package/dist/detectors/capitalised-term.d.ts.map +1 -1
  39. package/dist/detectors/capitalised-term.js +28 -9
  40. package/dist/detectors/capitalised-term.js.map +1 -1
  41. package/dist/detectors/citation-string.d.ts +6 -0
  42. package/dist/detectors/citation-string.d.ts.map +1 -1
  43. package/dist/detectors/citation-string.js +52 -0
  44. package/dist/detectors/citation-string.js.map +1 -1
  45. package/dist/detectors/contraction-emphasis.d.ts +3 -0
  46. package/dist/detectors/contraction-emphasis.d.ts.map +1 -0
  47. package/dist/detectors/contraction-emphasis.js +24 -0
  48. package/dist/detectors/contraction-emphasis.js.map +1 -0
  49. package/dist/detectors/contraction-mention.d.ts +12 -1
  50. package/dist/detectors/contraction-mention.d.ts.map +1 -1
  51. package/dist/detectors/contraction-mention.js +39 -6
  52. package/dist/detectors/contraction-mention.js.map +1 -1
  53. package/dist/detectors/contraction-mix.d.ts +3 -0
  54. package/dist/detectors/contraction-mix.d.ts.map +1 -0
  55. package/dist/detectors/contraction-mix.js +69 -0
  56. package/dist/detectors/contraction-mix.js.map +1 -0
  57. package/dist/detectors/date-format.d.ts.map +1 -1
  58. package/dist/detectors/date-format.js +13 -1
  59. package/dist/detectors/date-format.js.map +1 -1
  60. package/dist/detectors/duplicate-definition.d.ts +3 -0
  61. package/dist/detectors/duplicate-definition.d.ts.map +1 -0
  62. package/dist/detectors/duplicate-definition.js +16 -0
  63. package/dist/detectors/duplicate-definition.js.map +1 -0
  64. package/dist/detectors/final-period-captions.d.ts +15 -0
  65. package/dist/detectors/final-period-captions.d.ts.map +1 -0
  66. package/dist/detectors/final-period-captions.js +61 -0
  67. package/dist/detectors/final-period-captions.js.map +1 -0
  68. package/dist/detectors/heading-sentences.d.ts +10 -0
  69. package/dist/detectors/heading-sentences.d.ts.map +1 -0
  70. package/dist/detectors/heading-sentences.js +32 -0
  71. package/dist/detectors/heading-sentences.js.map +1 -0
  72. package/dist/detectors/lexicon.d.ts +1 -2
  73. package/dist/detectors/lexicon.d.ts.map +1 -1
  74. package/dist/detectors/lexicon.js +5 -55
  75. package/dist/detectors/lexicon.js.map +1 -1
  76. package/dist/detectors/list-lead-in.d.ts +1 -1
  77. package/dist/detectors/list-lead-in.d.ts.map +1 -1
  78. package/dist/detectors/list-lead-in.js +31 -10
  79. package/dist/detectors/list-lead-in.js.map +1 -1
  80. package/dist/detectors/missing-final-period.d.ts.map +1 -1
  81. package/dist/detectors/missing-final-period.js +3 -1
  82. package/dist/detectors/missing-final-period.js.map +1 -1
  83. package/dist/detectors/numbering-readings.d.ts +2 -2
  84. package/dist/detectors/numbering-readings.d.ts.map +1 -1
  85. package/dist/detectors/numbering-readings.js +7 -0
  86. package/dist/detectors/numbering-readings.js.map +1 -1
  87. package/dist/detectors/open-end-anaphor.d.ts +8 -0
  88. package/dist/detectors/open-end-anaphor.d.ts.map +1 -0
  89. package/dist/detectors/open-end-anaphor.js +22 -0
  90. package/dist/detectors/open-end-anaphor.js.map +1 -0
  91. package/dist/detectors/open-end-lead.d.ts +4 -0
  92. package/dist/detectors/open-end-lead.d.ts.map +1 -0
  93. package/dist/detectors/open-end-lead.js +34 -0
  94. package/dist/detectors/open-end-lead.js.map +1 -0
  95. package/dist/detectors/open-end-scope.d.ts.map +1 -1
  96. package/dist/detectors/open-end-scope.js +7 -1
  97. package/dist/detectors/open-end-scope.js.map +1 -1
  98. package/dist/detectors/ordinal-run-gaps.d.ts +1 -0
  99. package/dist/detectors/ordinal-run-gaps.d.ts.map +1 -1
  100. package/dist/detectors/ordinal-run-gaps.js +1 -1
  101. package/dist/detectors/ordinal-run-gaps.js.map +1 -1
  102. package/dist/detectors/orthographic-variant-open.d.ts +4 -2
  103. package/dist/detectors/orthographic-variant-open.d.ts.map +1 -1
  104. package/dist/detectors/orthographic-variant-open.js +5 -3
  105. package/dist/detectors/orthographic-variant-open.js.map +1 -1
  106. package/dist/detectors/orthography.d.ts.map +1 -1
  107. package/dist/detectors/orthography.js +14 -5
  108. package/dist/detectors/orthography.js.map +1 -1
  109. package/dist/detectors/placeholder-text.d.ts.map +1 -1
  110. package/dist/detectors/placeholder-text.js +14 -2
  111. package/dist/detectors/placeholder-text.js.map +1 -1
  112. package/dist/detectors/quote-without-source.d.ts +6 -2
  113. package/dist/detectors/quote-without-source.d.ts.map +1 -1
  114. package/dist/detectors/quote-without-source.js +33 -8
  115. package/dist/detectors/quote-without-source.js.map +1 -1
  116. package/dist/detectors/register-separate-part.d.ts +7 -0
  117. package/dist/detectors/register-separate-part.d.ts.map +1 -0
  118. package/dist/detectors/register-separate-part.js +32 -0
  119. package/dist/detectors/register-separate-part.js.map +1 -0
  120. package/dist/detectors/registry/contraction-mix.js +1 -1
  121. package/dist/detectors/registry/contraction-mix.js.map +1 -1
  122. package/dist/detectors/registry/duplicate-definition.js +1 -1
  123. package/dist/detectors/registry/duplicate-definition.js.map +1 -1
  124. package/dist/detectors/reproduced-text.d.ts +5 -0
  125. package/dist/detectors/reproduced-text.d.ts.map +1 -0
  126. package/dist/detectors/reproduced-text.js +19 -0
  127. package/dist/detectors/reproduced-text.js.map +1 -0
  128. package/dist/detectors/request-owner.d.ts +2 -1
  129. package/dist/detectors/request-owner.d.ts.map +1 -1
  130. package/dist/detectors/request-owner.js +31 -7
  131. package/dist/detectors/request-owner.js.map +1 -1
  132. package/dist/detectors/requirement-smell.d.ts.map +1 -1
  133. package/dist/detectors/requirement-smell.js +16 -5
  134. package/dist/detectors/requirement-smell.js.map +1 -1
  135. package/dist/detectors/sentence-counts.d.ts +2 -1
  136. package/dist/detectors/sentence-counts.d.ts.map +1 -1
  137. package/dist/detectors/sentence-counts.js +23 -7
  138. package/dist/detectors/sentence-counts.js.map +1 -1
  139. package/dist/detectors/sentence-ending.d.ts +1 -0
  140. package/dist/detectors/sentence-ending.d.ts.map +1 -1
  141. package/dist/detectors/sentence-ending.js +5 -2
  142. package/dist/detectors/sentence-ending.js.map +1 -1
  143. package/dist/detectors/sentence-length.d.ts.map +1 -1
  144. package/dist/detectors/sentence-length.js +13 -3
  145. package/dist/detectors/sentence-length.js.map +1 -1
  146. package/dist/detectors/sentence-parts.d.ts +2 -0
  147. package/dist/detectors/sentence-parts.d.ts.map +1 -1
  148. package/dist/detectors/sentence-parts.js +3 -1
  149. package/dist/detectors/sentence-parts.js.map +1 -1
  150. package/dist/detectors/signals.d.ts.map +1 -1
  151. package/dist/detectors/signals.js +6 -4
  152. package/dist/detectors/signals.js.map +1 -1
  153. package/dist/detectors/spelling-variety.d.ts.map +1 -1
  154. package/dist/detectors/spelling-variety.js +20 -2
  155. package/dist/detectors/spelling-variety.js.map +1 -1
  156. package/dist/detectors/structure-tree.d.ts +2 -1
  157. package/dist/detectors/structure-tree.d.ts.map +1 -1
  158. package/dist/detectors/structure-tree.js +2 -7
  159. package/dist/detectors/structure-tree.js.map +1 -1
  160. package/dist/detectors/structure.d.ts.map +1 -1
  161. package/dist/detectors/structure.js +19 -5
  162. package/dist/detectors/structure.js.map +1 -1
  163. package/dist/detectors/token-shape.d.ts.map +1 -1
  164. package/dist/detectors/token-shape.js +12 -6
  165. package/dist/detectors/token-shape.js.map +1 -1
  166. package/dist/detectors/undefined-term.d.ts +5 -0
  167. package/dist/detectors/undefined-term.d.ts.map +1 -1
  168. package/dist/detectors/undefined-term.js +22 -1
  169. package/dist/detectors/undefined-term.js.map +1 -1
  170. package/dist/detectors/unfilled-placeholder.d.ts.map +1 -1
  171. package/dist/detectors/unfilled-placeholder.js +15 -1
  172. package/dist/detectors/unfilled-placeholder.js.map +1 -1
  173. package/dist/detectors/unknown-word-context.d.ts +7 -0
  174. package/dist/detectors/unknown-word-context.d.ts.map +1 -1
  175. package/dist/detectors/unknown-word-context.js +11 -0
  176. package/dist/detectors/unknown-word-context.js.map +1 -1
  177. package/dist/detectors/unknown-word.d.ts.map +1 -1
  178. package/dist/detectors/unknown-word.js +5 -4
  179. package/dist/detectors/unknown-word.js.map +1 -1
  180. package/dist/latin-spacing-source.d.ts +12 -0
  181. package/dist/latin-spacing-source.d.ts.map +1 -0
  182. package/dist/latin-spacing-source.js +25 -0
  183. package/dist/latin-spacing-source.js.map +1 -0
  184. package/dist/name-separator.d.ts.map +1 -1
  185. package/dist/name-separator.js +4 -1
  186. package/dist/name-separator.js.map +1 -1
  187. package/dist/paragraph-length.d.ts +14 -0
  188. package/dist/paragraph-length.d.ts.map +1 -1
  189. package/dist/paragraph-length.js +25 -0
  190. package/dist/paragraph-length.js.map +1 -1
  191. package/dist/paragraph-runs.d.ts +2 -2
  192. package/dist/paragraph-runs.d.ts.map +1 -1
  193. package/dist/paragraph-runs.js +65 -14
  194. package/dist/paragraph-runs.js.map +1 -1
  195. package/dist/quote-echo.d.ts +6 -0
  196. package/dist/quote-echo.d.ts.map +1 -0
  197. package/dist/quote-echo.js +14 -0
  198. package/dist/quote-echo.js.map +1 -0
  199. package/dist/semantic.d.ts.map +1 -1
  200. package/dist/semantic.js +2 -2
  201. package/dist/semantic.js.map +1 -1
  202. package/dist/set-phrase-particles.d.ts +4 -0
  203. package/dist/set-phrase-particles.d.ts.map +1 -0
  204. package/dist/set-phrase-particles.js +26 -0
  205. package/dist/set-phrase-particles.js.map +1 -0
  206. package/dist/structure/alternative-clause.d.ts +7 -0
  207. package/dist/structure/alternative-clause.d.ts.map +1 -1
  208. package/dist/structure/alternative-clause.js +23 -8
  209. package/dist/structure/alternative-clause.js.map +1 -1
  210. package/dist/structure/cited-document-section.d.ts +2 -0
  211. package/dist/structure/cited-document-section.d.ts.map +1 -1
  212. package/dist/structure/cited-document-section.js +8 -0
  213. package/dist/structure/cited-document-section.js.map +1 -1
  214. package/dist/structure/issues.d.ts +3 -1
  215. package/dist/structure/issues.d.ts.map +1 -1
  216. package/dist/structure/issues.js +12 -8
  217. package/dist/structure/issues.js.map +1 -1
  218. package/dist/structure/numbering.d.ts +10 -1
  219. package/dist/structure/numbering.d.ts.map +1 -1
  220. package/dist/structure/numbering.js +41 -4
  221. package/dist/structure/numbering.js.map +1 -1
  222. package/dist/structure/unread-label.d.ts +13 -0
  223. package/dist/structure/unread-label.d.ts.map +1 -0
  224. package/dist/structure/unread-label.js +70 -0
  225. package/dist/structure/unread-label.js.map +1 -0
  226. package/dist/topic-clause.d.ts.map +1 -1
  227. package/dist/topic-clause.js +15 -2
  228. package/dist/topic-clause.js.map +1 -1
  229. package/package.json +3 -3
  230. package/rules/ai-generated-composite.yaml +1 -0
  231. package/rules/ai-tell.yaml +6 -2
  232. package/rules/bold-label-list.yaml +6 -6
  233. package/rules/closing-cliche.yaml +2 -2
  234. package/rules/colon-lead-in.yaml +2 -2
  235. package/rules/contraction-consistency.yaml +2 -2
  236. package/rules/date-format-consistency.yaml +2 -2
  237. package/rules/duplicate-definition.yaml +2 -2
  238. package/rules/honesty-marker.yaml +84 -0
  239. package/rules/latin-spacing.yaml +6 -4
  240. package/rules/max-paragraph-length.yaml +2 -2
  241. package/rules/max-sentence-length.yaml +4 -2
  242. package/rules/missing-final-period.yaml +2 -2
  243. package/rules/no-doubled-joshi.yaml +2 -2
  244. package/rules/no-mixed-desumasu.yaml +2 -2
  245. package/rules/nominalization.yaml +1 -1
  246. package/rules/numbering-gap.yaml +2 -2
  247. package/rules/one-sentence-paragraph-run.yaml +2 -2
  248. package/rules/orthographic-variant.yaml +2 -2
  249. package/rules/quote-without-source.yaml +2 -2
  250. package/rules/request-without-deadline.yaml +2 -2
  251. package/rules/requirement-smell.yaml +2 -2
  252. package/rules/spelling-consistency.yaml +4 -4
  253. package/rules/teki-overuse.yaml +2 -2
  254. package/rules/topic-predicate-distance.yaml +2 -2
  255. package/rules/undefined-acronym.yaml +3 -2
  256. package/rules/undefined-term.yaml +2 -2
  257. package/rules/unfilled-placeholder.yaml +2 -2
  258. package/rules/unknown-word.yaml +2 -2
  259. package/skills/chaff/SKILL.md +2 -1
  260. package/src/ai-score/human-shares.ts +1 -0
  261. package/src/alternative-words.ts +15 -0
  262. package/src/closing-code.ts +21 -0
  263. package/src/date-format.ts +54 -7
  264. package/src/detectors/acronym-context.ts +3 -2
  265. package/src/detectors/acronym-mention.ts +17 -0
  266. package/src/detectors/acronym-name.ts +18 -0
  267. package/src/detectors/blank-runs.ts +30 -0
  268. package/src/detectors/bold-label.ts +22 -6
  269. package/src/detectors/capitalised-term.ts +36 -9
  270. package/src/detectors/citation-string.ts +57 -0
  271. package/src/detectors/contraction-emphasis.ts +46 -0
  272. package/src/detectors/contraction-mention.ts +52 -6
  273. package/src/detectors/contraction-mix.ts +94 -0
  274. package/src/detectors/date-format.ts +18 -2
  275. package/src/detectors/duplicate-definition.ts +21 -0
  276. package/src/detectors/final-period-captions.ts +78 -0
  277. package/src/detectors/heading-sentences.ts +36 -0
  278. package/src/detectors/lexicon.ts +5 -61
  279. package/src/detectors/list-lead-in.ts +44 -10
  280. package/src/detectors/missing-final-period.ts +9 -1
  281. package/src/detectors/numbering-readings.ts +10 -2
  282. package/src/detectors/open-end-anaphor.ts +31 -0
  283. package/src/detectors/open-end-lead.ts +46 -0
  284. package/src/detectors/open-end-scope.ts +8 -1
  285. package/src/detectors/ordinal-run-gaps.ts +1 -1
  286. package/src/detectors/orthographic-variant-open.ts +8 -3
  287. package/src/detectors/orthography.ts +20 -7
  288. package/src/detectors/placeholder-text.ts +21 -4
  289. package/src/detectors/quote-without-source.ts +52 -10
  290. package/src/detectors/register-separate-part.ts +42 -0
  291. package/src/detectors/registry/contraction-mix.ts +1 -1
  292. package/src/detectors/registry/duplicate-definition.ts +1 -1
  293. package/src/detectors/reproduced-text.ts +33 -0
  294. package/src/detectors/request-owner.ts +31 -8
  295. package/src/detectors/requirement-smell.ts +21 -5
  296. package/src/detectors/sentence-counts.ts +27 -7
  297. package/src/detectors/sentence-ending.ts +6 -2
  298. package/src/detectors/sentence-length.ts +18 -3
  299. package/src/detectors/sentence-parts.ts +4 -1
  300. package/src/detectors/signals.ts +7 -4
  301. package/src/detectors/spelling-variety.ts +26 -2
  302. package/src/detectors/structure-tree.ts +2 -8
  303. package/src/detectors/structure.ts +22 -6
  304. package/src/detectors/token-shape.ts +16 -6
  305. package/src/detectors/undefined-term.ts +26 -1
  306. package/src/detectors/unfilled-placeholder.ts +20 -2
  307. package/src/detectors/unknown-word-context.ts +17 -0
  308. package/src/detectors/unknown-word.ts +15 -4
  309. package/src/latin-spacing-source.ts +33 -0
  310. package/src/name-separator.ts +4 -1
  311. package/src/paragraph-length.ts +31 -0
  312. package/src/paragraph-runs.ts +71 -13
  313. package/src/quote-echo.ts +18 -0
  314. package/src/semantic.ts +2 -3
  315. package/src/set-phrase-particles.ts +32 -0
  316. package/src/structure/alternative-clause.ts +32 -7
  317. package/src/structure/cited-document-section.ts +8 -0
  318. package/src/structure/issues.ts +19 -9
  319. package/src/structure/numbering.ts +50 -3
  320. package/src/structure/unread-label.ts +74 -0
  321. package/src/topic-clause.ts +16 -2
@@ -0,0 +1,32 @@
1
+ import type { Token } from "./plugin.ts";
2
+
3
+ // A particle inside a set phrase (絶滅のおそれのある種: おそれのある, "at risk of"): the phrase is read as one word, so its particle
4
+ // is not a link of a nesting chain. Pure.
5
+
6
+ const touches = (left: Token | undefined, right: Token | undefined): boolean => left !== undefined && right !== undefined && left.span.end === right.span.start;
7
+
8
+ /** How many words before a particle are read for a phrase's head: the tokenizer may cut it (おそれ as お and それ). */
9
+ const MAX_HEAD_WORDS = 3;
10
+
11
+ /** The words written straight before tokens[at], joined (おそれ from お and それ). */
12
+ const headBefore = (tokens: readonly Token[], at: number): string => {
13
+ const window = tokens.slice(Math.max(0, at - MAX_HEAD_WORDS), at + 1);
14
+ const gap = window.findLastIndex((token, index) => index < window.length - 1 && !touches(token, window[index + 1]));
15
+ return window
16
+ .slice(gap + 1, -1)
17
+ .map((token) => token.surface)
18
+ .join("");
19
+ };
20
+
21
+ /** Whether the particle at `at`, the words before it and the dictionary form of the word after it spell one of phrases (おそれのある). */
22
+ const isInPhrase = (tokens: readonly Token[], at: number, phrases: readonly string[]): boolean => {
23
+ const [particle, after] = [tokens[at], tokens[at + 1]];
24
+ if (particle === undefined || after === undefined || !touches(particle, after)) return false;
25
+ const tail = `${particle.surface}${after.lemma ?? after.surface}`;
26
+ const head = headBefore(tokens, at);
27
+ return phrases.some((phrase) => phrase.endsWith(tail) && phrase.length > tail.length && head.endsWith(phrase.slice(0, -tail.length)));
28
+ };
29
+
30
+ /** The counted particles (particles) of a sentence that stand inside one of phrases, each written whole (おそれのある). */
31
+ export const setPhraseParticles = (tokens: readonly Token[], particles: readonly string[], phrases: readonly string[]): ReadonlySet<Token> =>
32
+ new Set(tokens.filter((token, at) => particles.includes(token.surface) && isInPhrase(tokens, at, phrases)));
@@ -23,21 +23,46 @@ const isLabelLine = (line: string): boolean => {
23
23
  return text.startsWith("#") || ("【〔[[((".includes(text.charAt(0)) && "】〕]]))".includes(text.charAt(text.length - 1)));
24
24
  };
25
25
 
26
+ /** A division whose heading, or the label line just before it, the test names. */
27
+ const divisionNamedBy =
28
+ (source: string, names: (text: string) => boolean): ((node: StructureNode) => boolean) =>
29
+ (node) => {
30
+ const heading = node.attrs["heading"];
31
+ if (typeof heading === "string" && names(heading)) return true;
32
+ const label = previousLineOf(source, node.span.start);
33
+ return isLabelLine(label) && names(label);
34
+ };
35
+
26
36
  /**
27
37
  * A division its heading, or the label line just before it (【18条変更オプション条項:知財調停】), names as an alternative
28
38
  * (alternative-clause-label).
29
39
  */
30
40
  export const alternativeReader = (source: string, labels: readonly string[]): ((node: StructureNode) => boolean) => {
31
41
  const words = labels.filter((label) => label !== "");
32
- const names = (text: string): boolean => words.some((word) => text.includes(word));
33
- return (node) => {
34
- const heading = node.attrs["heading"];
35
- if (typeof heading === "string" && names(heading)) return true;
36
- const label = previousLineOf(source, node.span.start);
37
- return isLabelLine(label) && names(label);
38
- };
42
+ return divisionNamedBy(source, (text) => words.some((word) => text.includes(word)));
39
43
  };
40
44
 
45
+ /** One number or letter after the word (オプション2, Option B), not the first of a range or a list (オプション1~3, Options 1 and 2). */
46
+ const ONE_CHOICE = /^\s?(?:[0-90-9]+|[A-ZA-Z])(?![0-90-9A-Za-zA-Z])(?!\s*(?:[~〜~\-–—‐・、,,&&]|to\b|and\b|or\b))/u;
47
+
48
+ /** The text names one choice among numbered ones: an option word (option-choice-label) followed by its number or letter. */
49
+ export const namesOneChoice = (text: string, words: readonly string[]): boolean =>
50
+ words
51
+ .filter((word) => word !== "")
52
+ .some((word) =>
53
+ text
54
+ .split(word)
55
+ .slice(1)
56
+ .some((after) => ONE_CHOICE.test(after)),
57
+ );
58
+
59
+ /**
60
+ * A division its heading or label line names as one of numbered choices (第1条(オプション2:…), "Option B"): only one of
61
+ * them stays in the signed text (option-choice-label).
62
+ */
63
+ export const choiceReader = (source: string, words: readonly string[]): ((node: StructureNode) => boolean) =>
64
+ divisionNamedBy(source, (text) => namesOneChoice(text, words));
65
+
41
66
  /** Where a line starts with a division whose number is left blank (placeholder-division: 第●条): a new run of numbers. */
42
67
  export const placeholderStarts = (source: string, patterns: readonly string[]): number[] => {
43
68
  const heads = patterns.filter((pattern) => pattern !== "");
@@ -55,3 +55,11 @@ export const referencesInCitedDocuments = (tree: StructureNode): ReadonlySet<Str
55
55
  const quoted = visits.filter((visit) => visit.node.kind === "reference" && visit.inArticle && visit.heading !== undefined && cited.has(visit.heading));
56
56
  return new Set(quoted.map((visit) => visit.node));
57
57
  };
58
+
59
+ /** 見出しが他の文書の名前そのもので、その名前を文書がほかで他の文書として引いている節に引き写した条。Pure. */
60
+ export const articlesInCitedDocuments = (tree: StructureNode): readonly StructureNode[] => {
61
+ const visits = visitsOf(tree);
62
+ const cited = documentsCited(visits.map((visit) => visit.node));
63
+ if (cited.size === 0) return [];
64
+ return visits.filter((visit) => visit.node.kind === "article" && visit.heading !== undefined && cited.has(visit.heading)).map((visit) => visit.node);
65
+ };
@@ -209,18 +209,20 @@ export const referenceResolver = (tree: StructureNode, source: string): ((refere
209
209
  };
210
210
  };
211
211
 
212
- type Definition = { readonly node: StructureNode; readonly article: string };
212
+ /** choice: the division, one of several to choose from, that holds the definition (undefined when the text keeps it whatever is chosen). */
213
+ type Definition = { readonly node: StructureNode; readonly article: string; readonly choice: StructureNode | undefined };
213
214
 
214
- /** 定義を文書の順に、それが置かれた条の番地と一緒に並べる。範囲を限った定義は、その条の中でだけ比べるため。 */
215
- const definitionsInOrder = (tree: StructureNode): Definition[] => {
215
+ /** 定義を文書の順に、それが置かれた条の番地と、選ぶ条項の中ならその条項と一緒に並べる。範囲を限った定義は、その条の中でだけ比べるため。 */
216
+ const definitionsInOrder = (tree: StructureNode, isChoice: (node: StructureNode) => boolean): Definition[] => {
216
217
  const found: Definition[] = [];
217
- const pending: Definition[] = [{ node: tree, article: "" }];
218
+ const pending: Definition[] = [{ node: tree, article: "", choice: undefined }];
218
219
  while (pending.length > 0) {
219
220
  const current = pending.pop();
220
221
  if (current === undefined) break;
221
222
  if (current.node.kind === "definition") found.push(current);
222
223
  const article = current.node.kind === "article" ? current.node.address : current.article;
223
- current.node.children.toReversed().forEach((child) => pending.push({ node: child, article }));
224
+ const choice = current.choice ?? (current.node.kind !== "definition" && isChoice(current.node) ? current.node : undefined);
225
+ current.node.children.toReversed().forEach((child) => pending.push({ node: child, article, choice }));
224
226
  }
225
227
  return found;
226
228
  };
@@ -231,17 +233,25 @@ const definitionsInOrder = (tree: StructureNode): Definition[] => {
231
233
  * 添付した文書(partStarts から始まる Exhibit A など)は別の文書なので、その中の定義はその中でだけ比べる。
232
234
  * 同じ名前の後ろの括弧で同じ語を定義し直したもの(件名と本文の Financial Crimes Enforcement Network ("FinCEN"))は、
233
235
  * 一つの定義を書き直しただけで食い違えないので言わない。名前は source から読む。
236
+ * ひな形の選ぶ条項(オプション1、オプション2)の中の定義は、選ばなければ消える。先の定義が消えうるなら、後の定義は二度目と決まらない。
237
+ * そこで選ぶ条項の中の定義は、どれを選んでも残る定義と、同じ条項の中の定義とだけ比べる。
234
238
  */
235
- export const duplicateDefinitions = (tree: StructureNode, partStarts: readonly number[] = [], source = ""): StructureIssue[] => {
239
+ export const duplicateDefinitions = (
240
+ tree: StructureNode,
241
+ partStarts: readonly number[] = [],
242
+ source = "",
243
+ isChoice: (node: StructureNode) => boolean = () => false,
244
+ ): StructureIssue[] => {
236
245
  const first = new Map<string, StructureNode>();
237
- return definitionsInOrder(tree).flatMap(({ node, article }) => {
246
+ return definitionsInOrder(tree, isChoice).flatMap(({ node, article, choice }) => {
238
247
  const term = textOf(node, "term");
239
248
  const within = node.attrs["within"] === undefined ? article : textOf(node, "within");
240
249
  const scoped = node.attrs["scope"] === "local" ? `${within}\u0000${term}` : term;
241
250
  const key = `${partAt(partStarts, node.span.start)}\u0000${scoped}`;
242
- const earlier = first.get(key);
251
+ const choiceKey = choice === undefined ? undefined : `${choice.span.start}\u0000${key}`;
252
+ const earlier = first.get(key) ?? (choiceKey === undefined ? undefined : first.get(choiceKey));
243
253
  if (earlier === undefined) {
244
- first.set(key, node);
254
+ first.set(choiceKey ?? key, node);
245
255
  return [];
246
256
  }
247
257
  const name = nameBeforeDefinition(source, node);
@@ -3,6 +3,7 @@ import { inDocumentOrder, type StructureIssue } from "./issues.ts";
3
3
  import { numbersQuotedInside } from "./quoted-number.ts";
4
4
  import { isVersionHistory } from "./version-order.ts";
5
5
  import { withRepeatsNamed } from "./repeated-number.ts";
6
+ import { labelWrittenBetween } from "./unread-label.ts";
6
7
 
7
8
  const labelOf = (node: StructureNode): string => String(node.attrs["label"] ?? node.address);
8
9
 
@@ -17,6 +18,14 @@ export type ClauseReadings = { readonly isAlternative: (node: StructureNode) =>
17
18
 
18
19
  const NO_CLAUSES: ClauseReadings = { isAlternative: () => false, restarts: [] };
19
20
 
21
+ /**
22
+ * What a skip is checked against besides the tree. source: the text, for labels set out that the tree did not read
23
+ * (unread-label.ts). isPartialHeading: a heading saying what is under it is only some (抜粋, excerpt), where numbers skip by choice.
24
+ */
25
+ export type SkipReadings = { readonly source: string; readonly isPartialHeading: (heading: string) => boolean };
26
+
27
+ const NO_SKIPS: SkipReadings = { source: "", isPartialHeading: () => false };
28
+
20
29
  const OPENING_BRACKET = /^[([{〔【〈《]/u;
21
30
 
22
31
  /** 括弧で囲んだ番号((1)、(a))か。全角と半角は同じに見る。 */
@@ -74,7 +83,7 @@ const unreadBetween = (unread: readonly Span[], previous: Ordered, current: Orde
74
83
  * あいだに読めなかった範囲(unread)があれば、抜けた番号はそこにあるかもしれないので言わない。重なりは言う。
75
84
  */
76
85
  const breaksIn =
77
- (unread: readonly Span[], restarts: readonly Span[]) =>
86
+ (unread: readonly Span[], restarts: readonly Span[], isSkipAllowed: (previous: Ordered, current: Ordered) => boolean) =>
78
87
  (sequence: readonly Ordered[]): StructureIssue[] =>
79
88
  sequence.flatMap((current, index) => {
80
89
  const previous = sequence[index - 1];
@@ -84,6 +93,7 @@ const breaksIn =
84
93
  if (unreadBetween(restarts, previous, current)) return [];
85
94
  if (current.ordinal > previous.last + 1 && unreadBetween(unread, previous, current)) return [];
86
95
  if (current.ordinal > previous.last + 1 && current.ordinal <= previous.last + 1 + current.sideSteps) return [];
96
+ if (current.ordinal > previous.last + 1 && isSkipAllowed(previous, current)) return [];
87
97
  return [
88
98
  {
89
99
  offset: current.node.span.start,
@@ -92,6 +102,32 @@ const breaksIn =
92
102
  ];
93
103
  });
94
104
 
105
+ /** 抜粋と書いた見出し(## 根拠条文の抜粋)の下の節点。そこの番号は選んで引いたので、飛んでよい。 */
106
+ const underPartialHeadings = (tree: StructureNode, isPartialHeading: (heading: string) => boolean): Set<StructureNode> => {
107
+ const under = new Set<StructureNode>();
108
+ const pending: [StructureNode, boolean][] = [[tree, false]];
109
+ while (pending.length > 0) {
110
+ const [node, inherited] = pending.pop() ?? [tree, false];
111
+ const heading = node.attrs["heading"];
112
+ const partial = inherited || (node.kind === "section" && typeof heading === "string" && isPartialHeading(heading));
113
+ if (partial) under.add(node);
114
+ node.children.forEach((child) => pending.push([child, partial]));
115
+ }
116
+ return under;
117
+ };
118
+
119
+ /** 並びの始まりの番号。番号は 1 から振る(法令の項は 2 から)。 */
120
+ const LAST_START = 2;
121
+
122
+ /**
123
+ * 番号が一つしかなく、それが並びの始まりでない並び(用語集の見出し Section 508 Program、Section 508 Program Manager)は
124
+ * 名前で、番号の並びではない。法令の「2」「2」と続く項(第 1 項は番号が無い)は重なり。
125
+ */
126
+ const isOneNumber = (sequence: readonly Ordered[]): boolean => {
127
+ const [first] = sequence;
128
+ return first !== undefined && sequence.length > 1 && first.ordinal > LAST_START && sequence.every((ordered) => ordered.ordinal === first.ordinal);
129
+ };
130
+
95
131
  /**
96
132
  * 番号の抜けと重なり。比べるのは同じ親の子どうしだけ。英米の法令は章ごとに Section 101、201 と百の位を変えるので、
97
133
  * 文書全体の通し番号として比べると抜けに見える。最初の番号は見ない。日本語の項は 2 から番号を振る。
@@ -102,18 +138,29 @@ const breaksIn =
102
138
  * unread は、読み手には見えるが木に入らない範囲(Markdown の HTML ブロックの中の文字)を、始まりの順に並べたもの。
103
139
  * isSideStep は、番号の代わりに「and」と書いた手順(前の手順と並べて行う)。その数だけ、次の番号は飛んでよい。
104
140
  * clauses は、別案の条(並びに入れない)と、番号を空けた条の位置(そこから振り直す)。
141
+ * skips は、抜けを言わない場合: 抜けた番号を木が読まずに本文に書いてある((e) の前の行末の (d))か、抜粋と書いた見出しの下。
105
142
  */
106
143
  export const numberingBreaks = (
107
144
  tree: StructureNode,
108
145
  unread: readonly Span[] = [],
109
146
  isSideStep: (node: StructureNode) => boolean = () => false,
110
147
  clauses: ClauseReadings = NO_CLAUSES,
148
+ skips: SkipReadings = NO_SKIPS,
111
149
  ): StructureIssue[] => {
112
150
  const quoting = numbersQuotedInside(tree);
113
151
  const restarts = clauses.restarts.map((start) => ({ start, end: start }));
152
+ const partial = underPartialHeadings(tree, skips.isPartialHeading);
153
+ const spanOf = (ordered: Ordered): { label: string; ordinal: number; start: number; last: number } => ({
154
+ label: labelOf(ordered.node),
155
+ ordinal: ordered.ordinal,
156
+ start: ordered.node.span.start,
157
+ last: ordered.last,
158
+ });
159
+ const isSkipAllowed = (previous: Ordered, current: Ordered): boolean =>
160
+ partial.has(current.node) || labelWrittenBetween(skips.source, spanOf(previous), spanOf(current));
114
161
  const breaks = inDocumentOrder(tree)
115
162
  .flatMap((parent) => sequencesOf(parent, quoting, isSideStep, clauses))
116
- .filter((sequence) => !isVersionHistory(sequence.map((ordered) => labelOf(ordered.node))))
117
- .flatMap(breaksIn(unread, restarts));
163
+ .filter((sequence) => !isVersionHistory(sequence.map((ordered) => labelOf(ordered.node))) && !isOneNumber(sequence))
164
+ .flatMap(breaksIn(unread, restarts, isSkipAllowed));
118
165
  return withRepeatsNamed(tree, quoting, breaks, unread);
119
166
  };
@@ -0,0 +1,74 @@
1
+ // A number the tree did not read but the text writes: "(d)" run into the end of the line before "(e)", "6.1" and "6.2"
2
+ // written as plain paragraphs before the "6.3" the tree read, "(11)" joined to the heading of "(10)". A skip across such
3
+ // a label is not a missing number. Pure.
4
+
5
+ const LETTERS = "abcdefghijklmnopqrstuvwxyz";
6
+
7
+ /** At most this many missing numbers are looked for; a wider skip is not a few labels the tree missed. */
8
+ const MAX_MISSING = 30;
9
+
10
+ /** A label in brackets ((11)): in the heading before, a bare number may be any number in its words. */
11
+ const BRACKETED = /^[([]/u;
12
+ /** An HTML comment: text the reader does not see, so a label in it is not written. */
13
+ const HTML_COMMENT = /<!--[\s\S]*?-->/gu;
14
+
15
+ const escapeRegExp = (text: string): string => text.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
16
+
17
+ /** The label written with another number in place of its own: digits for digits, a letter in the same case for a letter. */
18
+ const relabel = (label: string, ordinal: number): ((other: number) => string) | undefined => {
19
+ const digits = [...label.matchAll(/\d+/gu)].findLast((match) => Number(match[0]) === ordinal);
20
+ if (digits !== undefined) return (other) => `${label.slice(0, digits.index)}${other}${label.slice(digits.index + digits[0].length)}`;
21
+ const letter = [...label.matchAll(/(?<![A-Za-z])[A-Za-z](?![A-Za-z])/gu)].findLast((match) => LETTERS.indexOf(match[0].toLowerCase()) + 1 === ordinal);
22
+ if (letter === undefined) return undefined;
23
+ const upper = letter[0] !== letter[0].toLowerCase();
24
+ return (other) => {
25
+ const written = LETTERS.charAt(other - 1);
26
+ return `${label.slice(0, letter.index)}${upper ? written.toUpperCase() : written}${label.slice(letter.index + 1)}`;
27
+ };
28
+ };
29
+
30
+ /**
31
+ * A label set out as one: at the start of a line behind the same marks as the later label (## for a heading, none for a
32
+ * paragraph), after a run of spaces within a line, or after a space in the heading that opens the node before
33
+ * ("##### (10) … (11) …", a label in brackets). A label in a sentence or in an HTML comment ("under clause (d)") is a reference, and a list item's "3." under
34
+ * headings numbered "## 4." is another numbering.
35
+ */
36
+ const setOutIn = (text: string, label: string, lead: string): boolean => {
37
+ const escaped = escapeRegExp(label);
38
+ const after = String.raw`(?![0-9A-Za-z])`;
39
+ const atLineStart = new RegExp(String.raw`^[ \t]*${escapeRegExp(lead)}[ \t]*${escaped}${after}`, "mu");
40
+ const afterSpaces = new RegExp(String.raw`\S[ \t]{2,}${escaped}${after}`, "u");
41
+ if (atLineStart.test(text) || afterSpaces.test(text)) return true;
42
+ const firstLine = text.split("\n", 1)[0] ?? "";
43
+ const bracketed = BRACKETED.test(label);
44
+ return bracketed && firstLine.trimStart().startsWith("#") && new RegExp(String.raw`\s${escaped}${after}`, "u").test(firstLine);
45
+ };
46
+
47
+ /** The marks before a label on its own line (## of a heading, > of a quotation), spaces left out. */
48
+ const leadOf = (source: string, start: number, label: string): string => {
49
+ const lineStart = source.lastIndexOf("\n", start - 1) + 1;
50
+ const lineEnd = source.indexOf("\n", start);
51
+ const line = source.slice(lineStart, lineEnd === -1 ? source.length : lineEnd).normalize("NFKC");
52
+ const at = line.indexOf(label);
53
+ return at === -1 ? "" : line.slice(0, at).trim();
54
+ };
55
+
56
+ export type LabelSpan = { readonly label: string; readonly ordinal: number; readonly start: number };
57
+
58
+ /**
59
+ * Whether the text between two numbered nodes sets out a label of the later one's kind with a number the skip leaves out
60
+ * (between previous's last number and current's), which the tree did not read as a node.
61
+ */
62
+ export const labelWrittenBetween = (source: string, previous: LabelSpan & { readonly last: number }, current: LabelSpan): boolean => {
63
+ const missing = current.ordinal - previous.last - 1;
64
+ if (missing < 1 || missing > MAX_MISSING || current.start <= previous.start) return false;
65
+ const label = current.label.normalize("NFKC");
66
+ const written = relabel(label, current.ordinal);
67
+ if (written === undefined) return false;
68
+ const lead = leadOf(source, current.start, label);
69
+ const text = source
70
+ .slice(source.lastIndexOf("\n", previous.start - 1) + 1, current.start)
71
+ .replaceAll(HTML_COMMENT, "")
72
+ .normalize("NFKC");
73
+ return Array.from({ length: missing }, (_, index) => written(previous.last + 1 + index)).some((missingLabel) => setOutIn(text, missingLabel, lead));
74
+ };
@@ -162,14 +162,28 @@ const isPoliteGa = (tokens: readonly Token[], at: number): boolean => {
162
162
  const ownGaSubjectBetween = (tokens: readonly Token[], from: number, to: number): boolean =>
163
163
  tokens.some((token, at) => at > from && at < to && isParticle(token, "が") && headsSubject(tokens, at));
164
164
 
165
+ /** A word of a name (赤澤, 大臣), not a noun that sets the time or the case (今後, 当日). */
166
+ const isNameWord = (token: Token): boolean => NOMINAL.has(token.pos) && token.features?.["Adverbial"] !== "Yes";
167
+
168
+ /**
169
+ * 石破総理大臣は…明らかにしましたが、赤澤大臣は…どう考えているか: a name and は straight after the comma is a topic of its own
170
+ * that the rest of the sentence is said of, when no subject of its own (が) follows it. A phrase before は (〜した行為は) is not one.
171
+ */
172
+ const newNameTopicRunsToEnd = (tokens: readonly Token[], comma: number, last: number): boolean => {
173
+ const topic = tokens.findIndex((token, index) => index > comma && !isNameWord(token));
174
+ const wa = tokens[topic];
175
+ return topic > comma + 1 && wa !== undefined && isParticle(wa, "は") && lastOwnSubject(tokens, last) <= topic;
176
+ };
177
+
165
178
  /**
166
179
  * 当社は…を営んでおりますが、…の重要性が乏しいため、…の記載は行っておりません: after 〜ますが、 the words up to the next comma
167
180
  * say something of a subject of their own (が) and a new topic takes what comes after, so the predicate before が was the
168
- * topic's, when no other subject stands between the topic and it (需要の停滞がありましたが is not). A topic straight after the
169
- * comma (〜ましたが、Bは…) or no subject of its own there is read to the end.
181
+ * topic's, when no other subject stands between the topic and it (需要の停滞がありましたが is not). A clause with no subject of
182
+ * its own there is read to the end, unless a name stands as a new topic straight after the comma (〜ましたが、B大臣は…).
170
183
  */
171
184
  const closesWithPoliteGa = (tokens: readonly Token[], topic: number, at: number, last: number): boolean => {
172
185
  if (!isPoliteGa(tokens, at) || !isComma(tokens[at + 1]) || subjectBetween(tokens, topic, at)) return false;
186
+ if (newNameTopicRunsToEnd(tokens, at + 1, last)) return true;
173
187
  const comma = tokens.findIndex((token, index) => index > at + 1 && index < last && isComma(token));
174
188
  const clauseEnd = comma === -1 ? last : comma;
175
189
  return ownGaSubjectBetween(tokens, at + 1, clauseEnd) && newTopicAfter(tokens, clauseEnd, last);