chaffjs 0.46.0 → 0.47.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 (45) hide show
  1. package/dist/detectors/bold-label.d.ts +6 -1
  2. package/dist/detectors/bold-label.d.ts.map +1 -1
  3. package/dist/detectors/bold-label.js +12 -1
  4. package/dist/detectors/bold-label.js.map +1 -1
  5. package/dist/detectors/char-shape.js +1 -1
  6. package/dist/detectors/char-shape.js.map +1 -1
  7. package/dist/detectors/closing-cliche.d.ts +2 -1
  8. package/dist/detectors/closing-cliche.d.ts.map +1 -1
  9. package/dist/detectors/closing-cliche.js +10 -2
  10. package/dist/detectors/closing-cliche.js.map +1 -1
  11. package/dist/detectors/en-shape.d.ts.map +1 -1
  12. package/dist/detectors/en-shape.js +5 -4
  13. package/dist/detectors/en-shape.js.map +1 -1
  14. package/dist/detectors/fullwidth-alnum.d.ts +4 -0
  15. package/dist/detectors/fullwidth-alnum.d.ts.map +1 -1
  16. package/dist/detectors/fullwidth-alnum.js +31 -9
  17. package/dist/detectors/fullwidth-alnum.js.map +1 -1
  18. package/dist/detectors/lexicon.d.ts.map +1 -1
  19. package/dist/detectors/lexicon.js +17 -3
  20. package/dist/detectors/lexicon.js.map +1 -1
  21. package/dist/detectors/unknown-word-context.d.ts +8 -0
  22. package/dist/detectors/unknown-word-context.d.ts.map +1 -1
  23. package/dist/detectors/unknown-word-context.js +38 -6
  24. package/dist/detectors/unknown-word-context.js.map +1 -1
  25. package/dist/detectors/unknown-word.d.ts.map +1 -1
  26. package/dist/detectors/unknown-word.js +11 -4
  27. package/dist/detectors/unknown-word.js.map +1 -1
  28. package/package.json +3 -3
  29. package/rules/adverb-overuse.yaml +2 -2
  30. package/rules/ai-tell.yaml +2 -2
  31. package/rules/bold-label-list.yaml +2 -2
  32. package/rules/closing-cliche.yaml +2 -2
  33. package/rules/date-outside-period.yaml +1 -1
  34. package/rules/fullwidth-alnum-consistency.yaml +2 -2
  35. package/rules/max-kanji-continuous.yaml +4 -3
  36. package/rules/ngram-repetition.yaml +2 -2
  37. package/rules/unknown-word.yaml +2 -2
  38. package/src/detectors/bold-label.ts +17 -2
  39. package/src/detectors/char-shape.ts +2 -2
  40. package/src/detectors/closing-cliche.ts +19 -3
  41. package/src/detectors/en-shape.ts +5 -4
  42. package/src/detectors/fullwidth-alnum.ts +39 -10
  43. package/src/detectors/lexicon.ts +19 -4
  44. package/src/detectors/unknown-word-context.ts +44 -6
  45. package/src/detectors/unknown-word.ts +16 -3
@@ -9,9 +9,11 @@ import { comparableText, entryIn, entryOpens } from "./lexicon-match.ts";
9
9
  // last 最後の段落にあるときだけ当たる言い回し(てみてほしい)。本文の途中なら普通の勧め
10
10
  // lead:/open:/end:<name> 前の語と文の終わりの語の組(気軽に〜ください)。closing-exhortation と同じ書き方
11
11
  // signoff 最後の段落の、この語で始まり「!」で終わる短い一行(Happy brewing!)
12
+ // stacked 結びにほかの定型があるときだけ当たる言い回し(feel free to)。一つなら普通の礼儀
12
13
 
13
14
  const LAST = "last";
14
15
  const SIGNOFF = "signoff";
16
+ const STACKED = "stacked";
15
17
 
16
18
  /** 一行の結びの挨拶と読む語の数の上限。「Happy brewing!」は二語、「Happy holidays, everyone!」は三語。 */
17
19
  const SIGNOFF_WORDS = 3;
