chaffjs 0.44.0 → 0.45.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 (173) hide show
  1. package/dist/detectors/abstract-length.d.ts +5 -1
  2. package/dist/detectors/abstract-length.d.ts.map +1 -1
  3. package/dist/detectors/abstract-length.js +13 -5
  4. package/dist/detectors/abstract-length.js.map +1 -1
  5. package/dist/detectors/adversative-ga.d.ts +1 -1
  6. package/dist/detectors/adversative-ga.d.ts.map +1 -1
  7. package/dist/detectors/adversative-ga.js +19 -2
  8. package/dist/detectors/adversative-ga.js.map +1 -1
  9. package/dist/detectors/bold-label.d.ts.map +1 -1
  10. package/dist/detectors/bold-label.js +56 -15
  11. package/dist/detectors/bold-label.js.map +1 -1
  12. package/dist/detectors/char-shape.d.ts +0 -6
  13. package/dist/detectors/char-shape.d.ts.map +1 -1
  14. package/dist/detectors/char-shape.js +4 -21
  15. package/dist/detectors/char-shape.js.map +1 -1
  16. package/dist/detectors/clause-shape.d.ts +2 -0
  17. package/dist/detectors/clause-shape.d.ts.map +1 -1
  18. package/dist/detectors/clause-shape.js +11 -2
  19. package/dist/detectors/clause-shape.js.map +1 -1
  20. package/dist/detectors/cleft-focus.d.ts +4 -0
  21. package/dist/detectors/cleft-focus.d.ts.map +1 -0
  22. package/dist/detectors/cleft-focus.js +31 -0
  23. package/dist/detectors/cleft-focus.js.map +1 -0
  24. package/dist/detectors/definition-use.d.ts.map +1 -1
  25. package/dist/detectors/definition-use.js +13 -3
  26. package/dist/detectors/definition-use.js.map +1 -1
  27. package/dist/detectors/empty-section.d.ts +1 -1
  28. package/dist/detectors/empty-section.d.ts.map +1 -1
  29. package/dist/detectors/empty-section.js +3 -2
  30. package/dist/detectors/empty-section.js.map +1 -1
  31. package/dist/detectors/fullwidth-alnum.d.ts +2 -2
  32. package/dist/detectors/fullwidth-alnum.d.ts.map +1 -1
  33. package/dist/detectors/fullwidth-alnum.js +19 -3
  34. package/dist/detectors/fullwidth-alnum.js.map +1 -1
  35. package/dist/detectors/heading-echo.d.ts.map +1 -1
  36. package/dist/detectors/heading-echo.js +20 -7
  37. package/dist/detectors/heading-echo.js.map +1 -1
  38. package/dist/detectors/heading-number.d.ts +7 -0
  39. package/dist/detectors/heading-number.d.ts.map +1 -0
  40. package/dist/detectors/heading-number.js +21 -0
  41. package/dist/detectors/heading-number.js.map +1 -0
  42. package/dist/detectors/lookalike-character.d.ts +2 -1
  43. package/dist/detectors/lookalike-character.d.ts.map +1 -1
  44. package/dist/detectors/lookalike-character.js +14 -1
  45. package/dist/detectors/lookalike-character.js.map +1 -1
  46. package/dist/detectors/mark-shape.d.ts +6 -1
  47. package/dist/detectors/mark-shape.d.ts.map +1 -1
  48. package/dist/detectors/mark-shape.js +31 -11
  49. package/dist/detectors/mark-shape.js.map +1 -1
  50. package/dist/detectors/middle-dot.d.ts +7 -0
  51. package/dist/detectors/middle-dot.d.ts.map +1 -1
  52. package/dist/detectors/middle-dot.js +19 -0
  53. package/dist/detectors/middle-dot.js.map +1 -1
  54. package/dist/detectors/named-by-context.d.ts +13 -0
  55. package/dist/detectors/named-by-context.d.ts.map +1 -0
  56. package/dist/detectors/named-by-context.js +10 -0
  57. package/dist/detectors/named-by-context.js.map +1 -0
  58. package/dist/detectors/placeholder-context.d.ts +16 -0
  59. package/dist/detectors/placeholder-context.d.ts.map +1 -0
  60. package/dist/detectors/placeholder-context.js +57 -0
  61. package/dist/detectors/placeholder-context.js.map +1 -0
  62. package/dist/detectors/placeholder-text.d.ts +2 -0
  63. package/dist/detectors/placeholder-text.d.ts.map +1 -1
  64. package/dist/detectors/placeholder-text.js +1 -1
  65. package/dist/detectors/placeholder-text.js.map +1 -1
  66. package/dist/detectors/registry/middle-dot.js +1 -1
  67. package/dist/detectors/sentence-counts.d.ts +2 -0
  68. package/dist/detectors/sentence-counts.d.ts.map +1 -1
  69. package/dist/detectors/sentence-counts.js +13 -4
  70. package/dist/detectors/sentence-counts.js.map +1 -1
  71. package/dist/detectors/signals.d.ts.map +1 -1
  72. package/dist/detectors/signals.js +10 -1
  73. package/dist/detectors/signals.js.map +1 -1
  74. package/dist/detectors/structure.d.ts.map +1 -1
  75. package/dist/detectors/structure.js +3 -2
  76. package/dist/detectors/structure.js.map +1 -1
  77. package/dist/detectors/template-sections.d.ts +1 -0
  78. package/dist/detectors/template-sections.d.ts.map +1 -1
  79. package/dist/detectors/template-sections.js +1 -1
  80. package/dist/detectors/template-sections.js.map +1 -1
  81. package/dist/detectors/unfilled-placeholder.d.ts.map +1 -1
  82. package/dist/detectors/unfilled-placeholder.js +18 -1
  83. package/dist/detectors/unfilled-placeholder.js.map +1 -1
  84. package/dist/detectors/verse-block.d.ts +4 -0
  85. package/dist/detectors/verse-block.d.ts.map +1 -0
  86. package/dist/detectors/verse-block.js +40 -0
  87. package/dist/detectors/verse-block.js.map +1 -0
  88. package/dist/document.d.ts.map +1 -1
  89. package/dist/document.js +1 -0
  90. package/dist/document.js.map +1 -1
  91. package/dist/email-parts.d.ts +2 -0
  92. package/dist/email-parts.d.ts.map +1 -1
  93. package/dist/email-parts.js +10 -7
  94. package/dist/email-parts.js.map +1 -1
  95. package/dist/eval.d.ts.map +1 -1
  96. package/dist/eval.js +3 -0
  97. package/dist/eval.js.map +1 -1
  98. package/dist/plugin.d.ts +2 -0
  99. package/dist/plugin.d.ts.map +1 -1
  100. package/dist/reasons.d.ts +2 -0
  101. package/dist/reasons.d.ts.map +1 -1
  102. package/dist/reasons.js +2 -0
  103. package/dist/reasons.js.map +1 -1
  104. package/dist/rule-of-three.d.ts +7 -2
  105. package/dist/rule-of-three.d.ts.map +1 -1
  106. package/dist/rule-of-three.js +11 -3
  107. package/dist/rule-of-three.js.map +1 -1
  108. package/dist/run.d.ts.map +1 -1
  109. package/dist/run.js +4 -1
  110. package/dist/run.js.map +1 -1
  111. package/dist/structure/cited-elsewhere.d.ts +5 -2
  112. package/dist/structure/cited-elsewhere.d.ts.map +1 -1
  113. package/dist/structure/cited-elsewhere.js +16 -6
  114. package/dist/structure/cited-elsewhere.js.map +1 -1
  115. package/dist/structure/definition-use.d.ts +2 -1
  116. package/dist/structure/definition-use.d.ts.map +1 -1
  117. package/dist/structure/definition-use.js +10 -2
  118. package/dist/structure/definition-use.js.map +1 -1
  119. package/dist/structure/issues.js +1 -1
  120. package/dist/structure/issues.js.map +1 -1
  121. package/package.json +3 -3
  122. package/rules/abstract-length.yaml +2 -2
  123. package/rules/adversative-ga-repeat.yaml +2 -2
  124. package/rules/attachment-not-attached.yaml +11 -6
  125. package/rules/bold-label-list.yaml +22 -6
  126. package/rules/bracket-width-consistency.yaml +2 -2
  127. package/rules/demonstrative-opener-run.yaml +2 -2
  128. package/rules/emoji-density.yaml +2 -2
  129. package/rules/empty-section.yaml +2 -2
  130. package/rules/fullwidth-alnum-consistency.yaml +2 -2
  131. package/rules/lookalike-character.yaml +8 -5
  132. package/rules/max-kanji-continuous.yaml +4 -0
  133. package/rules/paragraph-length-variance.yaml +27 -3
  134. package/rules/repeated-sentence-ending.yaml +2 -2
  135. package/rules/rule-of-three.yaml +2 -2
  136. package/rules/sentence-spacing-consistency.yaml +2 -2
  137. package/rules/teki-overuse.yaml +2 -0
  138. package/rules/unfilled-placeholder.yaml +2 -2
  139. package/rules/use-before-definition.yaml +2 -2
  140. package/src/detectors/abstract-length.ts +16 -6
  141. package/src/detectors/adversative-ga.ts +24 -2
  142. package/src/detectors/bold-label.ts +63 -16
  143. package/src/detectors/char-shape.ts +8 -22
  144. package/src/detectors/clause-shape.ts +14 -3
  145. package/src/detectors/cleft-focus.ts +40 -0
  146. package/src/detectors/definition-use.ts +23 -4
  147. package/src/detectors/empty-section.ts +3 -2
  148. package/src/detectors/fullwidth-alnum.ts +24 -3
  149. package/src/detectors/heading-echo.ts +21 -8
  150. package/src/detectors/heading-number.ts +20 -0
  151. package/src/detectors/lookalike-character.ts +19 -2
  152. package/src/detectors/mark-shape.ts +33 -6
  153. package/src/detectors/middle-dot.ts +22 -0
  154. package/src/detectors/named-by-context.ts +15 -0
  155. package/src/detectors/placeholder-context.ts +72 -0
  156. package/src/detectors/placeholder-text.ts +1 -1
  157. package/src/detectors/registry/middle-dot.ts +1 -1
  158. package/src/detectors/sentence-counts.ts +16 -4
  159. package/src/detectors/signals.ts +11 -1
  160. package/src/detectors/structure.ts +3 -2
  161. package/src/detectors/template-sections.ts +1 -1
  162. package/src/detectors/unfilled-placeholder.ts +22 -1
  163. package/src/detectors/verse-block.ts +48 -0
  164. package/src/document.ts +1 -0
  165. package/src/email-parts.ts +16 -7
  166. package/src/eval.ts +2 -0
  167. package/src/plugin.ts +2 -0
  168. package/src/reasons.ts +4 -0
  169. package/src/rule-of-three.ts +22 -5
  170. package/src/run.ts +3 -1
  171. package/src/structure/cited-elsewhere.ts +18 -6
  172. package/src/structure/definition-use.ts +10 -1
  173. package/src/structure/issues.ts +1 -1
