chaffjs 0.40.0 → 0.42.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/composite-signals.d.ts +6 -0
- package/dist/composite-signals.d.ts.map +1 -0
- package/dist/composite-signals.js +23 -0
- package/dist/composite-signals.js.map +1 -0
- package/dist/detectors/acronym-context.d.ts +4 -0
- package/dist/detectors/acronym-context.d.ts.map +1 -1
- package/dist/detectors/acronym-context.js +45 -0
- package/dist/detectors/acronym-context.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/acronym-series.d.ts +5 -0
- package/dist/detectors/acronym-series.d.ts.map +1 -1
- package/dist/detectors/acronym-series.js +18 -3
- package/dist/detectors/acronym-series.js.map +1 -1
- package/dist/detectors/ai-phrasing.d.ts.map +1 -1
- package/dist/detectors/ai-phrasing.js +7 -1
- package/dist/detectors/ai-phrasing.js.map +1 -1
- 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 +23 -8
- 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/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/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 +12 -4
- package/dist/detectors/contraction-mix.js.map +1 -1
- package/dist/detectors/contraction-set-apart.d.ts +14 -0
- package/dist/detectors/contraction-set-apart.d.ts.map +1 -0
- package/dist/detectors/contraction-set-apart.js +42 -0
- package/dist/detectors/contraction-set-apart.js.map +1 -0
- 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/densest-stretch.d.ts +11 -0
- package/dist/detectors/densest-stretch.d.ts.map +1 -0
- package/dist/detectors/densest-stretch.js +14 -0
- package/dist/detectors/densest-stretch.js.map +1 -0
- package/dist/detectors/doubled-word.d.ts.map +1 -1
- package/dist/detectors/doubled-word.js +8 -4
- package/dist/detectors/doubled-word.js.map +1 -1
- package/dist/detectors/fullwidth-alnum.d.ts +3 -0
- package/dist/detectors/fullwidth-alnum.d.ts.map +1 -1
- package/dist/detectors/fullwidth-alnum.js +11 -2
- package/dist/detectors/fullwidth-alnum.js.map +1 -1
- package/dist/detectors/gram-furniture.d.ts +9 -5
- package/dist/detectors/gram-furniture.d.ts.map +1 -1
- package/dist/detectors/gram-furniture.js +30 -13
- 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-openers.d.ts +11 -0
- package/dist/detectors/gram-openers.d.ts.map +1 -1
- package/dist/detectors/gram-openers.js +42 -1
- package/dist/detectors/gram-openers.js.map +1 -1
- package/dist/detectors/gram-predicate.d.ts.map +1 -1
- package/dist/detectors/gram-predicate.js +19 -1
- 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 +36 -10
- 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/known-correction.d.ts.map +1 -1
- package/dist/detectors/known-correction.js +11 -9
- package/dist/detectors/known-correction.js.map +1 -1
- package/dist/detectors/lexicon.d.ts +2 -2
- package/dist/detectors/lexicon.d.ts.map +1 -1
- package/dist/detectors/lexicon.js +15 -5
- 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 +46 -11
- package/dist/detectors/list-lead-in.js.map +1 -1
- package/dist/detectors/list-run.d.ts.map +1 -1
- package/dist/detectors/list-run.js +16 -2
- package/dist/detectors/list-run.js.map +1 -1
- package/dist/detectors/oxford-comma.d.ts.map +1 -1
- package/dist/detectors/oxford-comma.js +17 -2
- package/dist/detectors/oxford-comma.js.map +1 -1
- package/dist/detectors/phrase-match.d.ts.map +1 -1
- package/dist/detectors/phrase-match.js +3 -2
- package/dist/detectors/phrase-match.js.map +1 -1
- package/dist/detectors/quoted-pattern.d.ts +2 -0
- package/dist/detectors/quoted-pattern.d.ts.map +1 -0
- package/dist/detectors/quoted-pattern.js +13 -0
- package/dist/detectors/quoted-pattern.js.map +1 -0
- package/dist/detectors/reference-entry.d.ts +5 -0
- package/dist/detectors/reference-entry.d.ts.map +1 -1
- package/dist/detectors/reference-entry.js +6 -1
- package/dist/detectors/reference-entry.js.map +1 -1
- package/dist/detectors/relative-pronoun-mix.d.ts.map +1 -1
- package/dist/detectors/relative-pronoun-mix.js +7 -3
- package/dist/detectors/relative-pronoun-mix.js.map +1 -1
- package/dist/detectors/sentence-parts.d.ts +5 -0
- package/dist/detectors/sentence-parts.d.ts.map +1 -1
- package/dist/detectors/sentence-parts.js +8 -0
- package/dist/detectors/sentence-parts.js.map +1 -1
- package/dist/detectors/signals.d.ts.map +1 -1
- package/dist/detectors/signals.js +41 -7
- package/dist/detectors/signals.js.map +1 -1
- package/dist/detectors/superlative-context.d.ts +2 -1
- package/dist/detectors/superlative-context.d.ts.map +1 -1
- package/dist/detectors/superlative-context.js +9 -2
- package/dist/detectors/superlative-context.js.map +1 -1
- package/dist/detectors/title-list.d.ts +7 -0
- package/dist/detectors/title-list.d.ts.map +1 -0
- package/dist/detectors/title-list.js +14 -0
- package/dist/detectors/title-list.js.map +1 -0
- package/dist/detectors/token-shape.d.ts.map +1 -1
- package/dist/detectors/token-shape.js +5 -1
- package/dist/detectors/token-shape.js.map +1 -1
- package/dist/detectors/unknown-word-context.d.ts +2 -0
- package/dist/detectors/unknown-word-context.d.ts.map +1 -1
- package/dist/detectors/unknown-word-context.js +4 -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 +10 -3
- package/dist/detectors/unknown-word.js.map +1 -1
- package/dist/plugin.d.ts +2 -0
- package/dist/plugin.d.ts.map +1 -1
- 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/run.d.ts.map +1 -1
- package/dist/run.js +4 -2
- package/dist/run.js.map +1 -1
- package/dist/structure-shape/faq-sections.d.ts +7 -0
- package/dist/structure-shape/faq-sections.d.ts.map +1 -0
- package/dist/structure-shape/faq-sections.js +14 -0
- package/dist/structure-shape/faq-sections.js.map +1 -0
- package/dist/structure-shape/features.d.ts.map +1 -1
- package/dist/structure-shape/features.js +5 -3
- 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/package.json +3 -3
- package/rules/ai-generated-composite.yaml +2 -2
- package/rules/ai-structure.yaml +4 -4
- package/rules/ai-tell.yaml +2 -2
- package/rules/closing-cliche.yaml +3 -3
- package/rules/colon-lead-in.yaml +3 -2
- package/rules/contraction-consistency.yaml +4 -2
- package/rules/contrast-framing.yaml +3 -3
- package/rules/fullwidth-alnum-consistency.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 +2 -2
- package/rules/oxford-comma-consistency.yaml +2 -2
- package/rules/rule-of-three.yaml +2 -2
- package/rules/topic-predicate-distance.yaml +2 -2
- package/rules/undefined-acronym.yaml +5 -2
- package/rules/unknown-word.yaml +2 -2
- package/rules/unqualified-superlative.yaml +1 -1
- package/src/ai-score/human-shares.ts +1 -0
- package/src/composite-signals.ts +27 -0
- package/src/detectors/acronym-context.ts +58 -0
- package/src/detectors/acronym-file-type.ts +33 -0
- package/src/detectors/acronym-series.ts +22 -3
- package/src/detectors/ai-phrasing.ts +7 -1
- package/src/detectors/ai-structure.ts +24 -12
- package/src/detectors/bibliography-entry.ts +18 -0
- package/src/detectors/char-shape.ts +12 -7
- package/src/detectors/closing-cliche.ts +6 -1
- package/src/detectors/contraction-mention.ts +6 -2
- package/src/detectors/contraction-mix.ts +16 -4
- package/src/detectors/contraction-set-apart.ts +52 -0
- package/src/detectors/counter-edge.ts +35 -0
- package/src/detectors/densest-stretch.ts +21 -0
- package/src/detectors/doubled-word.ts +10 -10
- package/src/detectors/fullwidth-alnum.ts +19 -3
- package/src/detectors/gram-furniture.ts +36 -13
- package/src/detectors/gram-heading.ts +27 -0
- package/src/detectors/gram-openers.ts +54 -1
- package/src/detectors/gram-predicate.ts +24 -1
- package/src/detectors/gram-repetition.ts +44 -9
- package/src/detectors/gram-spread.ts +19 -0
- package/src/detectors/kanji-run-pieces.ts +10 -0
- package/src/detectors/known-correction.ts +15 -8
- package/src/detectors/lexicon.ts +17 -5
- package/src/detectors/list-lead-in.ts +59 -11
- package/src/detectors/list-run.ts +16 -2
- package/src/detectors/oxford-comma.ts +17 -2
- package/src/detectors/phrase-match.ts +3 -2
- package/src/detectors/quoted-pattern.ts +14 -0
- package/src/detectors/reference-entry.ts +8 -1
- package/src/detectors/relative-pronoun-mix.ts +5 -3
- package/src/detectors/sentence-parts.ts +15 -0
- package/src/detectors/signals.ts +47 -7
- package/src/detectors/superlative-context.ts +10 -2
- package/src/detectors/title-list.ts +21 -0
- package/src/detectors/token-shape.ts +20 -16
- package/src/detectors/unknown-word-context.ts +6 -0
- package/src/detectors/unknown-word.ts +10 -2
- package/src/plugin.ts +2 -0
- package/src/rule-of-three.ts +16 -2
- package/src/run.ts +4 -2
- package/src/structure-shape/faq-sections.ts +24 -0
- package/src/structure-shape/features.ts +5 -3
- package/src/structure-shape/template-outline.ts +12 -0
- package/src/topic-clause.ts +25 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// A bibliographic entry written as a line of a note, as Japanese papers print them (SIST 02): fields closed by a period,
|
|
2
|
+
// "著者. 題. 出版者, 2025, p. 369." The words of the entry are the cited work's, not the writer's. Pure.
|
|
3
|
+
|
|
4
|
+
/** A period that closes a field: a full-width one, or an ASCII one after a letter that is not ASCII, a closing quote or bracket, followed by a space. */
|
|
5
|
+
const FIELD_END = /.(?=\S)|(?<=\P{ASCII})\.(?=\s)/gu;
|
|
6
|
+
|
|
7
|
+
/** What a reference carries and a sentence of prose does not: a year, a quoted title in “”, a link, or a page. */
|
|
8
|
+
const PUBLICATION_MARKS: readonly RegExp[] = [/(?<!\d)(?:1[89]|20)\d{2}(?!\d)/u, /“[^”]/u, /https?:\/\//u, /\bpp?\. ?\d/u];
|
|
9
|
+
|
|
10
|
+
/** Fields closed before the end of the text that it takes to be an entry and not one sentence: author, title, then the rest. */
|
|
11
|
+
const MIN_FIELD_ENDS = 2;
|
|
12
|
+
|
|
13
|
+
/** Whether the text is a bibliographic entry: two fields or more closed by a period inside it, and a mark only a reference carries. */
|
|
14
|
+
export const isBibliographyEntry = (text: string): boolean => {
|
|
15
|
+
const body = text.trim();
|
|
16
|
+
const fieldEnds = [...body.matchAll(FIELD_END)].filter((match) => match.index + match[0].length < body.length);
|
|
17
|
+
return fieldEnds.length >= MIN_FIELD_ENDS && PUBLICATION_MARKS.some((mark) => mark.test(body));
|
|
18
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Detector, DetectorOptions, DocumentProfile, Finding, ProseDocument, Sentence, Span, Token } from "../plugin.ts";
|
|
2
2
|
import { compacted } from "./gram-place.ts";
|
|
3
|
-
import { leadingCounter, trailingPrefix } from "./counter-edge.ts";
|
|
3
|
+
import { leadingCounter, trailingOrdinals, trailingPrefix } from "./counter-edge.ts";
|
|
4
4
|
import { trailingAddressee, type AddresseeSuffix } from "./addressee-edge.ts";
|
|
5
5
|
import { leadingPrefix, trailingTitle, type AddresseeWords } from "./addressee-title-edge.ts";
|
|
6
6
|
import {
|
|
@@ -26,7 +26,8 @@ import { maskSpans } from "../mask.ts";
|
|
|
26
26
|
import { quotedSpans } from "../quoted-span.ts";
|
|
27
27
|
import { nameSpans } from "../team-names.ts";
|
|
28
28
|
import { isIn } from "../lexicon-genres.ts";
|
|
29
|
-
import { isTitledPerson, standaloneNames, unknownPieces } from "./kanji-run-pieces.ts";
|
|
29
|
+
import { holdsKnown, isInnerSuffix, isTitledPerson, standaloneNames, unknownPieces } from "./kanji-run-pieces.ts";
|
|
30
|
+
import { isBibliographyEntry } from "./bibliography-entry.ts";
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
33
|
* 文字の並びを見る検出。漢字の連なりが住所か名前かだけは、形態素解析の固有名詞と数で決める。
|
|
@@ -207,12 +208,13 @@ const isAddresseeLine = (run: KanjiRun, words: NameWords): boolean => {
|
|
|
207
208
|
/** 宛名の行か、名前の並びの 1 項目。どちらも役職を付けた名前を 1 つずつ並べたもので、尻の役職は名前の外。 */
|
|
208
209
|
const isNamedEntry = (run: KanjiRun, words: NameWords): boolean => run.listItem || isAddresseeLine(run, words);
|
|
209
210
|
|
|
210
|
-
/** 尻の外す語:
|
|
211
|
+
/** 尻の外す語: 数に付く接頭の語か条や項の番号か宛先に付く語と、宛名の行ならその前の役職(日本博物館協会会長殿の「会長殿」)。 */
|
|
211
212
|
const tailOf = (tokens: readonly Token[], run: KanjiRun, span: Span, words: NameWords): Tail | undefined => {
|
|
212
213
|
const prefix = trailingPrefix(tokens, span.end, run.text);
|
|
213
214
|
const suffix =
|
|
214
215
|
prefix === undefined
|
|
215
|
-
? (
|
|
216
|
+
? (trailingOrdinals(tokens, span, run.text) ??
|
|
217
|
+
trailingConjunction(tokens, span.start, span.end, run.text) ??
|
|
216
218
|
trailingAddressee(tokens, span.end, run.text, words.addressee) ??
|
|
217
219
|
trailingAddressee(tokens, span.end, run.text, words.listSuffixes))
|
|
218
220
|
: { surface: prefix.surface, start: prefix.span.start };
|
|
@@ -270,16 +272,18 @@ const surfaceWithin = (token: Token, span: Span): string =>
|
|
|
270
272
|
* 連なりの、知っている語(文書がほかで単独で使う組織の名前と法令の名前、連なりを閉じる決まった語、職名の前後の人の名前)を外した残りのうち、最も長いもの。
|
|
271
273
|
* 安全保障理事会常任理事国は(安全保障理事会を単独で使う文書なら)「常任理事国」、日本国内閣総理大臣は「日本国」を数える。
|
|
272
274
|
* 名前に普通の語が続く連なり(日本弁護士連合会臨時総会決議)は、名前がその文書で単独で使われていなければ、まるごと数える。
|
|
275
|
+
* 連なりの中ほどの、並べた物の後ろに付く語(高齢運転者等専用駐車区間の「等」)は、語の終わりを見せるので、その前後を別に数える。
|
|
273
276
|
*/
|
|
274
277
|
const longestUnknown = (tokens: readonly Token[], rest: Span, keptHead: string, context: RunContext): string => {
|
|
275
278
|
const runTokens = coveringOf(tokens, rest).flatMap((index) => tokens.slice(index, index + 1));
|
|
276
279
|
const words = runTokens.map((token) => surfaceWithin(token, rest));
|
|
277
280
|
const whole = words.join("");
|
|
278
281
|
// A run as long as the text holds no known word; reading it word by word would take a pass per word.
|
|
279
|
-
if (![...context.terms, ...context.words.knownWords]
|
|
282
|
+
if (!holdsKnown(words, [...context.terms, ...context.words.knownWords], context.words.listSuffixes)) return keptHead + whole;
|
|
280
283
|
const isKnown = (start: number, end: number): boolean => {
|
|
281
284
|
const piece = words.slice(start, end).join("");
|
|
282
285
|
return (
|
|
286
|
+
(end === start + 1 && isInnerSuffix(words, start, context.words.listSuffixes)) ||
|
|
283
287
|
context.terms.has(piece) ||
|
|
284
288
|
(end === words.length && context.words.knownWords.has(piece)) ||
|
|
285
289
|
isTitledPerson(runTokens, words, { start, end }, context.words.knownWords, context.maxKnownWords)
|
|
@@ -346,7 +350,7 @@ const nameWordsOf = (doc: ProseDocument): NameWords => {
|
|
|
346
350
|
closingForms: (doc.lexicons["organisation-form"] ?? []).filter((entry) => entry.position !== "before").map((entry) => entry.pattern),
|
|
347
351
|
units: patterns("organisation-unit"),
|
|
348
352
|
};
|
|
349
|
-
|
|
353
|
+
return {
|
|
350
354
|
topUnits: new Set(patterns("prefecture-unit")),
|
|
351
355
|
organisation,
|
|
352
356
|
title: { endings: patterns("title-ending") },
|
|
@@ -358,7 +362,6 @@ const nameWordsOf = (doc: ProseDocument): NameWords => {
|
|
|
358
362
|
listSuffixes: patterns("list-suffix"),
|
|
359
363
|
knownWords: new Set(patterns("kanji-known-word")),
|
|
360
364
|
};
|
|
361
|
-
return words;
|
|
362
365
|
};
|
|
363
366
|
|
|
364
367
|
const contextOf = (doc: ProseDocument, options: DetectorOptions): RunContext => {
|
|
@@ -385,10 +388,12 @@ const contextOf = (doc: ProseDocument, options: DetectorOptions): RunContext =>
|
|
|
385
388
|
/**
|
|
386
389
|
* 漢字が続くと、どこで語が切れるのか読み手が探すことになる。
|
|
387
390
|
* 「情報処理推進機構認定試験」は 12 字。ひらがなを 1 つ挟むだけで読める。
|
|
391
|
+
* 文献の記述(著者. 題. 出版者, 2025.)の連なりは、引いた文献の題や名前で、書き手が割れないので数えない。
|
|
388
392
|
*/
|
|
389
393
|
export const kanjiRun: Detector = (doc, options): Finding[] => {
|
|
390
394
|
const context = contextOf(doc, options);
|
|
391
395
|
return doc.sentences
|
|
396
|
+
.filter((sentence) => !isBibliographyEntry(sentence.text))
|
|
392
397
|
.map((sentence) => ({ sentence, run: longestKanji(sentence, context) }))
|
|
393
398
|
.filter(({ run }) => run.length > options.limit)
|
|
394
399
|
.map(({ sentence, run }) => ({
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Detector, Finding, LexiconEntry, Paragraph, Sentence } from "../plugin.ts";
|
|
2
2
|
import { closingSentences } from "../closing-paragraph.ts";
|
|
3
|
+
import { entriesForGenre } from "../lexicon-genres.ts";
|
|
3
4
|
import { exhortationIn, exhortationWords } from "./closing-exhortation.ts";
|
|
4
5
|
import { comparableText, entryIn, entryOpens } from "./lexicon-match.ts";
|
|
5
6
|
|
|
@@ -61,7 +62,11 @@ export const closingClicheWords = (lexicon: readonly LexiconEntry[]): Words => (
|
|
|
61
62
|
|
|
62
63
|
/** 結び(最後の節の終わりの段落)の定型の言い回し。limit は指摘に要る文の数。 */
|
|
63
64
|
export const closingCliche: Detector = (doc, options): Finding[] => {
|
|
64
|
-
const hits = closingClicheHits(
|
|
65
|
+
const hits = closingClicheHits(
|
|
66
|
+
closingSentences(doc.sections, doc.paragraphs),
|
|
67
|
+
doc.paragraphs,
|
|
68
|
+
closingClicheWords(entriesForGenre(options.lexicon ?? [], options.genre)),
|
|
69
|
+
);
|
|
65
70
|
if (hits.length < options.limit) return [];
|
|
66
71
|
return hits.map((hit) => ({
|
|
67
72
|
rule: "",
|
|
@@ -4,8 +4,9 @@ import type { Span } from "../plugin.ts";
|
|
|
4
4
|
|
|
5
5
|
// A short phrase in quotation marks ("Do Not Disturb", ‘we’re going to x’) is a name or an example: the form is mentioned,
|
|
6
6
|
// not used, so it says nothing about the document's register. Spoken words carry punctuation ("We don't know," she
|
|
7
|
-
// said)
|
|
8
|
-
// someone else's wording twice over.
|
|
7
|
+
// said) are counted, unless they are quoted inside another quotation (a platform's label in a quoted sentence), which is
|
|
8
|
+
// someone else's wording twice over. No form inside quotation marks is pointed at: the writer cannot change a quotation.
|
|
9
|
+
// The text must already have straight apostrophes, so a ’ left in it closes a quotation.
|
|
9
10
|
|
|
10
11
|
const MENTION_MARKS: Readonly<Record<string, string>> = { "“": "”", '"': '"', "‘": "’" };
|
|
11
12
|
/** More words than this in quotation marks is a quoted sentence, not a phrase named as an example. */
|
|
@@ -36,6 +37,9 @@ export const withoutMentions = (text: string): string => {
|
|
|
36
37
|
);
|
|
37
38
|
};
|
|
38
39
|
|
|
40
|
+
/** The text with everything in quotation marks blanked out. */
|
|
41
|
+
export const withoutQuotations = (text: string): string => blankSpans(text, quotedSpans(text, MENTION_MARKS));
|
|
42
|
+
|
|
39
43
|
/**
|
|
40
44
|
* The text (which starts at offset in the document) with the document ranges that fall in it blanked out.
|
|
41
45
|
* Ranges are in document coordinates; those outside the text change nothing.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { pairMinority, type PairUses } from "./contraction-minority.ts";
|
|
2
|
-
import { blankRanges, isTermList, withoutMentions } from "./contraction-mention.ts";
|
|
2
|
+
import { blankRanges, isTermList, withoutMentions, withoutQuotations } from "./contraction-mention.ts";
|
|
3
3
|
import { stressedWords } from "./contraction-emphasis.ts";
|
|
4
|
+
import { noticeSentences, talksAboutContractions, warningSentences } from "./contraction-set-apart.ts";
|
|
4
5
|
import { escapeRegExp, straightApostrophes } from "../orthography.ts";
|
|
5
6
|
import type { Detector, Finding, ProseDocument, Sentence, Span } from "../plugin.ts";
|
|
6
7
|
|
|
@@ -35,9 +36,11 @@ const comparableText = (text: string): string => {
|
|
|
35
36
|
|
|
36
37
|
/**
|
|
37
38
|
* 指してよい字。一語だけ強調した形(does **not**)は強勢のために選んだ形なので指さない。
|
|
38
|
-
*
|
|
39
|
+
* 引用符の中の形(引いた標語、回答の文言、人の言葉)は書き手が変えられないので指さない。
|
|
40
|
+
* どちらも、どちらが多いかを数えるときは数える。
|
|
39
41
|
*/
|
|
40
|
-
const pointableText = (sentence: Sentence, stressed: readonly Span[]): string =>
|
|
42
|
+
const pointableText = (sentence: Sentence, stressed: readonly Span[]): string =>
|
|
43
|
+
comparableText(withoutQuotations(straightApostrophes(blankRanges(sentence.text, sentence.span.start, stressed))));
|
|
41
44
|
|
|
42
45
|
/** 語のすぐ後ろが句読点か文の終わり。名前として挙げた語("Use cannot, instead of can't.")はそう並ぶ。 */
|
|
43
46
|
const bareUses = (text: string, word: string): number => [...text.matchAll(new RegExp(`\\b${escapeRegExp(word)}\\b(?=\\s*(?:\\p{P}|$))`, "gu"))].length;
|
|
@@ -72,13 +75,22 @@ const emphasisOf = (doc: ProseDocument): readonly Span[] => (doc.prose === undef
|
|
|
72
75
|
|
|
73
76
|
export const contractionMix: Detector = (doc, options): Finding[] => {
|
|
74
77
|
const stressed = emphasisOf(doc);
|
|
78
|
+
const notices = noticeSentences(doc.paragraphs, doc.lexicons["notice-phrase"] ?? []);
|
|
79
|
+
const terms = doc.lexicons["contraction-term"] ?? [];
|
|
80
|
+
const warnings = warningSentences(
|
|
81
|
+
doc.paragraphs,
|
|
82
|
+
doc.source,
|
|
83
|
+
(doc.lexicons["warning-label"] ?? []).map((entry) => entry.pattern),
|
|
84
|
+
);
|
|
85
|
+
const isWriters = (sentence: Sentence): boolean => !notices.has(sentence) && !talksAboutContractions(sentence, terms);
|
|
75
86
|
// 文ごとに数える。文をつないで数えると、箇条書きの「What we do」と次の項目「not every…」が「do not」と数えられる。
|
|
76
|
-
const texts = doc.sentences.map((sentence) => comparableText(sentence.text));
|
|
87
|
+
const texts = doc.sentences.map((sentence) => (isWriters(sentence) ? comparableText(sentence.text) : ""));
|
|
77
88
|
const mixed = (options.lexicon ?? []).flatMap((entry) => {
|
|
78
89
|
const pair = entry.instead_of === undefined ? undefined : mixedPair(texts, { short: entry.pattern.toLowerCase(), long: entry.instead_of.toLowerCase() });
|
|
79
90
|
return pair === undefined || pair.count > options.limit ? [] : [pair];
|
|
80
91
|
});
|
|
81
92
|
const hits = doc.sentences.flatMap((sentence) => {
|
|
93
|
+
if (!isWriters(sentence) || warnings.has(sentence)) return [];
|
|
82
94
|
const pointable = pointableText(sentence, stressed);
|
|
83
95
|
const pair = mixed.find((candidate) => usesMinority(pointable, candidate));
|
|
84
96
|
return pair === undefined ? [] : [{ sentence, pair }];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { entryIn } from "./lexicon-match.ts";
|
|
2
|
+
import { warningTextStart } from "../warning-action.ts";
|
|
3
|
+
import type { LexiconEntry, Paragraph, Sentence } from "../plugin.ts";
|
|
4
|
+
|
|
5
|
+
// Sentences whose forms say nothing about the writer's choice of register. Pure: reads paragraphs, sentences and the source;
|
|
6
|
+
// the words come from the language's notice-phrase, contraction-term and warning-label lexicons.
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The sentences of a paragraph of disclaimer wording (…does not constitute or imply its endorsement…). A publisher appends
|
|
10
|
+
* it to every page in its own fixed wording, so its forms are not the writer's: they are neither counted nor pointed at.
|
|
11
|
+
*/
|
|
12
|
+
export const noticeSentences = (paragraphs: readonly Paragraph[], cues: readonly LexiconEntry[]): Set<Sentence> =>
|
|
13
|
+
new Set(paragraphs.flatMap((paragraph) => (paragraph.sentences.some((sentence) => cues.some((cue) => entryIn(sentence, cue))) ? paragraph.sentences : [])));
|
|
14
|
+
|
|
15
|
+
/** A sentence about contractions (Avoid negative contractions like can't and don't.) names its forms; it does not use them. */
|
|
16
|
+
export const talksAboutContractions = (sentence: Sentence, terms: readonly LexiconEntry[]): boolean => terms.some((term) => entryIn(sentence, term));
|
|
17
|
+
|
|
18
|
+
/** A Hugo shortcode ({{< warning >}}, {{< alert title="Warning" >}}) or a Docusaurus admonition (:::caution) opening a paragraph. */
|
|
19
|
+
const SHORTCODE_OPENERS = ["{{<", "{{%"];
|
|
20
|
+
const SHORTCODE_TITLE = /\btitle\s*=\s*["']([^"']+)["']/u;
|
|
21
|
+
const ADMONITION = /^:::\s*([\w-]+)/u;
|
|
22
|
+
|
|
23
|
+
/** The name and the attributes of the shortcode opening the text, or undefined when none does. */
|
|
24
|
+
const shortcodeAt = (text: string): { readonly name: string; readonly attributes: string } | undefined => {
|
|
25
|
+
const end = text.indexOf("}}");
|
|
26
|
+
if (!SHORTCODE_OPENERS.some((opener) => text.startsWith(opener)) || end < 0) return undefined;
|
|
27
|
+
const [name = "", ...attributes] = text
|
|
28
|
+
.slice(3, end - 1)
|
|
29
|
+
.trim()
|
|
30
|
+
.split(" ");
|
|
31
|
+
return { name, attributes: attributes.join(" ") };
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const isLabel = (word: string | undefined, labels: readonly string[]): boolean =>
|
|
35
|
+
word !== undefined && labels.some((label) => label.toLowerCase() === word.trim().toLowerCase());
|
|
36
|
+
|
|
37
|
+
/** Whether a paragraph's text opens as a warning: a label (Warning:, > [!WARNING]), a shortcode or an admonition naming one. */
|
|
38
|
+
const opensWarning = (text: string, labels: readonly string[]): boolean => {
|
|
39
|
+
if (warningTextStart(text, labels) !== undefined) return true;
|
|
40
|
+
const shortcode = shortcodeAt(text);
|
|
41
|
+
if (shortcode !== undefined) return isLabel(shortcode.name, labels) || isLabel(SHORTCODE_TITLE.exec(shortcode.attributes)?.[1], labels);
|
|
42
|
+
return isLabel(ADMONITION.exec(text)?.[1], labels);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The sentences of a warning (Do not run this on an internet-facing cluster.). A warning spells the negative out for
|
|
47
|
+
* stress, as a writer bolds a word: counted, never pointed at.
|
|
48
|
+
*/
|
|
49
|
+
export const warningSentences = (paragraphs: readonly Paragraph[], source: string, labels: readonly string[]): Set<Sentence> =>
|
|
50
|
+
new Set(
|
|
51
|
+
paragraphs.flatMap((paragraph) => (opensWarning(source.slice(paragraph.span.start, paragraph.span.end).trimStart(), labels) ? paragraph.sentences : [])),
|
|
52
|
+
);
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The keys of the densest stretch, and the index of its first item. */
|
|
2
|
+
export type Stretch<K> = { readonly keys: ReadonlySet<K>; readonly first: number };
|
|
3
|
+
|
|
4
|
+
const keysIn = <K>(hitsPerItem: readonly (readonly K[])[], start: number, window: number): Set<K> => new Set(hitsPerItem.slice(start, start + window).flat());
|
|
5
|
+
|
|
6
|
+
const scoreOf = <K>(keys: ReadonlySet<K>, weightOf: (key: K) => number): number => [...keys].reduce((sum, key) => sum + weightOf(key), 0);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The run of at most `window` consecutive items whose distinct keys weigh the most, starting at an item with a key.
|
|
10
|
+
* The earliest wins a tie. When every hit fits in one window, that is all of them, from the first hit.
|
|
11
|
+
*/
|
|
12
|
+
export const densestStretch = <K>(hitsPerItem: readonly (readonly K[])[], window: number, weightOf: (key: K) => number): Stretch<K> | undefined =>
|
|
13
|
+
hitsPerItem.reduce<{ best: Stretch<K> | undefined; score: number }>(
|
|
14
|
+
(acc, hits, start) => {
|
|
15
|
+
if (hits.length === 0) return acc;
|
|
16
|
+
const keys = keysIn(hitsPerItem, start, window);
|
|
17
|
+
const score = scoreOf(keys, weightOf);
|
|
18
|
+
return acc.best !== undefined && score <= acc.score ? acc : { best: { keys, first: start }, score };
|
|
19
|
+
},
|
|
20
|
+
{ best: undefined, score: -Infinity },
|
|
21
|
+
).best;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Detector, Finding, Lexicon, Sentence, Token } from "../plugin.ts";
|
|
1
|
+
import type { Detector, Finding, Lexicon, Sentence, Span, Token } from "../plugin.ts";
|
|
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";
|
|
@@ -167,10 +167,8 @@ const DOCUMENT_KIND = "document-kind";
|
|
|
167
167
|
const FIXED_PHRASE = "fixed-phrase";
|
|
168
168
|
|
|
169
169
|
/** 語の読みの括弧(凛々(りり)しい)の中の重なり。読みは語の書き方ではなく、仮名を重ねて読む語もある。 */
|
|
170
|
-
const inReading = (sentence: Sentence, doubled: Doubled): boolean =>
|
|
171
|
-
|
|
172
|
-
(span) => sentence.span.start + span.start <= doubled.first.span.start && doubled.second.span.end <= sentence.span.start + span.end,
|
|
173
|
-
);
|
|
170
|
+
const inReading = (sentence: Sentence, readings: readonly Span[], doubled: Doubled): boolean =>
|
|
171
|
+
readings.some((span) => sentence.span.start + span.start <= doubled.first.span.start && doubled.second.span.end <= sentence.span.start + span.end);
|
|
174
172
|
|
|
175
173
|
export const doubledWord: Detector = (doc, options): Finding[] => {
|
|
176
174
|
const spaced = doc.lengthUnit === "word";
|
|
@@ -178,9 +176,11 @@ export const doubledWord: Detector = (doc, options): Finding[] => {
|
|
|
178
176
|
const cited = citedNamesOf(doc.structure);
|
|
179
177
|
const kinds = new Set((doc.lexicons[DOCUMENT_KIND] ?? []).map((entry) => entry.pattern));
|
|
180
178
|
const phrases = doc.lexicons[FIXED_PHRASE] ?? [];
|
|
181
|
-
return doc.sentences.flatMap((sentence) =>
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
179
|
+
return doc.sentences.flatMap((sentence) => {
|
|
180
|
+
// The readings are found once per sentence: once per doubled pair costs the pairs times the sentence's length.
|
|
181
|
+
const readings = furiganaSpans(sentence.text);
|
|
182
|
+
return [...doubledIn(doc.source, sentence.tokens ?? [], spaced, allowed, phrases), ...(spaced ? [] : doubledAcrossAside(doc.source, sentence.tokens ?? []))]
|
|
183
|
+
.filter((doubled) => !endsCitedName(doubled.first, doubled.second, cited, kinds) && !inReading(sentence, readings, doubled))
|
|
184
|
+
.map((doubled) => findingOf(sentence, doubled, spaced));
|
|
185
|
+
});
|
|
186
186
|
};
|
|
@@ -3,6 +3,7 @@ import { minorityWithin } from "../orthography.ts";
|
|
|
3
3
|
import { QUOTATION_MARKS, isWithinAny, quotedSpans } from "../quoted-span.ts";
|
|
4
4
|
import { isCitation } from "./kutoten-consistency.ts";
|
|
5
5
|
import { ALNUM_RUN, FULLWIDTH, HALFWIDTH } from "./alnum-number.ts";
|
|
6
|
+
import { gluedNumberSpans } from "./sentence-parts.ts";
|
|
6
7
|
|
|
7
8
|
const LETTER = /[A-Za-zA-Za-z]/u;
|
|
8
9
|
|
|
@@ -79,7 +80,14 @@ const continuesVersion = (text: string, start: number, run: string): boolean =>
|
|
|
79
80
|
|
|
80
81
|
const isListItemNumber = (source: string, offset: number): boolean => LIST_ITEM_HEAD.test(source.slice(source.lastIndexOf("\n", offset - 1) + 1, offset));
|
|
81
82
|
|
|
82
|
-
export type AlnumRun = {
|
|
83
|
+
export type AlnumRun = {
|
|
84
|
+
readonly kind: AlnumKind;
|
|
85
|
+
readonly fullwidth: boolean;
|
|
86
|
+
readonly written: string;
|
|
87
|
+
readonly offset: number;
|
|
88
|
+
/** 表のセルを続けてしまった数字(8,457 と 95.7 の 8,45795.7)の中。幅は書き手のものだが、セルの切れ目が分からず桁の数は当てにならない。 */
|
|
89
|
+
readonly glued: boolean;
|
|
90
|
+
};
|
|
83
91
|
|
|
84
92
|
/**
|
|
85
93
|
* 文の中の英字・数字の並び。全角と半角が混ざった並び、鉤括弧で引いたものの中、項目と注の番号、箇条書きの頭の番号、版の番号の続き(TLS1.2 の 2)は数えない。
|
|
@@ -87,6 +95,7 @@ export type AlnumRun = { readonly kind: AlnumKind; readonly fullwidth: boolean;
|
|
|
87
95
|
*/
|
|
88
96
|
export const alnumRunsIn = (sentence: Sentence, source: string): AlnumRun[] => {
|
|
89
97
|
const quoted: readonly Span[] = quotedSpans(sentence.text, QUOTATION_MARKS);
|
|
98
|
+
const glued = gluedNumberSpans(sentence.text);
|
|
90
99
|
return [...sentence.text.matchAll(ALNUM_RUN)]
|
|
91
100
|
.filter((match) => FULLWIDTH.test(match[0]) || HALFWIDTH.test(match[0]))
|
|
92
101
|
.filter((match) => !isWithinAny(quoted, { start: match.index, end: match.index + match[0].length }))
|
|
@@ -96,7 +105,13 @@ export const alnumRunsIn = (sentence: Sentence, source: string): AlnumRun[] => {
|
|
|
96
105
|
.filter((match) => !isInApostropheWord(sentence.text, match.index, match.index + match[0].length))
|
|
97
106
|
.filter((match) => !isNumberLabel(sentence.text, match.index + match[0].length, match[0]))
|
|
98
107
|
.filter((match) => !continuesVersion(sentence.text, match.index, match[0]))
|
|
99
|
-
.map((match) => ({
|
|
108
|
+
.map((match) => ({
|
|
109
|
+
kind: kindOf(match[0]),
|
|
110
|
+
fullwidth: FULLWIDTH.test(match[0]),
|
|
111
|
+
written: match[0],
|
|
112
|
+
offset: sentence.span.start + match.index,
|
|
113
|
+
glued: isWithinAny(glued, { start: match.index, end: match.index + match[0].length }),
|
|
114
|
+
}));
|
|
100
115
|
};
|
|
101
116
|
|
|
102
117
|
const KINDS: readonly AlnumKind[] = ["letter", "letters", "digit", "digits"];
|
|
@@ -157,6 +172,7 @@ const findingOf = ({ sentence, run }: Located, count: number, of: number, limit:
|
|
|
157
172
|
/**
|
|
158
173
|
* 英字・数字を全角で書くか半角で書くか。組(英字一字・英字の語・数字一字・数字の並び)ごとに、文書の中で少ないほうを指摘する。
|
|
159
174
|
* 少ないほうが組の limit パーセントを超えるほど多いなら、文書が使い分けている(数え方の語や見出しの書き方)と見て言わない。
|
|
175
|
+
* 表のセルを続けてしまった数字の中の並びは、書き方の票には入れるが、少ないほうとしては指摘しない(8,45795.7 の 8 は一桁の数ではない)。
|
|
160
176
|
* どちらが正しいかは決めない。
|
|
161
177
|
*/
|
|
162
178
|
export const fullwidthAlnum: Detector = (doc, options): Finding[] => {
|
|
@@ -169,7 +185,7 @@ export const fullwidthAlnum: Detector = (doc, options): Finding[] => {
|
|
|
169
185
|
const ofKind = located.filter(({ run }) => run.kind === kind);
|
|
170
186
|
const odd = minorityWithin(ofKind, ({ run }) => run.fullwidth, options.limit);
|
|
171
187
|
return odd
|
|
172
|
-
.filter(({ run }) => !isHalfwidthEnglishWord(run))
|
|
188
|
+
.filter(({ run }) => !isHalfwidthEnglishWord(run) && !run.glued)
|
|
173
189
|
.filter(departsFromHabit(ofKind, odd))
|
|
174
190
|
.map((entry) => findingOf(entry, odd.length, ofKind.length, options.limit));
|
|
175
191
|
});
|
|
@@ -59,22 +59,36 @@ const QUOTE_CLOSING_MAX = 12;
|
|
|
59
59
|
*/
|
|
60
60
|
const QUOTE_CLOSING = new RegExp(String.raw`[」』]([をがにでへ][^「」『』、,。.をがにでへ]{1,${String(QUOTE_CLOSING_MAX - 1)}}[。.]?)\s*$`, "u");
|
|
61
61
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
62
|
+
/** リンクの文字のすぐ後ろの結び([ノンス](…)を参照してください。)。題を受ける助詞と述語だけなのは引いた題の後ろと同じ。 */
|
|
63
|
+
const LINK_CLOSING = new RegExp(String.raw`[をがにでへ][^「」『』、,。.をがにでへ]{1,${String(QUOTE_CLOSING_MAX - 1)}}[。.]?\s*$`, "u");
|
|
64
|
+
|
|
65
|
+
/** 文の中の位置がリンクの文字か。リンクの無い文は、どの位置もリンクではない。 */
|
|
66
|
+
export type Linked = (index: number) => boolean;
|
|
67
|
+
|
|
68
|
+
const NOT_LINKED: Linked = () => false;
|
|
69
|
+
|
|
70
|
+
/** 引いた題か、リンクの文字の直後で結ぶ範囲。手引きや参照のページは、別のページを指すたびに同じ結びで指す。 */
|
|
71
|
+
const closingAfterReference = (text: string, linked: Linked): Span | undefined => {
|
|
72
|
+
const quoted = QUOTE_CLOSING.exec(text);
|
|
73
|
+
if (quoted !== null) return { start: quoted.index + 1, end: quoted.index + quoted[0].length };
|
|
74
|
+
const afterLink = LINK_CLOSING.exec(text);
|
|
75
|
+
return afterLink !== null && afterLink.index > 0 && linked(afterLink.index - 1)
|
|
76
|
+
? { start: afterLink.index, end: afterLink.index + afterLink[0].length }
|
|
77
|
+
: undefined;
|
|
65
78
|
};
|
|
66
79
|
|
|
67
|
-
const closingTextOf = (text: string): string | undefined => {
|
|
68
|
-
const span =
|
|
80
|
+
const closingTextOf = (text: string, linked: Linked): string | undefined => {
|
|
81
|
+
const span = closingAfterReference(text, linked);
|
|
69
82
|
return span === undefined ? undefined : text.slice(span.start, span.end).trimEnd();
|
|
70
83
|
};
|
|
71
84
|
|
|
72
85
|
/**
|
|
73
|
-
* 文書が参照の書式として使う結び:
|
|
74
|
-
*
|
|
86
|
+
* 文書が参照の書式として使う結び: 引いた題かリンクの直後で結ぶことが、その結びで終わる文の過半である結び(詳しくは「…」を確認してください。
|
|
87
|
+
* 詳しくは[…](…)を参照してください。)。引いた名前の後ろにたまたま来た書き手の結び(「生涯活躍のまち」を推進している。のほかは整備を
|
|
88
|
+
* 推進している。)は、ほとんどが題の後ろではない。linked は texts と同じ順の、文ごとのリンクの位置。
|
|
75
89
|
*/
|
|
76
|
-
export const referenceClosings = (texts: readonly string[]): ReadonlySet<string> => {
|
|
77
|
-
const afterQuote = texts.flatMap((text) => closingTextOf(text) ?? []);
|
|
90
|
+
export const referenceClosings = (texts: readonly string[], linked: readonly Linked[] = []): ReadonlySet<string> => {
|
|
91
|
+
const afterQuote = texts.flatMap((text, index) => closingTextOf(text, linked[index] ?? NOT_LINKED) ?? []);
|
|
78
92
|
return new Set(
|
|
79
93
|
[...new Set(afterQuote)].filter((closing) => {
|
|
80
94
|
const quoted = afterQuote.filter((each) => each === closing).length;
|
|
@@ -85,12 +99,12 @@ export const referenceClosings = (texts: readonly string[]): ReadonlySet<string>
|
|
|
85
99
|
};
|
|
86
100
|
|
|
87
101
|
/**
|
|
88
|
-
*
|
|
102
|
+
* 引いた題かリンクで終わる文の結び(詳しくは「入力可能な文字」を確認してください。)の範囲。
|
|
89
103
|
* closings(referenceClosings)にある結びだけを、参照の書式として数えない。
|
|
90
104
|
*/
|
|
91
|
-
export const quotedClosingSpans = (text: string, closings: ReadonlySet<string
|
|
92
|
-
const span =
|
|
93
|
-
const closing = closingTextOf(text);
|
|
105
|
+
export const quotedClosingSpans = (text: string, closings: ReadonlySet<string>, linked: Linked = NOT_LINKED): Span[] => {
|
|
106
|
+
const span = closingAfterReference(text, linked);
|
|
107
|
+
const closing = closingTextOf(text, linked);
|
|
94
108
|
return span !== undefined && closing !== undefined && closings.has(closing) ? [span] : [];
|
|
95
109
|
};
|
|
96
110
|
|
|
@@ -123,3 +137,12 @@ const JA_PART_REFERENCE = /[表図][\d0-9]+(?:[--..][\d0-9]+)*|第[\
|
|
|
123
137
|
*/
|
|
124
138
|
export const partReferenceSpans = (text: string, unit: LengthUnit): Span[] =>
|
|
125
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;
|
|
@@ -55,8 +55,13 @@ const IDENTIFIER_HEADING = /^(?:`[^`]+`|【[\w.:/-]+】|\[[\w.:/-]+\])$/u;
|
|
|
55
55
|
const DEFINITION_LINE = /(?:^|\n)[ \t]*(?:[-*+][ \t]+)?:[ \t]*$/u;
|
|
56
56
|
const DEFINITION_REACH = 12;
|
|
57
57
|
|
|
58
|
+
/** 平文の仕様書の定義の並び(「 qu: The OPTIONAL "qu" parameter …」)。行の頭の短い名前とコロンのあとに空白が 2 つ以上続く。 */
|
|
59
|
+
const PLAIN_DEFINITION = /^\s*[\p{L}\p{N}_.-]{1,24}:[ \t]{2,}\S/u;
|
|
60
|
+
|
|
58
61
|
const opensDefinition = (sentence: Sentence, source: string): boolean =>
|
|
59
|
-
/^\s*:\s/u.test(sentence.text) ||
|
|
62
|
+
/^\s*:\s/u.test(sentence.text) ||
|
|
63
|
+
PLAIN_DEFINITION.test(sentence.text) ||
|
|
64
|
+
DEFINITION_LINE.test(source.slice(Math.max(0, sentence.span.start - DEFINITION_REACH), sentence.span.start));
|
|
60
65
|
|
|
61
66
|
const BLANK_LINE = /\n[ \t]*\n/u;
|
|
62
67
|
|
|
@@ -119,3 +124,51 @@ export const rowEndCells = (sentences: readonly Sentence[], fragments: ReadonlyS
|
|
|
119
124
|
sentences.slice(index - CELLS_BEFORE, index).every((cell) => fragments.has(cell)) &&
|
|
120
125
|
LINE_END_NEXT.test(source.slice(sentence.span.end, sentence.span.end + LOOKAHEAD)),
|
|
121
126
|
);
|
|
127
|
+
|
|
128
|
+
const LABEL_END = /[::]\s*$/u;
|
|
129
|
+
const LINE_START_BEFORE = /(?:^|\n)[ \t]*$/u;
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 一行を占め、コロンで終わり、文書の中にまったく同じ形で 2 度以上出る文(The following rule has the force of law:)。後ろの枠や
|
|
133
|
+
* 一覧に付ける札で、項目ごとに同じ札を付けるのは書式。言い回しを変えた導入(When you join a meeting, follow these guidelines:)は札ではない。
|
|
134
|
+
*/
|
|
135
|
+
export const labelLines = (sentences: readonly Sentence[], source: string): Sentence[] => {
|
|
136
|
+
const lines = sentences.filter(
|
|
137
|
+
(sentence) =>
|
|
138
|
+
LABEL_END.test(sentence.text) &&
|
|
139
|
+
LINE_START_BEFORE.test(source.slice(Math.max(0, sentence.span.start - LINE_START_REACH), sentence.span.start)) &&
|
|
140
|
+
LINE_END_NEXT.test(source.slice(sentence.span.end, sentence.span.end + LOOKAHEAD)),
|
|
141
|
+
);
|
|
142
|
+
const counts = new Map<string, number>();
|
|
143
|
+
lines.forEach((line) => counts.set(line.text.trim(), (counts.get(line.text.trim()) ?? 0) + 1));
|
|
144
|
+
return lines.filter((line) => (counts.get(line.text.trim()) ?? 0) > 1);
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/** 数字を伏せた形。数字だけが違う文は、同じ書式に数を入れたもの。 */
|
|
148
|
+
const DIGITS = /[\d0-9]+/gu;
|
|
149
|
+
const HAS_DIGIT = /[\d0-9]/u;
|
|
150
|
+
|
|
151
|
+
/** 文の中の行(変換が報告の欄を一つの文につないでも、欄は行ごとに分かれている)。 */
|
|
152
|
+
const linesOf = (sentence: Sentence): string[] =>
|
|
153
|
+
sentence.text
|
|
154
|
+
.split("\n")
|
|
155
|
+
.map((line) => line.trim())
|
|
156
|
+
.filter((line) => HAS_DIGIT.test(line));
|
|
157
|
+
|
|
158
|
+
/** 文が行を独り占めしているか(行の頭か箇条書きの印の後ろから始まり、行の終わりで終わる)。段落の中の文は欄ではない。 */
|
|
159
|
+
const ownsLines = (sentence: Sentence, source: string): boolean =>
|
|
160
|
+
LINE_START.test(source.slice(Math.max(0, sentence.span.start - LINE_START_REACH), sentence.span.start)) &&
|
|
161
|
+
LINE_END_NEXT.test(source.slice(sentence.span.end, sentence.span.end + LOOKAHEAD));
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 行を独り占めし、数字だけが違う行が 2 度以上あって入った数が違うもの(There are currently 17 committers and 17 PMC members in this
|
|
165
|
+
* project.)。報告の書式を機械が埋めた欄で、書き手の言い回しではない。まったく同じ文の繰り返し(同じ説明を項目ごとに写したもの)と、
|
|
166
|
+
* 段落の中で数だけ替えて言い直す文(In case 3, we will look into it again.)は書式ではない。
|
|
167
|
+
*/
|
|
168
|
+
export const filledForms = (sentences: readonly Sentence[], source: string): Sentence[] => {
|
|
169
|
+
const formOf = (line: string): string => line.replace(DIGITS, "0");
|
|
170
|
+
const fillings = new Map<string, Set<string>>();
|
|
171
|
+
const owning = sentences.filter((sentence) => ownsLines(sentence, source));
|
|
172
|
+
owning.flatMap(linesOf).forEach((line) => fillings.set(formOf(line), (fillings.get(formOf(line)) ?? new Set()).add(line)));
|
|
173
|
+
return owning.filter((sentence) => linesOf(sentence).some((line) => (fillings.get(formOf(line))?.size ?? 0) > 1));
|
|
174
|
+
};
|
|
@@ -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 =>
|