chaffjs 0.23.0 → 0.24.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 (111) hide show
  1. package/dist/detectors/bare-comparative.d.ts +26 -0
  2. package/dist/detectors/bare-comparative.d.ts.map +1 -0
  3. package/dist/detectors/bare-comparative.js +117 -0
  4. package/dist/detectors/bare-comparative.js.map +1 -0
  5. package/dist/detectors/chat-citation.d.ts +6 -0
  6. package/dist/detectors/chat-citation.d.ts.map +1 -1
  7. package/dist/detectors/chat-citation.js +26 -1
  8. package/dist/detectors/chat-citation.js.map +1 -1
  9. package/dist/detectors/clause-shape.d.ts.map +1 -1
  10. package/dist/detectors/clause-shape.js +19 -5
  11. package/dist/detectors/clause-shape.js.map +1 -1
  12. package/dist/detectors/definition-use.d.ts.map +1 -1
  13. package/dist/detectors/definition-use.js +4 -2
  14. package/dist/detectors/definition-use.js.map +1 -1
  15. package/dist/detectors/generic-heading.d.ts +8 -0
  16. package/dist/detectors/generic-heading.d.ts.map +1 -0
  17. package/dist/detectors/generic-heading.js +26 -0
  18. package/dist/detectors/generic-heading.js.map +1 -0
  19. package/dist/detectors/ime-residue.d.ts +13 -0
  20. package/dist/detectors/ime-residue.d.ts.map +1 -0
  21. package/dist/detectors/ime-residue.js +27 -0
  22. package/dist/detectors/ime-residue.js.map +1 -0
  23. package/dist/detectors/lexicon.d.ts.map +1 -1
  24. package/dist/detectors/lexicon.js +9 -2
  25. package/dist/detectors/lexicon.js.map +1 -1
  26. package/dist/detectors/lookalike-character.d.ts +18 -0
  27. package/dist/detectors/lookalike-character.d.ts.map +1 -0
  28. package/dist/detectors/lookalike-character.js +50 -0
  29. package/dist/detectors/lookalike-character.js.map +1 -0
  30. package/dist/detectors/lowercase-name.d.ts +9 -0
  31. package/dist/detectors/lowercase-name.d.ts.map +1 -0
  32. package/dist/detectors/lowercase-name.js +40 -0
  33. package/dist/detectors/lowercase-name.js.map +1 -0
  34. package/dist/detectors/registry/bare-comparative.d.ts +3 -0
  35. package/dist/detectors/registry/bare-comparative.d.ts.map +1 -0
  36. package/dist/detectors/registry/bare-comparative.js +3 -0
  37. package/dist/detectors/registry/bare-comparative.js.map +1 -0
  38. package/dist/detectors/registry/calendar-name-case.d.ts +3 -0
  39. package/dist/detectors/registry/calendar-name-case.d.ts.map +1 -0
  40. package/dist/detectors/registry/calendar-name-case.js +3 -0
  41. package/dist/detectors/registry/calendar-name-case.js.map +1 -0
  42. package/dist/detectors/registry/generic-heading.d.ts +3 -0
  43. package/dist/detectors/registry/generic-heading.d.ts.map +1 -0
  44. package/dist/detectors/registry/generic-heading.js +3 -0
  45. package/dist/detectors/registry/generic-heading.js.map +1 -0
  46. package/dist/detectors/registry/ime-residue.d.ts +3 -0
  47. package/dist/detectors/registry/ime-residue.d.ts.map +1 -0
  48. package/dist/detectors/registry/ime-residue.js +3 -0
  49. package/dist/detectors/registry/ime-residue.js.map +1 -0
  50. package/dist/detectors/registry/lookalike-character.d.ts +3 -0
  51. package/dist/detectors/registry/lookalike-character.d.ts.map +1 -0
  52. package/dist/detectors/registry/lookalike-character.js +3 -0
  53. package/dist/detectors/registry/lookalike-character.js.map +1 -0
  54. package/dist/detectors/registry/unit-spacing-consistency.d.ts +3 -0
  55. package/dist/detectors/registry/unit-spacing-consistency.d.ts.map +1 -0
  56. package/dist/detectors/registry/unit-spacing-consistency.js +3 -0
  57. package/dist/detectors/registry/unit-spacing-consistency.js.map +1 -0
  58. package/dist/detectors/signals.d.ts.map +1 -1
  59. package/dist/detectors/signals.js +8 -4
  60. package/dist/detectors/signals.js.map +1 -1
  61. package/dist/detectors/structure-tree.d.ts.map +1 -1
  62. package/dist/detectors/structure-tree.js +32 -7
  63. package/dist/detectors/structure-tree.js.map +1 -1
  64. package/dist/detectors/technical-docs.d.ts.map +1 -1
  65. package/dist/detectors/technical-docs.js +14 -0
  66. package/dist/detectors/technical-docs.js.map +1 -1
  67. package/dist/detectors/unit-spacing.d.ts +25 -0
  68. package/dist/detectors/unit-spacing.d.ts.map +1 -0
  69. package/dist/detectors/unit-spacing.js +50 -0
  70. package/dist/detectors/unit-spacing.js.map +1 -0
  71. package/dist/quoted-span.d.ts +7 -0
  72. package/dist/quoted-span.d.ts.map +1 -1
  73. package/dist/quoted-span.js +20 -0
  74. package/dist/quoted-span.js.map +1 -1
  75. package/dist/structure/definition-use.d.ts +14 -4
  76. package/dist/structure/definition-use.d.ts.map +1 -1
  77. package/dist/structure/definition-use.js +29 -9
  78. package/dist/structure/definition-use.js.map +1 -1
  79. package/package.json +3 -3
  80. package/rules/calendar-name-case.yaml +55 -0
  81. package/rules/comparative-without-baseline.yaml +71 -0
  82. package/rules/cross-doc-broken-link.yaml +1 -1
  83. package/rules/cross-doc-duplicate-definition.yaml +1 -1
  84. package/rules/cross-doc-fact-conflict.yaml +1 -1
  85. package/rules/cross-doc-term-variant.yaml +5 -3
  86. package/rules/ime-residue.yaml +55 -0
  87. package/rules/lookalike-character.yaml +82 -0
  88. package/rules/numbered-generic-heading.yaml +111 -0
  89. package/rules/undefined-acronym.yaml +1 -0
  90. package/rules/unit-spacing-consistency.yaml +74 -0
  91. package/src/detectors/bare-comparative.ts +152 -0
  92. package/src/detectors/chat-citation.ts +28 -1
  93. package/src/detectors/clause-shape.ts +20 -6
  94. package/src/detectors/definition-use.ts +13 -3
  95. package/src/detectors/generic-heading.ts +30 -0
  96. package/src/detectors/ime-residue.ts +37 -0
  97. package/src/detectors/lexicon.ts +11 -2
  98. package/src/detectors/lookalike-character.ts +75 -0
  99. package/src/detectors/lowercase-name.ts +58 -0
  100. package/src/detectors/registry/bare-comparative.ts +4 -0
  101. package/src/detectors/registry/calendar-name-case.ts +4 -0
  102. package/src/detectors/registry/generic-heading.ts +4 -0
  103. package/src/detectors/registry/ime-residue.ts +4 -0
  104. package/src/detectors/registry/lookalike-character.ts +4 -0
  105. package/src/detectors/registry/unit-spacing-consistency.ts +4 -0
  106. package/src/detectors/signals.ts +9 -5
  107. package/src/detectors/structure-tree.ts +55 -30
  108. package/src/detectors/technical-docs.ts +13 -0
  109. package/src/detectors/unit-spacing.ts +71 -0
  110. package/src/quoted-span.ts +25 -0
  111. package/src/structure/definition-use.ts +42 -10