@@ -36,7 +38,12 @@ const lastParagraphSentences = (sentences: readonly Sentence[], paragraphs: read
36
38
  return paragraph === undefined ? [final] : sentences.filter((sentence) => sentence.span.start >= paragraph.span.start);
37
39
  };
38
40
 
39
- type Words = { readonly closing: ReturnType<typeof exhortationWords>; readonly last: readonly LexiconEntry[]; readonly signoff: readonly LexiconEntry[] };
41
+ type Words = {
42
+ readonly closing: ReturnType<typeof exhortationWords>;
43
+ readonly last: readonly LexiconEntry[];
44
+ readonly signoff: readonly LexiconEntry[];
45
+ readonly stacked: readonly LexiconEntry[];
46
+ };
40
47
 
41
48
  /** 最後の段落でだけ当たる語。言い回しなら語彙の語、挨拶の一行なら文そのもの。 */
42
49
  const lastIn = (sentence: Sentence, words: Words): string | undefined =>
@@ -45,11 +52,19 @@ const lastIn = (sentence: Sentence, words: Words): string | undefined =>
45
52
 
46
53
  type Hit = { readonly sentence: Sentence; readonly matched: string };
47
54
 
48
- /** 結びの、定型の言い回しのある文。一文に一つ。 */
55
+ /** 結びの、定型の言い回しのある文。一文に一つ。stacked の語は、ほかの定型が結びにあるときだけ数える。 */
49
56
  export const closingClicheHits = (sentences: readonly Sentence[], paragraphs: readonly Paragraph[], words: Words): Hit[] => {
50
57
  const last = new Set(lastParagraphSentences(sentences, paragraphs));
58
+ const stock = new Map<Sentence, string>(
59
+ sentences.flatMap((sentence): [Sentence, string][] => {
60
+ const matched = exhortationIn(sentence, words.closing) ?? (last.has(sentence) ? lastIn(sentence, words) : undefined);
61
+ return matched === undefined ? [] : [[sentence, matched]];
62
+ }),
63
+ );
64
+ const stackedIn = (sentence: Sentence): string | undefined =>
65
+ stock.size === 0 ? undefined : words.stacked.find((entry) => entryIn(sentence, entry))?.pattern;
51
66
  return sentences.flatMap((sentence) => {
52
- const matched = exhortationIn(sentence, words.closing) ?? (last.has(sentence) ? lastIn(sentence, words) : undefined);
67
+ const matched = stock.get(sentence) ?? stackedIn(sentence);
53
68
  return matched === undefined ? [] : [{ sentence, matched }];
54
69
  });
55
70
  };
@@ -58,6 +73,7 @@ export const closingClicheWords = (lexicon: readonly LexiconEntry[]): Words => (
58
73
  closing: exhortationWords(lexicon),
59
74
  last: lexicon.filter((entry) => entry.group === LAST),
60
75
  signoff: lexicon.filter((entry) => entry.group === SIGNOFF),
76
+ stacked: lexicon.filter((entry) => entry.group === STACKED),
61
77
  });
62
78
 
63
79
  /** 結び(最後の節の終わりの段落)の定型の言い回し。limit は指摘に要る文の数。 */
@@ -22,11 +22,11 @@ const LETTER = /\p{L}/u;
22
22
 
23
23
  /**
24
24
  * 文の途中の頭だけ大文字の -ly の語は、解析器が語尾で副詞と当てた名前(July、Kelly)か、題の語(Frequently Asked Questions)。
25
- * どちらも動詞に畳める副詞ではない。
25
+ * どちらも動詞に畳める副詞ではない。書き手の確かさを言う副詞(probably、apparently)も、畳む先の動詞が無い。
26
26
  */
