chaffjs 0.42.0 → 0.43.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 (165) hide show
  1. package/dist/detectors/bold-label.d.ts +2 -0
  2. package/dist/detectors/bold-label.d.ts.map +1 -1
  3. package/dist/detectors/bold-label.js +4 -0
  4. package/dist/detectors/bold-label.js.map +1 -1
  5. package/dist/detectors/clause-shape.d.ts +8 -2
  6. package/dist/detectors/clause-shape.d.ts.map +1 -1
  7. package/dist/detectors/clause-shape.js +14 -6
  8. package/dist/detectors/clause-shape.js.map +1 -1
  9. package/dist/detectors/document-shape.d.ts +6 -1
  10. package/dist/detectors/document-shape.d.ts.map +1 -1
  11. package/dist/detectors/document-shape.js +14 -4
  12. package/dist/detectors/document-shape.js.map +1 -1
  13. package/dist/detectors/doubled-word.d.ts.map +1 -1
  14. package/dist/detectors/doubled-word.js +5 -1
  15. package/dist/detectors/doubled-word.js.map +1 -1
  16. package/dist/detectors/en-shape.d.ts.map +1 -1
  17. package/dist/detectors/en-shape.js +12 -1
  18. package/dist/detectors/en-shape.js.map +1 -1
  19. package/dist/detectors/ending-refrain.d.ts +9 -0
  20. package/dist/detectors/ending-refrain.d.ts.map +1 -0
  21. package/dist/detectors/ending-refrain.js +52 -0
  22. package/dist/detectors/ending-refrain.js.map +1 -0
  23. package/dist/detectors/fact-consistency.d.ts.map +1 -1
  24. package/dist/detectors/fact-consistency.js +5 -1
  25. package/dist/detectors/fact-consistency.js.map +1 -1
  26. package/dist/detectors/gram-predicate.d.ts.map +1 -1
  27. package/dist/detectors/gram-predicate.js +12 -3
  28. package/dist/detectors/gram-predicate.js.map +1 -1
  29. package/dist/detectors/lexicon.d.ts.map +1 -1
  30. package/dist/detectors/lexicon.js +1 -0
  31. package/dist/detectors/lexicon.js.map +1 -1
  32. package/dist/detectors/lookalike-character.d.ts +2 -0
  33. package/dist/detectors/lookalike-character.d.ts.map +1 -1
  34. package/dist/detectors/lookalike-character.js +36 -9
  35. package/dist/detectors/lookalike-character.js.map +1 -1
  36. package/dist/detectors/mark-shape.d.ts +1 -1
  37. package/dist/detectors/mark-shape.d.ts.map +1 -1
  38. package/dist/detectors/mark-shape.js +29 -12
  39. package/dist/detectors/mark-shape.js.map +1 -1
  40. package/dist/detectors/number-style.d.ts +7 -1
  41. package/dist/detectors/number-style.d.ts.map +1 -1
  42. package/dist/detectors/number-style.js +21 -1
  43. package/dist/detectors/number-style.js.map +1 -1
  44. package/dist/detectors/number-unit.d.ts +7 -1
  45. package/dist/detectors/number-unit.d.ts.map +1 -1
  46. package/dist/detectors/number-unit.js +20 -6
  47. package/dist/detectors/number-unit.js.map +1 -1
  48. package/dist/detectors/particle-run-long-forms.d.ts +7 -0
  49. package/dist/detectors/particle-run-long-forms.d.ts.map +1 -0
  50. package/dist/detectors/particle-run-long-forms.js +23 -0
  51. package/dist/detectors/particle-run-long-forms.js.map +1 -0
  52. package/dist/detectors/particle-run-quoted-names.d.ts +6 -0
  53. package/dist/detectors/particle-run-quoted-names.d.ts.map +1 -0
  54. package/dist/detectors/particle-run-quoted-names.js +18 -0
  55. package/dist/detectors/particle-run-quoted-names.js.map +1 -0
  56. package/dist/detectors/register-separate-part.d.ts +9 -1
  57. package/dist/detectors/register-separate-part.d.ts.map +1 -1
  58. package/dist/detectors/register-separate-part.js +23 -0
  59. package/dist/detectors/register-separate-part.js.map +1 -1
  60. package/dist/detectors/register-voice.d.ts +4 -3
  61. package/dist/detectors/register-voice.d.ts.map +1 -1
  62. package/dist/detectors/register-voice.js +11 -4
  63. package/dist/detectors/register-voice.js.map +1 -1
  64. package/dist/detectors/register.d.ts.map +1 -1
  65. package/dist/detectors/register.js +9 -6
  66. package/dist/detectors/register.js.map +1 -1
  67. package/dist/detectors/sentence-ending.d.ts +1 -0
  68. package/dist/detectors/sentence-ending.d.ts.map +1 -1
  69. package/dist/detectors/sentence-ending.js +7 -3
  70. package/dist/detectors/sentence-ending.js.map +1 -1
  71. package/dist/detectors/signals.d.ts.map +1 -1
  72. package/dist/detectors/signals.js +5 -2
  73. package/dist/detectors/signals.js.map +1 -1
  74. package/dist/detectors/stacked-hedge.d.ts +4 -2
  75. package/dist/detectors/stacked-hedge.d.ts.map +1 -1
  76. package/dist/detectors/stacked-hedge.js +16 -3
  77. package/dist/detectors/stacked-hedge.js.map +1 -1
  78. package/dist/detectors/token-shape.d.ts.map +1 -1
  79. package/dist/detectors/token-shape.js +13 -1
  80. package/dist/detectors/token-shape.js.map +1 -1
  81. package/dist/detectors/unknown-word-context.d.ts +5 -0
  82. package/dist/detectors/unknown-word-context.d.ts.map +1 -1
  83. package/dist/detectors/unknown-word-context.js +8 -0
  84. package/dist/detectors/unknown-word-context.js.map +1 -1
  85. package/dist/detectors/unknown-word.d.ts.map +1 -1
  86. package/dist/detectors/unknown-word.js +3 -3
  87. package/dist/detectors/unknown-word.js.map +1 -1
  88. package/dist/detectors/weasel-word.d.ts.map +1 -1
  89. package/dist/detectors/weasel-word.js +23 -4
  90. package/dist/detectors/weasel-word.js.map +1 -1
  91. package/dist/facts/era-year.d.ts +2 -0
  92. package/dist/facts/era-year.d.ts.map +1 -0
  93. package/dist/facts/era-year.js +7 -0
  94. package/dist/facts/era-year.js.map +1 -0
  95. package/dist/facts/fact-scope.d.ts +1 -1
  96. package/dist/facts/fact-scope.d.ts.map +1 -1
  97. package/dist/facts/fact-scope.js +18 -8
  98. package/dist/facts/fact-scope.js.map +1 -1
  99. package/dist/facts/table-facts.d.ts.map +1 -1
  100. package/dist/facts/table-facts.js +18 -2
  101. package/dist/facts/table-facts.js.map +1 -1
  102. package/dist/own-title-quote.d.ts +7 -0
  103. package/dist/own-title-quote.d.ts.map +1 -0
  104. package/dist/own-title-quote.js +19 -0
  105. package/dist/own-title-quote.js.map +1 -0
  106. package/dist/structure/definition-use.d.ts.map +1 -1
  107. package/dist/structure/definition-use.js +3 -1
  108. package/dist/structure/definition-use.js.map +1 -1
  109. package/dist/structure-shape/features.d.ts.map +1 -1
  110. package/dist/structure-shape/features.js +15 -2
  111. package/dist/structure-shape/features.js.map +1 -1
  112. package/dist/word-use.d.ts.map +1 -1
  113. package/dist/word-use.js +9 -1
  114. package/dist/word-use.js.map +1 -1
  115. package/package.json +3 -3
  116. package/rules/adverb-overuse.yaml +2 -2
  117. package/rules/bracket-nesting.yaml +7 -2
  118. package/rules/bracket-width-consistency.yaml +2 -2
  119. package/rules/doubled-word.yaml +2 -2
  120. package/rules/excessive-hedging.yaml +2 -1
  121. package/rules/fact-conflict.yaml +2 -2
  122. package/rules/lookalike-character.yaml +2 -2
  123. package/rules/ngram-repetition.yaml +2 -2
  124. package/rules/no-doubled-joshi.yaml +2 -2
  125. package/rules/no-mixed-desumasu.yaml +3 -2
  126. package/rules/number-style-consistency.yaml +2 -2
  127. package/rules/number-without-unit.yaml +2 -2
  128. package/rules/orthographic-variant.yaml +2 -2
  129. package/rules/repeated-sentence-ending.yaml +2 -2
  130. package/rules/title-length.yaml +7 -2
  131. package/rules/undefined-acronym.yaml +2 -2
  132. package/rules/unknown-word.yaml +2 -2
  133. package/rules/unused-definition.yaml +4 -2
  134. package/src/detectors/bold-label.ts +6 -0
  135. package/src/detectors/clause-shape.ts +14 -12
  136. package/src/detectors/document-shape.ts +17 -6
  137. package/src/detectors/doubled-word.ts +9 -1
  138. package/src/detectors/en-shape.ts +15 -1
  139. package/src/detectors/ending-refrain.ts +66 -0
  140. package/src/detectors/fact-consistency.ts +4 -1
  141. package/src/detectors/gram-predicate.ts +14 -3
  142. package/src/detectors/lexicon.ts +1 -0
  143. package/src/detectors/lookalike-character.ts +40 -10
  144. package/src/detectors/mark-shape.ts +31 -12
  145. package/src/detectors/number-style.ts +29 -2
  146. package/src/detectors/number-unit.ts +29 -8
  147. package/src/detectors/particle-run-long-forms.ts +34 -0
  148. package/src/detectors/particle-run-quoted-names.ts +27 -0
  149. package/src/detectors/register-separate-part.ts +32 -1
  150. package/src/detectors/register-voice.ts +17 -4
  151. package/src/detectors/register.ts +9 -6
  152. package/src/detectors/sentence-ending.ts +10 -4
  153. package/src/detectors/signals.ts +5 -2
  154. package/src/detectors/stacked-hedge.ts +24 -7
  155. package/src/detectors/token-shape.ts +14 -1
  156. package/src/detectors/unknown-word-context.ts +9 -0
  157. package/src/detectors/unknown-word.ts +4 -2
  158. package/src/detectors/weasel-word.ts +31 -5
  159. package/src/facts/era-year.ts +7 -0
  160. package/src/facts/fact-scope.ts +20 -6
  161. package/src/facts/table-facts.ts +31 -3
  162. package/src/own-title-quote.ts +28 -0
  163. package/src/structure/definition-use.ts +4 -1
  164. package/src/structure-shape/features.ts +18 -2
  165. package/src/word-use.ts +10 -1
