chaffjs 0.41.0 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai-score/human-shares.d.ts.map +1 -1
- package/dist/ai-score/human-shares.js +1 -0
- package/dist/ai-score/human-shares.js.map +1 -1
- package/dist/detectors/acronym-file-type.d.ts +14 -0
- package/dist/detectors/acronym-file-type.d.ts.map +1 -0
- package/dist/detectors/acronym-file-type.js +26 -0
- package/dist/detectors/acronym-file-type.js.map +1 -0
- package/dist/detectors/ai-structure.d.ts +2 -1
- package/dist/detectors/ai-structure.d.ts.map +1 -1
- package/dist/detectors/ai-structure.js +17 -6
- package/dist/detectors/ai-structure.js.map +1 -1
- package/dist/detectors/bibliography-entry.d.ts +3 -0
- package/dist/detectors/bibliography-entry.d.ts.map +1 -0
- package/dist/detectors/bibliography-entry.js +15 -0
- package/dist/detectors/bibliography-entry.js.map +1 -0
- package/dist/detectors/bold-label.d.ts +2 -0
- package/dist/detectors/bold-label.d.ts.map +1 -1
- package/dist/detectors/bold-label.js +4 -0
- package/dist/detectors/bold-label.js.map +1 -1
- package/dist/detectors/char-shape.d.ts +1 -0
- package/dist/detectors/char-shape.d.ts.map +1 -1
- package/dist/detectors/char-shape.js +13 -8
- package/dist/detectors/char-shape.js.map +1 -1
- package/dist/detectors/clause-shape.d.ts +8 -2
- package/dist/detectors/clause-shape.d.ts.map +1 -1
- package/dist/detectors/clause-shape.js +14 -6
- package/dist/detectors/clause-shape.js.map +1 -1
- package/dist/detectors/closing-cliche.d.ts.map +1 -1
- package/dist/detectors/closing-cliche.js +2 -1
- package/dist/detectors/closing-cliche.js.map +1 -1
- package/dist/detectors/contraction-mention.d.ts +2 -0
- package/dist/detectors/contraction-mention.d.ts.map +1 -1
- package/dist/detectors/contraction-mention.js +5 -2
- package/dist/detectors/contraction-mention.js.map +1 -1
- package/dist/detectors/contraction-mix.d.ts.map +1 -1
- package/dist/detectors/contraction-mix.js +4 -3
- package/dist/detectors/contraction-mix.js.map +1 -1
- package/dist/detectors/counter-edge.d.ts +11 -0
- package/dist/detectors/counter-edge.d.ts.map +1 -1
- package/dist/detectors/counter-edge.js +29 -0
- package/dist/detectors/counter-edge.js.map +1 -1
- package/dist/detectors/document-shape.d.ts +6 -1
- package/dist/detectors/document-shape.d.ts.map +1 -1
- package/dist/detectors/document-shape.js +14 -4
- package/dist/detectors/document-shape.js.map +1 -1
- package/dist/detectors/doubled-word.d.ts.map +1 -1
- package/dist/detectors/doubled-word.js +5 -1
- package/dist/detectors/doubled-word.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/ending-refrain.d.ts +9 -0
- package/dist/detectors/ending-refrain.d.ts.map +1 -0
- package/dist/detectors/ending-refrain.js +52 -0
- package/dist/detectors/ending-refrain.js.map +1 -0
- package/dist/detectors/fact-consistency.d.ts.map +1 -1
- package/dist/detectors/fact-consistency.js +5 -1
- package/dist/detectors/fact-consistency.js.map +1 -1
- package/dist/detectors/gram-furniture.d.ts +1 -0
- package/dist/detectors/gram-furniture.d.ts.map +1 -1
- package/dist/detectors/gram-furniture.js +7 -0
- package/dist/detectors/gram-furniture.js.map +1 -1
- package/dist/detectors/gram-heading.d.ts +9 -0
- package/dist/detectors/gram-heading.d.ts.map +1 -0
- package/dist/detectors/gram-heading.js +20 -0
- package/dist/detectors/gram-heading.js.map +1 -0
- package/dist/detectors/gram-predicate.d.ts.map +1 -1
- package/dist/detectors/gram-predicate.js +31 -4
- package/dist/detectors/gram-predicate.js.map +1 -1
- package/dist/detectors/gram-repetition.d.ts.map +1 -1
- package/dist/detectors/gram-repetition.js +18 -3
- package/dist/detectors/gram-repetition.js.map +1 -1
- package/dist/detectors/gram-spread.d.ts +6 -0
- package/dist/detectors/gram-spread.d.ts.map +1 -0
- package/dist/detectors/gram-spread.js +18 -0
- package/dist/detectors/gram-spread.js.map +1 -0
- package/dist/detectors/kanji-run-pieces.d.ts +4 -0
- package/dist/detectors/kanji-run-pieces.d.ts.map +1 -1
- package/dist/detectors/kanji-run-pieces.js +7 -0
- package/dist/detectors/kanji-run-pieces.js.map +1 -1
- package/dist/detectors/lexicon.d.ts.map +1 -1
- package/dist/detectors/lexicon.js +1 -0
- package/dist/detectors/lexicon.js.map +1 -1
- package/dist/detectors/list-lead-in.d.ts +2 -2
- package/dist/detectors/list-lead-in.d.ts.map +1 -1
- package/dist/detectors/list-lead-in.js +28 -3
- package/dist/detectors/list-lead-in.js.map +1 -1
- package/dist/detectors/lookalike-character.d.ts +2 -0
- package/dist/detectors/lookalike-character.d.ts.map +1 -1
- package/dist/detectors/lookalike-character.js +36 -9
- package/dist/detectors/lookalike-character.js.map +1 -1
- package/dist/detectors/mark-shape.d.ts +1 -1
- package/dist/detectors/mark-shape.d.ts.map +1 -1
- package/dist/detectors/mark-shape.js +29 -12
- package/dist/detectors/mark-shape.js.map +1 -1
- package/dist/detectors/number-style.d.ts +7 -1
- package/dist/detectors/number-style.d.ts.map +1 -1
- package/dist/detectors/number-style.js +21 -1
- package/dist/detectors/number-style.js.map +1 -1
- package/dist/detectors/number-unit.d.ts +7 -1
- package/dist/detectors/number-unit.d.ts.map +1 -1
- package/dist/detectors/number-unit.js +20 -6
- package/dist/detectors/number-unit.js.map +1 -1
- package/dist/detectors/particle-run-long-forms.d.ts +7 -0
- package/dist/detectors/particle-run-long-forms.d.ts.map +1 -0
- package/dist/detectors/particle-run-long-forms.js +23 -0
- package/dist/detectors/particle-run-long-forms.js.map +1 -0
- package/dist/detectors/particle-run-quoted-names.d.ts +6 -0
- package/dist/detectors/particle-run-quoted-names.d.ts.map +1 -0
- package/dist/detectors/particle-run-quoted-names.js +18 -0
- package/dist/detectors/particle-run-quoted-names.js.map +1 -0
- package/dist/detectors/register-separate-part.d.ts +9 -1
- package/dist/detectors/register-separate-part.d.ts.map +1 -1
- package/dist/detectors/register-separate-part.js +23 -0
- package/dist/detectors/register-separate-part.js.map +1 -1
- package/dist/detectors/register-voice.d.ts +4 -3
- package/dist/detectors/register-voice.d.ts.map +1 -1
- package/dist/detectors/register-voice.js +11 -4
- package/dist/detectors/register-voice.js.map +1 -1
- package/dist/detectors/register.d.ts.map +1 -1
- package/dist/detectors/register.js +9 -6
- package/dist/detectors/register.js.map +1 -1
- package/dist/detectors/sentence-ending.d.ts +1 -0
- package/dist/detectors/sentence-ending.d.ts.map +1 -1
- package/dist/detectors/sentence-ending.js +7 -3
- package/dist/detectors/sentence-ending.js.map +1 -1
- package/dist/detectors/signals.d.ts.map +1 -1
- package/dist/detectors/signals.js +21 -6
- package/dist/detectors/signals.js.map +1 -1
- package/dist/detectors/stacked-hedge.d.ts +4 -2
- package/dist/detectors/stacked-hedge.d.ts.map +1 -1
- package/dist/detectors/stacked-hedge.js +16 -3
- package/dist/detectors/stacked-hedge.js.map +1 -1
- package/dist/detectors/token-shape.d.ts.map +1 -1
- package/dist/detectors/token-shape.js +13 -1
- package/dist/detectors/token-shape.js.map +1 -1
- package/dist/detectors/unknown-word-context.d.ts +5 -0
- package/dist/detectors/unknown-word-context.d.ts.map +1 -1
- package/dist/detectors/unknown-word-context.js +8 -0
- package/dist/detectors/unknown-word-context.js.map +1 -1
- package/dist/detectors/unknown-word.d.ts.map +1 -1
- package/dist/detectors/unknown-word.js +3 -3
- package/dist/detectors/unknown-word.js.map +1 -1
- package/dist/detectors/weasel-word.d.ts.map +1 -1
- package/dist/detectors/weasel-word.js +23 -4
- package/dist/detectors/weasel-word.js.map +1 -1
- package/dist/facts/era-year.d.ts +2 -0
- package/dist/facts/era-year.d.ts.map +1 -0
- package/dist/facts/era-year.js +7 -0
- package/dist/facts/era-year.js.map +1 -0
- package/dist/facts/fact-scope.d.ts +1 -1
- package/dist/facts/fact-scope.d.ts.map +1 -1
- package/dist/facts/fact-scope.js +18 -8
- package/dist/facts/fact-scope.js.map +1 -1
- package/dist/facts/table-facts.d.ts.map +1 -1
- package/dist/facts/table-facts.js +18 -2
- package/dist/facts/table-facts.js.map +1 -1
- package/dist/own-title-quote.d.ts +7 -0
- package/dist/own-title-quote.d.ts.map +1 -0
- package/dist/own-title-quote.js +19 -0
- package/dist/own-title-quote.js.map +1 -0
- package/dist/rule-of-three.d.ts +5 -1
- package/dist/rule-of-three.d.ts.map +1 -1
- package/dist/rule-of-three.js +11 -2
- package/dist/rule-of-three.js.map +1 -1
- package/dist/structure/definition-use.d.ts.map +1 -1
- package/dist/structure/definition-use.js +3 -1
- package/dist/structure/definition-use.js.map +1 -1
- package/dist/structure-shape/features.d.ts.map +1 -1
- package/dist/structure-shape/features.js +15 -2
- package/dist/structure-shape/features.js.map +1 -1
- package/dist/structure-shape/template-outline.d.ts +7 -0
- package/dist/structure-shape/template-outline.d.ts.map +1 -1
- package/dist/structure-shape/template-outline.js +10 -0
- package/dist/structure-shape/template-outline.js.map +1 -1
- package/dist/topic-clause.d.ts.map +1 -1
- package/dist/topic-clause.js +23 -1
- package/dist/topic-clause.js.map +1 -1
- package/dist/word-use.d.ts.map +1 -1
- package/dist/word-use.js +9 -1
- package/dist/word-use.js.map +1 -1
- package/package.json +3 -3
- package/rules/adverb-overuse.yaml +2 -2
- package/rules/ai-structure.yaml +2 -2
- package/rules/bracket-nesting.yaml +7 -2
- package/rules/bracket-width-consistency.yaml +2 -2
- package/rules/closing-cliche.yaml +3 -3
- package/rules/colon-lead-in.yaml +3 -2
- package/rules/contraction-consistency.yaml +2 -2
- package/rules/doubled-word.yaml +2 -2
- package/rules/excessive-hedging.yaml +2 -1
- package/rules/fact-conflict.yaml +2 -2
- package/rules/lookalike-character.yaml +2 -2
- package/rules/max-kanji-continuous.yaml +4 -3
- package/rules/ngram-repetition.yaml +2 -2
- package/rules/no-doubled-joshi.yaml +2 -2
- package/rules/no-mixed-desumasu.yaml +3 -2
- package/rules/number-style-consistency.yaml +2 -2
- package/rules/number-without-unit.yaml +2 -2
- package/rules/orthographic-variant.yaml +2 -2
- package/rules/repeated-sentence-ending.yaml +2 -2
- package/rules/rule-of-three.yaml +2 -2
- package/rules/title-length.yaml +7 -2
- package/rules/topic-predicate-distance.yaml +2 -2
- package/rules/undefined-acronym.yaml +3 -2
- package/rules/unknown-word.yaml +2 -2
- package/rules/unused-definition.yaml +4 -2
- package/src/ai-score/human-shares.ts +1 -0
- package/src/detectors/acronym-file-type.ts +33 -0
- package/src/detectors/ai-structure.ts +18 -10
- package/src/detectors/bibliography-entry.ts +18 -0
- package/src/detectors/bold-label.ts +6 -0
- package/src/detectors/char-shape.ts +12 -7
- package/src/detectors/clause-shape.ts +14 -12
- package/src/detectors/closing-cliche.ts +6 -1
- package/src/detectors/contraction-mention.ts +6 -2
- package/src/detectors/contraction-mix.ts +5 -3
- package/src/detectors/counter-edge.ts +35 -0
- package/src/detectors/document-shape.ts +17 -6
- package/src/detectors/doubled-word.ts +9 -1
- package/src/detectors/en-shape.ts +15 -1
- package/src/detectors/ending-refrain.ts +66 -0
- package/src/detectors/fact-consistency.ts +4 -1
- package/src/detectors/gram-furniture.ts +9 -0
- package/src/detectors/gram-heading.ts +27 -0
- package/src/detectors/gram-predicate.ts +38 -4
- package/src/detectors/gram-repetition.ts +23 -2
- package/src/detectors/gram-spread.ts +19 -0
- package/src/detectors/kanji-run-pieces.ts +10 -0
- package/src/detectors/lexicon.ts +1 -0
- package/src/detectors/list-lead-in.ts +36 -3
- package/src/detectors/lookalike-character.ts +40 -10
- package/src/detectors/mark-shape.ts +31 -12
- package/src/detectors/number-style.ts +29 -2
- package/src/detectors/number-unit.ts +29 -8
- package/src/detectors/particle-run-long-forms.ts +34 -0
- package/src/detectors/particle-run-quoted-names.ts +27 -0
- package/src/detectors/register-separate-part.ts +32 -1
- package/src/detectors/register-voice.ts +17 -4
- package/src/detectors/register.ts +9 -6
- package/src/detectors/sentence-ending.ts +10 -4
- package/src/detectors/signals.ts +24 -6
- package/src/detectors/stacked-hedge.ts +24 -7
- package/src/detectors/token-shape.ts +14 -1
- package/src/detectors/unknown-word-context.ts +9 -0
- package/src/detectors/unknown-word.ts +4 -2
- package/src/detectors/weasel-word.ts +31 -5
- package/src/facts/era-year.ts +7 -0
- package/src/facts/fact-scope.ts +20 -6
- package/src/facts/table-facts.ts +31 -3
- package/src/own-title-quote.ts +28 -0
- package/src/rule-of-three.ts +16 -2
- package/src/structure/definition-use.ts +4 -1
- package/src/structure-shape/features.ts +18 -2
- package/src/structure-shape/template-outline.ts +12 -0
- package/src/topic-clause.ts +25 -1
- package/src/word-use.ts +10 -1
|
@@ -65,3 +65,38 @@ export const trailingPrefix = (tokens: readonly Token[], end: number, run: strin
|
|
|
65
65
|
if (prefix === undefined || !isPrefix(prefix) || !isNumber(wordAfter(tokens, at))) return undefined;
|
|
66
66
|
return run.endsWith(prefix.surface) ? prefix : undefined;
|
|
67
67
|
};
|
|
68
|
+
|
|
69
|
+
const isBound = (token: Token | undefined): boolean => token?.features?.["Bound"] === "Yes" && !isPrefix(token);
|
|
70
|
+
|
|
71
|
+
/** 語の並び run の、end(添字、含まない)で終わる序数の項(第三十条、第二項)の頭の添字。項でなければ undefined。 */
|
|
72
|
+
const ordinalGroupStart = (run: readonly Token[], end: number): number | undefined => {
|
|
73
|
+
if (!isBound(run[end - 1])) return undefined;
|
|
74
|
+
const numbersStart = run.slice(0, end - 1).findLastIndex((token) => !isNumber(token)) + 1;
|
|
75
|
+
const prefixAt = numbersStart - 1;
|
|
76
|
+
return numbersStart < end - 1 && isPrefix(run[prefixAt]) ? prefixAt : undefined;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** 語の並び run の尻の、序数の項の並びの頭の添字。項が無ければ run.length。 */
|
|
80
|
+
const ordinalChainStart = (run: readonly Token[], end: number = run.length): number => {
|
|
81
|
+
const start = ordinalGroupStart(run, end);
|
|
82
|
+
return start === undefined ? end : ordinalChainStart(run, start);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 漢字の連なり run(文書全体の座標で span)の尻の、序数の項の並び(学校教育法第三十条第二項の「第三十条第二項」)。条や項の番号は
|
|
87
|
+
* 前の名前を指す番号で、前の複合語の一部ではない。項の前に名前が残るときだけ。そうでなければ undefined。
|
|
88
|
+
*/
|
|
89
|
+
export const trailingOrdinals = (
|
|
90
|
+
tokens: readonly Token[],
|
|
91
|
+
span: { readonly start: number; readonly end: number },
|
|
92
|
+
run: string,
|
|
93
|
+
): { readonly surface: string; readonly start: number } | undefined => {
|
|
94
|
+
const words = tokens.filter((token) => token.span.start < span.end && span.start < token.span.end);
|
|
95
|
+
const start = ordinalChainStart(words);
|
|
96
|
+
const first = words[start];
|
|
97
|
+
if (start === 0 || first === undefined) return undefined;
|
|
98
|
+
const chain = words.slice(start);
|
|
99
|
+
const joined = chain.every((token, at) => at === 0 || chain[at - 1]?.span.end === token.span.start);
|
|
100
|
+
const surface = chain.map((token) => token.surface).join("");
|
|
101
|
+
return joined && run.endsWith(surface) ? { surface, start: first.span.start } : undefined;
|
|
102
|
+
};
|
|
@@ -4,7 +4,7 @@ import { newContentMorphemes } from "./content-morphemes.ts";
|
|
|
4
4
|
import { formValueTokens, isFormBlock } from "./form-line.ts";
|
|
5
5
|
import { trigrams } from "./heading-overlap.ts";
|
|
6
6
|
import { findingAt, writtenHeadings } from "./markup-finding.ts";
|
|
7
|
-
import type { Detector, Finding, LengthUnit, MarkupHeading, Paragraph, ProseDocument, Section } from "../plugin.ts";
|
|
7
|
+
import type { Detector, Finding, LengthUnit, MarkupHeading, Paragraph, ProseDocument, Section, Token } from "../plugin.ts";
|
|
8
8
|
|
|
9
9
|
/** A title shorter than this many character trigrams matches any lead by chance. */
|
|
10
10
|
const MIN_TITLE_GRAMS = 4;
|
|
@@ -99,6 +99,17 @@ const QUESTION_END = /[??]["'”’))」』]*$/u;
|
|
|
99
99
|
|
|
100
100
|
export const isQuestionHeading = (heading: string): boolean => QUESTION_END.test(heading.trim());
|
|
101
101
|
|
|
102
|
+
const TRAILING_MARKS: ReadonlySet<string> = new Set(["PUNCT", "SYM"]);
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* A heading that ends on a particle closing a question (…補正することはできますか。): it asks as one ending in ? does. The
|
|
106
|
+
* particles come from the language's question-particle word list; a language without one has none.
|
|
107
|
+
*/
|
|
108
|
+
export const endsWithQuestionParticle = (tokens: readonly Token[], particles: ReadonlySet<string>): boolean => {
|
|
109
|
+
const last = tokens.findLast((token) => !TRAILING_MARKS.has(token.pos));
|
|
110
|
+
return last?.pos === "PART" && particles.has(last.surface);
|
|
111
|
+
};
|
|
112
|
+
|
|
102
113
|
/** A series label in brackets at a heading's end, a number and a colon opening it ("(490: Employee travel)"): the manual's reference. */
|
|
103
114
|
const SERIES_LABEL_END = /[((]\p{N}+[::][^()()]*[))]$/u;
|
|
104
115
|
const QUOTED = /「[^「」]*」/gu;
|
|
@@ -140,18 +151,18 @@ const variantOf = (heading: MarkupHeading, title: MarkupHeading | undefined): st
|
|
|
140
151
|
return heading === title ? "title" : undefined;
|
|
141
152
|
};
|
|
142
153
|
|
|
143
|
-
|
|
144
|
-
const unlabeledOf = (doc: ProseDocument, heading: MarkupHeading): string =>
|
|
145
|
-
doc.sections.find((section) => section.span.start === heading.end)?.unlabeledHeading ?? heading.text;
|
|
154
|
+
const sectionOf = (doc: ProseDocument, heading: MarkupHeading): Section | undefined => doc.sections.find((section) => section.span.start === heading.end);
|
|
146
155
|
|
|
147
156
|
/** Each title or heading longer than the limit. The first top-level heading is the title; every other is a heading. */
|
|
148
157
|
export const titleLength: Detector = (doc, options): Finding[] => {
|
|
149
158
|
const headings = writtenHeadings(doc);
|
|
150
159
|
const title = headings.find((heading) => heading.depth === 1);
|
|
151
160
|
const citedTitleActions = (doc.lexicons["cited-title-action"] ?? []).map((entry) => entry.pattern);
|
|
161
|
+
const questionParticles = new Set((doc.lexicons["question-particle"] ?? []).map((entry) => entry.pattern));
|
|
152
162
|
return headings
|
|
153
|
-
.map((heading) => ({ heading,
|
|
154
|
-
.
|
|
163
|
+
.map((heading) => ({ heading, section: sectionOf(doc, heading) }))
|
|
164
|
+
.map(({ heading, section }) => ({ heading, words: section?.unlabeledHeading ?? heading.text, tokens: section?.headingTokens ?? [] }))
|
|
165
|
+
.filter(({ words, tokens }) => !isParagraphHeading(words) && !isQuestionHeading(words) && !endsWithQuestionParticle(tokens, questionParticles))
|
|
155
166
|
.map(({ heading, words }) => ({ heading, count: headingLength(ownWordsOf(words, citedTitleActions), doc.lengthUnit) }))
|
|
156
167
|
.filter(({ count }) => count > options.limit)
|
|
157
168
|
.map(({ heading, count }) => ({
|
|
@@ -2,6 +2,7 @@ import type { Detector, Finding, Lexicon, Sentence, Span, Token } from "../plugi
|
|
|
2
2
|
import { citedNamesOf, endsCitedName } from "./cited-name.ts";
|
|
3
3
|
import { doubledAcrossAside } from "./doubled-across-aside.ts";
|
|
4
4
|
import { furiganaSpans } from "../furigana.ts";
|
|
5
|
+
import { isFlattenedTable } from "./sentence-parts.ts";
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* 語を書き損じて二度書いた(the the / 資料をを)。消し忘れと、書き換えの途中で残った語(our the platform)の二つ。
|
|
@@ -170,6 +171,9 @@ const FIXED_PHRASE = "fixed-phrase";
|
|
|
170
171
|
const inReading = (sentence: Sentence, readings: readonly Span[], doubled: Doubled): boolean =>
|
|
171
172
|
readings.some((span) => sentence.span.start + span.start <= doubled.first.span.start && doubled.second.span.end <= sentence.span.start + span.end);
|
|
172
173
|
|
|
174
|
+
/** 変換で潰れた表の文では、名詞の重なりは隣り合った二つのセル(株主資本|資本金)。付属語の重なり(をを)は書き損じのまま。 */
|
|
175
|
+
const isCellBoundary = (tableLike: boolean, doubled: Doubled): boolean => tableLike && doubled.first.pos === "NOUN" && doubled.second.pos === "NOUN";
|
|
176
|
+
|
|
173
177
|
export const doubledWord: Detector = (doc, options): Finding[] => {
|
|
174
178
|
const spaced = doc.lengthUnit === "word";
|
|
175
179
|
const allowed = options.lexicon ?? [];
|
|
@@ -179,8 +183,12 @@ export const doubledWord: Detector = (doc, options): Finding[] => {
|
|
|
179
183
|
return doc.sentences.flatMap((sentence) => {
|
|
180
184
|
// The readings are found once per sentence: once per doubled pair costs the pairs times the sentence's length.
|
|
181
185
|
const readings = furiganaSpans(sentence.text);
|
|
186
|
+
const tableLike = isFlattenedTable(sentence.text);
|
|
182
187
|
return [...doubledIn(doc.source, sentence.tokens ?? [], spaced, allowed, phrases), ...(spaced ? [] : doubledAcrossAside(doc.source, sentence.tokens ?? []))]
|
|
183
|
-
.filter(
|
|
188
|
+
.filter(
|
|
189
|
+
(doubled) =>
|
|
190
|
+
!endsCitedName(doubled.first, doubled.second, cited, kinds) && !inReading(sentence, readings, doubled) && !isCellBoundary(tableLike, doubled),
|
|
191
|
+
)
|
|
184
192
|
.map((doubled) => findingOf(sentence, doubled, spaced));
|
|
185
193
|
});
|
|
186
194
|
};
|
|
@@ -15,13 +15,27 @@ const wordsIn = (doc: ProseDocument): number => doc.sentences.reduce((sum, sente
|
|
|
15
15
|
|
|
16
16
|
const isLyAdverb = (token: Token): boolean => token.pos === "ADV" && token.surface.toLowerCase().endsWith("ly");
|
|
17
17
|
|
|
18
|
+
/** 頭だけ大文字の語。全部大文字(ONLY)は強調で、副詞のまま。 */
|
|
19
|
+
const CAPITALISED = /^\p{Lu}\p{Ll}/u;
|
|
20
|
+
|
|
21
|
+
const LETTER = /\p{L}/u;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 文の途中の頭だけ大文字の -ly の語は、解析器が語尾で副詞と当てた名前(July、Kelly)か、題の語(Frequently Asked Questions)。
|
|
25
|
+
* どちらも動詞に畳める副詞ではない。
|
|
26
|
+
*/
|
|
27
|
+
const lyAdverbsOf = (tokens: readonly Token[]): Token[] => {
|
|
28
|
+
const first = tokens.findIndex((token) => LETTER.test(token.surface));
|
|
29
|
+
return tokens.filter((token, at) => isLyAdverb(token) && !(at > first && CAPITALISED.test(token.surface)));
|
|
30
|
+
};
|
|
31
|
+
|
|
18
32
|
/**
|
|
19
33
|
* -ly 副詞は、動詞が弱いことの目印になる。「walked quickly」より「hurried」。
|
|
20
34
|
* 1 つずつは正しいので、件数ではなく密度で見る(bold-density と同じ)。
|
|
21
35
|
*/
|
|
22
36
|
export const adverbDensity: Detector = (doc, options): Finding[] => {
|
|
23
37
|
const words = wordsIn(doc);
|
|
24
|
-
const hits = doc.sentences.flatMap((sentence) => (sentence.tokens ?? []).
|
|
38
|
+
const hits = doc.sentences.flatMap((sentence) => lyAdverbsOf(sentence.tokens ?? []).map((token) => ({ sentence, token })));
|
|
25
39
|
const density = words === 0 ? 0 : Math.round((hits.length / words) * PER);
|
|
26
40
|
const first = hits[0];
|
|
27
41
|
if (words < MIN_WORDS || first === undefined || density <= options.limit) return [];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Sentence, Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
// A refrain: sentences in a row that end with the very same words, the word before the predicate included
|
|
4
|
+
// (いい船でした。…いい船でした。, …隊員がいます。…隊員がいます。). A speaker repeats a whole closing phrase on purpose;
|
|
5
|
+
// it is one figure, not the same ending said again and again (必要です。重要です。, Aが必要です。Bも必要です。). The predicate is a
|
|
6
|
+
// noun or an adjective with its copula, or a verb of being (verbs: いる, ある from the rule's word list); a definition said three
|
|
7
|
+
// times (…風を指します。…風を指します。) stays a run. Pure.
|
|
8
|
+
|
|
9
|
+
const TRAILING_POS: ReadonlySet<string> = new Set(["PUNCT", "SYM"]);
|
|
10
|
+
const TAIL_POS: ReadonlySet<string> = new Set(["AUX", "PART"]);
|
|
11
|
+
const CONTENT_POS: ReadonlySet<string> = new Set(["NOUN", "PROPN", "ADJ"]);
|
|
12
|
+
const VERB = "VERB";
|
|
13
|
+
|
|
14
|
+
const isSpace = (token: Token): boolean => token.surface.trim() === "";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A word that names a thing on its own: not a bound or dependent word (法 in 定額法, こと, の) and not a noun of an act (補助, 支援,
|
|
18
|
+
* 被害). A report repeats an act on purpose of record (…経費の補助を行った。…経費の補助を行った。), and that is the monotone run.
|
|
19
|
+
*/
|
|
20
|
+
const isFree = (token: Token | undefined): token is Token =>
|
|
21
|
+
token !== undefined && token.features?.["Bound"] !== "Yes" && token.features?.["NounType"] !== "Dependent" && token.features?.["VerbForm"] !== "Vnoun";
|
|
22
|
+
|
|
23
|
+
/** A noun or adjective said with its copula (船でした), or a verb of being (隊員がいます). */
|
|
24
|
+
const isPredicate = (token: Token | undefined, verbs: ReadonlySet<string>): boolean =>
|
|
25
|
+
isFree(token) && (CONTENT_POS.has(token.pos) || (token.pos === VERB && verbs.has(token.lemma ?? token.surface)));
|
|
26
|
+
|
|
27
|
+
/** Where the content word before the predicate stands: right before it, or before one particle (隊員|が|います). */
|
|
28
|
+
const argumentAt = (tokens: readonly Token[], head: number): number => {
|
|
29
|
+
const at = tokens[head - 1]?.pos === "ADP" ? head - 2 : head - 1;
|
|
30
|
+
const token = tokens[at];
|
|
31
|
+
return isFree(token) && CONTENT_POS.has(token.pos) ? at : -1;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The closing words of a sentence from the content word before its predicate: いい船でした for …いい船でした。, 隊員がいます for
|
|
36
|
+
* …隊員がいます。. A predicate after a て-form or a bound word (採用しております, こともできます, ないのです) has no such word.
|
|
37
|
+
*/
|
|
38
|
+
export const closingPhraseOf = (sentence: Sentence, verbs: ReadonlySet<string>): string | undefined => {
|
|
39
|
+
const tokens = (sentence.tokens ?? []).filter((token) => !isSpace(token));
|
|
40
|
+
const last = tokens.findLastIndex((token) => !TRAILING_POS.has(token.pos));
|
|
41
|
+
const head = tokens.slice(0, last + 1).findLastIndex((token) => !TAIL_POS.has(token.pos));
|
|
42
|
+
if (head <= 0 || head === last || !isPredicate(tokens[head], verbs)) return undefined;
|
|
43
|
+
const from = argumentAt(tokens, head);
|
|
44
|
+
if (from < 0) return undefined;
|
|
45
|
+
return tokens
|
|
46
|
+
.slice(from, last + 1)
|
|
47
|
+
.map((token) => token.surface)
|
|
48
|
+
.join("");
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** The fewest sentences that make a refrain. Two alike (…の被害もあります。…の被害もあります。) is the same ending said twice. */
|
|
52
|
+
const REFRAIN_MIN = 3;
|
|
53
|
+
|
|
54
|
+
type Block = { readonly phrase: string | undefined; readonly size: number };
|
|
55
|
+
|
|
56
|
+
const blocksOf = (phrases: readonly (string | undefined)[]): Block[] =>
|
|
57
|
+
phrases.reduce<Block[]>((blocks, phrase) => {
|
|
58
|
+
const last = blocks.at(-1);
|
|
59
|
+
return last !== undefined && phrase !== undefined && last.phrase === phrase
|
|
60
|
+
? [...blocks.slice(0, -1), { phrase, size: last.size + 1 }]
|
|
61
|
+
: [...blocks, { phrase, size: 1 }];
|
|
62
|
+
}, []);
|
|
63
|
+
|
|
64
|
+
/** How many beats a run of sentences holds: a refrain (REFRAIN_MIN sentences or more in a row with the same closing phrase) is one. */
|
|
65
|
+
export const beatsOf = (sentences: readonly Sentence[], verbs: ReadonlySet<string>): number =>
|
|
66
|
+
blocksOf(sentences.map((sentence) => closingPhraseOf(sentence, verbs))).reduce((beats, block) => beats + (block.size >= REFRAIN_MIN ? 1 : block.size), 0);
|
|
@@ -11,6 +11,7 @@ import { partsAt, scopedFacts, type ScopedFact } from "../facts/fact-scope.ts";
|
|
|
11
11
|
import { conditionPairConflicts, type ChangeSentence, type ChangeWords, type ConditionWord, type PairConflict, type WordAt } from "../facts/condition-pairs.ts";
|
|
12
12
|
import { scopeConflicts, summaryConflicts, type FactConflict } from "../facts/fact-conflicts.ts";
|
|
13
13
|
import { eventDateConflicts, type EventDateConflict, type EventWord } from "../facts/event-dates.ts";
|
|
14
|
+
import { followsEra } from "../facts/era-year.ts";
|
|
14
15
|
import { retentionConflicts, type LengthMark, type RetentionLength, type RetentionVerb, type RetentionWords } from "../facts/retention-periods.ts";
|
|
15
16
|
import type { DurationUnit } from "../derived/date-arithmetic.ts";
|
|
16
17
|
import { DURATION_LEXICONS, quantitiesOf, type Quantity } from "./derived-numbers.ts";
|
|
@@ -187,10 +188,12 @@ const endWithLongerUnit = (doc: ProseDocument, quantity: Quantity): number => {
|
|
|
187
188
|
return unitStart + longer;
|
|
188
189
|
};
|
|
189
190
|
|
|
191
|
+
/** 元号の年(平成29年法律第51号 の 29年)は暦の年で、保存期間ではない。 */
|
|
190
192
|
const retentionLengthsOf = (doc: ProseDocument, tree: StructureNode): RetentionLength[] =>
|
|
191
193
|
quantitiesOf(tree, doc.source).flatMap((quantity) => {
|
|
192
194
|
const unit = retentionUnitOf(doc, quantity.unit);
|
|
193
|
-
|
|
195
|
+
if (unit === undefined || followsEra(doc.source, quantity.start, patternsOf(doc, "calendar-era"))) return [];
|
|
196
|
+
return [{ start: quantity.start, end: endWithLongerUnit(doc, quantity), amount: quantity.amount, unit }];
|
|
194
197
|
});
|
|
195
198
|
|
|
196
199
|
const retentionVerbsOf = (doc: ProseDocument): RetentionVerb[] =>
|
|
@@ -137,3 +137,12 @@ const JA_PART_REFERENCE = /[表図][\d0-9]+(?:[--..][\d0-9]+)*|第[\
|
|
|
137
137
|
*/
|
|
138
138
|
export const partReferenceSpans = (text: string, unit: LengthUnit): Span[] =>
|
|
139
139
|
unit === "word" ? spansOf(text, LABEL_AND_NUMBER, (match) => PART_LABELS.has((match[1] ?? "").toLowerCase())) : spansOf(text, JA_PART_REFERENCE, () => true);
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 角括弧でくくった画面の部品の名前([許可]、[ブックマーク]、[内容を確認]リンク)と、それに付く助詞一つの範囲。画面の手引きは手順ごとに
|
|
143
|
+
* 違う部品を指して同じ形で書く([…]をクリックすると)。名前にかかる語句と同じく、部品の名前に付いた所からの語句は数えない。
|
|
144
|
+
* 後ろの片仮名の語(ボタン、リンク)も名前に入れる。
|
|
145
|
+
*/
|
|
146
|
+
const SCREEN_LABEL = /[[[【][^[\][]【】]+[\]]】][\p{Script=Katakana}ー]*[をがにでへ]/gu;
|
|
147
|
+
|
|
148
|
+
export const screenLabelSpans = (text: string, unit: LengthUnit): Span[] => (unit === "char" ? spansOf(text, SCREEN_LABEL, () => true) : []);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { LengthUnit } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
/** 語句を数える単位に分ける。英語は語、日本語は文字。 */
|
|
4
|
+
const unitsOf = (key: string, unit: LengthUnit): string[] => (unit === "char" ? [...key] : key.split(" "));
|
|
5
|
+
|
|
6
|
+
const joinUnits = (units: readonly string[], unit: LengthUnit): string => units.join(unit === "char" ? "" : " ");
|
|
7
|
+
|
|
8
|
+
/** 中身のある部分か。日本語は漢字か片仮名が 2 字続く(併記、ボタン)。英語は 2 語以上(create a ConfigMap)。 */
|
|
9
|
+
const CONTENT_RUN = /[\p{Script=Han}\p{Script=Katakana}ー]{2}/u;
|
|
10
|
+
|
|
11
|
+
const hasContent = (units: readonly string[], unit: LengthUnit): boolean => (unit === "char" ? CONTENT_RUN.test(units.join("")) : units.length >= 2);
|
|
12
|
+
|
|
13
|
+
/** units の連続する部分のうち、title にあって中身のある一番長いものの長さ(文字の数)。 */
|
|
14
|
+
const longestNamedRun = (units: readonly string[], title: string, unit: LengthUnit): number => {
|
|
15
|
+
const runs = units.flatMap((_, start) => units.slice(start + 1).map((__, offset) => units.slice(start, start + offset + 2)));
|
|
16
|
+
const named = runs.filter((run) => hasContent(run, unit) && title.includes(joinUnits(run, unit)));
|
|
17
|
+
return Math.max(0, ...named.map((run) => [...joinUnits(run, unit)].length));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 見出しが名付けた句か。語句の文字の過半が、見出しにある中身のある一続きの句(to create a ConfigMap の create a ConfigMap、
|
|
22
|
+
* 氏を併記した氏名 の 氏を併記した)。窓の幅は決まっているので、本文が見出しの句に前置詞や後ろの名詞を付けて使うと、窓は見出しの
|
|
23
|
+
* 句からはみ出す。文字で測るので、短い機能語だけの一続き(How you will be evaluated の will be)では過半に届かない。
|
|
24
|
+
* 題と見出しにある句は文書の主題で、名前と同じく、それに付いた語句も数えない。title は見出しを並べた文字列。
|
|
25
|
+
*/
|
|
26
|
+
export const namedByHeading = (key: string, title: string, unit: LengthUnit): boolean =>
|
|
27
|
+
title.includes(key) || longestNamedRun(unitsOf(key, unit), title, unit) * 2 > [...key].length;
|
|
@@ -25,6 +25,28 @@ const inCoordinatedNoun = (tokens: readonly Token[], index: number, unit: Length
|
|
|
25
25
|
NOUNS.has(tokens[index + 1]?.pos ?? "") &&
|
|
26
26
|
!isDependentNoun(tokens[index + 1]);
|
|
27
27
|
|
|
28
|
+
/** 名詞を並べる中黒(資質・能力)。半角(・)も同じ。 */
|
|
29
|
+
const NAKAGURO = new Set(["・", "・"]);
|
|
30
|
+
|
|
31
|
+
/** index から助動詞を飛ばした先(育成すべき の す から 資質)。 */
|
|
32
|
+
const afterAuxiliaries = (tokens: readonly Token[], index: number): number =>
|
|
33
|
+
tokens[index + 1]?.pos === "AUX" ? afterAuxiliaries(tokens, index + 1) : index + 1;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 日本語で、中黒で並べた名詞(資質・能力)を飾る動詞と助動詞(育成すべき資質・能力)。名詞を中黒で並べた語は文書の決まった用語で、
|
|
37
|
+
* それを飾る節も用語の一部として繰り返される。中黒の無い名詞の前の動詞(〜の中にあるコンポーネント)は言い回しの一部のまま。
|
|
38
|
+
*/
|
|
39
|
+
const modifiesNounPair = (tokens: readonly Token[], index: number, unit: LengthUnit): boolean => {
|
|
40
|
+
if (unit !== "char" || !PREDICATE_POS.has(tokens[index]?.pos ?? "")) return false;
|
|
41
|
+
const head = afterAuxiliaries(tokens, index);
|
|
42
|
+
return (
|
|
43
|
+
NOUNS.has(tokens[head]?.pos ?? "") &&
|
|
44
|
+
!isDependentNoun(tokens[head]) &&
|
|
45
|
+
NAKAGURO.has(tokens[head + 1]?.surface ?? "") &&
|
|
46
|
+
NOUNS.has(tokens[head + 2]?.pos ?? "")
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
28
50
|
/**
|
|
29
51
|
* 英語の大文字だけの語(AI、API)は略語の名前。解析器が動詞と読んでも(AI を ain't の ai と読む)述語ではない。
|
|
30
52
|
* 強調と分かった普通の語(NEVER、START)は除く。
|
|
@@ -42,7 +64,8 @@ const acronymAt = (tokens: readonly Token[], index: number, unit: LengthUnit): b
|
|
|
42
64
|
*/
|
|
43
65
|
const modifiesNoun = (tokens: readonly Token[], index: number, unit: LengthUnit): boolean =>
|
|
44
66
|
(unit === "word" && tokens[index]?.pos === "VERB" && (NOUNS.has(tokens[index + 1]?.pos ?? "") || acronymAt(tokens, index + 1, unit))) ||
|
|
45
|
-
inCoordinatedNoun(tokens, index, unit)
|
|
67
|
+
inCoordinatedNoun(tokens, index, unit) ||
|
|
68
|
+
modifiesNounPair(tokens, index, unit);
|
|
46
69
|
|
|
47
70
|
/** 英語で大文字の名前の一部になった動詞(Procedures Guide、the Location Object)と略語(AI)も、述語ではない。 */
|
|
48
71
|
const namePartAt = (tokens: readonly Token[], index: number, unit: LengthUnit): boolean =>
|
|
@@ -85,11 +108,11 @@ const OBJECT_START = new Set(["DET", "PRON", "NUM"]);
|
|
|
85
108
|
* 読み違い(the federal funds rate の rate、small business subcontracting)。窓の中に機能語があれば言い回し(your contract says)。
|
|
86
109
|
*/
|
|
87
110
|
/** 窓の最後の語が index で、その前が名詞と形容詞だけの用語(先頭の冠詞は許す)か。 */
|
|
88
|
-
const closesTerm = (tokens: readonly Token[], index: number, window: Span): boolean => {
|
|
111
|
+
const closesTerm = (tokens: readonly Token[], index: number, window: Span, minWords: number = MIN_COMPOUND_WORDS): boolean => {
|
|
89
112
|
const touched = tokens.flatMap((token, at) => (overlaps(token, window) ? [at] : []));
|
|
90
113
|
if (touched.at(-1) !== index) return false;
|
|
91
114
|
const before = touched.slice(0, -1).filter((at, order) => !(order === 0 && tokens[at]?.pos === "DET"));
|
|
92
|
-
return before.length >=
|
|
115
|
+
return before.length >= minWords && before.every((at) => COMPOUND_POS.has(tokens[at]?.pos ?? ""));
|
|
93
116
|
};
|
|
94
117
|
|
|
95
118
|
const endsNounCompound = (tokens: readonly Token[], index: number, window: Span, unit: LengthUnit): boolean => {
|
|
@@ -122,11 +145,22 @@ const opensNounSlot = (token: Token | undefined): boolean =>
|
|
|
122
145
|
const gerundAsNoun = (tokens: readonly Token[], index: number, unit: LengthUnit): boolean =>
|
|
123
146
|
unit === "word" && tokens[index]?.features?.["VerbForm"] === "Ger" && opensNounSlot(tokens[index - 1]);
|
|
124
147
|
|
|
148
|
+
const MODALS = new Set(["can", "could", "may", "might", "must", "shall", "should", "will", "would"]);
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 英語で、名詞の主語のすぐ後ろの助動詞 should や must で窓が終わる(the Manager should)。窓にあるのは誰がするかだけで、何をするかは
|
|
152
|
+
* 窓の外で出てくるたびに違う。手順書は手順ごとに役割を名指しし、名指しを繰り返すから誰の務めかが正確に読める。代名詞の主語(you should)は外さない。
|
|
153
|
+
*/
|
|
154
|
+
const modalAfterSubject = (tokens: readonly Token[], index: number, window: Span, unit: LengthUnit): boolean => {
|
|
155
|
+
const token = tokens[index];
|
|
156
|
+
return unit === "word" && token?.pos === "AUX" && MODALS.has(token.lemma ?? token.surface.toLowerCase()) && closesTerm(tokens, index, window, 1);
|
|
157
|
+
};
|
|
158
|
+
|
|
125
159
|
const countsAsPredicate = (tokens: readonly Token[], index: number, window: Span, unit: LengthUnit): boolean => {
|
|
126
160
|
const token = tokens[index];
|
|
127
161
|
if (token === undefined || !PREDICATE_POS.has(token.pos) || !overlaps(token, window)) return false;
|
|
128
162
|
if (modifiesNoun(tokens, index, unit) || namePartAt(tokens, index, unit) || endsNounCompound(tokens, index, window, unit)) return false;
|
|
129
|
-
if (gerundAsNoun(tokens, index, unit) || copulaAfterTerm(tokens, index, window, unit)) return false;
|
|
163
|
+
if (gerundAsNoun(tokens, index, unit) || copulaAfterTerm(tokens, index, window, unit) || modalAfterSubject(tokens, index, window, unit)) return false;
|
|
130
164
|
return !isCut(token, window) || !onlyNamesBeside(tokens, index, window, unit);
|
|
131
165
|
};
|
|
132
166
|
|
|
@@ -11,9 +11,12 @@ import {
|
|
|
11
11
|
quotedClosingSpans,
|
|
12
12
|
quotedSpans,
|
|
13
13
|
referenceClosings,
|
|
14
|
+
screenLabelSpans,
|
|
14
15
|
} from "./gram-furniture.ts";
|
|
15
16
|
import { hasPredicateIn } from "./gram-predicate.ts";
|
|
16
17
|
import { gramPassagesOf } from "./gram-passages.ts";
|
|
18
|
+
import { narrowestStretch, sentencePositions } from "./gram-spread.ts";
|
|
19
|
+
import { namedByHeading } from "./gram-heading.ts";
|
|
17
20
|
import { entryOpeners, filledForms, labelLines, nounFragments, rowEndCells, sectionOpeners } from "./gram-openers.ts";
|
|
18
21
|
import { nameSpans, touchesAny } from "../team-names.ts";
|
|
19
22
|
import type { Detector, Finding, LengthUnit, ProseDocument, Sentence, Span, StructureNode } from "../plugin.ts";
|
|
@@ -81,7 +84,7 @@ type Context = {
|
|
|
81
84
|
/** 数えない範囲: 名前(触れる窓を外す)と、飾り(過半を覆う窓を外す)。 */
|
|
82
85
|
const excludedIn = (context: Context, sentence: Sentence, text: string, source: ReturnType<typeof compacted>): { named: Span[]; mask: boolean[] } => {
|
|
83
86
|
const unit = context.doc.lengthUnit;
|
|
84
|
-
const named = [...nameSpans(text, context.names), ...fieldLabelSpans(text, unit), ...partReferenceSpans(text, unit)];
|
|
87
|
+
const named = [...nameSpans(text, context.names), ...fieldLabelSpans(text, unit), ...partReferenceSpans(text, unit), ...screenLabelSpans(text, unit)];
|
|
85
88
|
const linked = linkedIn(context.doc, sentence, source);
|
|
86
89
|
const mask = furnitureMask(text, linked);
|
|
87
90
|
[...fixedEndingSpans(text, context.endings), ...quotedSpans(text), ...quotedClosingSpans(text, context.closings, linked)].forEach((span) =>
|
|
@@ -152,6 +155,22 @@ const titleOf = (doc: ProseDocument): string =>
|
|
|
152
155
|
|
|
153
156
|
type Repeat = { readonly key: string; readonly occurrences: readonly Occurrence[] };
|
|
154
157
|
|
|
158
|
+
/**
|
|
159
|
+
* 上限を超える数の出現が収まる一番短い範囲が、これより長ければ数えない(文書の長さの単位)。読み手が前の出現を覚えているうちに次が
|
|
160
|
+
* 来ないので、言い直しとは読まれない。長い報告や論文が、測る出来事や量の名前(adopted a budget resolution、posts Grok judged true)を
|
|
161
|
+
* 節をまたいで使うのはこれ。値は chaff-corpus の判定済みの指摘で、正しい指摘の範囲より上、誤検知の範囲より下に置いた。
|
|
162
|
+
* 日本語の値は英語の値を MIN_DOCUMENT_LENGTH と同じ比で置き換えた。
|
|
163
|
+
*/
|
|
164
|
+
const READING_SPAN = { word: 4000, char: 10000 };
|
|
165
|
+
|
|
166
|
+
const spreadThin = (occurrences: readonly Occurrence[], positions: ReadonlyMap<Sentence, number>, limit: number, unit: LengthUnit): boolean => {
|
|
167
|
+
const stretch = narrowestStretch(
|
|
168
|
+
occurrences.map((occurrence) => positions.get(occurrence.sentence) ?? 0),
|
|
169
|
+
limit + 1,
|
|
170
|
+
);
|
|
171
|
+
return stretch !== undefined && stretch > READING_SPAN[unit];
|
|
172
|
+
};
|
|
173
|
+
|
|
155
174
|
/**
|
|
156
175
|
* 範囲(gram-passages.ts)ごとに数える。よくある質問の別々の答えは別の範囲。節の最初の文(gram-openers.ts)にだけ出るものは節のひな形。
|
|
157
176
|
* 節の書き出しは書き手の地の文でもあるので、ほかの所にも出れば書き出しのぶんも数える(節ごとの口癖)。
|
|
@@ -166,11 +185,13 @@ const repeatsOf = (doc: ProseDocument, limit: number): Repeat[] => {
|
|
|
166
185
|
const templates = templatesOf(doc);
|
|
167
186
|
const openers = sectionOpeners(doc.sections, doc.source);
|
|
168
187
|
const title = titleOf(doc);
|
|
188
|
+
const positions = sentencePositions(doc.sentences, doc.lengthUnit);
|
|
169
189
|
return gramPassagesOf(doc.sections).flatMap((sentences) =>
|
|
170
190
|
[...gramsOf(context, sentences).entries()].flatMap(([key, all]) => {
|
|
171
191
|
const occurrences = all.filter((occurrence) => !templates.has(occurrence.sentence));
|
|
172
192
|
const opensEverySection = occurrences.every((occurrence) => openers.has(occurrence.sentence));
|
|
173
|
-
|
|
193
|
+
const counted = occurrences.length > limit && isPhrasing(key, doc.lengthUnit) && !namedByHeading(key, title, doc.lengthUnit) && !opensEverySection;
|
|
194
|
+
return counted && !spreadThin(occurrences, positions, limit, doc.lengthUnit) ? [{ key, occurrences }] : [];
|
|
174
195
|
}),
|
|
175
196
|
);
|
|
176
197
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { lengthOf } from "../measure.ts";
|
|
2
|
+
import type { LengthUnit, Sentence } from "../plugin.ts";
|
|
3
|
+
|
|
4
|
+
/** 文の位置: その文より前の文の長さの合計(文書の長さの単位)。表や符号の塊は文ではないので、読む長さに入らない。 */
|
|
5
|
+
export const sentencePositions = (sentences: readonly Sentence[], unit: LengthUnit): ReadonlyMap<Sentence, number> => {
|
|
6
|
+
const positions = new Map<Sentence, number>();
|
|
7
|
+
sentences.reduce((position, sentence) => {
|
|
8
|
+
positions.set(sentence, position);
|
|
9
|
+
return position + lengthOf(sentence, unit);
|
|
10
|
+
}, 0);
|
|
11
|
+
return positions;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** 位置を count 個含む一番短い範囲の長さ。位置が count 個に満たなければ undefined。 */
|
|
15
|
+
export const narrowestStretch = (positions: readonly number[], count: number): number | undefined => {
|
|
16
|
+
if (count < 1 || positions.length < count) return undefined;
|
|
17
|
+
const sorted = positions.toSorted((left, right) => left - right);
|
|
18
|
+
return Math.min(...sorted.slice(count - 1).map((last, index) => last - (sorted[index] ?? last)));
|
|
19
|
+
};
|
|
@@ -84,3 +84,13 @@ export const isTitledPerson = (
|
|
|
84
84
|
(length) => isKnownWordAt(words, piece.end, piece.end + length, knownWords) || isKnownWordAt(words, piece.start - length, piece.start, knownWords),
|
|
85
85
|
);
|
|
86
86
|
};
|
|
87
|
+
|
|
88
|
+
/** words の index の語が、連なりの頭でも尻でもない、並べた物の後ろに付く語(高齢運転者等専用駐車区間の「等」)か。語の終わりを見せる。 */
|
|
89
|
+
export const isInnerSuffix = (words: readonly string[], index: number, suffixes: readonly string[]): boolean =>
|
|
90
|
+
index > 0 && index < words.length - 1 && suffixes.includes(words[index] ?? "");
|
|
91
|
+
|
|
92
|
+
/** 連なりの語(words)が、知っている語(known)か中ほどの並べた物の後ろに付く語(suffixes)を含むか。含まなければ、連なりをまるごと数える。 */
|
|
93
|
+
export const holdsKnown = (words: readonly string[], known: readonly string[], suffixes: readonly string[]): boolean => {
|
|
94
|
+
const whole = words.join("");
|
|
95
|
+
return known.some((word) => whole.includes(word)) || words.some((_, index) => isInnerSuffix(words, index, suffixes));
|
|
96
|
+
};
|
package/src/detectors/lexicon.ts
CHANGED
|
@@ -110,6 +110,7 @@ export const hedging: Detector = (doc, options): Finding[] => {
|
|
|
110
110
|
hedges: options.lexicon ?? [],
|
|
111
111
|
frames: doc.lexicons["hedge-frame"] ?? [],
|
|
112
112
|
scope: doc.lexicons["hedge-scope"] ?? [],
|
|
113
|
+
complements: doc.lexicons["hedge-complement"] ?? [],
|
|
113
114
|
});
|
|
114
115
|
const reported = new Set(stacked.map((entry) => entry.sentence.span.start));
|
|
115
116
|
const density = hedgingDensity(doc, options).filter((finding) => !reported.has(Number(finding.values["offset"])));
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { BulletList, Detector, DetectorOptions, Finding, ProseDocument, Sentence, Span } from "../plugin.ts";
|
|
1
|
+
import type { BulletList, Detector, DetectorOptions, Finding, LexiconEntry, ProseDocument, Sentence, Span, Token } from "../plugin.ts";
|
|
2
2
|
import { densityFindings } from "./ai-phrasing.ts";
|
|
3
3
|
import { endsWithColon } from "./lead-in.ts";
|
|
4
|
+
import { comparableText, comparableWords, entryIn, entryRanges } from "./lexicon-match.ts";
|
|
4
5
|
import { isMarkdownPath } from "../structure/markdown-path.ts";
|
|
5
6
|
|
|
6
7
|
/** Between a lead-in and its list: only blank space. A thematic break (`***`) separates them; bold marks sit inside the sentence. */
|
|
@@ -108,6 +109,36 @@ const opensWithCode = (source: string, item: Span): boolean => source.slice(item
|
|
|
108
109
|
/** A list whose every item opens with a code span names values, options or members (`omit`: never send …): a parts list, which can stay. */
|
|
109
110
|
const isValueList = (list: BulletList, source: string): boolean => list.itemSpans.length > 1 && list.itemSpans.every((item) => opensWithCode(source, item));
|
|
110
111
|
|
|
112
|
+
/** The language's words of a duty (should, must, なければならない), from the lead-in-duty lexicon. */
|
|
113
|
+
const DUTY_LEXICON = "lead-in-duty";
|
|
114
|
+
|
|
115
|
+
/** A pronoun right after another word opens a clause inside the lead-in (Questions you should ask:, what you must know:). */
|
|
116
|
+
const opensInnerClause = (tokens: readonly Token[], index: number): boolean =>
|
|
117
|
+
tokens[index - 1]?.pos === "PRON" && tokens[index - 2] !== undefined && tokens[index - 2]?.pos !== "PUNCT";
|
|
118
|
+
|
|
119
|
+
/** The duty word as the lead-in's own verb (the lead should:), not inside a clause that names the list (Questions you should ask:). */
|
|
120
|
+
const dutyAsVerb = (sentence: Sentence, entry: LexiconEntry): boolean => {
|
|
121
|
+
const tokens = sentence.tokens;
|
|
122
|
+
if (tokens === undefined || tokens.length === 0) return entryIn(sentence, entry);
|
|
123
|
+
return entryRanges(sentence, entry).some((range) => !opensInnerClause(tokens, range.start));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/** The lead-in without its colon and bold marks at the end. */
|
|
127
|
+
const withoutColon = (text: string): string => {
|
|
128
|
+
const chars = Array.from(text);
|
|
129
|
+
return chars.slice(0, chars.findLastIndex((char) => !LABEL_END.test(char)) + 1).join("");
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/** The duty at the lead-in's end, where a predicate-final language puts it (行わなければならない:). */
|
|
133
|
+
const closesLeadIn = (sentence: Sentence, entry: LexiconEntry): boolean => withoutColon(comparableText(sentence)).endsWith(comparableWords(entry.pattern));
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A lead-in that states a duty (the lead should:, Each entry must include:) hands over what has to be done or contained:
|
|
137
|
+
* the steps or parts of a procedure, a real list that can stay. A word with position: after counts only at the lead-in's end.
|
|
138
|
+
*/
|
|
139
|
+
const statesDuty = (sentence: Sentence, duties: readonly LexiconEntry[]): boolean =>
|
|
140
|
+
duties.some((entry) => (entry.position === "after" ? closesLeadIn(sentence, entry) : dutyAsVerb(sentence, entry)));
|
|
141
|
+
|
|
111
142
|
/**
|
|
112
143
|
* Prose sentences ending in a colon that hand the reader straight to a list (「以下の通りです:」, "Here's what you need:").
|
|
113
144
|
* An item of a list is not prose: 「例: `code`」 ends in a colon once its code is masked, and it leads nowhere.
|
|
@@ -118,6 +149,7 @@ export const colonLeadIns = (
|
|
|
118
149
|
listSpans: readonly Span[],
|
|
119
150
|
source: string,
|
|
120
151
|
markdown = true,
|
|
152
|
+
duties: readonly LexiconEntry[] = [],
|
|
121
153
|
): Sentence[] => {
|
|
122
154
|
const leadIns = lists.flatMap((list) => {
|
|
123
155
|
if (isValueList(list, source)) return [];
|
|
@@ -126,7 +158,7 @@ export const colonLeadIns = (
|
|
|
126
158
|
});
|
|
127
159
|
const fields = fieldLabels(leadIns);
|
|
128
160
|
const namesItsList = (sentence: Sentence): boolean => (markdown ? isBoldNounLabel(sentence, source) : isPlainNounLabel(sentence, source));
|
|
129
|
-
return leadIns.filter((sentence) => !fields.has(labelWords(sentence.text)) && !namesItsList(sentence));
|
|
161
|
+
return leadIns.filter((sentence) => !fields.has(labelWords(sentence.text)) && !namesItsList(sentence) && !statesDuty(sentence, duties));
|
|
130
162
|
};
|
|
131
163
|
|
|
132
164
|
/** Lead-ins in one or two sections are a how-to's procedure or a pair of lists; the rule's why is section after section. */
|
|
@@ -150,7 +182,8 @@ const sectionsHolding = (doc: ProseDocument, leadIns: readonly Sentence[]): numb
|
|
|
150
182
|
*/
|
|
151
183
|
export const colonLeadIn: Detector = (doc, options): Finding[] => {
|
|
152
184
|
const inDocumentLanguage = doc.sentences.filter((sentence) => sentence.embeddedLanguage === undefined);
|
|
153
|
-
const
|
|
185
|
+
const duties = doc.lexicons[DUTY_LEXICON] ?? [];
|
|
186
|
+
const leadIns = colonLeadIns(inDocumentLanguage, doc.lists, doc.listSpans, doc.source, isMarkdownPath(doc.path), duties);
|
|
154
187
|
if (sectionsHolding(doc, leadIns) < minSections(options)) return [];
|
|
155
188
|
const hits = leadIns.map((sentence) => ({ sentence, matched: sentence.text.trim() }));
|
|
156
189
|
return densityFindings(doc, hits, options.limit);
|
|
@@ -53,6 +53,32 @@ export const lookalikesIn = (source: string): Lookalike[] =>
|
|
|
53
53
|
(left, right) => left.span.start - right.span.start,
|
|
54
54
|
);
|
|
55
55
|
|
|
56
|
+
/** Words naming the characters themselves (濁点, NFD, Kangxi). A look-alike next to one is an example shown on purpose. */
|
|
57
|
+
const MENTION_LEXICON = "lookalike-mention";
|
|
58
|
+
|
|
59
|
+
const lineStartBefore = (source: string, at: number): number => source.lastIndexOf("\n", at - 1) + 1;
|
|
60
|
+
|
|
61
|
+
const lineEndAfter = (source: string, at: number): number => {
|
|
62
|
+
const end = source.indexOf("\n", at);
|
|
63
|
+
return end === -1 ? source.length : end;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** The look-alike's line with the line before and the line after it: an example and its explanation may sit on neighbouring lines (a code comment). */
|
|
67
|
+
const linesAround = (source: string, span: Span): string => {
|
|
68
|
+
const lineStart = lineStartBefore(source, span.start);
|
|
69
|
+
const lineEnd = lineEndAfter(source, span.end);
|
|
70
|
+
const start = lineStart === 0 ? 0 : lineStartBefore(source, lineStart - 1);
|
|
71
|
+
const end = lineEnd === source.length ? lineEnd : lineEndAfter(source, lineEnd + 1);
|
|
72
|
+
return source.slice(start, end).toLowerCase();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/** Whether a word that names these characters stands on or next to the look-alike's line (ホ゜ケット のような…濁点のチェック). */
|
|
76
|
+
export const isShownExample = (source: string, span: Span, mentions: readonly string[]): boolean => {
|
|
77
|
+
if (mentions.length === 0) return false;
|
|
78
|
+
const around = linesAround(source, span);
|
|
79
|
+
return mentions.some((mention) => around.includes(mention.toLowerCase()));
|
|
80
|
+
};
|
|
81
|
+
|
|
56
82
|
const QUOTE_REACH = 20;
|
|
57
83
|
|
|
58
84
|
const quoteOf = (source: string, span: Span): string =>
|
|
@@ -63,13 +89,17 @@ const quoteOf = (source: string, span: Span): string =>
|
|
|
63
89
|
|
|
64
90
|
const codesOf = (text: string): string => Array.from(text).map(codePointName).join(" ");
|
|
65
91
|
|
|
66
|
-
export const lookalikeCharacter: Detector = (doc): Finding[] =>
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
92
|
+
export const lookalikeCharacter: Detector = (doc): Finding[] => {
|
|
93
|
+
const mentions = (doc.lexicons[MENTION_LEXICON] ?? []).map((entry) => entry.pattern);
|
|
94
|
+
return lookalikesIn(doc.source)
|
|
95
|
+
.filter((lookalike) => !isShownExample(doc.source, lookalike.span, mentions))
|
|
96
|
+
.map((lookalike) => ({
|
|
97
|
+
rule: "",
|
|
98
|
+
severity: "warning",
|
|
99
|
+
line: 0,
|
|
100
|
+
column: 0,
|
|
101
|
+
quote: quoteOf(doc.source, lookalike.span),
|
|
102
|
+
values: { written: lookalike.written, usual: lookalike.usual, code: codesOf(lookalike.written), offset: lookalike.span.start },
|
|
103
|
+
variant: lookalike.kind,
|
|
104
|
+
}));
|
|
105
|
+
};
|