27
- const lyAdverbsOf = (tokens: readonly Token[]): Token[] => {
27
+ const lyAdverbsOf = (tokens: readonly Token[], stance: ReadonlySet<string>): Token[] => {
28
28
  const first = tokens.findIndex((token) => LETTER.test(token.surface));
29
- return tokens.filter((token, at) => isLyAdverb(token) && !(at > first && CAPITALISED.test(token.surface)));
29
+ return tokens.filter((token, at) => isLyAdverb(token) && !stance.has(token.surface.toLowerCase()) && !(at > first && CAPITALISED.test(token.surface)));
30
30
  };
31
31
 
32
32
  /**
@@ -35,7 +35,8 @@ const lyAdverbsOf = (tokens: readonly Token[]): Token[] => {
35
35
  */
36
36
  export const adverbDensity: Detector = (doc, options): Finding[] => {
37
37
  const words = wordsIn(doc);
38
- const hits = doc.sentences.flatMap((sentence) => lyAdverbsOf(sentence.tokens ?? []).map((token) => ({ sentence, token })));
38
+ const stance = new Set((doc.lexicons["stance-adverb"] ?? []).map((entry) => entry.pattern));
39
+ const hits = doc.sentences.flatMap((sentence) => lyAdverbsOf(sentence.tokens ?? [], stance).map((token) => ({ sentence, token })));
39
40
  const density = words === 0 ? 0 : Math.round((hits.length / words) * PER);
40
41
  const first = hits[0];
41
42
  if (words < MIN_WORDS || first === undefined || density <= options.limit) return [];
@@ -78,6 +78,11 @@ const continuesVersion = (text: string, start: number, run: string): boolean =>
78
78
  FULLWIDTH.test(run) === FULLWIDTH.test(text.charAt(start - 2)) &&
79
79
  VERSION_SO_FAR.test(text.slice(Math.max(0, start - 1 - VERSION_REACH), start - 1));
80
80
 
81
+ /** 電話番号(03-3581-1101、0570 - 064 - 556)。0 で始まり、ハイフンで三つの組につなぐ。三つの組を一つの幅で書いた一つの番号。 */
82
+ const PHONE_NUMBER = /(?<![0-90-9])[00][0-90-9]{1,4}(?:\s?[--‐−ー]\s?[0-90-9]{1,4}){2}(?![0-90-9])/gu;
83
+
84
+ const phoneSpans = (text: string): Span[] => [...text.matchAll(PHONE_NUMBER)].map((match) => ({ start: match.index, end: match.index + match[0].length }));
85
+
81
86
  const isListItemNumber = (source: string, offset: number): boolean => LIST_ITEM_HEAD.test(source.slice(source.lastIndexOf("\n", offset - 1) + 1, offset));
82
87
 
83
88
  export type AlnumRun = {
@@ -87,9 +92,13 @@ export type AlnumRun = {
87
92
  readonly offset: number;
88
93
  /** 表のセルを続けてしまった数字(8,457 と 95.7 の 8,45795.7)の中。幅は書き手のものだが、セルの切れ目が分からず桁の数は当てにならない。 */
89
94
  readonly glued: boolean;
95
+ /** 一つの書き物として書いたまとまりの位置。電話番号の組はその番号の頭、ほかは並び自身の位置。 */
96
+ readonly unit: number;
97
+ /** 英字一字と数字を違う幅で書いた並び(Q5)を分けた片方。 */
98
+ readonly split: boolean;
90
99
  };
91
100
 
92
- type Piece = { readonly 0: string; readonly index: number };
101
+ type Piece = { readonly 0: string; readonly index: number; readonly split?: boolean };
93
102
 
94
103
  /** 一つの幅で書いた部分。 */
95
104
  const SAME_WIDTH = /[A-Za-z0-9]+|[A-Za-z0-9]+/gu;
@@ -106,7 +115,7 @@ const isLetterAndNumber = (parts: readonly Piece[]): boolean =>
106
115
  */
107
116
  const widthPieces = (match: RegExpExecArray): Piece[] => {
108
117
  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 }));
118
+ const parts = [...match[0].matchAll(SAME_WIDTH)].map((part) => ({ 0: part[0], index: match.index + part.index, split: true }));
110
119
  return isLetterAndNumber(parts) ? parts : [];
111
120
  };
112
121
 