@@ -0,0 +1,37 @@
1
+ import type { Detector, Finding } from "../plugin.ts";
2
+ import { quoteAround } from "./quote-around.ts";
3
+
4
+ // A single Latin letter left inside a Japanese word by the input method (おもしrおい, 対応sた): a key pressed and not converted.
5
+
6
+ /** One stray letter, and where it is in the text. */
7
+ export type StrayLetter = { readonly offset: number; readonly letter: string };
8
+
9
+ // Hiragana or kanji before (after katakana a letter is a label: サイズm, プランb), kana after.
10
+ const STRAY = /(?<=[\p{Script=Hiragana}\p{Script=Han}々])[a-z](?=[\p{Script=Hiragana}\p{Script=Katakana}ー])/gu;
11
+ /** Marks that make the run around the letter a path, an address or an assignment (/docs/あaい, a=b). */
12
+ const PATH_MARK = /[/\\=@#]/u;
13
+
14
+ const runBefore = (text: string, offset: number): string => text.slice(0, offset).split(/\s/u).at(-1) ?? "";
15
+
16
+ /**
17
+ * One lower-case letter between Japanese: hiragana or kanji before, kana after. A letter followed by a particle names
18
+ * something (変数xが, まずxを, をxで), so it is left alone; the particles come from the lexicon.
19
+ */
20
+ export const strayLettersIn = (text: string, particles: readonly string[]): StrayLetter[] =>
21
+ [...text.matchAll(STRAY)].flatMap((match) => {
22
+ const namesSomething = particles.some((particle) => text.startsWith(particle, match.index + 1));
23
+ return namesSomething || PATH_MARK.test(runBefore(text, match.index)) ? [] : [{ offset: match.index, letter: match[0] }];
24
+ });
25
+
26
+ export const imeResidue: Detector = (doc): Finding[] => {
27
+ const text = doc.prose ?? doc.source;
28
+ const particles = (doc.lexicons["case-particle"] ?? []).map((entry) => entry.pattern);
29
+ return strayLettersIn(text, particles).map((stray) => ({
30
+ rule: "",
31
+ severity: "warning",
32
+ line: 0,
33
+ column: 0,
34
+ quote: quoteAround(text, stray.offset, stray.offset + 1),
35
+ values: { letter: stray.letter, offset: stray.offset },
36
+ }));
37
+ };
@@ -212,15 +212,24 @@ export const repeatedConjunction: Detector = (doc, options): Finding[] => {
212
212
  });
213
213
  };
214
214
 
215
+ /**
216
+ * 書き手が書いた語。引用符の中だけの語(words such as "delves" and "pivotal")は、例に挙げたか人の言葉を引いたもの。
217
+ * 品詞があれば当たった語の並びごとに見る(活用した形も原形で当たる)。無ければ字で照らす。
218
+ */
219
+ const writtenIn = (sentence: Sentence, entry: LexiconEntry): boolean =>
220
+ sentence.tokens === undefined || entry.tokens === undefined || entry.tokens.length === 0
221
+ ? entryIn(sentence, entry) && !onlyQuoted(sentence, entry)
222
+ : entryRanges(sentence, entry).some((range) => !quotedRange(sentence, range));
223
+
215
224
  /**
216
225
  * AI 生成の signal。1 つでは何も言えないので、重みを足し合わせて文書の点にする。
217
226
  * 単独で断じない。§20.2 の複合シグナルの入口で、これだけで「AI が書いた」とは言わない。
218
227
  */