@@ -18,6 +18,15 @@ export const isClosedCompound = (word: string, speller: Speller): boolean => {
18
18
  .some((split) => isKnownWord(word.slice(0, split), listed) && isKnownWord(word.slice(split), inflected));
19
19
  };
20
20
 
21
+ /**
22
+ * A word spelled -nce where the dictionary spells it -nse (recompence, expence, suspence): the older or British spelling of
23
+ * that word, as licence and defence are, not a slip.
24
+ */
25
+ export const isNceSpelling = (word: string, speller: Speller): boolean => {
26
+ const at = word.lastIndexOf("nce");
27
+ return at > 0 && isKnownWord(`${word.slice(0, at)}nse${word.slice(at + "nce".length)}`, speller);
28
+ };
29
+
21
30
  /** Letters a name keeps before its drug stem: fewer, and the word is too short to be read as a drug by its ending alone. */
22
31
  const MIN_DRUG_HEAD = 3;
23
32
 
@@ -13,6 +13,7 @@ import {
13
13
  inSearchTermList,
14
14
  isClosedCompound,
15
15
  isMention,
16
+ isNceSpelling,
16
17
  usedOnce,
17
18
  } from "./unknown-word-context.ts";
18
19
 
@@ -51,8 +52,9 @@ export const codeWordsOf = (source: string): Set<string> => {
51
52
  return words;
52
53
  };