@@ -1,4 +1,4 @@
1
- import type { Detector, DetectorOptions, Finding, ProseDocument, Span } from "../plugin.ts";
1
+ import type { Detector, DetectorOptions, Finding, Paragraph, ProseDocument, Span, Token } from "../plugin.ts";
2
2
 
3
3
  /** A bold run longer than this is a bold sentence, not a label. */
4
4
  const MAX_LABEL_CHARS = 40;
@@ -64,9 +64,41 @@ export const boldLabelItems = (doc: ProseDocument): Span[] => unquotedItems(doc,
64
64
  /** Bold lead sentences that report on their own (the rule's min_leads). */
65
65
  const DEFAULT_MIN_LEADS = 3;
66
66
 
67
- const minLeads = (options: DetectorOptions): number => {
68
- const value = options.settings?.["min_leads"];
69
- return typeof value === "number" ? value : DEFAULT_MIN_LEADS;
67
+ /** Paragraphs opened by a bold statement that report on their own (the rule's min_clause_paragraphs). */
68
+ const DEFAULT_MIN_CLAUSE_PARAGRAPHS = 3;
69
+
70
+ const countSetting = (options: DetectorOptions, name: string, fallback: number): number => {
71
+ const value = options.settings?.[name];
72
+ return typeof value === "number" ? value : fallback;
73
+ };
74
+
75
+ const minLeads = (options: DetectorOptions): number => countSetting(options, "min_leads", DEFAULT_MIN_LEADS);
76
+
77
+ const VERBAL = new Set(["VERB", "AUX"]);
78
+
79
+ /** Words that state something: a verb after a first word that is not one. An imperative (Talk to …), a term (Work schedule) or a gerund heading is not. */
80
+ const isStatement = (tokens: readonly Token[]): boolean => {
81
+ const words = tokens.filter((token) => token.pos !== "PUNCT" && token.pos !== "SYM");
82
+ const head = words[0];
83
+ return head !== undefined && !VERBAL.has(head.pos) && words.slice(1).some((token) => VERBAL.has(token.pos));
84
+ };
85
+
86
+ /** The bold run a paragraph opens with, as a label or lead sentence, and where in the paragraph its words end. */
87
+ const openingRunOf = (text: string): { words: string; end: number } | undefined => {
88
+ const match = BOLD_LABEL.exec(text) ?? BOLD_LEAD.exec(text);
89
+ const words = match?.[2];
90
+ if (match?.[1] === undefined || words === undefined || isCodeOnly(words.trim())) return undefined;
91
+ return { words: words.trim(), end: match[1].length + words.length };
92
+ };
93
+
94
+ /** The statement a paragraph opens with in bold (**Usage grew each week**: …), or undefined. A question (an FAQ's) is not a statement. */
95
+ const boldStatementOf = (doc: ProseDocument, paragraph: Paragraph): string | undefined => {
96
+ const { start, end } = paragraph.span;
97
+ const run = isQuotedAt(doc.source, start) ? undefined : openingRunOf(doc.source.slice(start, end));
98
+ if (run === undefined || /[??]$/u.test(run.words)) return undefined;
99
+ const runEnd = start + run.end;
100
+ const tokens = paragraph.sentences.flatMap((sentence) => sentence.tokens ?? []).filter((token) => token.span.start >= start && token.span.start < runEnd);
101
+ return isStatement(tokens) ? run.words.replace(/[.!。!]$/u, "") : undefined;
70
102
  };
71
103
 
72
104
  /** The count reported and the limit it reached: all the labelled items, or else the bold lead sentences alone, or undefined. */
@@ -76,6 +108,30 @@ const reached = (items: readonly Span[], leads: number, options: DetectorOptions
76
108
  return leads >= least ? { count: leads, limit: least } : undefined;
77
109
  };
78
110
 
111
+ const finding = (doc: ProseDocument, span: Span, label: string, found: { count: number; limit: number }, variant?: string): Finding => ({
112
+ rule: "bold-label-list",
113
+ severity: "info",
114
+ line: 0,
115
+ column: 0,
116
+ quote: (doc.source.slice(span.start, span.end).split("\n")[0] ?? "").trim(),
117
+ values: { label, count: found.count, limit: found.limit, offset: span.start },
118
+ ...(variant === undefined ? {} : { variant }),
119
+ });
120
+
121
+ /**
122
+ * Paragraphs that each open with a bold statement (**Usage grew each week**: …): findings set as captions. Human run-in heads
123
+ * are terms, imperatives or questions (**Work schedule**:, **Talk to the people:**, **Who is my audience?**), and are not counted.
124
+ */
125
+ const statementParagraphs = (doc: ProseDocument, options: DetectorOptions): Finding[] => {
126
+ const statements = doc.paragraphs.flatMap((paragraph) => {
127
+ const label = boldStatementOf(doc, paragraph);
128
+ return label === undefined ? [] : [{ span: paragraph.span, label }];
129
+ });
130
+ const first = statements[0];
131
+ const limit = countSetting(options, "min_clause_paragraphs", DEFAULT_MIN_CLAUSE_PARAGRAPHS);
132
+ return first === undefined || statements.length < limit ? [] : [finding(doc, first.span, first.label, { count: statements.length, limit }, "paragraphs")];
133
+ };
134
+
79
135
  /**
80
136
  * A count, not a density: a person writes one or two labels even in a long article. A bold lead sentence counts as a label.
81
137
  * Bold lead sentences (- **Check one result by hand.** …) report at fewer items than colon labels: human documents write a few
@@ -86,16 +142,7 @@ export const boldLabelList: Detector = (doc, options): Finding[] => {
86
142
  const first = items[0];
87
143
  const leads = items.filter((span) => boldLabelOf(doc.source.slice(span.start, span.end)) === undefined).length;
88
144
  const found = reached(items, leads, options);
89
- if (first === undefined || found === undefined) return [];
90
- const written = doc.source.slice(first.start, first.end);
91
- return [
92
- {
93
- rule: "bold-label-list",
94
- severity: "info",
95
- line: 0,
96
- column: 0,
97
- quote: (written.split("\n")[0] ?? "").trim(),
98
- values: { label: labelOrLeadOf(written) ?? "", count: found.count, limit: found.limit, offset: first.start },
99
- },
100
- ];
145
+ const paragraphs = statementParagraphs(doc, options);
146
+ if (first === undefined || found === undefined) return paragraphs;
147
+ return [finding(doc, first, labelOrLeadOf(doc.source.slice(first.start, first.end)) ?? "", found)];
101
148
  };
@@ -21,13 +21,13 @@ import { isOneName } from "./name-run.ts";
21
21
  import { isOrganisationHead, isOrganisationRun, type OrganisationWords } from "./organisation-run.ts";
22
22
  import { endsInTitle, inStatuteTitle, isTitlePosition, isTitledName, repeatedRuns, titleBody, type TitleWords } from "./titled-run.ts";
23
23
  import { proseText } from "../measure.ts";
24
- import { parallelDotCount } from "./middle-dot.ts";
25
24
  import { maskSpans } from "../mask.ts";
26
25
  import { quotedSpans } from "../quoted-span.ts";
27
26
  import { nameSpans } from "../team-names.ts";
28
27
  import { isIn } from "../lexicon-genres.ts";
29
28
  import { holdsKnown, isInnerSuffix, isTitledPerson, standaloneNames, unknownPieces } from "./kanji-run-pieces.ts";
30
29
  import { isBibliographyEntry } from "./bibliography-entry.ts";
30
+ import { isNamedByContext } from "./named-by-context.ts";
31
31
 
32
32
  /**
33
33
  * 文字の並びを見る検出。漢字の連なりが住所か名前かだけは、形態素解析の固有名詞と数で決める。
@@ -142,6 +142,10 @@ type NameWords = {
142
142
  readonly listSuffixes: readonly string[];
143
143
  /** 連なりを閉じる、読み手が一つの語と知っている長い語(内閣総理大臣、許可事業者)。その前で連なりを割る。 */
144
144
  readonly knownWords: ReadonlySet<string>;
145
+ /** 制度が区分に入れることを言う句(として認定)。連なりのすぐ後ろに続けば、連なりは区分の名前。 */
146
+ readonly designations: readonly string[];
147
+ /** お知らせやメールの題を閉じる語(お知らせ、ご案内)。 */
148
+ readonly noticeEndings: readonly string[];
145
149
  };
146
150
 
147
151
  type Tail = AddresseeSuffix;
@@ -253,7 +257,7 @@ const measuredOf = (tokens: readonly Token[], run: KanjiRun, span: Span, words:
253
257
  * 住所か名前かは、それを外した残りで決める(1日日本銀行の「日本銀行」)。品詞が無ければ判定しない。
254
258
  */
255
259
  const measuredRun = (tokens: readonly Token[] | undefined, run: KanjiRun, context: RunContext): string => {
256
- if (run.quoted || (context.headwords && run.alone)) return "";
260
+ if (run.quoted || (context.headwords && run.alone) || isNamedByContext(run, context.words)) return "";
257
261
  if (tokens === undefined || run.span === undefined) return run.text;
258
262
  const { rest, text, keptHead } = measuredOf(tokens, run, run.span, context.words);
259
263
  if (context.presented.has(text) || inStatuteTitle(tokens, run.brackets, context.words.fixedName)) return "";
@@ -361,6 +365,8 @@ const nameWordsOf = (doc: ProseDocument): NameWords => {
361
365
  references: patterns("reference-prefix"),
362
366
  listSuffixes: patterns("list-suffix"),
363
367
  knownWords: new Set(patterns("kanji-known-word")),
368
+ designations: patterns("designation-phrase"),
369
+ noticeEndings: patterns("notice-title-ending"),
364
370
  };
365
371
  };
366
372
 
@@ -405,23 +411,3 @@ export const kanjiRun: Detector = (doc, options): Finding[] => {
405
411
  values: { word: run, count: run.length, limit: options.limit, offset: sentence.span.start },
406
412
  }));
