chaffjs 0.51.0 → 0.52.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.
- package/dist/clause-particles.d.ts +10 -2
- package/dist/clause-particles.d.ts.map +1 -1
- package/dist/clause-particles.js +15 -3
- package/dist/clause-particles.js.map +1 -1
- package/dist/detectors/address-blank.d.ts +14 -0
- package/dist/detectors/address-blank.d.ts.map +1 -0
- package/dist/detectors/address-blank.js +41 -0
- package/dist/detectors/address-blank.js.map +1 -0
- package/dist/detectors/bold-emphasis-form.d.ts +1 -0
- package/dist/detectors/bold-emphasis-form.d.ts.map +1 -1
- package/dist/detectors/bold-emphasis-form.js +7 -1
- package/dist/detectors/bold-emphasis-form.js.map +1 -1
- package/dist/detectors/char-shape.d.ts.map +1 -1
- package/dist/detectors/char-shape.js +7 -8
- package/dist/detectors/char-shape.js.map +1 -1
- package/dist/detectors/closing-cliche.d.ts +1 -1
- package/dist/detectors/closing-cliche.d.ts.map +1 -1
- package/dist/detectors/closing-cliche.js +16 -3
- package/dist/detectors/closing-cliche.js.map +1 -1
- package/dist/detectors/contraction-mix.d.ts.map +1 -1
- package/dist/detectors/contraction-mix.js +3 -1
- package/dist/detectors/contraction-mix.js.map +1 -1
- package/dist/detectors/currency-notation.d.ts.map +1 -1
- package/dist/detectors/currency-notation.js +9 -1
- package/dist/detectors/currency-notation.js.map +1 -1
- package/dist/detectors/definition-use.d.ts.map +1 -1
- package/dist/detectors/definition-use.js +1 -0
- package/dist/detectors/definition-use.js.map +1 -1
- package/dist/detectors/en-shape.d.ts.map +1 -1
- package/dist/detectors/en-shape.js +12 -1
- package/dist/detectors/en-shape.js.map +1 -1
- package/dist/detectors/gram-ending.d.ts +12 -0
- package/dist/detectors/gram-ending.d.ts.map +1 -0
- package/dist/detectors/gram-ending.js +27 -0
- package/dist/detectors/gram-ending.js.map +1 -0
- package/dist/detectors/gram-repetition.d.ts.map +1 -1
- package/dist/detectors/gram-repetition.js +19 -3
- package/dist/detectors/gram-repetition.js.map +1 -1
- package/dist/detectors/inline-item-label.d.ts.map +1 -1
- package/dist/detectors/inline-item-label.js +14 -3
- package/dist/detectors/inline-item-label.js.map +1 -1
- package/dist/detectors/kanji-run-pieces.d.ts +21 -6
- package/dist/detectors/kanji-run-pieces.d.ts.map +1 -1
- package/dist/detectors/kanji-run-pieces.js +23 -8
- package/dist/detectors/kanji-run-pieces.js.map +1 -1
- package/dist/detectors/list-item-own.d.ts +11 -0
- package/dist/detectors/list-item-own.d.ts.map +1 -0
- package/dist/detectors/list-item-own.js +31 -0
- package/dist/detectors/list-item-own.js.map +1 -0
- package/dist/detectors/list-item-phrase.d.ts +1 -1
- package/dist/detectors/list-item-phrase.d.ts.map +1 -1
- package/dist/detectors/list-item-phrase.js +18 -7
- package/dist/detectors/list-item-phrase.js.map +1 -1
- package/dist/detectors/list-joint.d.ts +6 -0
- package/dist/detectors/list-joint.d.ts.map +1 -1
- package/dist/detectors/list-joint.js +18 -2
- package/dist/detectors/list-joint.js.map +1 -1
- package/dist/detectors/list-parallel.d.ts +3 -8
- package/dist/detectors/list-parallel.d.ts.map +1 -1
- package/dist/detectors/list-parallel.js +18 -32
- package/dist/detectors/list-parallel.js.map +1 -1
- package/dist/detectors/open-end-scope.d.ts.map +1 -1
- package/dist/detectors/open-end-scope.js +2 -2
- package/dist/detectors/open-end-scope.js.map +1 -1
- package/dist/detectors/oxford-comma.d.ts.map +1 -1
- package/dist/detectors/oxford-comma.js +2 -1
- package/dist/detectors/oxford-comma.js.map +1 -1
- package/dist/detectors/pseudo-heading.d.ts +2 -0
- package/dist/detectors/pseudo-heading.d.ts.map +1 -1
- package/dist/detectors/pseudo-heading.js +63 -2
- package/dist/detectors/pseudo-heading.js.map +1 -1
- package/dist/detectors/register-label-note.d.ts +8 -0
- package/dist/detectors/register-label-note.d.ts.map +1 -0
- package/dist/detectors/register-label-note.js +31 -0
- package/dist/detectors/register-label-note.js.map +1 -0
- package/dist/detectors/run-in-title.d.ts +6 -0
- package/dist/detectors/run-in-title.d.ts.map +1 -0
- package/dist/detectors/run-in-title.js +32 -0
- package/dist/detectors/run-in-title.js.map +1 -0
- package/dist/detectors/sentence-ending.d.ts.map +1 -1
- package/dist/detectors/sentence-ending.js +5 -1
- package/dist/detectors/sentence-ending.js.map +1 -1
- package/dist/detectors/sentence-length.d.ts.map +1 -1
- package/dist/detectors/sentence-length.js +12 -3
- package/dist/detectors/sentence-length.js.map +1 -1
- package/dist/detectors/technical-docs.d.ts.map +1 -1
- package/dist/detectors/technical-docs.js +25 -6
- package/dist/detectors/technical-docs.js.map +1 -1
- package/dist/detectors/token-shape.d.ts.map +1 -1
- package/dist/detectors/token-shape.js +4 -3
- package/dist/detectors/token-shape.js.map +1 -1
- package/dist/detectors/unfilled-placeholder.d.ts.map +1 -1
- package/dist/detectors/unfilled-placeholder.js +11 -1
- package/dist/detectors/unfilled-placeholder.js.map +1 -1
- package/dist/structure/definition-use.d.ts +5 -3
- package/dist/structure/definition-use.d.ts.map +1 -1
- package/dist/structure/definition-use.js +10 -2
- package/dist/structure/definition-use.js.map +1 -1
- package/dist/structure/forward-reference.d.ts +8 -0
- package/dist/structure/forward-reference.d.ts.map +1 -0
- package/dist/structure/forward-reference.js +18 -0
- package/dist/structure/forward-reference.js.map +1 -0
- package/dist/topic-clause.d.ts.map +1 -1
- package/dist/topic-clause.js +18 -5
- package/dist/topic-clause.js.map +1 -1
- package/package.json +3 -3
- package/rules/bold-emphasis.yaml +2 -2
- package/rules/closing-cliche.yaml +2 -2
- package/rules/contraction-consistency.yaml +2 -2
- package/rules/expletive-construction.yaml +2 -2
- package/rules/internal-jargon.yaml +1 -0
- package/rules/list-item-form-mix.yaml +2 -2
- package/rules/max-kanji-continuous.yaml +5 -2
- package/rules/max-sentence-length.yaml +2 -2
- package/rules/ngram-repetition.yaml +2 -2
- package/rules/no-doubled-joshi.yaml +2 -2
- package/rules/no-mixed-desumasu.yaml +2 -2
- package/rules/obligation-without-deadline.yaml +2 -2
- package/rules/preferred-term.yaml +1 -0
- package/rules/step-statement-mix.yaml +2 -2
- package/rules/topic-predicate-distance.yaml +2 -2
- package/rules/unfilled-placeholder.yaml +2 -2
- package/rules/use-before-definition.yaml +2 -2
- package/src/clause-particles.ts +19 -3
- package/src/detectors/address-blank.ts +49 -0
- package/src/detectors/bold-emphasis-form.ts +10 -1
- package/src/detectors/char-shape.ts +10 -11
- package/src/detectors/closing-cliche.ts +19 -2
- package/src/detectors/contraction-mix.ts +3 -1
- package/src/detectors/currency-notation.ts +17 -6
- package/src/detectors/definition-use.ts +1 -0
- package/src/detectors/en-shape.ts +14 -1
- package/src/detectors/gram-ending.ts +40 -0
- package/src/detectors/gram-repetition.ts +28 -4
- package/src/detectors/inline-item-label.ts +18 -3
- package/src/detectors/kanji-run-pieces.ts +35 -8
- package/src/detectors/list-item-own.ts +38 -0
- package/src/detectors/list-item-phrase.ts +20 -8
- package/src/detectors/list-joint.ts +31 -2
- package/src/detectors/list-parallel.ts +25 -38
- package/src/detectors/open-end-scope.ts +3 -2
- package/src/detectors/oxford-comma.ts +2 -1
- package/src/detectors/pseudo-heading.ts +75 -3
- package/src/detectors/register-label-note.ts +37 -0
- package/src/detectors/run-in-title.ts +35 -0
- package/src/detectors/sentence-ending.ts +5 -1
- package/src/detectors/sentence-length.ts +18 -5
- package/src/detectors/technical-docs.ts +31 -6
- package/src/detectors/token-shape.ts +4 -3
- package/src/detectors/unfilled-placeholder.ts +13 -1
- package/src/structure/definition-use.ts +13 -3
- package/src/structure/forward-reference.ts +21 -0
- package/src/topic-clause.ts +24 -5
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { isContent } from "./content-morphemes.ts";
|
|
2
|
+
import { endingRuns } from "./clause-shape.ts";
|
|
3
|
+
import type { Paragraph, Sentence, Span, Token } from "../plugin.ts";
|
|
4
|
+
|
|
5
|
+
/** 文の結びだけを作る語: 動詞にするだけの語と非自立の動詞(し、まいり)、助動詞(ます)、て・で、句点。中身の語は isContent が外す。 */
|
|
6
|
+
const ENDING_POS = new Set(["VERB", "AUX", "SCONJ", "PUNCT"]);
|
|
7
|
+
|
|
8
|
+
/** 文を閉じる記号。読点(しておりますが、)で終わる語句は文の途中。 */
|
|
9
|
+
const SENTENCE_STOP = /^[。..!?!?]$/u;
|
|
10
|
+
|
|
11
|
+
/** 続けて同じ形で終わる文の並びの、一番短い長さ。 */
|
|
12
|
+
const MIN_RUN_SENTENCES = 2;
|
|
13
|
+
|
|
14
|
+
const overlaps = (token: Token, window: Span): boolean => token.span.start < window.end && window.start < token.span.end;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 語句が文の結びだけでできているか(してまいります。、てまいりました。)。
|
|
18
|
+
* 中身の語に触れれば、窓の端で切れていても言い回し(取り組んでまいります。、実施しております。)。
|
|
19
|
+
*/
|
|
20
|
+
export const isSentenceEndingOnly = (tokens: readonly Token[], window: Span): boolean => {
|
|
21
|
+
const touched = tokens.filter((token) => overlaps(token, window) && token.surface.trim() !== "");
|
|
22
|
+
return (
|
|
23
|
+
SENTENCE_STOP.test(touched.at(-1)?.surface ?? "") &&
|
|
24
|
+
touched.some((token) => token.pos === "AUX") &&
|
|
25
|
+
touched.every((token) => ENDING_POS.has(token.pos) && !isContent(token))
|
|
26
|
+
);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 段落の中で、前か後ろの文と同じ形で終わる文(repeated-sentence-ending が数える並び、clause-shape.ts)。並びの中の結びの繰り返しは
|
|
31
|
+
* 文末の単調さで、その rule が見る。離れた所で何度も出る結び(演説の節ごとの 〜を実現してまいります。)は言い回しの癖として残る。
|
|
32
|
+
*/
|
|
33
|
+
export const sentencesInEndingRuns = (paragraphs: readonly Paragraph[], eras: readonly string[]): ReadonlySet<Sentence> =>
|
|
34
|
+
new Set(
|
|
35
|
+
paragraphs.flatMap((paragraph) =>
|
|
36
|
+
endingRuns(paragraph.sentences, eras)
|
|
37
|
+
.filter((run) => run.length >= MIN_RUN_SENTENCES)
|
|
38
|
+
.flatMap((run) => run.map(({ sentence }) => sentence)),
|
|
39
|
+
),
|
|
40
|
+
);
|
|
@@ -15,12 +15,13 @@ import {
|
|
|
15
15
|
screenLabelSpans,
|
|
16
16
|
} from "./gram-furniture.ts";
|
|
17
17
|
import { hasPredicateIn } from "./gram-predicate.ts";
|
|
18
|
+
import { isSentenceEndingOnly, sentencesInEndingRuns } from "./gram-ending.ts";
|
|
18
19
|
import { gramPassagesOf } from "./gram-passages.ts";
|
|
19
20
|
import { narrowestStretch, sentencePositions } from "./gram-spread.ts";
|
|
20
21
|
import { namedByHeading } from "./gram-heading.ts";
|
|
21
22
|
import { entryOpeners, filledForms, labelLines, nounFragments, rowEndCells, sectionOpeners } from "./gram-openers.ts";
|
|
22
23
|
import { nameSpans, touchesAny } from "../team-names.ts";
|
|
23
|
-
import type { Detector, Finding, LengthUnit, ProseDocument, Sentence, Span, StructureNode } from "../plugin.ts";
|
|
24
|
+
import type { Detector, Finding, LengthUnit, ProseDocument, Sentence, Span, StructureNode, Token } from "../plugin.ts";
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* 同じ言い回しの繰り返し。文字の並びで見るので、adapter の語の区切り(wordSplit)が要らない。英語の窓は空白で語の切れ目にそろえる。
|
|
@@ -203,11 +204,30 @@ const repeatsOf = (doc: ProseDocument, limit: number): Repeat[] => {
|
|
|
203
204
|
);
|
|
204
205
|
};
|
|
205
206
|
|
|
206
|
-
|
|
207
|
+
type TokenJudge = (tokens: readonly Token[], window: Span) => boolean;
|
|
208
|
+
|
|
209
|
+
/** 出現が文の中で占める範囲(文書の中の位置)を judge に見せる。品詞か範囲が無ければ決めない。 */
|
|
210
|
+
const judgeAt = (occurrence: Occurrence, judge: TokenJudge): boolean | undefined => {
|
|
207
211
|
const { sentence, place } = occurrence;
|
|
208
212
|
if (sentence.tokens === undefined || place === undefined) return undefined;
|
|
209
213
|
const start = sentence.span.start;
|
|
210
|
-
return
|
|
214
|
+
return judge(sentence.tokens, { start: start + place.start, end: start + place.end });
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
const predicateAt = (occurrence: Occurrence, unit: LengthUnit): boolean | undefined =>
|
|
218
|
+
judgeAt(occurrence, (tokens, window) => hasPredicateIn(tokens, window, unit));
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 出現の半分以上が、同じ形で終わる文の並びの中にある、文の結びだけの語句(gram-ending.ts)。結びの単調さは repeated-sentence-ending が見る。
|
|
222
|
+
* 品詞が無ければ見分けられないので、これまでどおり数える。
|
|
223
|
+
*/
|
|
224
|
+
const endingInRuns = (occurrences: readonly Occurrence[], inRuns: ReadonlySet<Sentence>): boolean => {
|
|
225
|
+
const owned = (occurrence: Occurrence): boolean | undefined => {
|
|
226
|
+
const endingOnly = judgeAt(occurrence, isSentenceEndingOnly);
|
|
227
|
+
return endingOnly === undefined ? undefined : endingOnly && inRuns.has(occurrence.sentence);
|
|
228
|
+
};
|
|
229
|
+
const judged = occurrences.map(owned).filter((found) => found !== undefined);
|
|
230
|
+
return judged.length > 0 && judged.filter(Boolean).length * 2 >= judged.length;
|
|
211
231
|
};
|
|
212
232
|
|
|
213
233
|
/**
|
|
@@ -222,9 +242,13 @@ const hasPredicate = (occurrences: readonly Occurrence[], unit: LengthUnit): boo
|
|
|
222
242
|
|
|
223
243
|
export const ngramRepetition: Detector = (doc, options): Finding[] => {
|
|
224
244
|
if (wordsOf(doc) < MIN_DOCUMENT_LENGTH[doc.lengthUnit]) return [];
|
|
245
|
+
const inRuns = sentencesInEndingRuns(
|
|
246
|
+
doc.paragraphs,
|
|
247
|
+
(doc.lexicons["calendar-era"] ?? []).map((entry) => entry.pattern),
|
|
248
|
+
);
|
|
225
249
|
const worst = repeatsOf(doc, options.limit)
|
|
226
250
|
.toSorted((left, right) => right.occurrences.length - left.occurrences.length)
|
|
227
|
-
.find(({ occurrences }) => hasPredicate(occurrences, doc.lengthUnit));
|
|
251
|
+
.find(({ occurrences }) => hasPredicate(occurrences, doc.lengthUnit) && !endingInRuns(occurrences, inRuns));
|
|
228
252
|
if (worst === undefined) return [];
|
|
229
253
|
const first = worst.occurrences.find((occurrence) => occurrence.place !== undefined) ?? worst.occurrences[0];
|
|
230
254
|
const at = first?.sentence;
|
|
@@ -10,8 +10,22 @@ type Pair = { readonly open: number; readonly close: number; readonly inner: str
|
|
|
10
10
|
/** A sentence's end right before the bracket: the item opens a sentence of a joined line. */
|
|
11
11
|
const AFTER_SENTENCE = /[。.!?]$/u;
|
|
12
12
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
13
|
+
/**
|
|
14
|
+
* A section's number right before the bracket, with one word after it (第3条) or bare (4): not inside a figure (1,054). A bare
|
|
15
|
+
* figure of three digits is a count, and its bracket part of it (831(16), a headcount with its temporary staff).
|
|
16
|
+
*/
|
|
17
|
+
const AFTER_NUMBER = /(?<![0-90-9,,..])(?:[0-90-9]{1,3}\p{Script=Han}|[0-90-9]{1,2})$/u;
|
|
18
|
+
|
|
19
|
+
/** A court case's number (令和3年(ワ)第5285号): its bracket holds the case's kind and is written as the prose's brackets are. */
|
|
20
|
+
const CASE_YEAR_BEFORE = /[0-90-9]年$/u;
|
|
21
|
+
const CASE_NUMBER_AFTER = /^第[0-90-9]+号/u;
|
|
22
|
+
const YEAR_REACH = 2;
|
|
23
|
+
|
|
24
|
+
/** How far after a case's kind its number (第5285号) is read. */
|
|
25
|
+
const CASE_NUMBER_REACH = 12;
|
|
26
|
+
|
|
27
|
+
const isCaseKind = (source: string, open: number, close: number): boolean =>
|
|
28
|
+
CASE_YEAR_BEFORE.test(source.slice(Math.max(0, open - YEAR_REACH), open)) && CASE_NUMBER_AFTER.test(source.slice(close + 1, close + 1 + CASE_NUMBER_REACH));
|
|
15
29
|
|
|
16
30
|
/** How far before a bracket the address's number is read. */
|
|
17
31
|
const NUMBER_REACH = 5;
|
|
@@ -30,7 +44,8 @@ const valueOf = (inner: string): number | undefined => {
|
|
|
30
44
|
const pairsOf = (source: string, isLabel: (inner: string) => boolean): Pair[] =>
|
|
31
45
|
[...source.matchAll(PAIR)].flatMap((match) => {
|
|
32
46
|
const inner = match[1] ?? match[2] ?? "";
|
|
33
|
-
|
|
47
|
+
const close = match.index + match[0].length - 1;
|
|
48
|
+
return isLabel(inner) && !isCaseKind(source, match.index, close) ? [{ open: match.index, close, inner }] : [];
|
|
34
49
|
});
|
|
35
50
|
|
|
36
51
|
/** The next number of the run, in the same width, opening a word rather than taking a particle. */
|
|
@@ -50,18 +50,35 @@ export const unknownPieces = (words: readonly string[], maxWords: number, isKnow
|
|
|
50
50
|
};
|
|
51
51
|
|
|
52
52
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
53
|
+
* 文書が単独の連なりとして使う名前(組織の名前の安全保障理事会、書式の名前の提供依頼申出書)のうち、字数が min から max までのもの。
|
|
54
|
+
* 名前は、組織の語か題を閉じる語(endings: 理事会、申出書)で閉じ、その前に 2 字以上あるもの。短い語(改革、決議)と、
|
|
55
|
+
* 名前の形をしない普通の複合語(内閣総理大臣、電子化手数料)は、単独で出ても割る手がかりにならない。上限を超える名前はそれ自体を数える。
|
|
56
56
|
*/
|
|
57
|
-
export const standaloneNames = (runs: readonly string[], min: number, max: number,
|
|
57
|
+
export const standaloneNames = (runs: readonly string[], min: number, max: number, endings: readonly string[]): Set<string> =>
|
|
58
58
|
new Set(
|
|
59
59
|
runs.filter((run) => {
|
|
60
60
|
const length = [...run].length;
|
|
61
|
-
return length >= min && length <= max &&
|
|
61
|
+
return length >= min && length <= max && endings.some((ending) => run.endsWith(ending) && length - [...ending].length >= MIN_BODY_LENGTH);
|
|
62
62
|
}),
|
|
63
63
|
);
|
|
64
64
|
|
|
65
|
+
/** 語の、範囲(span)の中の字。連なりの頭や尻で外した語にまたがる語は、範囲の中だけ。 */
|
|
66
|
+
export const surfaceWithin = (token: Token, span: Span): string =>
|
|
67
|
+
token.surface.slice(Math.max(0, span.start - token.span.start), Math.max(0, span.end - token.span.start));
|
|
68
|
+
|
|
69
|
+
const isNumber = (token: Token | undefined): boolean => token?.features?.["NumType"] === "Card";
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 連なりの語(runTokens、その字が words)の [start, end) が、漢数字と街区の単位(units: 丁目)だけの番地(駅前大通二丁目の「二丁目」)か。
|
|
73
|
+
* 算用数字(2丁目)なら連なりはそこで切れるので、読み手はその前と後ろの切れ目を迷わない。後ろが 1 字だけ(新橋二丁目課)なら、
|
|
74
|
+
* その字は番地に付いた語で、切れ目にならない。
|
|
75
|
+
*/
|
|
76
|
+
export const isBlockNumber = (runTokens: readonly Token[], words: readonly string[], piece: Span, units: readonly string[]): boolean =>
|
|
77
|
+
piece.end - piece.start >= 2 &&
|
|
78
|
+
units.includes(words[piece.end - 1] ?? "") &&
|
|
79
|
+
runTokens.slice(piece.start, piece.end - 1).every(isNumber) &&
|
|
80
|
+
[...words.slice(piece.end).join("")].length !== 1;
|
|
81
|
+
|
|
65
82
|
/** words の [from, to) が決まった語(内閣総理大臣)か。 */
|
|
66
83
|
const isKnownWordAt = (words: readonly string[], from: number, to: number, knownWords: ReadonlySet<string>): boolean =>
|
|
67
84
|
from >= 0 && to <= words.length && knownWords.has(words.slice(from, to).join(""));
|
|
@@ -89,8 +106,18 @@ export const isTitledPerson = (
|
|
|
89
106
|
export const isInnerSuffix = (words: readonly string[], index: number, suffixes: readonly string[]): boolean =>
|
|
90
107
|
index > 0 && index < words.length - 1 && suffixes.includes(words[index] ?? "");
|
|
91
108
|
|
|
92
|
-
/**
|
|
93
|
-
export
|
|
109
|
+
/** 連なりの中で語の切れ目を見せる印: 中ほどの並べた物の後ろに付く語と前を指す語(suffixes)と、漢数字の後ろの街区の単位(blockUnits)。 */
|
|
110
|
+
export type BreakMarks = { readonly suffixes: readonly string[]; readonly blockUnits: readonly string[] };
|
|
111
|
+
|
|
112
|
+
/** 連なりの語の [start, end) が、切れ目を見せる印(中ほどの「等」、漢数字の番地)か。 */
|
|
113
|
+
export const isBreakMark = (runTokens: readonly Token[], words: readonly string[], piece: Span, marks: BreakMarks): boolean =>
|
|
114
|
+
(piece.end === piece.start + 1 && isInnerSuffix(words, piece.start, marks.suffixes)) || isBlockNumber(runTokens, words, piece, marks.blockUnits);
|
|
115
|
+
|
|
116
|
+
/** 連なりの語(runTokens、その字が words)が、知っている語(known)か切れ目を見せる印を含むか。含まなければ、連なりをまるごと数える。 */
|
|
117
|
+
export const holdsKnown = (runTokens: readonly Token[], words: readonly string[], known: readonly string[], marks: BreakMarks): boolean => {
|
|
94
118
|
const whole = words.join("");
|
|
95
|
-
return
|
|
119
|
+
return (
|
|
120
|
+
known.some((word) => whole.includes(word)) ||
|
|
121
|
+
words.some((word, index) => isInnerSuffix(words, index, marks.suffixes) || (index > 0 && marks.blockUnits.includes(word) && isNumber(runTokens[index - 1])))
|
|
122
|
+
);
|
|
96
123
|
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// An item's own words: the sentences in its span, less a list nested in it or a note line under it. Pure.
|
|
2
|
+
|
|
3
|
+
import type { ProseDocument, Sentence, Span } from "../plugin.ts";
|
|
4
|
+
|
|
5
|
+
const within = (inner: Span, outer: Span): boolean => inner.start >= outer.start && inner.end <= outer.end;
|
|
6
|
+
|
|
7
|
+
/** The item's own sentences: those in its span, less those of a list nested inside it. */
|
|
8
|
+
export const itemSentences = (doc: ProseDocument, item: Span): Sentence[] => {
|
|
9
|
+
const nested = doc.lists.map((list) => list.span).filter((span) => span.start > item.start && within(span, item));
|
|
10
|
+
return doc.sentences.filter((sentence) => within(sentence.span, item) && !nested.some((span) => within(sentence.span, span)));
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Where an item's own words end: before a line in it that opens, unindented, with a note lead (※応募書類は返却できません。,
|
|
15
|
+
* "Note: …"). Markdown reads such a line as the last item's, but the note is about the whole list.
|
|
16
|
+
*/
|
|
17
|
+
export const itemOwnEnd = (source: string, item: Span, noteLeads: readonly string[]): number => {
|
|
18
|
+
const lineStarts = [...source.slice(item.start, item.end).matchAll(/\n/gu)].map((match) => item.start + match.index + 1);
|
|
19
|
+
const noteLine = lineStarts.find((start) => noteLeads.some((lead) => lead !== "" && source.startsWith(lead, start)));
|
|
20
|
+
return noteLine === undefined ? item.end : noteLine - 1;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/** The sentence up to `end`: a note line under the item is not part of the item's last sentence. */
|
|
24
|
+
const cutAt = (sentence: Sentence, end: number, source: string): Sentence =>
|
|
25
|
+
sentence.span.end <= end
|
|
26
|
+
? sentence
|
|
27
|
+
: {
|
|
28
|
+
...sentence,
|
|
29
|
+
span: { start: sentence.span.start, end },
|
|
30
|
+
text: source.slice(sentence.span.start, end),
|
|
31
|
+
...(sentence.tokens === undefined ? {} : { tokens: sentence.tokens.filter((token) => token.span.end <= end) }),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The item's own sentences, up to a note line under it. */
|
|
35
|
+
export const ownSentences = (doc: ProseDocument, item: Span, noteLeads: readonly string[]): Sentence[] => {
|
|
36
|
+
const end = itemOwnEnd(doc.source, item, noteLeads);
|
|
37
|
+
return itemSentences(doc, item).flatMap((sentence) => (sentence.span.start < end ? [cutAt(sentence, end, doc.source)] : []));
|
|
38
|
+
};
|
|
@@ -11,6 +11,8 @@ export const SENTENCE_END = /[.!?。.!?]["'”’))」』]*$/u;
|
|
|
11
11
|
export const isFiniteVerb = (token: Token): boolean =>
|
|
12
12
|
(token.pos === "VERB" || token.pos === "AUX") && !["Ger", "Part"].includes(token.features?.["VerbForm"] ?? "");
|
|
13
13
|
|
|
14
|
+
export const isVerb = (token: Token): boolean => token.pos === "VERB" || token.pos === "AUX";
|
|
15
|
+
|
|
14
16
|
/** A determiner that opens an object (the, a, your): no noun phrase goes on from its head with one. */
|
|
15
17
|
export const opensObject = (token: Token | undefined): boolean => token?.features?.["PronType"] === "Art" || token?.features?.["Poss"] === "Yes";
|
|
16
18
|
|
|
@@ -24,7 +26,7 @@ export const isPastForm = (token: Token): boolean =>
|
|
|
24
26
|
/ed$/iu.test(token.surface) &&
|
|
25
27
|
(token.lemma !== token.surface.toLowerCase() || !/eed$/iu.test(token.surface));
|
|
26
28
|
|
|
27
|
-
/** A relative pronoun after a noun (text that is …, staff who work …): the verbs
|
|
29
|
+
/** A relative pronoun after a noun (text that is …, staff who work …): the verbs of its clause belong to the noun phrase. */
|
|
28
30
|
const RELATIVE_PRONOUNS = new Set(["that", "which", "who", "whom", "whose"]);
|
|
29
31
|
|
|
30
32
|
const opensRelativeClause = (tokens: readonly Token[], at: number): boolean =>
|
|
@@ -34,16 +36,28 @@ const opensRelativeClause = (tokens: readonly Token[], at: number): boolean =>
|
|
|
34
36
|
const VERB_GROUP = new Set(["VERB", "AUX", "ADV", "PART"]);
|
|
35
37
|
|
|
36
38
|
/**
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
39
|
+
* Where a relative clause's verb group starts: right after the pronoun (that is left justified), or at its first verb when
|
|
40
|
+
* a subject or a word the tagger misread comes first (text that the user types, a layer that wicks moisture). A comma
|
|
41
|
+
* ends the search.
|
|
42
|
+
*/
|
|
43
|
+
const clauseVerbStart = (tokens: readonly Token[], at: number): number => {
|
|
44
|
+
const next = tokens.findIndex((token, index) => index > at && (isVerb(token) || token.pos === "PUNCT"));
|
|
45
|
+
const found = tokens[next];
|
|
46
|
+
return found !== undefined && isVerb(found) ? next : at + 1;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The positions of a relative clause's own verb group: the verbs, with adverbs and "not", of each relative clause (that is
|
|
51
|
+
* left justified, who are assessed). A verb after the group (Team members who are assessed … are eligible) is the item's
|
|
52
|
+
* own.
|
|
40
53
|
*/
|
|
41
54
|
const relativeVerbGroups = (tokens: readonly Token[]): ReadonlySet<number> => {
|
|
42
55
|
const positions = new Set<number>();
|
|
43
56
|
tokens.forEach((_token, at) => {
|
|
44
57
|
if (!opensRelativeClause(tokens, at)) return;
|
|
45
|
-
const
|
|
46
|
-
|
|
58
|
+
const start = clauseVerbStart(tokens, at);
|
|
59
|
+
const end = tokens.findIndex((token, index) => index >= start && !VERB_GROUP.has(token.pos));
|
|
60
|
+
Array.from({ length: Math.max(0, (end === -1 ? tokens.length : end) - start) }, (_unused, offset) => positions.add(start + offset));
|
|
47
61
|
});
|
|
48
62
|
return positions;
|
|
49
63
|
};
|
|
@@ -70,8 +84,6 @@ const isPhraseVerb = (tokens: readonly Token[], at: number, relative: ReadonlySe
|
|
|
70
84
|
return tokens[at - 1]?.pos === "PART" || relative.has(at);
|
|
71
85
|
};
|
|
72
86
|
|
|
73
|
-
export const isVerb = (token: Token): boolean => token.pos === "VERB" || token.pos === "AUX";
|
|
74
|
-
|
|
75
87
|
/** Whether an item opens as a noun phrase and holds only the verbs of that phrase (Text that is at least 11pt). */
|
|
76
88
|
export const isNounPhraseWithVerbs = (tokens: readonly Token[]): boolean => {
|
|
77
89
|
if (!opensNounPhrase(tokens)) return false;
|
|
@@ -1,8 +1,19 @@
|
|
|
1
1
|
// An and / or that joins words inside the last item rather than the list's last two items: an item not finished yet
|
|
2
2
|
// ("on or before", "a flexible and lightweight grid") or a run that repeats the conjunction without commas. Pure.
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
commaBefore,
|
|
5
|
+
contentCount,
|
|
6
|
+
firstAt,
|
|
7
|
+
firstContent,
|
|
8
|
+
hasDeterminer,
|
|
9
|
+
isBareIndefiniteModifier,
|
|
10
|
+
isBareNounPhrase,
|
|
11
|
+
isBarePreposition,
|
|
12
|
+
lastAt,
|
|
13
|
+
lastContent,
|
|
14
|
+
} from "./list-item.ts";
|
|
4
15
|
import type { ItemScope } from "./list-sentence.ts";
|
|
5
|
-
import { lastOf, type View } from "./list-view.ts";
|
|
16
|
+
import { lastOf, secondLastOf, type View } from "./list-view.ts";
|
|
6
17
|
import type { TokenRange } from "./token-column.ts";
|
|
7
18
|
|
|
8
19
|
/**
|
|
@@ -42,3 +53,21 @@ export const repeatsConjunction = (scope: ItemScope, at: number, view: View, aft
|
|
|
42
53
|
const startsLike = (conjunction: number, end: number): boolean => firstContent(scope, { start: conjunction + 1, end })?.pos === kind;
|
|
43
54
|
return kind !== undefined && ((bareBefore && startsLike(before, at)) || (bareLater && startsLike(later, afterItem.end)));
|
|
44
55
|
};
|
|
56
|
+
|
|
57
|
+
/** 冠詞を連れない名詞 1 語だけの項目(taxis)。 */
|
|
58
|
+
const isBareNoun = (scope: ItemScope, item: TokenRange): boolean =>
|
|
59
|
+
contentCount(scope, item) === 1 && firstContent(scope, item)?.pos === "NOUN" && !hasDeterminer(scope, item);
|
|
60
|
+
|
|
61
|
+
const endsPlural = (scope: ItemScope, item: TokenRange): boolean => lastContent(scope, item)?.features?.["Number"] === "Plur";
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 読点の無い or が、複数形の名詞 1 語と名詞 1 語を結び、その前の項目が複数形の名詞で終わらなければ、or は最後の項目の中で 2 つの
|
|
65
|
+
* 選択肢を結ぶ(airfare, train tickets, parking, taxis or rideshares)。前の項目も複数形なら同じ形の並び(apples, pears or plums)、
|
|
66
|
+
* or の前が単数なら選択肢の並び(by card, cheque or cash)。解析器は見慣れない複数形(rideshares)に数を付けないので、or の後ろは数を問わない。
|
|
67
|
+
*/
|
|
68
|
+
export const pairsAlternatives = (scope: ItemScope, at: number, view: View, afterItem: TokenRange): boolean => {
|
|
69
|
+
const [previous, last] = [secondLastOf(view), lastOf(view)];
|
|
70
|
+
if (previous === undefined || last === undefined || commaBefore(scope, at)) return false;
|
|
71
|
+
if (scope.sentence.tokens[at]?.surface.toLowerCase() !== "or") return false;
|
|
72
|
+
return isBareNoun(scope, last.item) && endsPlural(scope, last.item) && isBareNoun(scope, afterItem) && !endsPlural(scope, previous.item);
|
|
73
|
+
};
|
|
@@ -8,6 +8,7 @@ import { endsAsHeadline, markOrSkipped, withoutFootnoteReference, withoutTrailin
|
|
|
8
8
|
import type { BulletList, Detector, Finding, ProseDocument, Sentence, Span, Token } from "../plugin.ts";
|
|
9
9
|
import { isLinkLabel } from "./link-label.ts";
|
|
10
10
|
import { itemLayout, splitAtLeadIns } from "./list-layout.ts";
|
|
11
|
+
import { itemOwnEnd, ownSentences } from "./list-item-own.ts";
|
|
11
12
|
import { closesAsSentence, isFiniteVerb, isNounPhraseWithVerbs, isPastForm, isVerb, NOMINAL, NOUN_HEAD, opensObject } from "./list-item-phrase.ts";
|
|
12
13
|
import { opensWithClause } from "./list-item-clause.ts";
|
|
13
14
|
import { openingSeries, seriesTakesObject } from "./list-verb-series.ts";
|
|
@@ -179,6 +180,8 @@ export type HeadContext = {
|
|
|
179
180
|
readonly subjectPronouns: ReadonlySet<string>;
|
|
180
181
|
/** Words that open a prohibition before a noun (No calls), from prohibition-opener. */
|
|
181
182
|
readonly prohibitionOpeners: ReadonlySet<string>;
|
|
183
|
+
/** Whether the list is numbered: the steps of a procedure. */
|
|
184
|
+
readonly numbered: boolean;
|
|
182
185
|
};
|
|
183
186
|
|
|
184
187
|
export const NO_HEAD_CONTEXT: HeadContext = {
|
|
@@ -188,6 +191,7 @@ export const NO_HEAD_CONTEXT: HeadContext = {
|
|
|
188
191
|
headlineEndings: new Set(),
|
|
189
192
|
subjectPronouns: new Set(),
|
|
190
193
|
prohibitionOpeners: new Set(),
|
|
194
|
+
numbered: false,
|
|
191
195
|
};
|
|
192
196
|
|
|
193
197
|
type HeadReading = "verb" | "noun" | "unjudged" | undefined;
|
|
@@ -297,12 +301,24 @@ const instructionAfterOpener = (tokens: readonly Token[], at: number, context: H
|
|
|
297
301
|
/** A question (Do you have …?, What is …?) opens with its question form, neither an instruction nor a noun phrase. */
|
|
298
302
|
const isQuestion = (sentence: Sentence | undefined): boolean => /\?["'”’)]*$/u.test(sentence?.text.trim() ?? "");
|
|
299
303
|
|
|
304
|
+
/**
|
|
305
|
+
* A later sentence of a numbered step that opens with a verb: the step says what the machine does and then gives the order
|
|
306
|
+
* (The printer feeds a blank label. Throw that label away.), so its noun opening is not a form of its own. A bulleted tip
|
|
307
|
+
* that opens with a statement (There is often more than one way. Discuss tradeoffs.) is still the odd one among orders.
|
|
308
|
+
*/
|
|
309
|
+
const opensWithVerb = (sentence: Sentence, context: HeadContext): boolean => {
|
|
310
|
+
const tokens = sentence.tokens ?? [];
|
|
311
|
+
const at = tokens.findIndex((token) => !SKIPPED_AT_END.has(token.pos));
|
|
312
|
+
return at !== -1 && !isQuestion(sentence) && headAt(tokens, at, context) === "verb";
|
|
313
|
+
};
|
|
314
|
+
|
|
300
315
|
const englishHead = (sentences: readonly Sentence[], context: HeadContext): string | undefined => {
|
|
301
316
|
const tokens = sentences[0]?.tokens ?? [];
|
|
302
317
|
const at = tokens.findIndex((token) => !SKIPPED_AT_END.has(token.pos));
|
|
303
318
|
if (at === -1 || isTitleCase(sentences[0]?.text ?? "") || isQuestion(sentences[0])) return undefined;
|
|
304
319
|
const instruction = instructionAfterOpener(tokens, at, context);
|
|
305
|
-
|
|
320
|
+
const head = headAt(tokens, instruction === -1 ? at : instruction, context);
|
|
321
|
+
return head === "noun" && context.numbered && sentences.slice(1).some((sentence) => opensWithVerb(sentence, context)) ? undefined : head;
|
|
306
322
|
};
|
|
307
323
|
|
|
308
324
|
/** An item's form on an axis, or undefined when its words do not tell. Japanese items have no head axis (the verb comes last). */
|
|
@@ -317,41 +333,6 @@ export const formOf = (axis: Axis, sentences: readonly Sentence[], language: str
|
|
|
317
333
|
return axis === "ending" ? englishEnding(sentences, context) : englishHead(sentences, context);
|
|
318
334
|
};
|
|
319
335
|
|
|
320
|
-
const within = (inner: Span, outer: Span): boolean => inner.start >= outer.start && inner.end <= outer.end;
|
|
321
|
-
|
|
322
|
-
/** The item's own sentences: those in its span, less those of a list nested inside it. */
|
|
323
|
-
export const itemSentences = (doc: ProseDocument, item: Span): Sentence[] => {
|
|
324
|
-
const nested = doc.lists.map((list) => list.span).filter((span) => span.start > item.start && within(span, item));
|
|
325
|
-
return doc.sentences.filter((sentence) => within(sentence.span, item) && !nested.some((span) => within(sentence.span, span)));
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Where an item's own words end: before a line in it that opens, unindented, with a note lead (※応募書類は返却できません。,
|
|
330
|
-
* "Note: …"). Markdown reads such a line as the last item's, but the note is about the whole list.
|
|
331
|
-
*/
|
|
332
|
-
export const itemOwnEnd = (source: string, item: Span, noteLeads: readonly string[]): number => {
|
|
333
|
-
const lineStarts = [...source.slice(item.start, item.end).matchAll(/\n/gu)].map((match) => item.start + match.index + 1);
|
|
334
|
-
const noteLine = lineStarts.find((start) => noteLeads.some((lead) => lead !== "" && source.startsWith(lead, start)));
|
|
335
|
-
return noteLine === undefined ? item.end : noteLine - 1;
|
|
336
|
-
};
|
|
337
|
-
|
|
338
|
-
/** The sentence up to `end`: a note line under the item is not part of the item's last sentence. */
|
|
339
|
-
const cutAt = (sentence: Sentence, end: number, source: string): Sentence =>
|
|
340
|
-
sentence.span.end <= end
|
|
341
|
-
? sentence
|
|
342
|
-
: {
|
|
343
|
-
...sentence,
|
|
344
|
-
span: { start: sentence.span.start, end },
|
|
345
|
-
text: source.slice(sentence.span.start, end),
|
|
346
|
-
...(sentence.tokens === undefined ? {} : { tokens: sentence.tokens.filter((token) => token.span.end <= end) }),
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
/** The item's own sentences, up to a note line under it. */
|
|
350
|
-
const ownSentences = (doc: ProseDocument, item: Span, noteLeads: readonly string[]): Sentence[] => {
|
|
351
|
-
const end = itemOwnEnd(doc.source, item, noteLeads);
|
|
352
|
-
return itemSentences(doc, item).flatMap((sentence) => (sentence.span.start < end ? [cutAt(sentence, end, doc.source)] : []));
|
|
353
|
-
};
|
|
354
|
-
|
|
355
336
|
type Item = { readonly sentences: readonly Sentence[]; readonly form: string };
|
|
356
337
|
|
|
357
338
|
const findingOf = (axis: Axis, item: Item, count: number, of: number, limit: number): Finding => {
|
|
@@ -394,6 +375,7 @@ const wordCount = (sentences: readonly Sentence[]): number =>
|
|
|
394
375
|
/** An item wholly in bold (**Nice to have**): the label of the items after it, not one of them. */
|
|
395
376
|
const BOLD_LABEL = /^(\*\*|__)[^*_]+\1[::]?$/u;
|
|
396
377
|
const LIST_MARKER = /^\s*(?:\d+[.)]|[-*+])\s+/u;
|
|
378
|
+
const NUMBERED_ITEM = /^\s*\d+[.)]\s/u;
|
|
397
379
|
|
|
398
380
|
const isBoldLabel = (written: string): boolean => BOLD_LABEL.test(written.replace(LIST_MARKER, "").trim());
|
|
399
381
|
|
|
@@ -407,7 +389,8 @@ const runsOn = (sentences: readonly Sentence[]): boolean => RUNNING_ON.test(sent
|
|
|
407
389
|
const wordsOf = (doc: ProseDocument, lexicon: string): ReadonlySet<string> =>
|
|
408
390
|
new Set((doc.lexicons[lexicon] ?? []).map((entry) => entry.pattern.toLowerCase()));
|
|
409
391
|
|
|
410
|
-
const headContext = (doc: ProseDocument, items: readonly (readonly Sentence[])[]): HeadContext => ({
|
|
392
|
+
const headContext = (doc: ProseDocument, items: readonly (readonly Sentence[])[], numbered: boolean): HeadContext => ({
|
|
393
|
+
numbered,
|
|
411
394
|
instructionVerbs: wordsOf(doc, "instruction-verb"),
|
|
412
395
|
verbOrAdjective: wordsOf(doc, "verb-or-adjective"),
|
|
413
396
|
headlineEndings: wordsOf(doc, "headline-ending"),
|
|
@@ -451,7 +434,11 @@ const groupFindings = (doc: ProseDocument, itemSpans: readonly Span[], heads: re
|
|
|
451
434
|
if (span === undefined || written === undefined || notes.has(index) || isDatedTitle(written)) return false;
|
|
452
435
|
return isJudged(written, sentences) && !isLinkLabel(written, linkEndings) && !(heads.includes(span) && isGroupLabel(sentences, doc.language));
|
|
453
436
|
});
|
|
454
|
-
const context = headContext(
|
|
437
|
+
const context = headContext(
|
|
438
|
+
doc,
|
|
439
|
+
judged,
|
|
440
|
+
writtenOf.every((written) => NUMBERED_ITEM.test(written)),
|
|
441
|
+
);
|
|
455
442
|
return axesOf(judged).flatMap((axis) => {
|
|
456
443
|
const items = judged.flatMap((sentences) => {
|
|
457
444
|
const form = formOf(axis, sentences, doc.language, context);
|
|
@@ -51,8 +51,9 @@ const startsAt = (tokens: readonly Token[], entries: readonly LexiconEntry[], in
|
|
|
51
51
|
return entryEndsAt(tokens, entry, end) && (entry.position !== "after" || closesSentence(tokens, end));
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
-
/** A comma right after a predicate closes the clause the list stands in (消去し、). */
|
|
55
|
-
const closesClause = (tokens: readonly Token[], index: number): boolean =>
|
|
54
|
+
/** A comma right after a predicate closes the clause the list stands in (消去し、). A space (code masked out, NG の前) does not. */
|
|
55
|
+
const closesClause = (tokens: readonly Token[], index: number): boolean =>
|
|
56
|
+
tokens[index]?.pos === PUNCT && (tokens[index]?.surface.trim() ?? "") !== "" && PREDICATE_POS.has(tokens[index - 1]?.pos ?? "");
|
|
56
57
|
|
|
57
58
|
/** A condition word that closes a clause about an act (行った際, 判断される場合): it follows a predicate. */
|
|
58
59
|
const conditionAt = (tokens: readonly Token[], index: number, words: ScopeWords): boolean =>
|
|
@@ -56,7 +56,7 @@ import { dashBreaks } from "./dash-break.ts";
|
|
|
56
56
|
import { namesDivision, qualifiesPrevious } from "./list-comma-aside.ts";
|
|
57
57
|
import { inExcerpt } from "./quoted-excerpt.ts";
|
|
58
58
|
import { listContinues, walker, type Walk } from "./list-run.ts";
|
|
59
|
-
import { joinsUnfinished, repeatsConjunction } from "./list-joint.ts";
|
|
59
|
+
import { joinsUnfinished, pairsAlternatives, repeatsConjunction } from "./list-joint.ts";
|
|
60
60
|
import { insideSentenceCaseTitle } from "./title-list.ts";
|
|
61
61
|
import { insideByline } from "./byline-list.ts";
|
|
62
62
|
import { withoutOpeningConjunction } from "./list-opening-conjunction.ts";
|
|
@@ -352,6 +352,7 @@ const insideLastItem = (scope: ItemScope, at: number, view: View, afterItem: Tok
|
|
|
352
352
|
sharesNoun(scope, at, view, afterItem) ||
|
|
353
353
|
joinsObjects(scope, at, view, afterItem) ||
|
|
354
354
|
joinsUnfinished(scope, at, view, afterItem) ||
|
|
355
|
+
pairsAlternatives(scope, at, view, afterItem) ||
|
|
355
356
|
repeatsConjunction(scope, at, view, afterItem);
|
|
356
357
|
|
|
357
358
|
/**
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Detector, Finding, MarkupHeading, Paragraph, ProseDocument } from "../plugin.ts";
|
|
1
|
+
import type { BulletList, Detector, Finding, MarkupHeading, Paragraph, ProseDocument, Span } from "../plugin.ts";
|
|
2
2
|
import { isQuotedAt } from "./bold-label.ts";
|
|
3
3
|
import { findingAt, writtenHeadings } from "./markup-finding.ts";
|
|
4
4
|
|
|
@@ -22,11 +22,39 @@ const FIELD_COLON = /[::]/u;
|
|
|
22
22
|
/** Longer than this, a bold line is a sentence set in bold for emphasis, not the name of a section. */
|
|
23
23
|
const MAX_HEADING_CHARS = 80;
|
|
24
24
|
|
|
25
|
+
/** A number that opens an item's title: `(5)`, `1 `, `2.`. A numbered title is a heading whatever its wording. */
|
|
26
|
+
const ITEM_NUMBER = /^(?:[((][0-90-9]+[))]|[0-90-9]+[..]?\s)/u;
|
|
27
|
+
|
|
28
|
+
/** A Japanese reading comma: an unnumbered bold line that joins clauses is a statement set in bold, not a section's name. */
|
|
29
|
+
const CLAUSE_COMMA = /、/u;
|
|
30
|
+
|
|
31
|
+
const isEmphasisedStatement = (text: string): boolean => CLAUSE_COMMA.test(text) && !ITEM_NUMBER.test(text);
|
|
32
|
+
|
|
33
|
+
const OPENING_BRACKET = /[((]/u;
|
|
34
|
+
const CLOSING_BRACKET = /[))]/u;
|
|
35
|
+
|
|
36
|
+
const bracketDepthChange = (char: string): number => {
|
|
37
|
+
if (OPENING_BRACKET.test(char)) return 1;
|
|
38
|
+
return CLOSING_BRACKET.test(char) ? -1 : 0;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** Whether one bracket pair wraps the whole text, `(Example of a README)`: a caption over what follows, not a section. */
|
|
42
|
+
export const isBracketedCaption = (text: string): boolean => {
|
|
43
|
+
const chars = [...text];
|
|
44
|
+
if (chars.length < 2 || !OPENING_BRACKET.test(chars[0] ?? "")) return false;
|
|
45
|
+
const depths = chars.reduce<number[]>((acc, char) => [...acc, (acc.at(-1) ?? 0) + bracketDepthChange(char)], []);
|
|
46
|
+
return depths.at(-1) === 0 && depths.slice(0, -1).every((depth) => depth > 0);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/** The bold run's words, or undefined when the paragraph is not one bold line. */
|
|
50
|
+
const boldLineText = (paragraph: string): string | undefined => BOLD_PARAGRAPH.exec(paragraph.trim())?.[2]?.trim();
|
|
51
|
+
|
|
25
52
|
/** The bold line's words, or undefined when the paragraph is not one bold line that could name a section. */
|
|
26
53
|
export const pseudoHeadingText = (paragraph: string): string | undefined => {
|
|
27
|
-
const text =
|
|
54
|
+
const text = boldLineText(paragraph);
|
|
28
55
|
if (text === undefined || text.length > MAX_HEADING_CHARS) return undefined;
|
|
29
56
|
if (WHOLE_LINK.test(text) || WHOLE_CODE.test(text) || CLOSING_PUNCTUATION.test(text) || FIELD_COLON.test(text)) return undefined;
|
|
57
|
+
if (isBracketedCaption(text) || isEmphasisedStatement(text)) return undefined;
|
|
30
58
|
return text;
|
|
31
59
|
};
|
|
32
60
|
|
|
@@ -41,6 +69,49 @@ const headsBody = (doc: ProseDocument, paragraph: Paragraph, headings: readonly
|
|
|
41
69
|
return !headings.some((heading) => heading.start === nextStart);
|
|
42
70
|
};
|
|
43
71
|
|
|
72
|
+
/** The lines above `offset` in its block, the run of lines no blank line breaks. */
|
|
73
|
+
const linesAbove = (source: string, offset: number): string[] => {
|
|
74
|
+
const above = source.slice(0, offset).split("\n").slice(0, -1);
|
|
75
|
+
const blank = above.findLastIndex((line) => line.trim() === "");
|
|
76
|
+
return above.slice(blank + 1);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** Whether a bracketed caption opens the block: the bold lines under it are titles inside the example it shows. */
|
|
80
|
+
const underCaption = (source: string, offset: number): boolean =>
|
|
81
|
+
linesAbove(source, offset).some((line) => {
|
|
82
|
+
const text = boldLineText(line);
|
|
83
|
+
return text !== undefined && isBracketedCaption(text);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const LIST_ITEM_MARKER = /^\s*(?:[-*+]|\d{1,9}[.)])\s+/u;
|
|
87
|
+
|
|
88
|
+
/** Where the next block after `end` starts, or -1 at the end of the document. */
|
|
89
|
+
const nextBlockStart = (source: string, end: number): number => {
|
|
90
|
+
const next = source.slice(end).search(/\S/u);
|
|
91
|
+
return next === -1 ? -1 : end + next;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const isLinkItem = (source: string, item: Span): boolean => WHOLE_LINK.test(source.slice(item.start, item.end).replace(LIST_ITEM_MARKER, "").trim());
|
|
95
|
+
|
|
96
|
+
/** The list starting at `start` when every item is a link alone: a list of references. */
|
|
97
|
+
const linkListAt = (doc: ProseDocument, start: number): BulletList | undefined => {
|
|
98
|
+
const list = doc.lists.find((candidate) => candidate.span.start === start);
|
|
99
|
+
return list?.itemSpans.every((item) => isLinkItem(doc.source, item)) === true ? list : undefined;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/** Whether plain text, not a heading or another bold line, starts at `start`. */
|
|
103
|
+
const plainTextAt = (doc: ProseDocument, start: number, headings: readonly MarkupHeading[]): boolean => {
|
|
104
|
+
const paragraph = doc.paragraphs.find((candidate) => candidate.span.start === start);
|
|
105
|
+
if (paragraph === undefined || headings.some((heading) => heading.start === start)) return false;
|
|
106
|
+
return boldLineText(doc.source.slice(paragraph.span.start, paragraph.span.end)) === undefined;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/** Whether the bold line labels a list of links and the section's text resumes under it: no section for a heading to open. */
|
|
110
|
+
const labelsLinksMidSection = (doc: ProseDocument, paragraph: Paragraph, headings: readonly MarkupHeading[]): boolean => {
|
|
111
|
+
const list = linkListAt(doc, nextBlockStart(doc.source, paragraph.span.end));
|
|
112
|
+
return list !== undefined && plainTextAt(doc, nextBlockStart(doc.source, list.span.end), headings);
|
|
113
|
+
};
|
|
114
|
+
|
|
44
115
|
/**
|
|
45
116
|
* A bold line standing alone, after the document has begun its outline with real headings: the writer marks sections with
|
|
46
117
|
* headings, and this one was set in bold instead, so a table of contents and a screen reader do not see it.
|
|
@@ -53,7 +124,8 @@ export const pseudoHeading: Detector = (doc): Finding[] => {
|
|
|
53
124
|
return doc.paragraphs.flatMap((paragraph) => {
|
|
54
125
|
if (paragraph.span.start < firstSection.start || isQuotedAt(doc.source, paragraph.span.start)) return [];
|
|
55
126
|
const text = pseudoHeadingText(doc.source.slice(paragraph.span.start, paragraph.span.end));
|
|
56
|
-
if (text === undefined || !headsBody(doc, paragraph, headings)) return [];
|
|
127
|
+
if (text === undefined || !headsBody(doc, paragraph, headings) || underCaption(doc.source, paragraph.span.start)) return [];
|
|
128
|
+
if (labelsLinksMidSection(doc, paragraph, headings)) return [];
|
|
57
129
|
return [findingAt(doc, paragraph.span, { heading: text })];
|
|
58
130
|
});
|
|
59
131
|
};
|