53
54
 
54
- /** 辞書の語か、派生した語に活用の語尾を付けた語(operationalises、rightsized)か、四字以上の辞書の語二つを続けた語(crowdwork)。 */
55
- const isFormedWord = (word: string, speller: Speller): boolean => isKnownWord(word, speller) || hasTwoEndings(word, speller) || isClosedCompound(word, speller);
55
+ /** 辞書の語か、派生した語に活用の語尾を付けた語(operationalises、rightsized)か、四字以上の辞書の語二つを続けた語(crowdwork)か、-nse の語の -nce の綴り(recompence)。 */
56
+ const isFormedWord = (word: string, speller: Speller): boolean =>
57
+ isKnownWord(word, speller) || hasTwoEndings(word, speller) || isClosedCompound(word, speller) || isNceSpelling(word, speller);
56
58
 
57
59
  /**
58
60
  * 置かれた場所から、名前や用語と分かる語。名指した語("mailto"、(jchaplin))、検索語の並び(vaccin*)、外国語の句(bête noire)、
@@ -1,7 +1,8 @@
1
- import type { Detector, LexiconEntry, ProseDocument, Sentence } from "../plugin.ts";
1
+ import type { Detector, LexiconEntry, ProseDocument, Sentence, Span } from "../plugin.ts";
2
2
  import { entryRanges } from "./lexicon-match.ts";
3
3
  import { phraseMatchWhere } from "./phrase-match.ts";
4
4
  import { followsToldQuote, type TellingCues } from "./reported-speech.ts";
5
+ import { citationMarks, citationWordsOf, referenceListsOf } from "./citation-marks.ts";
5
6
 
6
7
  const cuesOf = (doc: ProseDocument): TellingCues => {
7
8
  const entries = doc.lexicons["telling-cue"] ?? [];
@@ -9,16 +10,41 @@ const cuesOf = (doc: ProseDocument): TellingCues => {
9
10
  return { occasion: inGroup("occasion"), sayer: inGroup("sayer") };
10
11
  };
11
12
 
13
+ /** Where the document names its sources: its reference lists, and the in-text citations ([11, 49], Smith (2020)). */
14
+ type Sources = { readonly referenceLists: readonly Span[]; readonly citations: readonly Span[] };
15
+
16
+ const sourcesOf = (doc: ProseDocument): Sources => {
17
+ const words = citationWordsOf(doc);
18
+ const referenceLists = referenceListsOf(doc, words);
19
+ return { referenceLists, citations: citationMarks(doc.prose ?? doc.source, referenceLists, words) };
20
+ };
21
+
22
+ const isInside = (spans: readonly Span[], at: number): boolean => spans.some((span) => span.start <= at && at < span.end);
23
+
24
+ /**
25
+ * A phrase that names its source: an entry of a reference list (a cited headline, "…, experts say") is the cited work's
26
+ * words, and a citation later in the sentence ("previous studies showing … [11, 49]") says which studies.
27
+ */
28
+ const namesSource = (sources: Sources, sentence: Sentence, phraseStart: number): boolean =>
29
+ isInside(sources.referenceLists, sentence.span.start) ||
30
+ sources.citations.some((citation) => phraseStart <= citation.start && citation.start < sentence.span.end);
31
+
12
32
  /**
13
33
  * A weasel phrase counts unless every use of it in the sentence reports a quotation told on a named occasion or by a named
14
- * sayer (申し込んだら、「…」と言われています). Without tokens the uses cannot be placed, so each one counts.
34
+ * sayer (申し込んだら、「…」と言われています), or names its source. Without tokens the uses cannot be placed, so each one counts.
15
35
  */