407
413
  };
408
-
409
- /**
410
- * 中黒で並べると、どこまでが 1 つの項目か分からなくなる。
411
- * 「企画・開発・運用・保守体制」は、保守体制が 1 つなのか保守と体制なのか読めない。
412
- * 語彙表(nakaguro-word)の、中黒を含めて 1 語の語(キャッシュ・フロー)の中黒は並べていないので数えない。
413
- */
414
- export const middleDot: Detector = (doc, options): Finding[] => {
415
- const words = (options.lexicon ?? []).map((entry) => entry.pattern).toSorted((left, right) => right.length - left.length);
416
- return doc.sentences
417
- .map((sentence) => ({ sentence, count: parallelDotCount(sentence.text, words) }))
418
- .filter(({ count }) => count > options.limit)
419
- .map(({ sentence, count }) => ({
420
- rule: "no-nakaguro-parallel",
421
- severity: "info",
422
- line: 0,
423
- column: 0,
424
- quote: sentence.text.trim(),
425
- values: { count, limit: options.limit, offset: sentence.span.start },
426
- }));
427
- };
@@ -52,18 +52,29 @@ export const endingOf = (sentence: Sentence): string | undefined => {
52
52
  return [headToken !== undefined && VERBAL_POS.has(headToken.pos) ? headToken.surface : "", ...tail].join("");
53
53
  };