219
228
  export const aiTell: Detector = (doc, options): Finding[] => {
220
229
  const entries = [...new Map((options.lexicon ?? []).map((entry) => [entry.pattern.toLowerCase(), entry])).values()];
221
- const found = entries.filter((entry) => doc.sentences.some((sentence) => entryIn(sentence, entry)));
230
+ const found = entries.filter((entry) => doc.sentences.some((sentence) => writtenIn(sentence, entry)));
222
231
  const score = Math.round(found.reduce((sum, entry) => sum + (entry.weight ?? 1), 0) * 10);
223
- const first = doc.sentences.find((sentence) => found.some((entry) => entryIn(sentence, entry)));
232
+ const first = doc.sentences.find((sentence) => found.some((entry) => writtenIn(sentence, entry)));
224
233
  if (first === undefined || score <= options.limit) return [];
225
234
  return [
226
235
  {
@@ -0,0 +1,75 @@
1
+ import type { Detector, Finding, Span } from "../plugin.ts";
2
+ import { codePointName } from "./invisible-character.ts";
3
+
4
+ /**
5
+ * A character that looks like a common one but is another code point, so search, sorting and a dictionary miss it.
6
+ * radical: a Kangxi or supplementary radical (⼈ U+2F08 for 人), from PDF text or an input method's radical list.
7
+ * compatibility: a CJK compatibility ideograph (or one of its supplement) that Unicode maps to a unified one (樂 U+F914 for 樂).
8
+ * decomposed: a letter and a separate combining mark (か + U+3099 for が, e + U+0301 for é), from file names or text
9
+ * copied on macOS. spacing-mark: kana followed by the spacing ゛ or ゜, typed in place of the combined letter.
10
+ */
11
+ export type LookalikeKind = "radical" | "compatibility" | "decomposed" | "spacing-mark";
12
+
13
+ export type Lookalike = { readonly span: Span; readonly kind: LookalikeKind; readonly written: string; readonly usual: string };
14
+
15
+ const RADICAL = /[\u2E80-\u2EFF\u2F00-\u2FDF]/u;
16
+ const COMPATIBILITY = /[\uF900-\uFAFF\u{2F800}-\u{2FA1F}]/u;
17
+ /** A letter or mark base and the combining marks after it. */
18
+ const WITH_MARKS = /\P{M}\p{M}+/gu;
19
+ /** Kana followed by the spacing voiced or semi-voiced mark (か゛, は゜). */
20
+ const SPACING_MARK = /[\p{Script=Hiragana}\p{Script=Katakana}][゛゜]/gu;
21
+ const COMBINING_OF: Readonly<Record<string, string>> = { "゛": "゙", "゜": "゚" };
22
+ const SINGLE_CHAR = /[\u2E80-\u2EFF\u2F00-\u2FDF\uF900-\uFAFF\u{2F800}-\u{2FA1F}]/gu;
23
+
24
+ /** A radical or compatibility ideograph, with the unified ideograph it stands for. One that maps to nothing is left alone. */
25
+ const singleLookalikes = (source: string): Lookalike[] =>
26
+ [...source.matchAll(SINGLE_CHAR)].flatMap((match) => {
27
+ const written = match[0];
28
+ const radical = RADICAL.test(written);
29
+ const usual = written.normalize(radical ? "NFKC" : "NFC");
30
+ if (usual === written || (!radical && !COMPATIBILITY.test(written))) return [];
31
+ return [{ span: { start: match.index, end: match.index + written.length }, kind: radical ? "radical" : "compatibility", written, usual }];
32
+ });
33
+
34
+ /** A base and combining marks that compose into one precomposed letter. Marks with no precomposed form are writing, not a slip. */
35
+ const decomposedLookalikes = (source: string): Lookalike[] =>
36
+ [...source.matchAll(WITH_MARKS)].flatMap((match) => {
37
+ const usual = match[0].normalize("NFC");
38
+ if (Array.from(usual).length >= Array.from(match[0]).length) return [];
39
+ return [{ span: { start: match.index, end: match.index + match[0].length }, kind: "decomposed", written: match[0], usual }];
40
+ });
41
+
42
+ /** Kana and a spacing ゛ or ゜ whose combining form makes one letter (か゛ → が). */
43
+ const spacingMarkLookalikes = (source: string): Lookalike[] =>
44
+ [...source.matchAll(SPACING_MARK)].flatMap((match) => {
45
+ const [kana = "", mark = ""] = Array.from(match[0]);
46
+ const usual = `${kana}${COMBINING_OF[mark] ?? ""}`.normalize("NFC");
47
+ if (Array.from(usual).length !== 1) return [];
48
+ return [{ span: { start: match.index, end: match.index + match[0].length }, kind: "spacing-mark", written: match[0], usual }];
49
+ });
50
+
51
+ export const lookalikesIn = (source: string): Lookalike[] =>
52
+ [...singleLookalikes(source), ...decomposedLookalikes(source), ...spacingMarkLookalikes(source)].toSorted(
53
+ (left, right) => left.span.start - right.span.start,
54
+ );
55
+
56
+ const QUOTE_REACH = 20;
57
+
58
+ const quoteOf = (source: string, span: Span): string =>
59
+ source
60
+ .slice(Math.max(0, span.start - QUOTE_REACH), span.end + QUOTE_REACH)
61
+ .replace(/\s+/gu, " ")
62
+ .trim();
63
+
64
+ const codesOf = (text: string): string => Array.from(text).map(codePointName).join(" ");
65
+
66
+ export const lookalikeCharacter: Detector = (doc): Finding[] =>
67
+ lookalikesIn(doc.source).map((lookalike) => ({
68
+ rule: "",
69
+ severity: "warning",
70
+ line: 0,
71
+ column: 0,
72
+ quote: quoteOf(doc.source, lookalike.span),
73
+ values: { written: lookalike.written, usual: lookalike.usual, code: codesOf(lookalike.written), offset: lookalike.span.start },
74
+ variant: lookalike.kind,
75
+ }));
@@ -0,0 +1,58 @@
1
+ import type { Detector, Finding } from "../plugin.ts";
2
+ import { quoteAround } from "./quote-around.ts";
3
+
4
+ // A word English always capitalises (a weekday, a month) written in lower case ("on monday"). The words come from the rule's
5
+ // lexicon "calendar-name"; the pattern is the capitalised form.
6
+
7
+ export type LowercaseName = { readonly offset: number; readonly written: string; readonly usual: string };
8
+
9
+ /** Characters that make the word part of a name in code, a path, an address or a tag (monday.md, /july/, @friday, #june). */
10
+ const NOT_PROSE_BESIDE = /[\p{L}\p{N}_/.@#:=-]/u;
11
+
12
+ /** A period or colon ends the sentence or clause unless a name continues after it (monday.md, june:2). */
13
+ const SENTENCE_STOP = /[.:]/u;
14
+ const WORD_CHAR = /[\p{L}\p{N}_]/u;
15
+
16
+ const joinsAfter = (text: string, end: number): boolean =>
17
+ SENTENCE_STOP.test(text.charAt(end)) ? WORD_CHAR.test(text.charAt(end + 1)) : NOT_PROSE_BESIDE.test(text.charAt(end));
18
+
19
+ /** A value after a key or a field marker (day: monday, day = monday, "day": "monday", * * monday) is data, not prose. */
20
+ const VALUE_MARK = /[:=*]/u;
21
+ const STRAIGHT_QUOTE = /["']/u;
22
+ const SPACE_OR_QUOTE = /[\s"']/u;
23
+ /** How far back to look past spaces and quotes for the key's mark. */
24
+ const VALUE_REACH = 8;
25
+
26
+ const markBefore = (text: string, start: number): string =>
27
+ Array.from(text.slice(Math.max(0, start - VALUE_REACH), start))
28
+ .reverse()
29
+ .find((char) => !SPACE_OR_QUOTE.test(char)) ?? "";
30
+
31
+ const isValue = (text: string, start: number): boolean => STRAIGHT_QUOTE.test(text.charAt(start - 1)) || VALUE_MARK.test(markBefore(text, start));
32
+
33
+ const capitalOf = (word: string): string => `${word.charAt(0).toUpperCase()}${word.slice(1)}`;
34
+
35
+ export const lowercaseNamesIn = (text: string, names: readonly string[]): LowercaseName[] => {
36
+ const lower = new Map(names.map((name) => [name.toLowerCase(), capitalOf(name)]));
37
+ return [...text.matchAll(/\p{Ll}+/gu)].flatMap((match) => {
38
+ const usual = lower.get(match[0]);
39
+ const end = match.index + match[0].length;
40
+ if (usual === undefined || NOT_PROSE_BESIDE.test(text.charAt(match.index - 1)) || isValue(text, match.index) || joinsAfter(text, end)) return [];
41
+ return [{ offset: match.index, written: match[0], usual }];
42
+ });
43
+ };
44
+
45
+ export const lowercaseName: Detector = (doc): Finding[] => {
46
+ const text = doc.prose ?? doc.source;
47
+ return lowercaseNamesIn(
48
+ text,
49
+ (doc.lexicons["calendar-name"] ?? []).map((entry) => entry.pattern),
50
+ ).map((name) => ({
51
+ rule: "",
52
+ severity: "warning",
53
+ line: 0,
54
+ column: 0,
55
+ quote: quoteAround(text, name.offset, name.offset + name.written.length),
56
+ values: { written: name.written, usual: name.usual, offset: name.offset },
57
+ }));
58
+ };
@@ -0,0 +1,4 @@
1
+ import type { Detector } from "../../plugin.ts";
2
+ import { bareComparative } from "../bare-comparative.ts";
3
+
4
+ export const detector: Detector = bareComparative;
@@ -0,0 +1,4 @@
1
+ import type { Detector } from "../../plugin.ts";
2
+ import { lowercaseName } from "../lowercase-name.ts";
3
+
4
+ export const detector: Detector = lowercaseName;
@@ -0,0 +1,4 @@
1
+ import type { Detector } from "../../plugin.ts";
2
+ import { genericHeading } from "../generic-heading.ts";
3
+
4
+ export const detector: Detector = genericHeading;
@@ -0,0 +1,4 @@
1
+ import type { Detector } from "../../plugin.ts";
2
+ import { imeResidue } from "../ime-residue.ts";
3
+
4
+ export const detector: Detector = imeResidue;
@@ -0,0 +1,4 @@
1
+ import type { Detector } from "../../plugin.ts";
2
+ import { lookalikeCharacter } from "../lookalike-character.ts";
3
+
4
+ export const detector: Detector = lookalikeCharacter;
@@ -0,0 +1,4 @@
1
+ import type { Detector } from "../../plugin.ts";
2
+ import { unitSpacing } from "../unit-spacing.ts";
3
+
4
+ export const detector: Detector = unitSpacing;
@@ -11,6 +11,7 @@ import { evidenceSpans, hasNumeral, startsWithin } from "./concrete-evidence.ts"
11
11
  import { letteredIndexEntries } from "./lettered-index.ts";
12
12
  import { hasPredicateIn } from "./gram-predicate.ts";
13
13
  import { nameSpans, touchesAny } from "../team-names.ts";
14
+ import { referenceListSpans } from "../reference-lists.ts";
14
15
  import type { Detector, Finding, ProseDocument, Section, Sentence } from "../plugin.ts";
15
16
 
16
17
  const PER = 1000;
@@ -250,9 +251,12 @@ const firstHits = (hits: readonly AcronymHit[]): Map<string, Hit> => {
250
251
  return seen;
251
252
  };
252
253
 
253
- /** 日付の中の月(SEP 01, 2022)は略語ではない。 */
254
- const outsideDates = (hits: readonly AcronymHit[], dates: readonly Span[]): AcronymHit[] =>
255
- hits.filter(({ hit }) => !dates.some((span) => span.start <= hit.offset && hit.offset < span.end));
254
+ /**
255
+ * 範囲の外の略語。日付の中の月(SEP 01, 2022)は略語ではない。文献一覧の誌名や会議名(Appeared in: LREC 2008.)は
256
+ * 引いた文献の書き方で、本文が説明する略語ではない。
257
+ */
258
+ const outsideSpans = (hits: readonly AcronymHit[], spans: readonly Span[]): AcronymHit[] =>
259
+ hits.filter(({ hit }) => !spans.some((span) => span.start <= hit.offset && hit.offset < span.end));
256
260
 
257
261
  /**
258
262
  * どこか 1 か所で展開してあればよい。初出が節の見出し代わりの語(「5.3. DPA.」)で、
@@ -300,11 +304,11 @@ export const undefinedAcronym: Detector = (doc, options): Finding[] => {
300
304
  const headings = doc.sections.map((section) => section.heading);
301
305
  const explainedAlone = (word: string): boolean =>
302
306
  common.has(word) || entries.has(word) || isExpanded(body, word, expandedAt) || headings.some((heading) => expandsInHeading(heading, word, expandedAt));
303
- const hits = acronymsOf(doc, notationOf(doc));
307
+ const hits = outsideSpans(acronymsOf(doc, notationOf(doc)), referenceListSpans(doc.source, patternsOf(doc, "reference-list-heading")));
304
308
  const unexplained = new Set([...firstHits(hits).keys()].filter((acronym) => !isExplained(acronym, explainedAlone)));
305
309
  // 日付を読むには文書の木を作る。上限に届かない文書では作らない。
306
310
  if (unexplained.size < options.limit) return [];
307
- const bare = [...firstHits(outsideDates(hits, dateSpans(doc)))].filter(([acronym]) => unexplained.has(acronym));
311
+ const bare = [...firstHits(outsideSpans(hits, dateSpans(doc)))].filter(([acronym]) => unexplained.has(acronym));
308
312
  if (bare.length < options.limit) return [];
309
313
  return bare.map(([acronym, hit]) => ({
310
314
  rule: "undefined-acronym",
@@ -6,6 +6,7 @@ import { dateOrderBreaks } from "../structure/date-order.ts";
6
6
  import { totalMismatches, type Amount } from "../structure/total.ts";
7
7
  import { rangeFrameOf, reversedRanges, type DatedSpan, type RangeWords } from "../structure/date-range.ts";
8
8
  import { percentSumMismatches, type ShareWords } from "../structure/percent-sum.ts";
9
+ import { isQuotedAlone } from "../quoted-span.ts";
9
10
 
10
11
  const QUOTE_LENGTH = 80;
11
12
 
@@ -42,22 +43,43 @@ const dayName = (doc: ProseDocument, day: unknown): string => {
42
43
  return doc.lexicons["weekday"]?.[index]?.pattern ?? String(index);
43
44
  };
44
45
 
45
- export const dateWeekdayMismatch: Detector = (doc): Finding[] =>
46
- doc.structure === undefined
47
- ? []
48
- : weekdayMismatches(doc.structure).map((issue) => ({
49
- rule: "date-weekday-mismatch",
50
- severity: "error",
51
- line: 0,
52
- column: 0,
53
- quote: quoteAt(doc.source, issue.offset),
54
- values: {
55
- date: String(issue.values["date"]),
56
- written: dayName(doc, issue.values["written"]),
57
- actual: dayName(doc, issue.values["actual"]),
58
- offset: issue.offset,
59
- },
60
- }));
46
+ const datedSpans = (tree: NonNullable<ProseDocument["structure"]>): DatedSpan[] =>
47
+ inDocumentOrder(tree).flatMap((node) => (node.kind === "date" ? [{ offset: node.span.start, end: node.span.end, value: String(node.attrs["value"]) }] : []));
48
+
49
+ /** 引用符の中で日付の横にあってよい語は、曜日の一語だけ("Monday, December 5, 2026"、「2026年12月5日(月)」)。 */
50
+ const WEEKDAY_WORDS = 1;
51
+
52
+ /**
53
+ * 引用符で日付だけを挙げたもの("Monday, December 5, 2026" falls on a Saturday)は、誤りの例で、文書の日付ではない。
54
+ * from から until の日付の終わりまでを見る。
55
+ */
56
+ const quotedDates = (doc: ProseDocument, from: DatedSpan | undefined, until: DatedSpan | undefined, wordsBesides = 0): boolean =>
57
+ from !== undefined && until !== undefined && isQuotedAlone(doc.source, { start: from.offset, end: until.end }, wordsBesides);
58
+
59
+ const dateAt = (dates: readonly DatedSpan[], offset: number): number => dates.findIndex((date) => date.offset === offset);
60
+
61
+ export const dateWeekdayMismatch: Detector = (doc): Finding[] => {
62
+ if (doc.structure === undefined) return [];
63
+ const dates = datedSpans(doc.structure);
64
+ return weekdayMismatches(doc.structure)
65
+ .filter((issue) => {
66
+ const date = dates[dateAt(dates, issue.offset)];
67
+ return !quotedDates(doc, date, date, WEEKDAY_WORDS);
68
+ })
69
+ .map((issue) => ({
70
+ rule: "date-weekday-mismatch",
71
+ severity: "error",
72
+ line: 0,
73
+ column: 0,
74
+ quote: quoteAt(doc.source, issue.offset),
75
+ values: {
76
+ date: String(issue.values["date"]),
77
+ written: dayName(doc, issue.values["written"]),
78
+ actual: dayName(doc, issue.values["actual"]),
79
+ offset: issue.offset,
80
+ },
81
+ }));
82
+ };
61
83
 
62
84
  /** 木の日付を、原文の位置と一緒に並べる。 */
63
85
  const datedPoints = (tree: NonNullable<ProseDocument["structure"]>): { offset: number; value: string }[] =>
@@ -99,9 +121,6 @@ export const totalMismatch: Detector = (doc): Finding[] =>
99
121
  values: { ...issue.values, offset: issue.offset },
100
122
  }));
101
123
 
102
- const datedSpans = (tree: NonNullable<ProseDocument["structure"]>): DatedSpan[] =>
103
- inDocumentOrder(tree).flatMap((node) => (node.kind === "date" ? [{ offset: node.span.start, end: node.span.end, value: String(node.attrs["value"]) }] : []));
104
-
105
124
  const rangeWordsOf = (doc: ProseDocument): RangeWords => ({
106
125
  connectors: (doc.lexicons["range-connector"] ?? []).map((entry) => entry.pattern),
107
126
  openers: (doc.lexicons["range-opener"] ?? []).map((entry) => entry.pattern),
@@ -111,17 +130,23 @@ const rangeWordsOf = (doc: ProseDocument): RangeWords => ({
111
130
  });
112
131
 
113
132
  /** 期間の終わりが始まりより前。範囲の記号と語は言語パッケージの語彙表(range-connector、range-opener、range-closer)から取る。 */
114
- export const dateRangeReversed: Detector = (doc): Finding[] =>
115
- doc.structure === undefined
116
- ? []
117
- : reversedRanges(doc.source, datedSpans(doc.structure), rangeWordsOf(doc)).map((issue) => ({
118
- rule: "date-range-reversed",
119
- severity: "warning",
120
- line: 0,
121
- column: 0,
122
- quote: quoteAt(doc.source, issue.offset),
123
- values: { ...issue.values, offset: issue.offset },
124
- }));
133
+ export const dateRangeReversed: Detector = (doc): Finding[] => {
134
+ if (doc.structure === undefined) return [];
135
+ const dates = datedSpans(doc.structure);
136
+ return reversedRanges(doc.source, dates, rangeWordsOf(doc))
137
+ .filter((issue) => {
138
+ const start = dateAt(dates, issue.offset);
139
+ return !quotedDates(doc, dates[start], dates[start + 1]);
140
+ })
141
+ .map((issue) => ({
142
+ rule: "date-range-reversed",
143
+ severity: "warning",
144
+ line: 0,
145
+ column: 0,
146
+ quote: quoteAt(doc.source, issue.offset),
147
+ values: { ...issue.values, offset: issue.offset },
148
+ }));
149
+ };
125
150
 
126
151
  const shareWordsOf = (doc: ProseDocument): ShareWords => ({
127
152
  labels: (doc.lexicons["share-label"] ?? []).map((entry) => entry.pattern),
@@ -4,6 +4,7 @@
4
4
  // shell-prompt lexicons.
5
5
  import { codeFences, codeSpans, type CodeFence } from "./code-fences.ts";
6
6
  import { quoteAt } from "./structure-tree.ts";
7
+ import { escapeRegExp } from "../orthography.ts";
7
8
  import type { Detector, Finding, ProseDocument, Span, Token } from "../plugin.ts";
8
9
 
9
10
  const patternsOf = (doc: ProseDocument, id: string): string[] => (doc.lexicons[id] ?? []).map((entry) => entry.pattern);
@@ -112,10 +113,22 @@ export const commandsIn = (heading: string): string[] => {
112
113
  const collapse = (text: string): string => text.replace(/\s+/gu, " ");
113
114
  const OPTION_CHAR = /[A-Za-z0-9_-]/u;
114
115
 
116
+ /**
117
+ * A command with arguments whose program the code runs under its package's name: chaff grade as npx chaffjs grade,
118
+ * python -m venv as python3 -m venv. The arguments must follow as written.
119
+ */
120
+ const showsUnderPackageName = (text: string, wanted: string): boolean => {
121
+ const [program = "", ...rest] = wanted.split(" ");
122
+ if (rest.length === 0) return false;
123
+ const pattern = `(?<![A-Za-z0-9_.-])${escapeRegExp(program)}[A-Za-z0-9_.-]* ${rest.map(escapeRegExp).join(" ")}(?![A-Za-z0-9_-])`;
124
+ return new RegExp(pattern, "u").test(text);
125
+ };
126
+
115
127
  /** Whether the code holds the command as a whole word: --force is not --force-with-lease, init is not initial. */
116
128
  export const showsCommand = (code: string, command: string): boolean => {
117
129
  const text = collapse(code);
118
130
  const wanted = collapse(command.trim());
131
+ if (showsUnderPackageName(text, wanted)) return true;
119
132
  const state = { at: text.indexOf(wanted) };
120
133
  while (state.at !== -1) {
121
134
  const before = text.charAt(state.at - 1);
@@ -0,0 +1,71 @@
1
+ import type { Detector, Finding } from "../plugin.ts";
2
+ import { escapeRegExp } from "../orthography.ts";
3
+ import { formMinority } from "./form-minority.ts";
4
+ import { quoteAround } from "./quote-around.ts";
5
+
6
+ // One document writing a number and a unit symbol both with a space between them ("5 GB") and without ("5GB"). Neither is
7
+ // called right; per unit, the less common way is reported. The symbols come from each language's lexicon "unit-symbol".
8
+
9
+ /** A number and its unit as written. form is "spaced" or "touching". */
10
+ export type WrittenQuantity = { readonly offset: number; readonly written: string; readonly unit: string; readonly form: string };
11
+
12
+ /** The less common way for one unit, with an example of the usual way and how many quantities of that unit there are. */
13
+ export type UnitMinority = { readonly odd: WrittenQuantity; readonly usual: string; readonly count: number; readonly of: number };
14
+
15
+ // Grouping commas, then at most one decimal point: 1.2.3 is a version, not a number.
16
+ const NUMBER = "[0-9]+(?:,[0-9]{3})*(?:\\.[0-9]+)?";
17
+ // A plain space, a no-break space or a narrow no-break space (the SI brochure's thin space between number and unit).
18
+ const GAP = "[ \\u00A0\\u202F]?";
19
+
20
+ /**
21
+ * Numbers followed by a unit symbol, at most one space between. The number must not be part of a word, a version or an
22
+ * address (v1.2GB, x86, 1.2.3GB), nor follow a hyphen: "10-8 cm" is as often a lost exponent as a range (5–10 GB, with
23
+ * an en dash, is read). The symbol must not run on into a word, a rate or a file name (5 GBit, 5 GB/s, 5GB.zip).
24
+ */
25
+ export const quantitiesIn = (text: string, symbols: readonly string[]): WrittenQuantity[] => {
26
+ if (symbols.length === 0) return [];
27
+ const units = symbols
28
+ .toSorted((left, right) => right.length - left.length)
29
+ .map(escapeRegExp)
30
+ .join("|");
31
+ const pattern = new RegExp(`(?<![A-Za-z0-9_.,/:#-])${NUMBER}(?<gap>${GAP})(?<unit>${units})(?![A-Za-z0-9_/]|\\.[A-Za-z0-9_])`, "gu");
32
+ return [...text.matchAll(pattern)].map((match) => ({
33
+ offset: match.index,
34
+ written: match[0],
35
+ unit: match.groups?.["unit"] ?? "",
36
+ form: match.groups?.["gap"] === "" ? "touching" : "spaced",
37
+ }));
38
+ };
39
+
40
+ const minorityOf = (ofUnit: readonly WrittenQuantity[], limit: number): UnitMinority[] => {
41
+ const minority = formMinority(ofUnit, limit);
42
+ if (minority === undefined) return [];
43
+ return minority.odd.map((quantity) => ({ odd: quantity, usual: minority.usual.written, count: minority.odd.length, of: ofUnit.length }));
44
+ };
45
+
46
+ /** Per unit, the quantities written the less common way. "5 GB" and "500ms" in one document are two units, each consistent. */
47
+ export const unitMinorities = (quantities: readonly WrittenQuantity[], limit: number): UnitMinority[] =>
48
+ [...new Set(quantities.map((quantity) => quantity.unit))]
49
+ .flatMap((unit) =>
50
+ minorityOf(
51
+ quantities.filter((quantity) => quantity.unit === unit),
52
+ limit,
53
+ ),
54
+ )
55
+ .toSorted((left, right) => left.odd.offset - right.odd.offset);
56
+
57
+ export const unitSpacing: Detector = (doc, options): Finding[] => {
58
+ const text = doc.prose ?? doc.source;
59
+ const quantities = quantitiesIn(
60
+ text,
61
+ (doc.lexicons["unit-symbol"] ?? []).map((entry) => entry.pattern),
62
+ );
63
+ return unitMinorities(quantities, options.limit).map(({ odd, usual, count, of }) => ({
64
+ rule: "",
65
+ severity: "info",
66
+ line: 0,
67
+ column: 0,
68
+ quote: quoteAround(text, odd.offset, odd.offset + odd.written.length),
69
+ values: { written: odd.written, usual, count, of, offset: odd.offset },
70
+ }));
71
+ };
@@ -41,5 +41,30 @@ export const quotedSpans = (text: string, marks: Marks = KAGI_MARKS): Span[] =>
41
41
  export const quotedIn = (sentence: Sentence, marks: Marks = KAGI_MARKS): Span[] =>
42
42
  quotedSpans(sentence.text, marks).map((span) => ({ start: sentence.span.start + span.start - 1, end: sentence.span.start + span.end + 1 }));
43
43
 
44
+ const MARKS = /[\s\p{P}]+/u;
45
+ const LETTERS = /^\p{L}+$/u;
46
+
47
+ /** 記号と空白のあいだに残る語が、字だけの語で、多くとも allowed 個か。 */
48
+ const holdsAtMost = (text: string, allowed: number): boolean => {
49
+ const words = text.split(MARKS).filter((word) => word !== "");
50
+ return words.length <= allowed && words.every((word) => LETTERS.test(word));
51
+ };
52
+
53
+ /**
54
+ * span だけを引いた引用符("delves"、"?utm_source=chatgpt.com"、"October 26, 2026 – October 19, 2026")の中か。
55
+ * 引用符が語だけを囲めば、その語は使ったのではなく例として挙げたもの。周りの記号と空白は語の一部に数えない。
56
+ * wordsBesides は、span のほかに引用符の中にあってよい語の数(日付なら曜日の一語)。
57
+ * 話した言葉の引用("We ship on Monday, October 26," she said.)は語のほかの字を持つので当たらない。引用は一行の中だけを見る。
58
+ */
59
+ export const isQuotedAlone = (text: string, span: Span, wordsBesides = 0): boolean => {
60
+ const lineStart = text.lastIndexOf("\n", span.start - 1) + 1;
61
+ const newline = text.indexOf("\n", span.end);
62
+ const line = text.slice(lineStart, newline === -1 ? text.length : newline);
63
+ const [start, end] = [span.start - lineStart, span.end - lineStart];
64
+ return quotedSpans(line, QUOTATION_MARKS).some(
65
+ (quoted) => quoted.start <= start && end <= quoted.end && holdsAtMost(`${line.slice(quoted.start, start)} ${line.slice(end, quoted.end)}`, wordsBesides),
66
+ );
67
+ };
68
+
44
69
  /** inner が、spans のどれかにまるごと入っているか。 */
45
70
  export const isWithinAny = (spans: readonly Span[], inner: Span): boolean => spans.some((span) => inner.start >= span.start && inner.end <= span.end);
@@ -4,7 +4,13 @@ import { inDocumentOrder } from "./issues.ts";
4
4
 
5
5
  // 定義した語が本文で使われているか、定義より前に使われていないか。どちらも木の定義と、本文の文の字面だけで決まる。
6
6
 
7
- export type DefinedTerm = { readonly term: string; readonly span: Span; readonly line: number; readonly inline: boolean };
7
+ /** bare は、定義の語(means・以下…という・the)の無い括弧の引用(("Seller"))。例を引く括弧と同じ形。 */
8
+ export type DefinedTerm = { readonly term: string; readonly span: Span; readonly line: number; readonly inline: boolean; readonly bare?: boolean };
9
+
10
+ /** 文書の位置 [start, end) の語が、使ったのではなく引用符で例として挙げただけか。 */
11
+ export type Mentioned = (start: number, end: number) => boolean;
12
+
13
+ const NEVER_MENTIONED: Mentioned = () => false;
8
14
 
9
15
  /** 本文の一続き(文)。start は文書の中の位置。 */
10
16
  export type BodyText = { readonly start: number; readonly text: string };
@@ -30,7 +36,8 @@ export const definedTerms = (tree: StructureNode): DefinedTerm[] =>
30
36
  .replaceAll(EMPHASIS_MARKS, "")
31
37
  .trim()
32
38
  : "";
33
- return isTerm(term) ? [{ term, span: node.span, line: node.line, inline: node.attrs["placement"] === "inline" }] : [];
39
+ const bare = node.attrs["form"] === "bare" ? { bare: true } : {};
40
+ return isTerm(term) ? [{ term, span: node.span, line: node.line, inline: node.attrs["placement"] === "inline", ...bare }] : [];
34
41
  });
35
42
 
36
43
  const LATIN_EDGE = /^[\p{Script=Latin}\p{N}]|[\p{Script=Latin}\p{N}]$/u;
@@ -56,11 +63,23 @@ const termPattern = (term: string, caseMatch: CaseMatch): RegExp => {
56
63
 
57
64
  const inside = (offset: number, spans: readonly Span[]): boolean => spans.some((span) => span.start <= offset && offset < span.end);
58
65
 
59
- /** 語の現れ(文書の中の位置)を順に。定義そのもの("Seller" means、(the "Seller"))の中の現れは使用に数えない。 */
60
- export const usesOf = (term: string, texts: readonly BodyText[], definitionSpans: readonly Span[], caseMatch: CaseMatch = "loose"): number[] => {
66
+ /**
67
+ * 語の現れ(文書の中の位置)を順に。定義そのもの("Seller" means、(the "Seller"))の中の現れは使用に数えない。
68
+ * mentioned が言う現れ(引用符で例として挙げただけ)も数えない。
69
+ */
70
+ export const usesOf = (
71
+ term: string,
72
+ texts: readonly BodyText[],
73
+ definitionSpans: readonly Span[],
74
+ caseMatch: CaseMatch = "loose",
75
+ mentioned: Mentioned = NEVER_MENTIONED,
76
+ ): number[] => {
61
77
  const pattern = termPattern(term, caseMatch);
62
78
  return texts.flatMap((body) =>
63
- [...body.text.matchAll(pattern)].map((match) => body.start + match.index).filter((offset) => !inside(offset, definitionSpans)),
79
+ [...body.text.matchAll(pattern)]
80
+ .filter((match) => !mentioned(body.start + match.index, body.start + match.index + match[0].length))
81
+ .map((match) => body.start + match.index)
82
+ .filter((offset) => !inside(offset, definitionSpans)),
64
83
  );
65
84
  };
66
85
 
@@ -78,10 +97,20 @@ const groupsOf = (terms: readonly DefinedTerm[]): TermGroup[] => {
78
97
  };
79
98
 
80
99
  /** 定義したのに、定義の外で一度も使っていない語。同じ語の定義が二つあれば、最初の一つだけを言う(二つ目は duplicate-definition)。 */
81
- export const unusedDefinitions = (terms: readonly DefinedTerm[], texts: readonly BodyText[]): DefinedTerm[] =>
82
- groupsOf(terms)
83
- .filter((group) => usesOf(group.first.term, texts, group.spans).length === 0)
100
+ export const unusedDefinitions = (terms: readonly DefinedTerm[], texts: readonly BodyText[], mentioned: Mentioned = NEVER_MENTIONED): DefinedTerm[] => {
101
+ const groups = groupsOf(terms);
102
+ const examples = quotesExamples(groups, texts, mentioned);
103
+ return groups
104
+ .filter((group) => !(examples && group.first.bare === true) && usesOf(group.first.term, texts, group.spans).length === 0)
84
105
  .map((group) => group.first);
106
+ };
107
+
108
+ /**
109
+ * 定義の語の無い括弧の引用(("Seller"))を、文書が一つも本文で使っていなければ、それは定義ではなく例の引用
110
+ * (Plain verbs, not metaphors ("silently fails"))。引用符で挙げただけの現れは使用に数えない。
111
+ */
112
+ const quotesExamples = (groups: readonly TermGroup[], texts: readonly BodyText[], mentioned: Mentioned): boolean =>
113
+ !groups.some((group) => group.first.bare === true && usesOf(group.first.term, texts, group.spans, "loose", mentioned).length > 0);
85
114
 
86
115
  /** offset を含む文。texts は start の昇順で、二分探索で引く。 */
87
116
  const sentenceAt = (texts: readonly BodyText[], offset: number): BodyText | undefined => {
@@ -118,13 +147,16 @@ const isDistinctive = (term: string): boolean => CAPITAL_OR_NON_LATIN.test(term)
118
147
  * 文の途中で括弧に入れて定義した語((the "Seller")、以下「甲」という)を、その定義の文より前で使っている所。最初の一つだけ。
119
148
  * 定義の文の中の現れ(株式会社GovTech東京(以下「GovTech東京」という。))は、定義する名前そのものなので数えない。札の文も数えない。
120
149
  * 定義の条("Seller" means、「甲」とは)は条の並びの頭にも終わりにも置く書き方があるので、前で使っても言わない。
150
+ * 定義の語の無い括弧の引用(("The key point is"))は例の引用と同じ形なので、その語を引用符で挙げただけの現れ
151
+ * (Its sentences open with "The key point is")は使用ではない。定義の語のある定義((the "Seller")、以下「買主」という)は、
152
+ * 引用符に入れた現れ(The "Seller" ships)も使用。
121
153
  */
122
- export const usesBeforeDefinition = (terms: readonly DefinedTerm[], texts: readonly BodyText[]): TermUse[] =>
154
+ export const usesBeforeDefinition = (terms: readonly DefinedTerm[], texts: readonly BodyText[], mentioned: Mentioned = NEVER_MENTIONED): TermUse[] =>
123
155
  groupsOf(terms)
124
156
  .filter((group) => group.first.inline && isDistinctive(group.first.term))
125
157
  .flatMap(({ first: defined, spans }) => {
126
158
  const defining = sentenceAt(texts, defined.span.start)?.start ?? defined.span.start;
127
- const uses = usesOf(defined.term, texts, spans, "exact");
159
+ const uses = usesOf(defined.term, texts, spans, "exact", defined.bare === true ? mentioned : NEVER_MENTIONED);
128
160
  const first = uses.find((offset) => !isLabel(sentenceAt(texts, offset), defined.term));
129
161
  return first !== undefined && first < defining ? [{ term: defined, offset: first }] : [];
130
162
  });