16
- const isWeaselUse = (doc: ProseDocument, sentence: Sentence, entry: LexiconEntry): boolean => {
36
+ const isWeaselUse = (doc: ProseDocument, sources: Sources, sentence: Sentence, entry: LexiconEntry): boolean => {
17
37
  const tokens = sentence.tokens ?? [];
18
38
  const ranges = entryRanges(sentence, entry);
19
39
  if (ranges.length === 0) return true;
20
40
  const cues = cuesOf(doc);
21
- return !ranges.every((range) => followsToldQuote(doc.source.slice(sentence.span.start, tokens[range.start]?.span.start ?? sentence.span.start), cues));
41
+ return !ranges.every((range) => {
42
+ const start = tokens[range.start]?.span.start ?? sentence.span.start;
43
+ return followsToldQuote(doc.source.slice(sentence.span.start, start), cues) || namesSource(sources, sentence, start);
44
+ });
22
45
  };
23
46
 
24
- export const weaselWord: Detector = (doc, options) => phraseMatchWhere(doc, options, (sentence, entry) => isWeaselUse(doc, sentence, entry));
47
+ export const weaselWord: Detector = (doc, options) => {
48
+ const sources = sourcesOf(doc);
49
+ return phraseMatchWhere(doc, options, (sentence, entry) => isWeaselUse(doc, sources, sentence, entry));
50
+ };
@@ -0,0 +1,7 @@
1
+ /** 元号のすぐ後ろ(空白一つまで)の数。暦の年(平成29年、令和 3 年)で、年数という長さではない。元号は語彙表 calendar-era から。 */
2
+ const ERA_GAP = /[ \t\u3000]?$/u;
3
+
4
+ export const followsEra = (source: string, start: number, eras: readonly string[]): boolean => {
5
+ const head = source.slice(0, start).replace(ERA_GAP, "");
6
+ return eras.some((era) => era !== "" && head.endsWith(era));
7
+ };
@@ -11,7 +11,7 @@ import { withoutEdgeMarks } from "./trim-marks.ts";
11
11
  */
12
12
  export type FactPart = "opening" | "summary" | "body";
13
13
 
14
- /** record: 同じ範囲で、行の頭に何度も書かれた名前。催しや論文ごとの記録の欄で、値が違っても食い違いではない。 */
14
+ /** record: 同じ範囲で、行の頭(か行の中の文の切れ目の後ろ)に何度も書かれた名前。催しや論文ごとの記録の欄で、値が違っても食い違いではない。 */
15
15
  export type ScopedFact = Fact & { readonly scope: string; readonly part: FactPart; readonly record: boolean };
16
16
 
17
17
  const CONTAINERS: ReadonlySet<string> = new Set(["section", "chapter", "article"]);
@@ -99,15 +99,29 @@ const partOf = (around: readonly StructureNode[], tree: StructureNode, offset: n
99
99
  return isOpening(around.at(-1), tree, offset) ? "opening" : "body";
100
100
  };
101
101
 
102
- /** 名前ごとに、頭にその名前を書いた行の番号。 */
102
+ /** 行の中の文の切れ目のすぐ後ろ。参考文献の項目は、書誌の文のあとに「External Links: …」と欄を続ける。 */
103
+ const INLINE_LABEL = /[.!?。.!?][ \t]+([^.!?。.!?::|]{1,24})[::]/gu;
104
+
105
+ /** 文の切れ目の後ろから始まる「名前:」の名前。行の頭の名前と同じ書き方に揃える。表の行は読まない。 */
106
+ const inlineLabelsOf = (line: Line): string[] =>
107
+ line.text.trimStart().startsWith("|")
108
+ ? []
109
+ : [...line.text.matchAll(INLINE_LABEL)].flatMap((match) => {
110
+ const label = withoutEdgeMarks(match[1] ?? "");
111
+ return /\p{L}/u.test(label) ? [label.normalize("NFKC").toLowerCase()] : [];
112
+ });
113
+
114
+ /** 名前ごとに、その名前の欄を書いた行の番号。欄は行の頭か、行の中の文の切れ目の後ろ。 */
103
115
  const headLines = (lines: readonly Line[]): ReadonlyMap<string, readonly number[]> => {
104
116
  const found = new Map<string, number[]>();
105
117
  lines.forEach((line, index) => {
106
118
  const head = headLabelOf(line);
107
- if (head === undefined) return;
108
- const indexes = found.get(head);
109
- if (indexes === undefined) found.set(head, [index]);
110
- else indexes.push(index);
119
+ const labels = new Set([...(head === undefined ? [] : [head]), ...inlineLabelsOf(line)]);
120
+ labels.forEach((label) => {
121
+ const indexes = found.get(label);
122
+ if (indexes === undefined) found.set(label, [index]);
123
+ else indexes.push(index);
124
+ });
111
125
  });
112
126
  return found;
113
127
  };
@@ -112,17 +112,44 @@ const groupsOf = <T>(items: readonly T[], width: number | undefined): T[][] =>
112
112
  /** columnGroups: 見出しを繰り返す表を、並びごとに読む(並びの升には columnGroup が付く)。 */
113
113
  export type TableReading = { readonly columnGroups?: boolean };
114
114
 
115
- type RowReading = { readonly values: readonly FactValue[]; readonly source: string; readonly totals?: TotalColumns; readonly counts?: CountLabelWords };
115
+ /** repeated: 並びの中で二つ以上の行に書いた行の見出し(groupLabelKey)。 */
116
+ type RowReading = {
117
+ readonly values: readonly FactValue[];
118
+ readonly source: string;
119
+ readonly repeated: ReadonlySet<string>;
120
+ readonly totals?: TotalColumns;
121
+ readonly counts?: CountLabelWords;
122
+ };
116
123
 
117
124
  /** 一つの行の升の値。見出しを繰り返す表(width がある)は、並びごとに読んで columnGroup を付ける。 */
118
125
  const groupedRowFacts = (row: Line, headings: readonly string[], width: number | undefined, at: RowReading): TableFact[] => {
119
126
  const headingGroups = groupsOf(headings, width);
120
127
  return groupsOf(cellsOf(row), width).flatMap((cells, group) => {
121
- const facts = rowFacts(cells, headingGroups[group] ?? [], at.values, at.source, at.totals, at.counts);
128
+ const read = rowFacts(cells, headingGroups[group] ?? [], at.values, at.source, at.totals, at.counts);
129
+ const facts = at.repeated.has(groupLabelKey(cells, group)) ? read.map((fact) => withRowCells(fact, cells)) : read;
122
130
  return width === undefined ? facts : facts.map((fact) => ({ ...fact, columnGroup: group }));
123
131
  });
124
132
  };
125
133
 
134
+ const groupLabelKey = (cells: readonly Cell[], group: number): string => `${String(group)}\u0000${keyOf(plain(cells[0]?.text ?? ""))}`;
135
+
136
+ /** 一つの表の並びごとに、二つ以上の行に書いた行の見出し。 */
137
+ const repeatedRowLabels = (rows: readonly Line[], width: number | undefined): ReadonlySet<string> => {
138
+ const keys = rows.flatMap((row) =>
139
+ groupsOf(cellsOf(row), width).flatMap((cells, group) => (plain(cells[0]?.text ?? "") === "" ? [] : [groupLabelKey(cells, group)])),
140
+ );
141
+ return new Set(keys.filter((key, index) => keys.indexOf(key) !== index));
142
+ };
143
+
144
+ /**
145
+ * 同じ見出しの行が二つある表の升は、ほかの升(行の見出しと自分の升を除く)も名前に入れる。状態や条件の列が違う二つの行
146
+ * (Enabled with at least 1 と at least 2)は別の行で、値の違いは食い違いではない。ほかの升がすべて同じ行どうしだけを比べる。
147
+ */
148
+ const withRowCells = (fact: TableFact, cells: readonly Cell[]): TableFact => {
149
+ const others = cells.slice(1).filter((cell) => !inside(cell, fact.value));
150
+ return { ...fact, key: [fact.key, ...others.map((cell) => keyOf(plain(cell.text)))].join("\u0000") };
151
+ };
152
+
126
153
  /** 文書の表すべての升の値。 */
127
154
  export const tableFacts = (
128
155
  source: string,
@@ -134,7 +161,8 @@ export const tableFacts = (
134
161
  tablesOf(linesOf(source)).flatMap((table) => {
135
162
  const headings = cellsOf(table.header).map((cell) => plain(cell.text));
136
163
  const width = reading.columnGroups === true ? columnGroupWidth(headings) : undefined;
137
- const at: RowReading = { values, source, ...(totals === undefined ? {} : { totals }), ...(counts === undefined ? {} : { counts }) };
164
+ const repeated = repeatedRowLabels(table.rows, width);
165
+ const at: RowReading = { values, source, repeated, ...(totals === undefined ? {} : { totals }), ...(counts === undefined ? {} : { counts }) };
138
166
  return table.rows.flatMap((row) => groupedRowFacts(row, headings, width, at));
139
167
  });
140
168
 
@@ -0,0 +1,28 @@
1
+ import type { MarkupHeading, Span } from "./plugin.ts";
2
+ import { escapeRegExp } from "./orthography.ts";
3
+
4
+ // Where a document quotes one of its own headings within a line of other words: an article citing itself
5
+ // (常川真央. Machine-actionable DMPs(maDMPs)の動向. カレントアウェアネス.).
6
+
7
+ /** A heading shorter than this is a word or two (概要, (1)), which the text repeats as a word, not as a quoted title. */
8
+ const MIN_TITLE_LENGTH = 8;
9
+
10
+ /** A line's list marker: a line that holds only one and the heading is a table of contents' entry. */
11
+ const LIST_MARKER = /^(?:[-*+]|\d+\.) /u;
12
+
13
+ const lineAround = (source: string, start: number, end: number): string =>
14
+ source.slice(source.lastIndexOf("\n", start - 1) + 1, source.indexOf("\n", end) === -1 ? source.length : source.indexOf("\n", end));
15
+
16
+ /**
17
+ * The places in `source`, outside the heading itself and outside a line that is only the heading (a table of contents),
18
+ * where the text repeats a heading of MIN_TITLE_LENGTH or more that holds one of `marks`.
19
+ */
20
+ export const ownTitleQuoteSpans = (source: string, headings: readonly MarkupHeading[], marks: ReadonlySet<string>): Span[] =>
21
+ headings
22
+ .filter((heading) => heading.text.length >= MIN_TITLE_LENGTH && heading.text.split("").some((char) => marks.has(char)))
23
+ .flatMap((heading) =>
24
+ [...source.matchAll(new RegExp(escapeRegExp(heading.text), "gu"))]
25
+ .map((match) => ({ start: match.index, end: match.index + heading.text.length }))
26
+ .filter((span) => span.start < heading.start || span.start >= heading.end)
27
+ .filter((span) => lineAround(source, span.start, span.end).trim().replace(LIST_MARKER, "").trim() !== heading.text),
28
+ );
@@ -61,13 +61,16 @@ const SINGLE_KANJI = /^\p{Script=Han}$/u;
61
61
  /** loose は最初の一字の大小を問わない(文頭の "Personal data")。exact は書いたとおりだけ("Access" は "early access" に当たらない)。 */
62
62
  type CaseMatch = "loose" | "exact";
63
63
 
64
+ /** 語の間の空白は、続いた空白や改行でもよい(Normal Business\nHours、桁を揃えた写しの Normal Business Hours)。 */
65
+ const spacedLoosely = (escaped: string): string => escaped.replace(/ +/gu, String.raw`\s+`);
66
+
64
67
  /**
65
68
  * 語の現れを探す形。英字で始まる・終わる語は語の途中に当たらない("Term" は "Terms" には当たり、"Termination" には当たらない)。
66
69
  * 複数形と所有の形(Services、Policies、Seller's)は同じ語の使用。
67
70
  */
68
71
  const termPattern = (term: string, caseMatch: CaseMatch): RegExp => {
69
72
  const first = term.charAt(0);
70
- const rest = /\p{Ll}y$/u.test(term) ? `${escapeRegExp(term.slice(1, -1))}(?:y|ie)` : escapeRegExp(term.slice(1));
73
+ const rest = spacedLoosely(/\p{Ll}y$/u.test(term) ? `${escapeRegExp(term.slice(1, -1))}(?:y|ie)` : escapeRegExp(term.slice(1)));
71
74
  const caseless = caseMatch === "exact" || first.toUpperCase() === first.toLowerCase();
72
75
  const head = caseless ? escapeRegExp(first) : `[${escapeRegExp(first.toUpperCase())}${escapeRegExp(first.toLowerCase())}]`;
73
76
  if (SINGLE_KANJI.test(term)) return new RegExp(String.raw`(?<!\p{Script=Han})${head}`, "gu");
@@ -1,7 +1,7 @@
1
1
  import { lengthOf } from "../measure.ts";
2
2
  import type { Lexicon, ProseDocument, Section, Sentence } from "../plugin.ts";
3
3
  import { coefficientOfVariation } from "../detectors/structure.ts";
4
- import { boldLabelItems } from "../detectors/bold-label.ts";
4
+ import { boldLabelItems, isQuotedAt, standingBoldLabelOf } from "../detectors/bold-label.ts";
5
5
  import { MIN_DOCUMENT_LENGTH } from "../detectors/signals.ts";
6
6
  import { bookendOf, headingFormOf, headingWords, proConPairs } from "./heading-words.ts";
7
7
  import { restatementPercent } from "./restatement.ts";
@@ -165,9 +165,25 @@ const threeItemLists = (doc: ProseDocument): FeatureValue => {
165
165
  const emojiHeadings = (doc: ProseDocument): FeatureValue =>
166
166
  measured("emoji-headings", doc.sections.filter((section) => section.depth > 0 && EMOJI.test(section.heading)).length);
167
167
 
168
+ /** The paragraphs and list items, as written, that are not quoted: where a bold label can stand in place of a heading. */
169
+ const blocksOf = (doc: ProseDocument): string[] =>
170
+ [...doc.paragraphs.map((paragraph) => paragraph.span), ...doc.lists.flatMap((list) => list.itemSpans)]
171
+ .filter((span) => !isQuotedAt(doc.source, span.start))
172
+ .map((span) => doc.source.slice(span.start, span.end));
173
+
174
+ /** Bold labels that are a side of a pair and nothing more (`**Pros:**`, `**デメリット:**`): a heading set in bold. */
175
+ const proConLabels = (doc: ProseDocument, lexicon: Lexicon): string[] => {
176
+ const sides = new Set(lexicon.flatMap((entry) => [entry.pattern, entry.instead_of ?? ""]).map((side) => side.toLowerCase()));
177
+ return blocksOf(doc).flatMap((block) => {
178
+ const label = standingBoldLabelOf(block);
179
+ return label !== undefined && sides.has(headingWords(label)) ? [headingWords(label)] : [];
180
+ });
181
+ };
182
+
168
183
  const proCon = (doc: ProseDocument, words: readonly string[]): FeatureValue => {
169
184
  if (words.length < MIN_BOOKEND_HEADINGS) return notMeasured("pro-con", "too-few-headings");
170
- const pairs = proConPairs(words, lexiconOf(doc, "pro-con-heading"));
185
+ const lexicon = lexiconOf(doc, "pro-con-heading");
186
+ const pairs = proConPairs([...words, ...proConLabels(doc, lexicon)], lexicon);
171
187
  return measured("pro-con", pairs.length, pairs.join(", "));
172
188
  };
173
189
 
package/src/word-use.ts CHANGED
@@ -140,9 +140,18 @@ const isCompoundVerbHead = ({ token, following }: InContext): boolean => {
140
140
  /** A verb written straight onto the verb before it (貼り付ける, 書き込む): the tail of a compound verb, not the verb alone (番号をつける). */
141
141
  const isCompoundVerbTail = ({ token, previous }: InContext): boolean => token.pos === "VERB" && previous?.pos === "VERB" && isTouching(previous, token);
142
142
 
143
+ /**
144
+ * A noun joined by や or と to a noun after it (物やサービス): it names a thing alongside the other, as the full noun does, even
145
+ * where the tokenizer reads it as the dependent noun (延長するもの).
146
+ */
147
+ const isCoordinatedThing = ({ token, following }: InContext): boolean => {
148
+ const [joiner, partner] = following ?? [];
149
+ return token.pos === "NOUN" && joiner?.pos === "CCONJ" && partner?.pos === "NOUN" && partner.features?.["NounType"] !== "Dependent";
150
+ };
151
+
143
152
  /** A word used freely, or as a helper: a bound noun after a word is a suffix, and anything else bound a helper. */
144
153
  const helperUseOf = (word: InContext, lightVerbs: ReadonlySet<string>): string => {
145
- if (!isHelper(word) && !isLightVerbAfterQuotative(word, lightVerbs)) return "free";
154
+ if ((!isHelper(word) && !isLightVerbAfterQuotative(word, lightVerbs)) || isCoordinatedThing(word)) return "free";
146
155
  return word.token.pos === "NOUN" && word.token.features?.["NounType"] !== "Dependent" ? "suffix" : "bound";
147
156
  };
148
157