54
54
 
55
- type Ended = { readonly sentence: Sentence; readonly ending: string | undefined };
55
+ type Ended = { readonly sentence: Sentence; readonly ending: string | undefined; readonly opensItem: boolean };
56
+
57
+ /** A label at the head of a sentence: a bracketed name ([通信関連], 【部品】) or a name with leader dots (化学品事業……). */
58
+ const ITEM_LABEL = /^\s*(?:[[【[][^]】\]\n]{1,30}[]】\]]|[^\s。、]{1,20}[…‥]+)/u;
59
+
60
+ /** The sentence opens an item of a list written as running text: a new entry, not the next sentence of the one before. */
61
+ const opensItem = (sentence: Sentence): boolean => ITEM_LABEL.test(sentence.text);
56
62
 
57
63
  /**
58
64
  * Runs of consecutive sentences with the same ending. A sentence with no ending to compare closes a run, and so does a
59
65
  * question run together with the next one inside a single sentence (これはリストか?順序付きか?などです。): it is not the writer's ending.
66
+ * A sentence opening a labelled item ([通信関連]…) starts a new run: entries of a list end alike by design.
60
67
  */
61
68
  export const endingRuns = (sentences: readonly Sentence[]): Ended[][] =>
62
69
  sentences
63
- .map((sentence, index) => ({ sentence, ending: continuesThrough(sentences, index) ? undefined : endingOf(sentence) }))
70
+ .map((sentence, index) => ({
71
+ sentence,
72
+ ending: continuesThrough(sentences, index) ? undefined : endingOf(sentence),
73
+ opensItem: opensItem(sentence),
74
+ }))
64
75
  .reduce<Ended[][]>((runs, ended) => {
65
76
  const last = runs.at(-1) ?? [];
66
- const continues = ended.ending !== undefined && last[0]?.ending === ended.ending;
77
+ const continues = ended.ending !== undefined && !ended.opensItem && last[0]?.ending === ended.ending;
67
78
  return continues ? [...runs.slice(0, -1), [...last, ended]] : [...runs, [ended]];
68
79
  }, [])
69
80
  .filter((run) => run[0]?.ending !== undefined);
@@ -0,0 +1,40 @@
1
+ // The focus of a cleft sentence: what it states, after the clause that only presents it (いちばん大きく変わったのは、
2
+ // 季節に気づくようになったことだ。 states 季節に気づくようになった). Pure; the topic markers and closing words come
3
+ // from the language package's cleft-frame lexicon, so a language without one has no clefts.
4
+ import type { LexiconEntry, Span } from "../plugin.ts";
5
+
6
+ const SENTENCE_END = new Set("。..!?!? \t\n\u3000");
7
+ const FOCUS_START = new Set("、,, \t\n\u3000");
8
+
9
+ const withoutTrailing = (text: string, marks: ReadonlySet<string>): string => {
10
+ const chars = [...text];
11
+ const kept = chars.findLastIndex((char) => !marks.has(char));
12
+ return chars.slice(0, kept + 1).join("");
13
+ };
14
+
15
+ const leadingCount = (text: string, marks: ReadonlySet<string>): number => {
16
+ const at = [...text].findIndex((char) => !marks.has(char));
17
+ return at === -1 ? text.length : at;
18
+ };
19
+
20
+ const patternsOf = (entries: readonly LexiconEntry[], group: string): string[] =>
21
+ entries.filter((entry) => entry.group === group).map((entry) => entry.pattern);
22
+
23
+ const firstTopicEnd = (body: string, topics: readonly string[]): number | undefined => {
24
+ const ends = topics.flatMap((topic) => {
25
+ const at = body.indexOf(topic);
26
+ return at > 0 ? [at + topic.length] : [];
27
+ });
28
+ return ends.length === 0 ? undefined : Math.min(...ends);
29
+ };
30
+
31
+ /** The focus's offsets in `text`, when the text opens with a topic clause and ends with a closing word; otherwise undefined. */
32
+ export const cleftFocus = (text: string, entries: readonly LexiconEntry[]): Span | undefined => {
33
+ const body = withoutTrailing(text, SENTENCE_END);
34
+ const close = patternsOf(entries, "close").find((pattern) => body.endsWith(pattern));
35
+ const topicEnd = firstTopicEnd(body, patternsOf(entries, "topic"));
36
+ if (close === undefined || topicEnd === undefined) return undefined;
37
+ const start = topicEnd + leadingCount(body.slice(topicEnd), FOCUS_START);
38
+ const end = body.length - close.length;
39
+ return start < end ? { start, end } : undefined;
40
+ };
@@ -1,5 +1,13 @@
1
- import type { Detector, Finding, ProseDocument } from "../plugin.ts";
2
- import { definedTerms, unusedDefinitions, usesBeforeDefinition, type BodyText, type IsTitle, type Mentioned } from "../structure/definition-use.ts";
1
+ import type { Detector, Finding, ProseDocument, StructureNode } from "../plugin.ts";
2
+ import {
3
+ definedTerms,
4
+ unusedDefinitions,
5
+ usesBeforeDefinition,
6
+ type BodyText,
7
+ type DefinedTerm,
8
+ type IsTitle,
9
+ type Mentioned,
10
+ } from "../structure/definition-use.ts";
3
11
  import { titleLineNumbers } from "../structure/title-lines.ts";