@@ -117,6 +126,7 @@ const widthPieces = (match: RegExpExecArray): Piece[] => {
117
126
  export const alnumRunsIn = (sentence: Sentence, source: string): AlnumRun[] => {
118
127
  const quoted: readonly Span[] = quotedSpans(sentence.text, QUOTATION_MARKS);
119
128
  const glued = gluedNumberSpans(sentence.text);
129
+ const phones = phoneSpans(sentence.text);
120
130
  return [...sentence.text.matchAll(ALNUM_RUN)]
121
131
  .flatMap(widthPieces)
122
132
  .filter((match) => !isWithinAny(quoted, { start: match.index, end: match.index + match[0].length }))
@@ -132,6 +142,8 @@ export const alnumRunsIn = (sentence: Sentence, source: string): AlnumRun[] => {
132
142
  written: match[0],
133
143
  offset: sentence.span.start + match.index,
134
144
  glued: isWithinAny(glued, { start: match.index, end: match.index + match[0].length }),
145
+ unit: sentence.span.start + (phones.find((phone) => phone.start <= match.index && match.index < phone.end)?.start ?? match.index),
146
+ split: "split" in match && match.split === true,
135
147
  }));
136
148
  };
137
149
 
@@ -163,20 +175,34 @@ const datelineOf = (doc: ProseDocument): Span | undefined => {
163
175
  return first !== undefined && DATELINE.test(doc.source.slice(first.span.start, first.span.end).trim()) ? first.span : undefined;
164
176
  };
165
177
 
178
+ /**
179
+ * 見出しの文字(### Q5:…の Q5:…)を、文と同じに読む一続き。文書の頭の題(# ノロウイルスに関するQ&A)は載せたページの名前なので読まない。
180
+ * 見出しから数えるのは、英字一字と数字を違う幅で書いた並び(Q5)だけ。見出しはほかの数を本文と別の幅で書くことが多いが(## 1 概要)、
181
+ * 一つの並びの中で幅が変わるのは書き損じ。
182
+ */
183
+ const headingSentences = (doc: ProseDocument): Sentence[] =>
184
+ (doc.markup?.headings ?? []).flatMap((heading, index) => {
185
+ if (index === 0 && heading.depth === 1) return [];
186
+ const start = doc.source.indexOf(heading.text, heading.start);
187
+ return start < 0 || start >= heading.end || heading.text === "" ? [] : [{ span: { start, end: start + heading.text.length }, text: heading.text }];
188
+ });
189
+
166
190
  /** 多いほうの英字の語が何種類あれば、文書の書き方と見るか。BAR を二度書いただけなら、それはその語の書き方。 */
167
191
  const HABIT_WORDS = 2;
168
192
 
169
193
  const wordOf = (run: AlnumRun): string => run.written.normalize("NFKC").toUpperCase();
170
194
 
171
195
  /**
172
- * 英字の語の少ないほうを、文書の書き方からはずれたと言えるか。多いほうが二種類以上の語か、少ないほうと同じ語(URL と URL)のとき。
173
- * 全角の BAR だけの文書の DELICATE は、別の語を別の書き方で書いただけ。
196
+ * 少ないほうを、文書の書き方からはずれたと言えるか。英字の語は、多いほうが二種類以上の語か、少ないほうと同じ語(URL と URL)のとき。
197
+ * 全角の BAR だけの文書の DELICATE は、別の語を別の書き方で書いただけ。ほかの組は、多いほうが二つ以上のまとまりにわたるとき。
198
+ * 多いほうが電話番号一つの組(0570 - 064 - 556)だけなら、それはその番号の書き方で、文書の書き方ではない。
174
199
  */
175
200
  const departsFromHabit =
176
201
  (ofKind: readonly Located[], odd: readonly Located[]) =>
177
202
  ({ run }: Located): boolean => {
178
- if (run.kind !== "letters") return true;
179
- const usual = new Set(ofKind.filter((entry) => !odd.includes(entry)).map((entry) => wordOf(entry.run)));
203
+ const usualRuns = ofKind.filter((entry) => !odd.includes(entry)).map((entry) => entry.run);
204
+ if (run.kind !== "letters") return new Set(usualRuns.map((usual) => usual.unit)).size >= HABIT_WORDS;
205
+ const usual = new Set(usualRuns.map(wordOf));
180
206
  return usual.size >= HABIT_WORDS || usual.has(wordOf(run));
181
207
  };
182
208
 
