chaffjs 0.40.0 → 0.41.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/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-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.map +1 -1
- package/dist/detectors/ai-structure.js +6 -2
- package/dist/detectors/ai-structure.js.map +1 -1
- package/dist/detectors/contraction-mix.d.ts.map +1 -1
- package/dist/detectors/contraction-mix.js +8 -1
- 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/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 +8 -5
- package/dist/detectors/gram-furniture.d.ts.map +1 -1
- package/dist/detectors/gram-furniture.js +23 -13
- package/dist/detectors/gram-furniture.js.map +1 -1
- 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-repetition.d.ts.map +1 -1
- package/dist/detectors/gram-repetition.js +18 -7
- package/dist/detectors/gram-repetition.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.map +1 -1
- package/dist/detectors/list-lead-in.js +18 -8
- 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 +25 -3
- 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/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/package.json +3 -3
- package/rules/ai-generated-composite.yaml +2 -2
- package/rules/ai-structure.yaml +2 -2
- package/rules/ai-tell.yaml +2 -2
- package/rules/colon-lead-in.yaml +2 -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/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/undefined-acronym.yaml +4 -2
- package/rules/unknown-word.yaml +2 -2
- package/rules/unqualified-superlative.yaml +1 -1
- package/src/composite-signals.ts +27 -0
- package/src/detectors/acronym-context.ts +58 -0
- package/src/detectors/acronym-series.ts +22 -3
- package/src/detectors/ai-phrasing.ts +7 -1
- package/src/detectors/ai-structure.ts +6 -2
- package/src/detectors/contraction-mix.ts +11 -1
- package/src/detectors/contraction-set-apart.ts +52 -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 +27 -13
- package/src/detectors/gram-openers.ts +54 -1
- package/src/detectors/gram-repetition.ts +21 -7
- package/src/detectors/known-correction.ts +15 -8
- package/src/detectors/lexicon.ts +17 -5
- package/src/detectors/list-lead-in.ts +23 -8
- 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 +28 -3
- 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/run.ts +4 -2
- package/src/structure-shape/faq-sections.ts +24 -0
- package/src/structure-shape/features.ts +5 -3
|
@@ -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
|
+
};
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
} from "./gram-furniture.ts";
|
|
15
15
|
import { hasPredicateIn } from "./gram-predicate.ts";
|
|
16
16
|
import { gramPassagesOf } from "./gram-passages.ts";
|
|
17
|
-
import { entryOpeners, nounFragments, rowEndCells, sectionOpeners } from "./gram-openers.ts";
|
|
17
|
+
import { entryOpeners, filledForms, labelLines, nounFragments, rowEndCells, sectionOpeners } from "./gram-openers.ts";
|
|
18
18
|
import { nameSpans, touchesAny } from "../team-names.ts";
|
|
19
19
|
import type { Detector, Finding, LengthUnit, ProseDocument, Sentence, Span, StructureNode } from "../plugin.ts";
|
|
20
20
|
|
|
@@ -82,8 +82,9 @@ type Context = {
|
|
|
82
82
|
const excludedIn = (context: Context, sentence: Sentence, text: string, source: ReturnType<typeof compacted>): { named: Span[]; mask: boolean[] } => {
|
|
83
83
|
const unit = context.doc.lengthUnit;
|
|
84
84
|
const named = [...nameSpans(text, context.names), ...fieldLabelSpans(text, unit), ...partReferenceSpans(text, unit)];
|
|
85
|
-
const
|
|
86
|
-
|
|
85
|
+
const linked = linkedIn(context.doc, sentence, source);
|
|
86
|
+
const mask = furnitureMask(text, linked);
|
|
87
|
+
[...fixedEndingSpans(text, context.endings), ...quotedSpans(text), ...quotedClosingSpans(text, context.closings, linked)].forEach((span) =>
|
|
87
88
|
mask.fill(true, span.start, span.end),
|
|
88
89
|
);
|
|
89
90
|
return { named, mask };
|
|
@@ -127,14 +128,23 @@ const gramsOf = (context: Context, sentences: readonly Sentence[]): Tally => {
|
|
|
127
128
|
*/
|
|
128
129
|
const templatesOf = (doc: ProseDocument): ReadonlySet<Sentence> => {
|
|
129
130
|
const fragments = new Set(nounFragments(doc.sentences, doc.source));
|
|
130
|
-
return new Set([
|
|
131
|
+
return new Set([
|
|
132
|
+
...entryOpeners(doc.sections, doc.source),
|
|
133
|
+
...fragments,
|
|
134
|
+
...rowEndCells(doc.sentences, fragments, doc.source),
|
|
135
|
+
...labelLines(doc.sentences, doc.source),
|
|
136
|
+
...filledForms(doc.sentences, doc.source),
|
|
137
|
+
]);
|
|
131
138
|
};
|
|
132
139
|
|
|
133
|
-
/**
|
|
140
|
+
/**
|
|
141
|
+
* 文書の題と見出し。題や見出しにある言い回しは文書の主題で、繰り返して当たり前(CCライセンスを付与する際の…、題を小見出しに置いた
|
|
142
|
+
* Guidance on Lending to Individuals Not Legally Authorized to Work、節の名前の 高度専門職業人養成に特化した新たな大学院制度)。
|
|
143
|
+
*/
|
|
134
144
|
const titleOf = (doc: ProseDocument): string =>
|
|
135
145
|
keyOf(
|
|
136
146
|
doc.sections
|
|
137
|
-
.filter((section) => section.depth
|
|
147
|
+
.filter((section) => section.depth >= 1)
|
|
138
148
|
.map((section) => compacted(section.heading, doc.lengthUnit).text)
|
|
139
149
|
.join("\n"),
|
|
140
150
|
doc.lengthUnit,
|
|
@@ -147,7 +157,11 @@ type Repeat = { readonly key: string; readonly occurrences: readonly Occurrence[
|
|
|
147
157
|
* 節の書き出しは書き手の地の文でもあるので、ほかの所にも出れば書き出しのぶんも数える(節ごとの口癖)。
|
|
148
158
|
*/
|
|
149
159
|
const repeatsOf = (doc: ProseDocument, limit: number): Repeat[] => {
|
|
150
|
-
const
|
|
160
|
+
const sources = doc.sentences.map((sentence) => compacted(sentence.text, doc.lengthUnit));
|
|
161
|
+
const closings = referenceClosings(
|
|
162
|
+
sources.map((source) => source.text),
|
|
163
|
+
doc.sentences.map((sentence, index) => linkedIn(doc, sentence, sources[index] ?? compacted(sentence.text, doc.lengthUnit))),
|
|
164
|
+
);
|
|
151
165
|
const context = { doc, names: namesOf(doc), endings: fixedEndingsOf(doc), closings };
|
|
152
166
|
const templates = templatesOf(doc);
|
|
153
167
|
const openers = sectionOpeners(doc.sections, doc.source);
|
|
@@ -45,16 +45,23 @@ const tokenHits = (sentence: Sentence, pair: Pair, source: string): Hit[] => {
|
|
|
45
45
|
};
|
|
46
46
|
|
|
47
47
|
/** 字の並びで当たった所のうち、両端が文の語の切れ目にあるもの。語彙表の語を解析器が文の中と違う切り方にした(「ますす」)ときの拾い直し。 */
|
|
48
|
-
const alignedTextHits = (sentence: Sentence, pair: Pair): Hit[] =>
|
|
48
|
+
const alignedTextHits = (sentence: Sentence, pair: Pair, edges: WordEdges): Hit[] =>
|
|
49
|
+
textHits(sentence, pair).filter((hit) => edges.starts.has(hit.offset) && edges.ends.has(hit.offset + pair.wrong.length));
|
|
50
|
+
|
|
51
|
+
/** 文の語の始まりと終わり。組ごとに作り直すと、組の数と文の長さの積になる。 */
|
|
52
|
+
type WordEdges = { readonly starts: ReadonlySet<number>; readonly ends: ReadonlySet<number> };
|
|
53
|
+
|
|
54
|
+
const edgesOf = (sentence: Sentence): WordEdges => {
|
|
49
55
|
const tokens = sentence.tokens ?? [];
|
|
50
|
-
|
|
51
|
-
const ends = new Set(tokens.map((token) => token.span.end));
|
|
52
|
-
return textHits(sentence, pair).filter((hit) => starts.has(hit.offset) && ends.has(hit.offset + pair.wrong.length));
|
|
56
|
+
return { starts: new Set(tokens.map((token) => token.span.start)), ends: new Set(tokens.map((token) => token.span.end)) };
|
|
53
57
|
};
|
|
54
58
|
|
|
55
|
-
const hitsIn = (sentence: Sentence,
|
|
56
|
-
|
|
57
|
-
return
|
|
59
|
+
const hitsIn = (sentence: Sentence, pairs: readonly Pair[], source: string): Hit[] => {
|
|
60
|
+
const edges = edgesOf(sentence);
|
|
61
|
+
return pairs.flatMap((pair) => {
|
|
62
|
+
if (sentence.tokens === undefined || (pair.entry.tokens?.length ?? 0) === 0) return textHits(sentence, pair);
|
|
63
|
+
return [...tokenHits(sentence, pair, source), ...alignedTextHits(sentence, pair, edges)];
|
|
64
|
+
});
|
|
58
65
|
};
|
|
59
66
|
|
|
60
67
|
/** チームが names に並べた名前の中の字(「黒ひげ危機一発」の「危機一発」)は、その字のまま正しい。 */
|
|
@@ -75,7 +82,7 @@ const firstAtEachOffset = (hits: readonly Hit[]): Hit[] => hits.filter((hit, at)
|
|
|
75
82
|
*/
|
|
76
83
|
export const knownCorrection: Detector = (doc, options): Finding[] => {
|
|
77
84
|
const pairs = pairsOf(options.lexicon ?? []);
|
|
78
|
-
const hits = doc.sentences.flatMap((sentence) =>
|
|
85
|
+
const hits = doc.sentences.flatMap((sentence) => hitsIn(sentence, pairs, doc.source));
|
|
79
86
|
return firstAtEachOffset(hits.toSorted((left, right) => left.offset - right.offset))
|
|
80
87
|
.filter((hit) => !isInTeamName(doc, hit))
|
|
81
88
|
.map((hit) => ({
|
package/src/detectors/lexicon.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { joinWords } from "./word-list.ts";
|
|
2
|
+
import { densestStretch } from "./densest-stretch.ts";
|
|
2
3
|
import { wordsOf } from "./structure.ts";
|
|
3
4
|
import type { Detector, Finding, Lexicon, LexiconEntry, ProseDocument, Sentence, Span, Token } from "../plugin.ts";
|
|
4
5
|
import { entryIn, entryOpens, entryRanges, type TokenRange } from "./lexicon-match.ts";
|
|
5
|
-
import { sentencesWithHeadings } from "./heading-sentences.ts";
|
|
6
|
+
import { headingSentences, sentencesWithHeadings } from "./heading-sentences.ts";
|
|
6
7
|
import { scoped, scopeMarkersOf, type ScopeMarkers } from "./superlative-scope.ts";
|
|
7
8
|
import { makesWord, namesQuantity } from "./superlative-name.ts";
|
|
8
9
|
import { namesAmount } from "./superlative-amount.ts";
|
|
@@ -255,15 +256,26 @@ const writtenIn = (sentence: Sentence, entry: LexiconEntry): boolean =>
|
|
|
255
256
|
: entryRanges(sentence, entry).some((range) => !quotedRange(sentence, range));
|
|
256
257
|
|
|
257
258
|
/**
|
|
258
|
-
*
|
|
259
|
-
*
|
|
259
|
+
* How many sentences one pile-up may span. Generated articles fit inside it whole; a long text (a listing of many authors'
|
|
260
|
+
* abstracts, a novel) gathers one phrase here and one there, and only a stretch of it is scored.
|
|
261
|
+
*/
|
|
262
|
+
const AI_TELL_STRETCH = 100;
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* AI 生成の signal。1 つでは何も言えないので、重みを足し合わせて点にする。見出しも読む(「〜をわかりやすく解説」のような題)。
|
|
266
|
+
* 点は AI_TELL_STRETCH 文の中で最も重なった所のもの。単独で断じない。§20.2 の複合シグナルの入口で、これだけで「AI が書いた」とは言わない。
|
|
260
267
|
*/
|
|
261
268
|
export const aiTell: Detector = (doc, options): Finding[] => {
|
|
262
269
|
const entries = [...new Map((options.lexicon ?? []).map((entry) => [entry.pattern.toLowerCase(), entry])).values()];
|
|
263
270
|
const sentences = sentencesWithHeadings(doc);
|
|
264
|
-
const
|
|
271
|
+
const headings = new Set(headingSentences(doc).map((heading) => heading.span.start));
|
|
272
|
+
const counts = (sentence: Sentence, entry: LexiconEntry): boolean =>
|
|
273
|
+
(entry.heading_only !== true || headings.has(sentence.span.start)) && writtenIn(sentence, entry);
|
|
274
|
+
const hitsPerSentence = sentences.map((sentence) => entries.filter((entry) => counts(sentence, entry)));
|
|
275
|
+
const stretch = densestStretch(hitsPerSentence, AI_TELL_STRETCH, (entry) => entry.weight ?? 1);
|
|
276
|
+
const found = entries.filter((entry) => stretch?.keys.has(entry) === true);
|
|
265
277
|
const score = Math.round(found.reduce((sum, entry) => sum + (entry.weight ?? 1), 0) * 10);
|
|
266
|
-
const first =
|
|
278
|
+
const first = stretch === undefined ? undefined : sentences[stretch.first];
|
|
267
279
|
if (first === undefined || score <= options.limit) return [];
|
|
268
280
|
return [
|
|
269
281
|
{
|
|
@@ -45,26 +45,41 @@ const MAX_LABEL_WORDS = 3;
|
|
|
45
45
|
const CLAUSE_POS: ReadonlySet<string> = new Set(["VERB", "AUX", "PRON", "SCONJ"]);
|
|
46
46
|
const NAME_POS: ReadonlySet<string> = new Set(["NOUN", "PROPN"]);
|
|
47
47
|
|
|
48
|
+
/** A verb right after a preposition is a gerund inside the phrase (Guidelines for addressing …), not the verb of a clause. */
|
|
49
|
+
const makesClause = (pos: string, before: string | undefined): boolean => CLAUSE_POS.has(pos) && !(pos === "VERB" && before === "ADP");
|
|
50
|
+
|
|
48
51
|
/** A noun phrase of any length, read from the adapter's parts of speech. Without them, only the short label counts. */
|
|
49
52
|
const isNounPhrase = (sentence: Sentence): boolean => {
|
|
50
53
|
const tokens = sentence.tokens ?? [];
|
|
51
|
-
return tokens.some((token) => NAME_POS.has(token.pos)) && !tokens.some((token) =>
|
|
54
|
+
return tokens.some((token) => NAME_POS.has(token.pos)) && !tokens.some((token, index) => makesClause(token.pos, tokens[index - 1]?.pos));
|
|
52
55
|
};
|
|
53
56
|
|
|
54
57
|
const isNounLabel = (words: string, sentence: Sentence): boolean =>
|
|
55
58
|
NOUN_LABEL.test(words) && (words.split(/\s+/u).length <= MAX_LABEL_WORDS || isNounPhrase(sentence));
|
|
56
59
|
|
|
60
|
+
/** A template keeps at least this many words fixed around its entry's name; fewer, and two different labels only look alike. */
|
|
61
|
+
const MIN_FIXED_WORDS = 3;
|
|
62
|
+
|
|
63
|
+
/** One label of a template that carries its entry's name: the same words but one (Guidelines for addressing High-sev / Low-sev issues). */
|
|
64
|
+
const sameButOneWord = (left: string, right: string): boolean => {
|
|
65
|
+
const leftWords = left.split(/\s+/u);
|
|
66
|
+
const rightWords = right.split(/\s+/u);
|
|
67
|
+
const differing = leftWords.filter((word, index) => word !== rightWords[index]).length;
|
|
68
|
+
return leftWords.length === rightWords.length && differing === 1 && leftWords.length - differing >= MIN_FIXED_WORDS;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const sameTemplate = (left: string, right: string): boolean => left === right || sameButOneWord(left, right);
|
|
72
|
+
|
|
57
73
|
/**
|
|
58
|
-
* A field of an entry's template: the same noun label before the list of each entry (**参考:** before each entry's link)
|
|
74
|
+
* A field of an entry's template: the same noun label before the list of each entry (**参考:** before each entry's link),
|
|
75
|
+
* or the same label with the entry's name in it (Guidelines for addressing High-sev issues: in each severity's section).
|
|
59
76
|
* It names the field the way a table's column does; it does not lead into anything.
|
|
60
77
|
*/
|
|
61
78
|
const fieldLabels = (leadIns: readonly Sentence[]): ReadonlySet<string> => {
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const isField = (words: string, [first, ...rest]: readonly Sentence[]): boolean => first !== undefined && rest.length > 0 && isNounLabel(words, first);
|
|
67
|
-
return new Set([...usesByLabel].filter(([words, uses]) => isField(words, uses)).map(([words]) => words));
|
|
79
|
+
const labels = leadIns.map((sentence) => ({ words: labelWords(sentence.text), sentence }));
|
|
80
|
+
const isField = ({ words, sentence }: (typeof labels)[number], index: number): boolean =>
|
|
81
|
+
labels.some((other, otherIndex) => otherIndex !== index && sameTemplate(words, other.words)) && isNounLabel(words, sentence);
|
|
82
|
+
return new Set(labels.filter(isField).map(({ words }) => words));
|
|
68
83
|
};
|
|
69
84
|
|
|
70
85
|
/** A line that is only a bold label (**Pros:**, **参考**:), and the label inside the bold marks. */
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Whether a list goes on past an and / or. Then that and / or is inside an item, not the list's last joint, and it casts no
|
|
2
2
|
// vote on the serial comma. Pure.
|
|
3
|
-
import { hasAdposition, hasVerb, isClause, itemAfter, leadShape } from "./list-item.ts";
|
|
3
|
+
import { hasAdposition, hasVerb, isClause, itemAfter, leadShape, openingOf } from "./list-item.ts";
|
|
4
4
|
import { isListConjunction, type ItemScope } from "./list-sentence.ts";
|
|
5
5
|
import type { TokenRange } from "./token-column.ts";
|
|
6
6
|
|
|
@@ -30,13 +30,27 @@ const wordsOf = (scope: ItemScope, item: TokenRange): string =>
|
|
|
30
30
|
/** 並びの残りを言う語だけの項目(and so forth / or the like)。 */
|
|
31
31
|
const endsOpenList = (scope: ItemScope, item: TokenRange): boolean => scope.sentence.words.openListEnd?.has(wordsOf(scope, item)) ?? false;
|
|
32
32
|
|
|
33
|
+
/**
|
|
34
|
+
* and / or の項目が、並びの前で使った前置詞をもう一度置いてから、shape の項目を続けるか(filled at the West Station, Indian Cove,
|
|
35
|
+
* Black Rock and Cottonwood campgrounds, and at Park Headquarters)。前置詞を言い直しただけで、同じ並びの最後の項目。
|
|
36
|
+
* 動詞を連れれば節の頭(and at night we slept)。
|
|
37
|
+
*/
|
|
38
|
+
const repeatsPreposition = (scope: ItemScope, item: TokenRange, shape: string): boolean => {
|
|
39
|
+
const opening = openingOf(scope, item);
|
|
40
|
+
if (opening?.pos !== "ADP" || hasVerb(scope, item)) return false;
|
|
41
|
+
const at = scope.sentence.tokens.indexOf(opening);
|
|
42
|
+
const surface = opening.surface.toLowerCase();
|
|
43
|
+
const usedBefore = scope.sentence.tokens.slice(0, at).some((token) => token.pos === "ADP" && token.surface.toLowerCase() === surface);
|
|
44
|
+
return usedBefore && leadShape(scope, { start: at + 1, end: item.end }) === shape;
|
|
45
|
+
};
|
|
46
|
+
|
|
33
47
|
/**
|
|
34
48
|
* 読点のあとに、頭の形が shape の and / or の項目か、並びの残りを言う語(and so forth)が来るか。and / or が来なければ undefined。
|
|
35
49
|
*/
|
|
36
50
|
const conjunctionAfter = (scope: ItemScope, end: number, shape: string): boolean | undefined => {
|
|
37
51
|
if (!isListConjunction(scope.sentence.tokens[end + 1])) return undefined;
|
|
38
52
|
const following = itemAfter(scope, end + 1);
|
|
39
|
-
return leadShape(scope, following) === shape || endsOpenList(scope, following);
|
|
53
|
+
return leadShape(scope, following) === shape || endsOpenList(scope, following) || repeatsPreposition(scope, following, shape);
|
|
40
54
|
};
|
|
41
55
|
|
|
42
56
|
const QUOTE_MARKS: ReadonlySet<string> = new Set(['"', "“", "”", "'", "‘", "’"]);
|
|
@@ -56,6 +56,7 @@ import { namesDivision, qualifiesPrevious } from "./list-comma-aside.ts";
|
|
|
56
56
|
import { inExcerpt } from "./quoted-excerpt.ts";
|
|
57
57
|
import { listContinues, walker, type Walk } from "./list-run.ts";
|
|
58
58
|
import { joinsUnfinished, repeatsConjunction } from "./list-joint.ts";
|
|
59
|
+
import { insideSentenceCaseTitle } from "./title-list.ts";
|
|
59
60
|
|
|
60
61
|
const EMPTY: TokenRange = { start: 0, end: 0 };
|
|
61
62
|
|
|
@@ -121,6 +122,19 @@ const afterClosedList = (view: View): View => {
|
|
|
121
122
|
return isItem(scope, rest) ? { ...after(view, closing), pre: rest } : withoutLead(after(view, closing));
|
|
122
123
|
};
|
|
123
124
|
|
|
125
|
+
/**
|
|
126
|
+
* 動詞で終わる最初の項目のすぐ後ろの、前置詞で始まる項目は、動詞と目的語のあいだに読点で挟んだ句(if it received, at its most
|
|
127
|
+
* recent examination, a rating of "1" or "2" and it satisfies)。動詞も句も並びの項目ではなく、並びがあるなら句の後ろの目的語から。
|
|
128
|
+
* 句の後ろも前置詞で始まれば、前置詞の句の並び(collected, in batches, in files, and in tables)なので外さない。
|
|
129
|
+
*/
|
|
130
|
+
const afterVerbAside = (view: View): View => {
|
|
131
|
+
const { scope } = view.list;
|
|
132
|
+
const [verb, aside] = [firstOf(view), secondOf(view)];
|
|
133
|
+
if (verb === undefined || aside === undefined || lastContent(scope, verb.item)?.pos !== "VERB" || openingOf(scope, aside.item)?.pos !== "ADP") return view;
|
|
134
|
+
const object = firstOf(after(view, aside));
|
|
135
|
+
return object !== undefined && opensAsNoun(scope, object.item) ? after(view, aside) : view;
|
|
136
|
+
};
|
|
137
|
+
|
|
124
138
|
/**
|
|
125
139
|
* 項目として立つ切れ端か。副詞だけ(does not の not)や、目的語を後ろに残した前置詞で終わるもの(or impose conditions upon,
|
|
126
140
|
* certain transmittals / directly related to, and necessary for, the licensing)は、読点の後ろへ続く句の頭で、項目ではない。
|
|
@@ -345,7 +359,7 @@ const listAt = (list: ItemList, at: number, walk: Walk): boolean | undefined =>
|
|
|
345
359
|
const { scope } = list;
|
|
346
360
|
if (closesExampleAside(wholeList(list), at)) return undefined;
|
|
347
361
|
const afterItem = itemAfter(scope, at);
|
|
348
|
-
const opened = afterClosedList(withoutLead(afterClauseTurn(withoutCoordinateAdjective(wholeList(list)), afterItem)));
|
|
362
|
+
const opened = afterClosedList(withoutLead(afterVerbAside(afterClauseTurn(withoutCoordinateAdjective(wholeList(list)), afterItem))));
|
|
349
363
|
const items = fromParticiplePhrase(fromExample(opened, afterItem), afterItem);
|
|
350
364
|
if (listContinues(scope, afterItem, walk) || isAppositive(scope, at, items, afterItem) || insideLastItem(scope, at, items, afterItem)) return undefined;
|
|
351
365
|
if (namesDivision(items) || qualifiesPrevious(items)) return undefined;
|
|
@@ -368,12 +382,13 @@ export const listVerdicts = (tokens: readonly Token[], words: ListWords, source:
|
|
|
368
382
|
const readItems = listReader(sentence);
|
|
369
383
|
const breaks = dashBreaks(tokens, source);
|
|
370
384
|
const excerpt = inExcerpt(tokens, source, words.elision ?? []);
|
|
385
|
+
const inTitle = insideSentenceCaseTitle(tokens);
|
|
371
386
|
const walk = walker();
|
|
372
387
|
return tokens.flatMap((token, at) => {
|
|
373
388
|
if (!isJudgedConjunction(token, at)) return [];
|
|
374
389
|
// 郵便番号で終わる住所のあとの and / or は、住所の行を区切る読点のあと。並びの読点ではない。
|
|
375
390
|
// 最後の読点のあとに前後を空けたダッシュがあれば(Washington, DC – The …)、読点はダッシュの前で閉じた日付行や見出しのもの。
|
|
376
|
-
const notList = sentence.inCitedTitle(at) || excerpt(at) || followsPostcode(source, token.span.start) || breaks[at] === true;
|
|
391
|
+
const notList = sentence.inCitedTitle(at) || inTitle(at) || excerpt(at) || followsPostcode(source, token.span.start) || breaks[at] === true;
|
|
377
392
|
return [notList ? undefined : listAt(readItems(at), at, walk)];
|
|
378
393
|
});
|
|
379
394
|
};
|
|
@@ -2,6 +2,7 @@ import type { Detector, DetectorOptions, Finding, Lexicon, LexiconEntry, ProseDo
|
|
|
2
2
|
import { entryEnds, entryIn } from "./lexicon-match.ts";
|
|
3
3
|
import { mentionCuesOf, onlyMentioned } from "./mentioned-phrase.ts";
|
|
4
4
|
import { closingSentences } from "../closing-paragraph.ts";
|
|
5
|
+
import { quotedPattern } from "./quoted-pattern.ts";
|
|
5
6
|
|
|
6
7
|
/** 冒頭は先頭 2 段落ぶん。「どの記事にも当てはまる書き出し」は冒頭にあるときだけ問題。 */
|
|
7
8
|
const OPENING_SENTENCES = 4;
|
|
@@ -24,7 +25,7 @@ type IsUsed = (sentence: Sentence, entry: LexiconEntry) => boolean;
|
|
|
24
25
|
const hitsIn = (sentences: readonly Sentence[], lexicon: Lexicon, isUsed: IsUsed = () => true): Hit[] =>
|
|
25
26
|
sentences.flatMap((sentence) => {
|
|
26
27
|
const matched = lexicon.find((entry) => entryIn(sentence, entry) && isUsed(sentence, entry));
|
|
27
|
-
return matched === undefined ? [] : [{ sentence, matched: matched.pattern }];
|
|
28
|
+
return matched === undefined ? [] : [{ sentence, matched: quotedPattern(matched.pattern) }];
|
|
28
29
|
});
|
|
29
30
|
|
|
30
31
|
const findingsOf = (hits: readonly Hit[], limit: number, count = hits.length): Finding[] =>
|
|
@@ -74,7 +75,7 @@ const usesIn = (sentence: Sentence, lexicon: Lexicon): Uses | undefined => {
|
|
|
74
75
|
const found = lexicon.map((entry) => ({ entry, ends: entryEnds(sentence, entry) })).filter(({ ends }) => ends.length > 0);
|
|
75
76
|
const first = found.toSorted((a, b) => Math.min(...a.ends) - Math.min(...b.ends))[0];
|
|
76
77
|
if (first === undefined) return undefined;
|
|
77
|
-
return { sentence, matched: first.entry.pattern, count: new Set(found.flatMap(({ ends }) => ends)).size };
|
|
78
|
+
return { sentence, matched: quotedPattern(first.entry.pattern), count: new Set(found.flatMap(({ ends }) => ends)).size };
|
|
78
79
|
};
|
|
79
80
|
|
|
80
81
|
/**
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A comma or question mark at the edge of a lexicon pattern (、正直 / honestly,) only says where the phrase stands
|
|
3
|
+
* in the sentence; the message quotes the phrase without it. A period is kept: it may end an abbreviation (etc.).
|
|
4
|
+
*/
|
|
5
|
+
const EDGE_MARKS: ReadonlySet<string> = new Set([" ", "、", ",", ",", "?", "?"]);
|
|
6
|
+
|
|
7
|
+
const isEdgeMark = (char: string): boolean => EDGE_MARKS.has(char);
|
|
8
|
+
|
|
9
|
+
export const quotedPattern = (pattern: string): string => {
|
|
10
|
+
const chars = [...pattern];
|
|
11
|
+
const start = chars.findIndex((char) => !isEdgeMark(char));
|
|
12
|
+
const end = chars.findLastIndex((char) => !isEdgeMark(char));
|
|
13
|
+
return start === -1 ? pattern : chars.slice(start, end + 1).join("");
|
|
14
|
+
};
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
type Span = { readonly start: number; readonly end: number };
|
|
9
9
|
|
|
10
10
|
/** A label in square brackets opening a line, then a wide gap: two spaces or a tab. A link definition ([x]: url) has a colon. */
|
|
11
|
-
const ENTRY_START = /^(?<indent>[ \t]*)\[[A-Za-z0-9][\w.:+-]
|
|
11
|
+
const ENTRY_START = /^(?<indent>[ \t]*)\[(?<label>[A-Za-z0-9][\w.:+-]*)\](?: {2,}|\t)\S/u;
|
|
12
12
|
|
|
13
13
|
type Line = { readonly text: string; readonly start: number };
|
|
14
14
|
|
|
@@ -40,3 +40,10 @@ export const referenceEntrySpans = (source: string): Span[] => {
|
|
|
40
40
|
return [{ start: line.start, end: last.start + last.text.length }];
|
|
41
41
|
});
|
|
42
42
|
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* The labels of the entries ([UNIX] The Open Group, "The Single UNIX Specification"). The entry says what the label names,
|
|
46
|
+
* so the label written in prose (a UNIX timestamp) is explained by it.
|
|
47
|
+
*/
|
|
48
|
+
export const referenceEntryLabels = (source: string): ReadonlySet<string> =>
|
|
49
|
+
new Set(linesOf(source).flatMap((line) => ENTRY_START.exec(line.text)?.groups?.["label"] ?? []));
|
|
@@ -39,16 +39,18 @@ const opensRelative = (noun: Token | undefined, verb: Token | undefined, next: T
|
|
|
39
39
|
|
|
40
40
|
/** which / that の後ろの、副詞を飛ばした最初の語(the file which usually stores の stores)。 */
|
|
41
41
|
const verbAfter = (tokens: readonly Token[], at: number): number => {
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
let next = at + 1;
|
|
43
|
+
while (tokens[next]?.pos === "ADV") next += 1;
|
|
44
|
+
return next;
|
|
44
45
|
};
|
|
45
46
|
|
|
46
47
|
/** 語彙表の組(which / that)で、tokens の中の限定の関係節の頭。 */
|
|
47
48
|
export const relativeUses = (tokens: readonly Token[], kinds: Lexicon): RelativeUse[] =>
|
|
48
49
|
tokens.flatMap((word, at) => {
|
|
49
50
|
const entry = kinds.find((candidate) => candidate.pattern.toLowerCase() === word.surface.toLowerCase());
|
|
51
|
+
if (entry?.group === undefined) return [];
|
|
50
52
|
const verb = verbAfter(tokens, at);
|
|
51
|
-
if (
|
|
53
|
+
if (!opensRelative(tokens[at - 1], tokens[verb], tokens[verb + 1])) return [];
|
|
52
54
|
return [{ word, kind: entry.group }];
|
|
53
55
|
});
|
|
54
56
|
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
// a converter ran together (…設立1945年7月日染興業…, 39,0439,677…), and a quotation the writer introduces with a colon.
|
|
4
4
|
// Each is measured part by part, or not at all. Pure: the text in, the parts out.
|
|
5
5
|
|
|
6
|
+
import type { Span } from "../plugin.ts";
|
|
7
|
+
|
|
6
8
|
const LINE_BREAK = /\n/u;
|
|
7
9
|
/** A sentence's end on a line: a stop, then any closing quotes or brackets. */
|
|
8
10
|
const LINE_ENDS_SENTENCE = /[.?!。?!][\s"'”’)\])」』]*$/u;
|
|
@@ -55,6 +57,10 @@ const nonEmpty = (parts: readonly string[]): string[] => parts.map((part) => par
|
|
|
55
57
|
/** Whether the text holds cells of numbers a converter ran together (39,0439,677). */
|
|
56
58
|
export const hasGluedNumbers = (text: string): boolean => GLUED_NUMBER.test(text);
|
|
57
59
|
|
|
60
|
+
/** Where the text holds numbers of several cells run together (8,45795.7 for 8,457 and 95.7): no digit in them can be read as one number. */
|
|
61
|
+
export const gluedNumberSpans = (text: string): Span[] =>
|
|
62
|
+
[...text.matchAll(NUMBER_RUN)].filter((run) => GLUED_NUMBER.test(run[0])).map((run) => ({ start: run.index, end: run.index + run[0].length }));
|
|
63
|
+
|
|
58
64
|
/** The cells of a table a converter flattened into one sentence, cut at the glued numbers or at each row's date. */
|
|
59
65
|
export const flattenedTableParts = (text: string): readonly string[] | undefined => {
|
|
60
66
|
if (hasGluedNumbers(text)) return nonEmpty(text.replace(NUMBER_RUN, (run) => (GLUED_NUMBER.test(run) ? CUT : run)).split(CUT));
|
|
@@ -71,3 +77,12 @@ export const colonQuotationParts = (text: string): readonly string[] | undefined
|
|
|
71
77
|
if (quotes.length === 0) return undefined;
|
|
72
78
|
return nonEmpty([text.replace(COLON_QUOTATION, ":"), ...quotes]);
|
|
73
79
|
};
|
|
80
|
+
|
|
81
|
+
/** A header cell closed by its unit in brackets (所有割合(%), 従業員数(人), 資本金(百万円)). */
|
|
82
|
+
const UNIT_HEADER = /[((](?:%|%|人|名|株|件|台|社|円|千円|百万円)[))]/gu;
|
|
83
|
+
/** Fewer unit headers than this are a sentence that names a unit or two, not a table's header row. */
|
|
84
|
+
const MIN_UNIT_HEADERS = 3;
|
|
85
|
+
|
|
86
|
+
/** Whether a sentence is a table a converter flattened: glued numbers, rows opened by dates, or a header row of cells closed by units. */
|
|
87
|
+
export const isFlattenedTable = (text: string): boolean =>
|
|
88
|
+
flattenedTableParts(text) !== undefined || (text.match(UNIT_HEADER)?.length ?? 0) >= MIN_UNIT_HEADERS;
|
package/src/detectors/signals.ts
CHANGED
|
@@ -7,6 +7,9 @@ import { isExplained } from "./acronym-compound.ts";
|
|
|
7
7
|
import { statedAcronyms } from "./acronym-definition.ts";
|
|
8
8
|
import { isBracketed, isInHeading, isNamePart } from "./acronym-lone.ts";
|
|
9
9
|
import { linkedListTerms } from "./acronym-link.ts";
|
|
10
|
+
import { headingSeries } from "./acronym-series.ts";
|
|
11
|
+
import { citationSpans } from "./acronym-name.ts";
|
|
12
|
+
import { referenceEntryLabels } from "./reference-entry.ts";
|
|
10
13
|
import { caseVariantsIn, spelledOutAcronyms, writtenAsWordIn } from "./acronym-mention.ts";
|
|
11
14
|
import { conjugatedForms } from "./conjugated-form.ts";
|
|
12
15
|
import { knownByDocument } from "./acronym-known.ts";
|
|
@@ -25,6 +28,11 @@ export const MIN_DOCUMENT_LENGTH = { word: 200, char: 500 };
|
|
|
25
28
|
|
|
26
29
|
const bodyOf = (doc: ProseDocument): string => doc.sentences.map(proseText).join(" ");
|
|
27
30
|
|
|
31
|
+
/** 全角の英数字を半角に(ICO(Initial Coin Offering)の ICO を ICO と読む)。1 文字ずつ置き換えるので位置は変わらない。 */
|
|
32
|
+
const FULL_WIDTH_ALNUM = /[A-Za-z0-9]/gu;
|
|
33
|
+
const FULL_WIDTH_OFFSET = 0xfee0;
|
|
34
|
+
const halfWidth = (text: string): string => text.replaceAll(FULL_WIDTH_ALNUM, (character) => String.fromCharCode(character.charCodeAt(0) - FULL_WIDTH_OFFSET));
|
|
35
|
+
|
|
28
36
|
type Hit = { readonly sentence: Sentence; readonly offset: number };
|
|
29
37
|
|
|
30
38
|
const findIn = (doc: ProseDocument, pattern: RegExp): Hit[] =>
|
|
@@ -175,6 +183,13 @@ const expandsInHeading = (heading: string, acronym: string, expandedAt: Expanded
|
|
|
175
183
|
|
|
176
184
|
const patternsOf = (doc: ProseDocument, id: string): string[] => (doc.lexicons[id] ?? []).map((entry) => entry.pattern);
|
|
177
185
|
|
|
186
|
+
/**
|
|
187
|
+
* 法令集の名前(CFR)を、この文書が引用の形(12 CFR 1005.20)で書いているなら、引用の外(the List of CFR Sections Affected)でも
|
|
188
|
+
* 読み手の知る名前と読む。引用の形で書いていない文書の CFR は略語のまま数える。
|
|
189
|
+
*/
|
|
190
|
+
const citedSeriesOf = (doc: ProseDocument, body: string): string[] =>
|
|
191
|
+
patternsOf(doc, "citation-series").filter((series) => citationSpans(body, [series]).length > 0 || titledCodeSpans(body, [series]).length > 0);
|
|
192
|
+
|
|
178
193
|
const notationOf = (doc: ProseDocument): NotAcronymSpans => lexiconNotationOf((id) => patternsOf(doc, id));
|
|
179
194
|
|
|
180
195
|
/** 定義の語。定義の動詞は、語彙表の形(という)と、この文書で活用して書かれた形(といいます)の両方で照らす。 */
|
|
@@ -203,7 +218,7 @@ const isNamePartAt = (acronym: string, hit: Hit): boolean => {
|
|
|
203
218
|
* 書き手が展開する文書では、展開の抜けは抜けとして読める。名前の一部の略語(SRI International)は、そのときも指さない。
|
|
204
219
|
*/
|
|
205
220
|
export const undefinedAcronym: Detector = (doc, options): Finding[] => {
|
|
206
|
-
const body = bodyOf(doc);
|
|
221
|
+
const body = halfWidth(bodyOf(doc));
|
|
207
222
|
// HTTP のメソッド名(GET)と SQL のキーワード(VIEW)は略語ではなく、税の名前(VAT、GST)は読み手が名前として知るので、通じる略語と同じく展開を求めない。
|
|
208
223
|
// 並べた名前(JAXA)も、繋いだ略語(JAXA-ISAS)の片割れとして説明済みに数える。
|
|
209
224
|
// 文書が自分で知らせた語(select を書いた文書の SELECT、リンクで引いた講演の場 [Jane Roe, FOSDEM 2024](…))も同じ。
|
|
@@ -211,6 +226,7 @@ export const undefinedAcronym: Detector = (doc, options): Finding[] => {
|
|
|
211
226
|
...(options.lexicon ?? []).map((entry) => entry.pattern),
|
|
212
227
|
...knownByDocument(doc.source, doc.links).concat(patternsOf(doc, "http-method"), patternsOf(doc, "sql-keyword"), patternsOf(doc, "tax-code")),
|
|
213
228
|
...(doc.names ?? []),
|
|
229
|
+
...citedSeriesOf(doc, body),
|
|
214
230
|
]);
|
|
215
231
|
const expandedAt = expansionAt(definitionWordsOf(doc));
|
|
216
232
|
const stated = statedAcronyms(doc.source, { statements: patternsOf(doc, "definition-statement"), markers: patternsOf(doc, "definition-marker") });
|
|
@@ -222,8 +238,17 @@ export const undefinedAcronym: Detector = (doc, options): Finding[] => {
|
|
|
222
238
|
);
|
|
223
239
|
// 略語のすぐ後ろのダッシュで挟んだ同格(the VIX—a measure of …—)、同じ文の前に書いた名前(Small Business Administration … SBA)、
|
|
224
240
|
// 2 つ以上の番号で引く続き物の名前(BCP 78、BCP 14)も、略語の説明と読む。
|
|
225
|
-
|
|
226
|
-
const headings = doc.sections.map((section) => section.heading);
|
|
241
|
+
// 文献一覧の見出し語([UNIX] The Open Group, "The Single UNIX Specification")と、見出しに番号で立てた書簡の続き物(### SR 26-8: …)も。
|
|
242
|
+
const headings = doc.sections.map((section) => halfWidth(section.heading));
|
|
243
|
+
const entries = new Set([
|
|
244
|
+
...termEntryAcronyms(doc.source),
|
|
245
|
+
...stated,
|
|
246
|
+
...linkedListTerms(doc.source),
|
|
247
|
+
...spelled,
|
|
248
|
+
...explainedInText(body, doc.sentences),
|
|
249
|
+
...referenceEntryLabels(doc.source),
|
|
250
|
+
...headingSeries(headings),
|
|
251
|
+
]);
|
|
227
252
|
// 見出しが略語の名前を書き出していれば(## Federal Open Market Committee の下の FOMC)、説明済み。
|
|
228
253
|
const spelledInHeading = headingsSpellingOut(headings);
|
|
229
254
|
const explainedAsWritten = (word: string): boolean =>
|
|
@@ -73,15 +73,23 @@ const verbAt = (tokens: readonly Token[], range: TokenRange, end: number): numbe
|
|
|
73
73
|
return end - 1 > range.end && isVerbal(tokens[end - 1]) ? end - 1 : undefined;
|
|
74
74
|
};
|
|
75
75
|
|
|
76
|
+
/** The first token after the verb at `at` and the auxiliaries and verbs that follow it (are addressed, should be fixed). */
|
|
77
|
+
const pastVerbs = (tokens: readonly Token[], at: number): number => {
|
|
78
|
+
const rest = tokens.slice(at + 1).findIndex((token) => !isVerbal(token));
|
|
79
|
+
return rest < 0 ? tokens.length : at + 1 + rest;
|
|
80
|
+
};
|
|
81
|
+
|
|
76
82
|
/**
|
|
77
83
|
* 最上級の名詞句が主語で、述語がどれかを指しているか(The best plan is the one you keep・The most important divergence is
|
|
78
|
-
* the second
|
|
84
|
+
* the second)、順番のどこに来るかを言うか(the most critical vulnerabilities are addressed first)。どれが一番なのかを決める
|
|
85
|
+
* 言い方で、一番だという主張ではない。
|
|
79
86
|
*/
|
|
80
87
|
export const pointsAtReferent = (tokens: readonly Token[], range: TokenRange, referents: Lexicon): boolean => {
|
|
81
88
|
const end = phraseEnd(tokens, range.end);
|
|
82
89
|
const at = verbAt(tokens, range, end);
|
|
83
90
|
if (at === undefined) return false;
|
|
84
|
-
const
|
|
91
|
+
const object = pastVerbs(tokens, at);
|
|
92
|
+
const next = tokens[object]?.pos === "DET" ? object + 1 : object;
|
|
85
93
|
return isWord(tokens[next], referents);
|
|
86
94
|
};
|
|
87
95
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// A list of titles written in sentence case (The bridge witch, The story of bubble gum, The law of finance and regulation). Pure.
|
|
2
|
+
import type { Token } from "../plugin.ts";
|
|
3
|
+
|
|
4
|
+
/** Items that open with a capitalised article after a comma or colon: a title's first word, not the writer's sentence. */
|
|
5
|
+
const TITLE_ITEMS = 2;
|
|
6
|
+
|
|
7
|
+
const ITEM_BREAKS: ReadonlySet<string> = new Set([",", ":"]);
|
|
8
|
+
|
|
9
|
+
const isCapitalised = (token: Token | undefined): boolean => /^\p{Lu}/u.test(token?.surface ?? "");
|
|
10
|
+
|
|
11
|
+
const opensTitle = (tokens: readonly Token[], at: number): boolean =>
|
|
12
|
+
at > 0 && tokens[at]?.pos === "DET" && isCapitalised(tokens[at]) && ITEM_BREAKS.has(tokens[at - 1]?.surface ?? "");
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Whether the and / or at `at` is inside one of the titles: the sentence lists titles (two or more items open with a capitalised
|
|
16
|
+
* article mid-sentence) and the word after the and / or is lower case. The joint between two titles is followed by a capital.
|
|
17
|
+
*/
|
|
18
|
+
export const insideSentenceCaseTitle = (tokens: readonly Token[]): ((at: number) => boolean) => {
|
|
19
|
+
const titles = tokens.filter((__token, at) => opensTitle(tokens, at)).length;
|
|
20
|
+
return (at) => titles >= TITLE_ITEMS && /^\p{Ll}/u.test(tokens[at + 1]?.surface ?? "");
|
|
21
|
+
};
|