4
12
  import { contentsLineNumbers } from "../structure/contents-lines.ts";
5
13
  import { summarisesLaterDefinition, summarySections } from "../structure/summary-lines.ts";
@@ -7,6 +15,7 @@ import { expansionConflicts, expansionsIn } from "../acronym-expansions.ts";
7
15
  import { linesOf, lineNumberAt } from "../structure/lines.ts";
8
16
  import { isQuotedAlone } from "../quoted-span.ts";
9
17
  import { quoteAt } from "./structure-tree.ts";
18
+ import { articlesInCitedDocuments } from "../structure/cited-document-section.ts";
10
19
 
11
20
  const bodyOf = (doc: ProseDocument): BodyText[] => doc.sentences.map((sentence) => ({ start: sentence.span.start, text: sentence.text }));
12
21
 
@@ -50,19 +59,29 @@ const titleIn = (doc: ProseDocument): IsTitle => {
50
59
  const lines = linesOf(doc.source);
51
60
  const headings = doc.markup?.headings ?? [];
52
61
  const numbers = new Set([...titleLineNumbers(doc.structure, lines, headings), ...contentsLineNumbers(lines, headings, patternsOf(doc, "toc-heading"))]);
53
- const summaries = summarySections(lines, headings, [...patternsOf(doc, "summary-heading"), ...patternsOf(doc, "headnote-heading")]);
62
+ const summaries = summarySections(lines, headings, [
63
+ ...patternsOf(doc, "summary-heading"),
64
+ ...patternsOf(doc, "headnote-heading"),
65
+ ...patternsOf(doc, "introduction-heading"),
66
+ ]);
54
67
  return (offset, definedAt) => {
55
68
  const line = lineNumberAt(lines, offset) ?? 0;
56
69
  return numbers.has(line) || summarisesLaterDefinition(summaries, line, lineNumberAt(lines, definedAt) ?? 0);
57
70
  };
58
71
  };
59
72
 
73
+ /** 他の文書を引き写した節の条(「## 参考:根拠条文」の下の第9条)の定義は、その文書の定義で、この文書の語の定義ではない。 */
74
+ const ownDefinitions = (tree: StructureNode): DefinedTerm[] => {
75
+ const cited = articlesInCitedDocuments(tree).map((article) => article.span);
76
+ return definedTerms(tree).filter((term) => !cited.some((span) => span.start <= term.span.start && term.span.start < span.end));
77
+ };
78
+
60
79
  /** 文の途中で括弧に入れて定義した語を、定義より前で使っている所。 */
61
80
  export const useBeforeDefinition: Detector = (doc): Finding[] =>
62
81
  doc.structure === undefined
63
82
  ? []