@@ -198,10 +224,13 @@ const findingOf = ({ sentence, run }: Located, count: number, of: number, limit:
198
224
  */
199
225
  export const fullwidthAlnum: Detector = (doc, options): Finding[] => {
200
226
  const dateline = datelineOf(doc);
201
- const located: Located[] = doc.sentences
202
- .filter((sentence) => sentence.embeddedLanguage === undefined && !isCitation(sentence, doc.source))
203
- .filter((sentence) => dateline === undefined || !isWithinAny([dateline], sentence.span))
204
- .flatMap((sentence) => alnumRunsIn(sentence, doc.source).map((run) => ({ sentence, run })));
227
+ const located: Located[] = [
228
+ ...doc.sentences
229
+ .filter((sentence) => sentence.embeddedLanguage === undefined && !isCitation(sentence, doc.source))
230
+ .filter((sentence) => dateline === undefined || !isWithinAny([dateline], sentence.span))
231
+ .flatMap((sentence) => alnumRunsIn(sentence, doc.source).map((run) => ({ sentence, run }))),
232
+ ...headingSentences(doc).flatMap((sentence) => alnumRunsIn(sentence, doc.source).flatMap((run) => (run.split ? [{ sentence, run }] : []))),
233
+ ];
205
234
  return KINDS.flatMap((kind) => {
206
235
  const ofKind = located.filter(({ run }) => run.kind === kind);
207
236
  const odd = minorityWithin(ofKind, ({ run }) => run.fullwidth, options.limit);
@@ -269,10 +269,23 @@ export const repeatedConjunction: Detector = (doc, options): Finding[] => {
269
269
  * 書き手が書いた語。引用符の中だけの語(words such as "delves" and "pivotal")は、例に挙げたか人の言葉を引いたもの。
270
270
  * 品詞があれば当たった語の並びごとに見る(活用した形も原形で当たる)。無ければ字で照らす。
271
271
  */
272
- const writtenIn = (sentence: Sentence, entry: LexiconEntry): boolean =>
272
+ const writtenIn = (sentence: Sentence, entry: LexiconEntry, isHeading: boolean): boolean =>
273
273
  sentence.tokens === undefined || entry.tokens === undefined || entry.tokens.length === 0
274
274
  ? entryIn(sentence, entry) && !onlyQuoted(sentence, entry)
275
- : entryRanges(sentence, entry).some((range) => !quotedRange(sentence, range));
275
+ : entryRanges(sentence, entry).some((range) => !quotedRange(sentence, range) && (isHeading || !inTitleCasedName(sentence.tokens ?? [], range)));
276
+
277
+ const LETTER = /\p{L}/u;
278
+ const CAPITALISED = /^\p{Lu}/u;
279
+
280
+ /**
281
+ * Whether the phrase is capitalised word by word past the sentence's first word: part of a name (a mood board called
282
+ * Inspiring and Empowering, a programme title), not the writer's own phrasing. A heading's title case is the writer's.
283
+ */
284
+ const inTitleCasedName = (tokens: readonly Token[], range: TokenRange): boolean => {
285
+ const firstWord = tokens.findIndex((token) => LETTER.test(token.surface));
286
+ const words = tokens.slice(range.start, range.end).filter((token) => LETTER.test(token.surface));
287
+ return range.start > firstWord && words.length > 0 && words.every((token) => CAPITALISED.test(token.surface));
288
+ };
276
289
 
277
290
  /**
278
291
  * How many sentences one pile-up may span. Generated articles fit inside it whole; a long text (a listing of many authors'
@@ -288,8 +301,10 @@ export const aiTell: Detector = (doc, options): Finding[] => {
288
301
  const entries = [...new Map((options.lexicon ?? []).map((entry) => [entry.pattern.toLowerCase(), entry])).values()];
289
302
  const sentences = sentencesWithHeadings(doc);
290
303
  const headings = new Set(headingSentences(doc).map((heading) => heading.span.start));
291
- const counts = (sentence: Sentence, entry: LexiconEntry): boolean =>
292
- (entry.heading_only !== true || headings.has(sentence.span.start)) && writtenIn(sentence, entry);
304
+ const counts = (sentence: Sentence, entry: LexiconEntry): boolean => {
305
+ const isHeading = headings.has(sentence.span.start);
306
+ return (entry.heading_only !== true || isHeading) && writtenIn(sentence, entry, isHeading);
307
+ };
293
308
  const hitsPerSentence = sentences.map((sentence) => entries.filter((entry) => counts(sentence, entry)));
294
309
  const stretch = densestStretch(hitsPerSentence, AI_TELL_STRETCH, (entry) => entry.weight ?? 1);
295
310
  const found = entries.filter((entry) => stretch?.keys.has(entry) === true);
@@ -5,17 +5,55 @@ import { isAddress, isKnownWord, stemsOf, type CheckedWord, type Speller } from
5
5
  /** Each half of a closed compound keeps this many letters: a shorter half (open + end, pip + line) is more often a slip at the joint. */
6
6
  const MIN_COMPOUND_PART = 4;
7
7
 
8
+ /** Where a word splits into the two halves of a closed compound (flag + salute in flagsalute). */
9
+ const compoundSplits = (word: string, speller: Speller): number[] => {
10
+ const listed: Speller = { ...speller, suffixes: [], prefixes: [] };
11
+ const inflected: Speller = { ...listed, suffixes: speller.suffixes.filter(({ inflection }) => inflection === true) };
12
+ return [...Array(Math.max(0, word.length - 2 * MIN_COMPOUND_PART + 1)).keys()]
13
+ .map((index) => index + MIN_COMPOUND_PART)
14
+ .filter((split) => isKnownWord(word.slice(0, split), listed) && isKnownWord(word.slice(split), inflected));
15
+ };
16
+
8
17
  /**
9
18
  * Two dictionary words of four letters or more written as one (crowdwork, crowdworkers): a closed compound, not a slip. The
10
19
  * head is a word as listed and the tail takes only an inflection: a prefix or a derived ending on a half (misre + presenation)
11
20
  * reads too many slips as words.
12
21
  */
13
- export const isClosedCompound = (word: string, speller: Speller): boolean => {
14
- const listed: Speller = { ...speller, suffixes: [], prefixes: [] };
15
- const inflected: Speller = { ...listed, suffixes: speller.suffixes.filter(({ inflection }) => inflection === true) };
16
- return [...Array(Math.max(0, word.length - 2 * MIN_COMPOUND_PART + 1)).keys()]
17
- .map((index) => index + MIN_COMPOUND_PART)
18
- .some((split) => isKnownWord(word.slice(0, split), listed) && isKnownWord(word.slice(split), inflected));
22
+ export const isClosedCompound = (word: string, speller: Speller): boolean => compoundSplits(word, speller).length > 0;
23
+
24
+ const QUOTE_MARK = /["“”‘’']/u;
25
+
26
+ /** Where the line of source around offset starts and ends. */
27
+ const lineAround = (source: string, offset: number): readonly [number, number] => {
28
+ const end = source.indexOf("\n", offset);
29
+ return [source.lastIndexOf("\n", offset - 1) + 1, end === -1 ? source.length : end];
30
+ };
31
+
32
+ /** Text set in quotation marks at either end (Not "user name", "factcheck this"): a spelling named or searched for, not used. */
33
+ export const isQuotedAt = (source: string, start: number, end: number): boolean =>
34
+ QUOTE_MARK.test(source.charAt(start - 1)) || QUOTE_MARK.test(source.charAt(end));
35
+
36
+ /**
37
+ * The fewest uses of the apart form before the joined one counts as a slip. A document that writes a compound both ways a
38
+ * few times (rule making twice, rulemaking once) is choosing between two spellings, which is orthographic-variant's.
39
+ */
40
+ const MIN_APART_USES = 3;
41
+
42
+ /**
43
+ * How the document writes a closed compound apart elsewhere (flag salute, flag-salute beside flagsalute), its most common
44
+ * such form in lower case, or undefined. Written apart three times or more, the joined form is two words run together, not the writer's term.
45
+ * A form in quotation marks or on the word's own line (Do not use white list or whitelist) names spellings, not uses one.
46
+ */
47
+ export const writtenApart = (word: string, speller: Speller, source: string, offset: number): string | undefined => {
48
+ const [lineStart, lineEnd] = lineAround(source, offset);
49
+ const forms = compoundSplits(word, speller).flatMap((split) => {
50
+ const apart = new RegExp(`(?<![\\p{L}-])${word.slice(0, split)}[ -]${word.slice(split)}(?![\\p{L}-])`, "giu");
51
+ return [...source.matchAll(apart)]
52
+ .filter((match) => (match.index < lineStart || match.index >= lineEnd) && !isQuotedAt(source, match.index, match.index + match[0].length))
53
+ .map((match) => match[0].toLowerCase());
54
+ });
55
+ if (forms.length < MIN_APART_USES) return undefined;
56
+ return [...countsOf(forms)].toSorted((left, right) => right[1] - left[1])[0]?.[0];
19
57
  };
20
58
 
21
59
  /**
@@ -13,8 +13,10 @@ import {
13
13
  inSearchTermList,
14
14
  isClosedCompound,
15
15
  isMention,
16
+ isQuotedAt,
16
17
  isNceSpelling,
17
18
  usedOnce,
19
+ writtenApart,
18
20
  } from "./unknown-word-context.ts";
19
21
 
20
22
  /** これより短い語は見ない。短い語は一字違いの語が多すぎて言い直しが決まらず(tost は test か toast か)、略した語(impl)も多い。 */
@@ -52,7 +54,7 @@ export const codeWordsOf = (source: string): Set<string> => {
52
54
  return words;
53
55
  };
54
56
 
55
- /** 辞書の語か、派生した語に活用の語尾を付けた語(operationalises、rightsized)か、四字以上の辞書の語二つを続けた語(crowdwork)か、-nse の語の -nce の綴り(recompence)。 */
57
+ /** 辞書の語か、派生した語に活用の語尾を付けた語(operationalises、rightsized)か、四字以上の辞書の語二つを続けた語(crowdwork。文書が別の所で分けて書いていれば続けた打ち間違い)か、-nse の語の -nce の綴り(recompence)。 */
56
58
  const isFormedWord = (word: string, speller: Speller): boolean =>
57
59
  isKnownWord(word, speller) || hasTwoEndings(word, speller) || isClosedCompound(word, speller) || isNceSpelling(word, speller);
58
60
 
@@ -80,9 +82,20 @@ export const unknownWord: Detector = (doc): Finding[] => {
80
82
  const words = wordsToCheck(doc.prose ?? doc.source, MIN_LENGTH, MAX_LENGTH).filter(({ offset }) => !isSplitWord(doc.source, offset));
81
83
  const used = new Set(words.map(({ word }) => word).filter((word) => isKnownWord(word, speller)));
82
84
  const unformed = new Set(words.map(({ word }) => word).filter((word) => !known.has(word) && !isFormedWord(word, speller)));
85
+ const apartAt = (word: string, offset: number): string | undefined =>
86
+ known.has(word) || isKnownWord(word, speller) || !isClosedCompound(word, speller) || isQuotedAt(doc.source, offset, offset + word.length)
87
+ ? undefined
88
+ : writtenApart(word, speller, doc.prose ?? doc.source, offset);
83
89
  return usedOnce(words, speller).flatMap(({ offset, word }) => {
84
- if (!unformed.has(word) || hasDrugStem(word, drugStems) || hasSiblingName(word, unformed) || isTermByPlace(doc.source, offset, word)) return [];
85
- const suggestion = suggestionFor(word, speller, used);
90
+ const apart = apartAt(word, offset);
91
+ if (
92
+ !(unformed.has(word) || apart !== undefined) ||
93
+ hasDrugStem(word, drugStems) ||
94
+ hasSiblingName(word, unformed) ||
95
+ isTermByPlace(doc.source, offset, word)
96
+ )
97
+ return [];
98
+ const suggestion = apart ?? suggestionFor(word, speller, used);
86
99
  if (suggestion === undefined) return [];
87
100
  return [{ rule: "", severity: "warning", line: 0, column: 0, quote: quoteAt(doc.source, offset), values: { word, suggestion, offset } }];
88
101
  });