64
83
  : usesBeforeDefinition(
65
- definedTerms(doc.structure),
84
+ ownDefinitions(doc.structure),
66
85
  bodyOf(doc),
67
86
  mentionedIn(doc),
68
87
  titleIn(doc),
@@ -1,5 +1,6 @@
1
1
  import type { Detector, Finding, MarkupHeading } from "../plugin.ts";
2
2
  import { findingAt, writtenHeadings } from "./markup-finding.ts";
3
+ import { opensSubsection } from "./heading-number.ts";
3
4
 
4
5
  /** HTML のコメント(「<!-- 後で書く -->」)。読み手には見えない。一つずつ外す(間の本文ごと一つのコメントと読まない)。 */
5
6
  const HTML_COMMENT = /<!--[\s\S]*?-->/gu;
@@ -8,12 +9,12 @@ const showsNothing = (written: string): boolean => written.replace(HTML_COMMENT,
8
9
 
9
10
  /**
10
11
  * 中身の無い節。見出しのすぐ後ろに、同じ深さか浅い見出しが来るもの(remark-lint の no-empty-sections)。
11
- * 深い見出しが続くのは、節を小さな節に分けただけなので中身がある。文書の最後の見出しは、後ろに何も無ければ空。
12
+ * 深い見出しが続くのは、節を小さな節に分けただけなので中身がある。同じ深さでも、番号が節の下の番号(3. の後の 3.1、4. の後の (1))なら同じ。文書の最後の見出しは、後ろに何も無ければ空。
12
13
  */
13
14
  export const emptySections = (source: string, headings: readonly MarkupHeading[]): MarkupHeading[] =>
14
15
  headings.filter((heading, index) => {
15
16
  const next = headings[index + 1];
16
- if (next !== undefined && next.depth > heading.depth) return false;
17
+ if (next !== undefined && (next.depth > heading.depth || (next.depth === heading.depth && opensSubsection(heading.text, next.text)))) return false;
17
18
  return showsNothing(source.slice(heading.end, next?.start ?? source.length));
18
19
  });
19
20
 
@@ -89,15 +89,36 @@ export type AlnumRun = {
89
89
  readonly glued: boolean;
90
90
  };
91
91
 
92
+ type Piece = { readonly 0: string; readonly index: number };
93
+
94
+ /** 一つの幅で書いた部分。 */
95
+ const SAME_WIDTH = /[A-Za-z0-9]+|[A-Za-z0-9]+/gu;
96
+ const ONE_LETTER = /^[A-Za-zA-Za-z]$/u;
97
+ const DIGITS = /^[0-90-9]+$/u;
98
+
99
+ /** 英字一字と数字の、幅の違う組(Q5、H30、600w)か。 */
100
+ const isLetterAndNumber = (parts: readonly Piece[]): boolean =>
101
+ parts.length === 2 && parts.some((part) => ONE_LETTER.test(part[0])) && parts.some((part) => DIGITS.test(part[0]));
102
+
103
+ /**
104
+ * 並びを、一つの幅で書いた部分に分ける。英字一字と数字を違う幅で書いた並び(Q5、H30)は、英字と数字に分けてそれぞれの組で数える。
105
+ * 「一字の英字は全角、2桁以上は半角」の H30 は、H も 30 もその組の中でそろう。ほかの混ざった並び(IoT)は数えない。
106
+ */
107
+ const widthPieces = (match: RegExpExecArray): Piece[] => {
108
+ if (FULLWIDTH.test(match[0]) || HALFWIDTH.test(match[0])) return [match];
109
+ const parts = [...match[0].matchAll(SAME_WIDTH)].map((part) => ({ 0: part[0], index: match.index + part.index }));
110
+ return isLetterAndNumber(parts) ? parts : [];
111
+ };
112
+
92
113
  /**
93
- * 文の中の英字・数字の並び。全角と半角が混ざった並び、鉤括弧で引いたものの中、項目と注の番号、箇条書きの頭の番号、版の番号の続き(TLS1.2 の 2)は数えない。
94
- * offset は文書の中の位置。
114
+ * 文の中の英字・数字の並び。鉤括弧で引いたものの中、項目と注の番号、箇条書きの頭の番号、版の番号の続き(TLS1.2 の 2)は数えない。
115
+ * 英字一字と数字を違う幅で書いた並びは分ける。offset は文書の中の位置。
95
116
  */
96
117
  export const alnumRunsIn = (sentence: Sentence, source: string): AlnumRun[] => {
97
118
  const quoted: readonly Span[] = quotedSpans(sentence.text, QUOTATION_MARKS);
98
119
  const glued = gluedNumberSpans(sentence.text);
99
120
  return [...sentence.text.matchAll(ALNUM_RUN)]
100
- .filter((match) => FULLWIDTH.test(match[0]) || HALFWIDTH.test(match[0]))
121
+ .flatMap(widthPieces)
101
122
  .filter((match) => !isWithinAny(quoted, { start: match.index, end: match.index + match[0].length }))
102
123
  .filter((match) => !isLabel(sentence.text, match.index, match.index + match[0].length))
103
124
  .filter((match) => !isListItemNumber(source, sentence.span.start + match.index))
@@ -1,4 +1,5 @@
1
1
  import { lengthOf, proseText } from "../measure.ts";
2
+ import { cleftFocus } from "./cleft-focus.ts";
2
3
  import { newContentMorphemes } from "./content-morphemes.ts";
3
4
  import { echoedHeadingUnits, trigrams } from "./heading-overlap.ts";
4
5
  import { fieldValueStart } from "./field-label.ts";
@@ -9,7 +10,7 @@ import { handsOver } from "./lead-in.ts";
9
10
  import { holdsLink, namesLocation } from "./location-pointer.ts";
10
11
  import { withoutQuotedVariants } from "./quoted-variant.ts";
11
12
  import { namesHeadingSubject } from "./subject-name-line.ts";
12
- import type { Detector, Finding, LengthUnit, ProseDocument, Section, Sentence } from "../plugin.ts";
13
+ import type { Detector, Finding, LengthUnit, ProseDocument, Section, Sentence, Token } from "../plugin.ts";
13
14
 
14
15
  /** 見出しが短すぎると、偶然の一致で 100% になる。これ未満の見出しは見ない。 */
15
16
  const MIN_GRAMS = 4;
@@ -46,11 +47,23 @@ const NEW_CONTENT_MORPHEMES = 1;
46
47
  /** 重なりを測る見出し。頭の番号の札(例 3:、Step 3:、1.)は本文で繰り返されないので、数えると短い見出しほど重なりが下がる。 */
47
48
  const measuredHeading = (section: Section): string => section.unlabeledHeading ?? section.heading;
48
49
 
49
- const addsLittle = (section: Section, unit: LengthUnit): boolean => {
50
+ /**
51
+ * 内容を測る文の語。分裂文(いちばん大きく変わったのは、季節の移り変わりに気づくようになったことだ。)は焦点の語だけ:
52
+ * 主題の節は焦点を差し出すだけで、焦点が見出しを言い直していれば、文は見出しを繰り返している。
53
+ */
54
+ const statedTokens = (doc: ProseDocument, sentence: Sentence): readonly Token[] | undefined => {
55
+ const focus = cleftFocus(sentence.text, doc.lexicons["cleft-frame"] ?? []);
56
+ if (sentence.tokens === undefined || focus === undefined) return sentence.tokens;
57
+ const [start, end] = [sentence.span.start + focus.start, sentence.span.start + focus.end];
58
+ return sentence.tokens.filter((token) => token.span.start >= start && token.span.end <= end);
59
+ };
60
+
61
+ const addsLittle = (doc: ProseDocument, section: Section, unit: LengthUnit): boolean => {
50
62
  const first = section.firstSentence;
51
63
  if (first === undefined) return false;
52
- if (unit === "char" && first.tokens !== undefined && section.headingTokens !== undefined)
53
- return newContentMorphemes(titleTokens(section.heading, measuredHeading(section), section.headingTokens), first.tokens) <= NEW_CONTENT_MORPHEMES;
64
+ const stated = statedTokens(doc, first);
65
+ if (unit === "char" && stated !== undefined && section.headingTokens !== undefined)
66
+ return newContentMorphemes(titleTokens(section.heading, measuredHeading(section), section.headingTokens), stated) <= NEW_CONTENT_MORPHEMES;
54
67
  return lengthOf(first, unit) - echoedHeadingUnits(measuredHeading(section), proseText(first), unit) <= NEW_MATERIAL[unit];
55
68
  };
56
69
 
@@ -74,10 +87,10 @@ const pointsElsewhere = (doc: ProseDocument, section: Section): boolean => {
74
87
  * 予告や指示の語(covers, below, 説明, 以下)だけを述語にする文は、見出しを言い直しているだけで繰り返し。
75
88
  */
76
89
  const statesClaim = (doc: ProseDocument, section: Section): boolean => {
77
- const first = section.firstSentence;
78
- if (first?.tokens === undefined || section.headingTokens === undefined) return false;
90
+ const stated = section.firstSentence === undefined ? undefined : statedTokens(doc, section.firstSentence);
91
+ if (stated === undefined || section.headingTokens === undefined) return false;
79
92
  const heading = titleTokens(section.heading, measuredHeading(section), section.headingTokens);
80
- return makesClaim(heading, first.tokens, announcerWords(doc.lexicons["heading-announcer"] ?? []));
93
+ return makesClaim(heading, stated, announcerWords(doc.lexicons["heading-announcer"] ?? []));
81
94
  };
82
95
 
83
96
  /** A figure, with its thousands separators (1,320,000), compared without them. */
@@ -138,7 +151,7 @@ const echoedText = (section: Section): string =>
138
151
  export const headingEcho: Detector = (doc, options): Finding[] => {
139
152
  const leadIns = (doc.lexicons["lead-in"] ?? []).map((entry) => entry.pattern);
140
153
  return doc.sections
141
- .filter((section) => section.heading.length > 0 && section.firstSentence !== undefined && addsLittle(section, doc.lengthUnit))
154
+ .filter((section) => section.heading.length > 0 && section.firstSentence !== undefined && addsLittle(doc, section, doc.lengthUnit))
142
155
  .filter(
143
156
  (section) =>
144
157
  !leadsIn(doc, section, leadIns) &&
@@ -0,0 +1,20 @@
1
+ /** A heading's number at its head: 3, 4.2, 4.2.1 (a full stop after it, as in "3.", is not part of it). */
2
+ const DOTTED_NUMBER = /^(\d{1,3}(?:\.\d{1,3})*)\.?(?!\d)/u;
3
+ /** The first item of a bracketed series under a numbered heading: (1), (1). */
4
+ const FIRST_BRACKETED = /^\(1\)/u;
5
+
6
+ const numberOf = (heading: string): string | undefined => DOTTED_NUMBER.exec(heading.normalize("NFKC").trim())?.[1];
7
+
8
+ /**
9
+ * Whether `child`, the next heading at the same depth, opens a subsection of `parent` by its number: 3. then 3.1, 4.2 then
10
+ * 4.2.1, or a numbered heading then its first bracketed item ((1)). Some documents number their subsections at the
11
+ * heading level of the section itself, so the depth alone does not show the split.
12
+ */
13
+ export const opensSubsection = (parent: string, child: string): boolean => {
14
+ const parentNumber = numberOf(parent);
15
+ if (parentNumber === undefined) return false;
16
+ const normalizedChild = child.normalize("NFKC").trim();
17
+ if (FIRST_BRACKETED.test(normalizedChild)) return true;
18
+ const childNumber = numberOf(normalizedChild);
19
+ return childNumber !== undefined && childNumber.startsWith(`${parentNumber}.`) && !childNumber.slice(parentNumber.length + 1).includes(".");
20
+ };
@@ -7,8 +7,9 @@ import { codePointName } from "./invisible-character.ts";
7
7
  * compatibility: a CJK compatibility ideograph (or one of its supplement) that Unicode maps to a unified one (樂 U+F914 for 樂).
8
8
  * decomposed: a letter and a separate combining mark (か + U+3099 for が, e + U+0301 for é), from file names or text
9
9
  * copied on macOS. spacing-mark: kana followed by the spacing ゛ or ゜, typed in place of the combined letter.
10
+ * dash: a dash or minus between katakana (ニュ-ス, U+FF0D), typed in place of the long-vowel mark ー.
10
11
  */
11
- export type LookalikeKind = "radical" | "compatibility" | "decomposed" | "spacing-mark";
12
+ export type LookalikeKind = "radical" | "compatibility" | "decomposed" | "spacing-mark" | "dash";
12
13
 
13
14
  export type Lookalike = { readonly span: Span; readonly kind: LookalikeKind; readonly written: string; readonly usual: string };
14
15
 
@@ -48,8 +49,24 @@ const spacingMarkLookalikes = (source: string): Lookalike[] =>
48
49
  return [{ span: { start: match.index, end: match.index + match[0].length }, kind: "spacing-mark", written: match[0], usual }];
49
50
  });
50
51
 
52
+ const LONG_VOWEL = "ー";
53
+ /**
54
+ * A dash, a minus or a box line after a katakana letter that can be lengthened and before another katakana letter. The
55
+ * half-width - is left out: between katakana it joins the parts of a template or a range (<アルゴリズム>-<ハッシュ>, ァ-ヶ).
56
+ */
57
+ const DASH_FOR_LONG_VOWEL = /(?<=[ァ-ヂツ-ヲヴ-ヺ])[\u2010-\u2015\u2212\uFF0D\u2500\u2501](?=[ァ-ヺ])/gu;
58
+
59
+ /** A dash typed in place of the long-vowel mark between katakana (ニュ-ス → ニュース). */
60
+ const dashLookalikes = (source: string): Lookalike[] =>
61
+ [...source.matchAll(DASH_FOR_LONG_VOWEL)].map((match) => ({
62
+ span: { start: match.index, end: match.index + match[0].length },
63
+ kind: "dash",
64
+ written: match[0],
65
+ usual: LONG_VOWEL,
66
+ }));
67
+
51
68
  export const lookalikesIn = (source: string): Lookalike[] =>
52
- [...singleLookalikes(source), ...decomposedLookalikes(source), ...spacingMarkLookalikes(source)].toSorted(
69
+ [...singleLookalikes(source), ...decomposedLookalikes(source), ...spacingMarkLookalikes(source), ...dashLookalikes(source)].toSorted(
53
70
  (left, right) => left.span.start - right.span.start,
54
71
  );
55
72
 
@@ -11,6 +11,7 @@ import { lineStarts } from "../position.ts";
11
11
  import { isTypewritten } from "./typewritten.ts";
12
12
  import { divisionLabelPattern, isDivisionSeparator } from "./division-label-separator.ts";
13
13
  import { isInCopiedText } from "./hyphen-copied-text.ts";
14
+ import { verseBlockSpans } from "./verse-block.ts";
14
15
 
15
16
  /** The letters a mark must touch to count as part of the document's text, by language. */
16
17
  const SCRIPT: Readonly<Record<string, RegExp>> = {
@@ -180,6 +181,22 @@ export const noteMarkerOffsets = (source: string): ReadonlySet<number> =>
180
181
 
181
182
  const NOTE_KIND = "\u0000note";
182
183
 
184
+ /**
185
+ * A note's number cited in the prose (注(24)で示した): an author writes it as the notes' labels are, or as the prose's
186
+ * brackets are, and either is the document's own notation for it.
187
+ */
188
+ const NOTE_ADDRESS = /(?<=[注註])(?:\([0-90-9]{1,3}\)|([0-90-9]{1,3}))/gu;
189
+
190
+ /** The offsets in `source` of the brackets of every note's cited number. */
191
+ export const noteAddressOffsets = (source: string): ReadonlySet<number> =>
192
+ new Set([...source.matchAll(NOTE_ADDRESS)].flatMap((match) => [match.index, match.index + match[0].length - 1]));
193
+
194
+ const NOTE_ADDRESS_KIND = "\u0000noteaddress";
195
+
196
+ /** A note's cited number is a kind of its own, compared only with the other cited note numbers. */
197
+ const withNoteAddressKind = (mark: Mark, addresses: ReadonlySet<number>): Mark =>
198
+ addresses.has(mark.offset) ? { ...mark, form: { ...mark.form, kind: `${mark.form.kind}${NOTE_ADDRESS_KIND}` } } : mark;
199
+
183
200
  /** A note marker's bracket is a kind of its own, compared only with the other note markers' brackets. */
184
201
  const withNoteKind = (mark: Mark, notes: ReadonlySet<number>): Mark =>
185
202
  notes.has(mark.offset) && !mark.form.kind.endsWith(ENUMERATOR_KIND) ? { ...mark, form: { ...mark.form, kind: `${mark.form.kind}${NOTE_KIND}` } } : mark;
@@ -199,8 +216,11 @@ export const markConsistency: Detector = (doc, options): Finding[] => {
199
216
  const enumerators = enumeratorBracketOffsets(doc.source);
200
217
  const doubled = doubledParenOffsets(doc.source);
201
218
  const notes = noteMarkerOffsets(doc.source);
219
+ const noteAddresses = noteAddressOffsets(doc.source);
202
220
  const marks = ownSentences(doc).flatMap((sentence) =>
203
- marksIn(doc.source, sentence, forms, script).map((mark) => withNoteKind(withDoubledKind(withEnumeratorKind(mark, enumerators), doubled), notes)),
221
+ marksIn(doc.source, sentence, forms, script).map((mark) =>
222
+ withNoteAddressKind(withNoteKind(withDoubledKind(withEnumeratorKind(mark, enumerators), doubled), notes), noteAddresses),
223
+ ),
204
224
  );
205
225
  const kinds = [...new Set(marks.map((mark) => mark.form.kind))];
206
226
  return kinds.flatMap((kind) =>
@@ -219,18 +239,25 @@ export const markConsistency: Detector = (doc, options): Finding[] => {
219
239
  const SENTENCE_END = /(?<![\p{L}\d.])\p{Ll}{2,}[.!?]["'”’)\]]?$/u;
220
240
  /** The next sentence starts with a capital, past an opening quote: not a citation ("[ISO99]") or an aside in brackets. */
221
241
  const SENTENCE_START = /^["'“‘]?\p{Lu}/u;
242
+ /** "Affiliation: …", "Note: …": a field's label opens what follows, as in an author block joined into one line. */
243
+ const FIELD_LABEL = /^\p{Lu}\p{Ll}+:\s/u;
222
244
  const ONE_SPACE = " ";
223
245
  const TWO_SPACES = " ";
224
246
 
225
- /** The space between two sentences on one line, when the first ends with a period, question or exclamation mark. spaced is two spaces. */
226
- export const sentenceGaps = (doc: ProseDocument): Sided[] =>
227
- ownSentences(doc).flatMap((sentence, at, sentences) => {
247
+ /**
248
+ * The space between two sentences on one line, when the first ends with a period, question or exclamation mark. spaced is
249
+ * two spaces. A pause inside a verse line ("I'm nobody! Who are you?") is the stanza's layout, not the prose's spacing.
250
+ */
251
+ export const sentenceGaps = (doc: ProseDocument): Sided[] => {
252
+ const stanzas = verseBlockSpans(doc.source);
253
+ return ownSentences(doc).flatMap((sentence, at, sentences) => {
228
254
  const next = sentences[at + 1];
229
- if (next === undefined || !SENTENCE_END.test(sentence.text) || !SENTENCE_START.test(next.text)) return [];
255
+ if (next === undefined || !SENTENCE_END.test(sentence.text) || !SENTENCE_START.test(next.text) || FIELD_LABEL.test(next.text)) return [];
230
256
  const gap = doc.source.slice(sentence.span.end, next.span.start);
231
- if (gap !== ONE_SPACE && gap !== TWO_SPACES) return [];
257
+ if ((gap !== ONE_SPACE && gap !== TWO_SPACES) || isWithinSorted(stanzas, { start: sentence.span.end, end: next.span.start })) return [];
232
258
  return [{ sentence: next, spaced: gap === TWO_SPACES, offset: sentence.span.end }];
233
259
  });
260
+ };
234
261
 
235
262
  /** One space between sentences and two, mixed in one document: the less common is reported. */
236
263
  export const sentenceSpacing: Detector = (doc, options): Finding[] =>
@@ -1,3 +1,5 @@
1
+ import type { Detector, Finding } from "../plugin.ts";
2
+
1
3
  const MIDDLE_DOT = /・/gu;
2
4
 
3
5
  /** 文頭か行頭(前に空白だけ)の「・」。前に並べる項目が無いので、箇条書きの印。 */
@@ -13,3 +15,23 @@ const withoutWordDots = (text: string, words: readonly string[]): string =>
13
15
  */
14
16
  export const parallelDotCount = (text: string, words: readonly string[] = []): number =>
15
17
  [...withoutWordDots(text, words).replace(BULLET_DOT, "$1").matchAll(MIDDLE_DOT)].length;
18
+
19
+ /**
20
+ * 中黒で並べると、どこまでが 1 つの項目か分からなくなる。
21
+ * 「企画・開発・運用・保守体制」は、保守体制が 1 つなのか保守と体制なのか読めない。
22
+ * 語彙表(nakaguro-word)の、中黒を含めて 1 語の語(キャッシュ・フロー)の中黒は並べていないので数えない。
23
+ */
24
+ export const middleDot: Detector = (doc, options): Finding[] => {
25
+ const words = (options.lexicon ?? []).map((entry) => entry.pattern).toSorted((left, right) => right.length - left.length);
26
+ return doc.sentences
27
+ .map((sentence) => ({ sentence, count: parallelDotCount(sentence.text, words) }))
28
+ .filter(({ count }) => count > options.limit)
29
+ .map(({ sentence, count }) => ({
30
+ rule: "no-nakaguro-parallel",
31
+ severity: "info",
32
+ line: 0,
33
+ column: 0,
34
+ quote: sentence.text.trim(),
35
+ values: { count, limit: options.limit, offset: sentence.span.start },
36
+ }));
37
+ };
@@ -0,0 +1,15 @@
1
+ /** 連なりの前後の字と、名前を示す句の語彙。 */
2
+ type Surroundings = { readonly before: string; readonly next: string };
3
+ type ContextWords = { readonly designations: readonly string[]; readonly noticeEndings: readonly string[] };
4
+
5
+ /** 連なりの後ろ(next)が、制度が区分に入れることを言う句で始まるか(認定希少種保全動植物園等として認定)。 */
6
+ const isDesignated = (run: Surroundings, words: ContextWords): boolean => words.designations.some((phrase) => run.next.startsWith(phrase));
7
+
8
+ /** 行がお知らせの題で、連なりがその「の」の前か([e-Gov]〜設定解除完了のお知らせ)。題は句点で閉じない一行。 */
9
+ const isNoticeTitle = (run: Surroundings, words: ContextWords): boolean => {
10
+ const next = run.next.trim();
11
+ return !run.before.includes("\n") && words.noticeEndings.some((ending) => next === `の${ending}`);
12
+ };
13
+
14
+ /** 前後の字が、漢字の連なりを名前と示しているか: 制度が決めた区分の名前か、送り手が付けたお知らせの題の名前。 */
15
+ export const isNamedByContext = (run: Surroundings, words: ContextWords): boolean => isDesignated(run, words) || isNoticeTitle(run, words);