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
|
@@ -11,10 +11,13 @@ import {
|
|
|
11
11
|
quotedClosingSpans,
|
|
12
12
|
quotedSpans,
|
|
13
13
|
referenceClosings,
|
|
14
|
+
screenLabelSpans,
|
|
14
15
|
} from "./gram-furniture.ts";
|
|
15
16
|
import { hasPredicateIn } from "./gram-predicate.ts";
|
|
16
17
|
import { gramPassagesOf } from "./gram-passages.ts";
|
|
17
|
-
import {
|
|
18
|
+
import { narrowestStretch, sentencePositions } from "./gram-spread.ts";
|
|
19
|
+
import { namedByHeading } from "./gram-heading.ts";
|
|
20
|
+
import { entryOpeners, filledForms, labelLines, nounFragments, rowEndCells, sectionOpeners } from "./gram-openers.ts";
|
|
18
21
|
import { nameSpans, touchesAny } from "../team-names.ts";
|
|
19
22
|
import type { Detector, Finding, LengthUnit, ProseDocument, Sentence, Span, StructureNode } from "../plugin.ts";
|
|
20
23
|
|
|
@@ -81,9 +84,10 @@ type Context = {
|
|
|
81
84
|
/** 数えない範囲: 名前(触れる窓を外す)と、飾り(過半を覆う窓を外す)。 */
|
|
82
85
|
const excludedIn = (context: Context, sentence: Sentence, text: string, source: ReturnType<typeof compacted>): { named: Span[]; mask: boolean[] } => {
|
|
83
86
|
const unit = context.doc.lengthUnit;
|
|
84
|
-
const named = [...nameSpans(text, context.names), ...fieldLabelSpans(text, unit), ...partReferenceSpans(text, unit)];
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
+
const named = [...nameSpans(text, context.names), ...fieldLabelSpans(text, unit), ...partReferenceSpans(text, unit), ...screenLabelSpans(text, unit)];
|
|
88
|
+
const linked = linkedIn(context.doc, sentence, source);
|
|
89
|
+
const mask = furnitureMask(text, linked);
|
|
90
|
+
[...fixedEndingSpans(text, context.endings), ...quotedSpans(text), ...quotedClosingSpans(text, context.closings, linked)].forEach((span) =>
|
|
87
91
|
mask.fill(true, span.start, span.end),
|
|
88
92
|
);
|
|
89
93
|
return { named, mask };
|
|
@@ -127,14 +131,23 @@ const gramsOf = (context: Context, sentences: readonly Sentence[]): Tally => {
|
|
|
127
131
|
*/
|
|
128
132
|
const templatesOf = (doc: ProseDocument): ReadonlySet<Sentence> => {
|
|
129
133
|
const fragments = new Set(nounFragments(doc.sentences, doc.source));
|
|
130
|
-
return new Set([
|
|
134
|
+
return new Set([
|
|
135
|
+
...entryOpeners(doc.sections, doc.source),
|
|
136
|
+
...fragments,
|
|
137
|
+
...rowEndCells(doc.sentences, fragments, doc.source),
|
|
138
|
+
...labelLines(doc.sentences, doc.source),
|
|
139
|
+
...filledForms(doc.sentences, doc.source),
|
|
140
|
+
]);
|
|
131
141
|
};
|
|
132
142
|
|
|
133
|
-
/**
|
|
143
|
+
/**
|
|
144
|
+
* 文書の題と見出し。題や見出しにある言い回しは文書の主題で、繰り返して当たり前(CCライセンスを付与する際の…、題を小見出しに置いた
|
|
145
|
+
* Guidance on Lending to Individuals Not Legally Authorized to Work、節の名前の 高度専門職業人養成に特化した新たな大学院制度)。
|
|
146
|
+
*/
|
|
134
147
|
const titleOf = (doc: ProseDocument): string =>
|
|
135
148
|
keyOf(
|
|
136
149
|
doc.sections
|
|
137
|
-
.filter((section) => section.depth
|
|
150
|
+
.filter((section) => section.depth >= 1)
|
|
138
151
|
.map((section) => compacted(section.heading, doc.lengthUnit).text)
|
|
139
152
|
.join("\n"),
|
|
140
153
|
doc.lengthUnit,
|
|
@@ -142,21 +155,43 @@ const titleOf = (doc: ProseDocument): string =>
|
|
|
142
155
|
|
|
143
156
|
type Repeat = { readonly key: string; readonly occurrences: readonly Occurrence[] };
|
|
144
157
|
|
|
158
|
+
/**
|
|
159
|
+
* 上限を超える数の出現が収まる一番短い範囲が、これより長ければ数えない(文書の長さの単位)。読み手が前の出現を覚えているうちに次が
|
|
160
|
+
* 来ないので、言い直しとは読まれない。長い報告や論文が、測る出来事や量の名前(adopted a budget resolution、posts Grok judged true)を
|
|
161
|
+
* 節をまたいで使うのはこれ。値は chaff-corpus の判定済みの指摘で、正しい指摘の範囲より上、誤検知の範囲より下に置いた。
|
|
162
|
+
* 日本語の値は英語の値を MIN_DOCUMENT_LENGTH と同じ比で置き換えた。
|
|
163
|
+
*/
|
|
164
|
+
const READING_SPAN = { word: 4000, char: 10000 };
|
|
165
|
+
|
|
166
|
+
const spreadThin = (occurrences: readonly Occurrence[], positions: ReadonlyMap<Sentence, number>, limit: number, unit: LengthUnit): boolean => {
|
|
167
|
+
const stretch = narrowestStretch(
|
|
168
|
+
occurrences.map((occurrence) => positions.get(occurrence.sentence) ?? 0),
|
|
169
|
+
limit + 1,
|
|
170
|
+
);
|
|
171
|
+
return stretch !== undefined && stretch > READING_SPAN[unit];
|
|
172
|
+
};
|
|
173
|
+
|
|
145
174
|
/**
|
|
146
175
|
* 範囲(gram-passages.ts)ごとに数える。よくある質問の別々の答えは別の範囲。節の最初の文(gram-openers.ts)にだけ出るものは節のひな形。
|
|
147
176
|
* 節の書き出しは書き手の地の文でもあるので、ほかの所にも出れば書き出しのぶんも数える(節ごとの口癖)。
|
|
148
177
|
*/
|
|
149
178
|
const repeatsOf = (doc: ProseDocument, limit: number): Repeat[] => {
|
|
150
|
-
const
|
|
179
|
+
const sources = doc.sentences.map((sentence) => compacted(sentence.text, doc.lengthUnit));
|
|
180
|
+
const closings = referenceClosings(
|
|
181
|
+
sources.map((source) => source.text),
|
|
182
|
+
doc.sentences.map((sentence, index) => linkedIn(doc, sentence, sources[index] ?? compacted(sentence.text, doc.lengthUnit))),
|
|
183
|
+
);
|
|
151
184
|
const context = { doc, names: namesOf(doc), endings: fixedEndingsOf(doc), closings };
|
|
152
185
|
const templates = templatesOf(doc);
|
|
153
186
|
const openers = sectionOpeners(doc.sections, doc.source);
|
|
154
187
|
const title = titleOf(doc);
|
|
188
|
+
const positions = sentencePositions(doc.sentences, doc.lengthUnit);
|
|
155
189
|
return gramPassagesOf(doc.sections).flatMap((sentences) =>
|
|
156
190
|
[...gramsOf(context, sentences).entries()].flatMap(([key, all]) => {
|
|
157
191
|
const occurrences = all.filter((occurrence) => !templates.has(occurrence.sentence));
|
|
158
192
|
const opensEverySection = occurrences.every((occurrence) => openers.has(occurrence.sentence));
|
|
159
|
-
|
|
193
|
+
const counted = occurrences.length > limit && isPhrasing(key, doc.lengthUnit) && !namedByHeading(key, title, doc.lengthUnit) && !opensEverySection;
|
|
194
|
+
return counted && !spreadThin(occurrences, positions, limit, doc.lengthUnit) ? [{ key, occurrences }] : [];
|
|
160
195
|
}),
|
|
161
196
|
);
|
|
162
197
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { lengthOf } from "../measure.ts";
|
|
2
|
+
import type { LengthUnit, Sentence } from "../plugin.ts";
|
|
3
|
+
|
|
4
|
+
/** 文の位置: その文より前の文の長さの合計(文書の長さの単位)。表や符号の塊は文ではないので、読む長さに入らない。 */
|
|
5
|
+
export const sentencePositions = (sentences: readonly Sentence[], unit: LengthUnit): ReadonlyMap<Sentence, number> => {
|
|
6
|
+
const positions = new Map<Sentence, number>();
|
|
7
|
+
sentences.reduce((position, sentence) => {
|
|
8
|
+
positions.set(sentence, position);
|
|
9
|
+
return position + lengthOf(sentence, unit);
|
|
10
|
+
}, 0);
|
|
11
|
+
return positions;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** 位置を count 個含む一番短い範囲の長さ。位置が count 個に満たなければ undefined。 */
|
|
15
|
+
export const narrowestStretch = (positions: readonly number[], count: number): number | undefined => {
|
|
16
|
+
if (count < 1 || positions.length < count) return undefined;
|
|
17
|
+
const sorted = positions.toSorted((left, right) => left - right);
|
|
18
|
+
return Math.min(...sorted.slice(count - 1).map((last, index) => last - (sorted[index] ?? last)));
|
|
19
|
+
};
|
|
@@ -84,3 +84,13 @@ export const isTitledPerson = (
|
|
|
84
84
|
(length) => isKnownWordAt(words, piece.end, piece.end + length, knownWords) || isKnownWordAt(words, piece.start - length, piece.start, knownWords),
|
|
85
85
|
);
|
|
86
86
|
};
|
|
87
|
+
|
|
88
|
+
/** words の index の語が、連なりの頭でも尻でもない、並べた物の後ろに付く語(高齢運転者等専用駐車区間の「等」)か。語の終わりを見せる。 */
|
|
89
|
+
export const isInnerSuffix = (words: readonly string[], index: number, suffixes: readonly string[]): boolean =>
|
|
90
|
+
index > 0 && index < words.length - 1 && suffixes.includes(words[index] ?? "");
|
|
91
|
+
|
|
92
|
+
/** 連なりの語(words)が、知っている語(known)か中ほどの並べた物の後ろに付く語(suffixes)を含むか。含まなければ、連なりをまるごと数える。 */
|
|
93
|
+
export const holdsKnown = (words: readonly string[], known: readonly string[], suffixes: readonly string[]): boolean => {
|
|
94
|
+
const whole = words.join("");
|
|
95
|
+
return known.some((word) => whole.includes(word)) || words.some((_, index) => isInnerSuffix(words, index, suffixes));
|
|
96
|
+
};
|
|
@@ -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
|
{
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { BulletList, Detector, DetectorOptions, Finding, ProseDocument, Sentence, Span } from "../plugin.ts";
|
|
1
|
+
import type { BulletList, Detector, DetectorOptions, Finding, LexiconEntry, ProseDocument, Sentence, Span, Token } from "../plugin.ts";
|
|
2
2
|
import { densityFindings } from "./ai-phrasing.ts";
|
|
3
3
|
import { endsWithColon } from "./lead-in.ts";
|
|
4
|
+
import { comparableText, comparableWords, entryIn, entryRanges } from "./lexicon-match.ts";
|
|
4
5
|
import { isMarkdownPath } from "../structure/markdown-path.ts";
|
|
5
6
|
|
|
6
7
|
/** Between a lead-in and its list: only blank space. A thematic break (`***`) separates them; bold marks sit inside the sentence. */
|
|
@@ -45,26 +46,41 @@ const MAX_LABEL_WORDS = 3;
|
|
|
45
46
|
const CLAUSE_POS: ReadonlySet<string> = new Set(["VERB", "AUX", "PRON", "SCONJ"]);
|
|
46
47
|
const NAME_POS: ReadonlySet<string> = new Set(["NOUN", "PROPN"]);
|
|
47
48
|
|
|
49
|
+
/** A verb right after a preposition is a gerund inside the phrase (Guidelines for addressing …), not the verb of a clause. */
|
|
50
|
+
const makesClause = (pos: string, before: string | undefined): boolean => CLAUSE_POS.has(pos) && !(pos === "VERB" && before === "ADP");
|
|
51
|
+
|
|
48
52
|
/** A noun phrase of any length, read from the adapter's parts of speech. Without them, only the short label counts. */
|
|
49
53
|
const isNounPhrase = (sentence: Sentence): boolean => {
|
|
50
54
|
const tokens = sentence.tokens ?? [];
|
|
51
|
-
return tokens.some((token) => NAME_POS.has(token.pos)) && !tokens.some((token) =>
|
|
55
|
+
return tokens.some((token) => NAME_POS.has(token.pos)) && !tokens.some((token, index) => makesClause(token.pos, tokens[index - 1]?.pos));
|
|
52
56
|
};
|
|
53
57
|
|
|
54
58
|
const isNounLabel = (words: string, sentence: Sentence): boolean =>
|
|
55
59
|
NOUN_LABEL.test(words) && (words.split(/\s+/u).length <= MAX_LABEL_WORDS || isNounPhrase(sentence));
|
|
56
60
|
|
|
61
|
+
/** A template keeps at least this many words fixed around its entry's name; fewer, and two different labels only look alike. */
|
|
62
|
+
const MIN_FIXED_WORDS = 3;
|
|
63
|
+
|
|
64
|
+
/** One label of a template that carries its entry's name: the same words but one (Guidelines for addressing High-sev / Low-sev issues). */
|
|
65
|
+
const sameButOneWord = (left: string, right: string): boolean => {
|
|
66
|
+
const leftWords = left.split(/\s+/u);
|
|
67
|
+
const rightWords = right.split(/\s+/u);
|
|
68
|
+
const differing = leftWords.filter((word, index) => word !== rightWords[index]).length;
|
|
69
|
+
return leftWords.length === rightWords.length && differing === 1 && leftWords.length - differing >= MIN_FIXED_WORDS;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const sameTemplate = (left: string, right: string): boolean => left === right || sameButOneWord(left, right);
|
|
73
|
+
|
|
57
74
|
/**
|
|
58
|
-
* A field of an entry's template: the same noun label before the list of each entry (**参考:** before each entry's link)
|
|
75
|
+
* A field of an entry's template: the same noun label before the list of each entry (**参考:** before each entry's link),
|
|
76
|
+
* or the same label with the entry's name in it (Guidelines for addressing High-sev issues: in each severity's section).
|
|
59
77
|
* It names the field the way a table's column does; it does not lead into anything.
|
|
60
78
|
*/
|
|
61
79
|
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));
|
|
80
|
+
const labels = leadIns.map((sentence) => ({ words: labelWords(sentence.text), sentence }));
|
|
81
|
+
const isField = ({ words, sentence }: (typeof labels)[number], index: number): boolean =>
|
|
82
|
+
labels.some((other, otherIndex) => otherIndex !== index && sameTemplate(words, other.words)) && isNounLabel(words, sentence);
|
|
83
|
+
return new Set(labels.filter(isField).map(({ words }) => words));
|
|
68
84
|
};
|
|
69
85
|
|
|
70
86
|
/** A line that is only a bold label (**Pros:**, **参考**:), and the label inside the bold marks. */
|
|
@@ -93,6 +109,36 @@ const opensWithCode = (source: string, item: Span): boolean => source.slice(item
|
|
|
93
109
|
/** A list whose every item opens with a code span names values, options or members (`omit`: never send …): a parts list, which can stay. */
|
|
94
110
|
const isValueList = (list: BulletList, source: string): boolean => list.itemSpans.length > 1 && list.itemSpans.every((item) => opensWithCode(source, item));
|
|
95
111
|
|
|
112
|
+
/** The language's words of a duty (should, must, なければならない), from the lead-in-duty lexicon. */
|
|
113
|
+
const DUTY_LEXICON = "lead-in-duty";
|
|
114
|
+
|
|
115
|
+
/** A pronoun right after another word opens a clause inside the lead-in (Questions you should ask:, what you must know:). */
|
|
116
|
+
const opensInnerClause = (tokens: readonly Token[], index: number): boolean =>
|
|
117
|
+
tokens[index - 1]?.pos === "PRON" && tokens[index - 2] !== undefined && tokens[index - 2]?.pos !== "PUNCT";
|
|
118
|
+
|
|
119
|
+
/** The duty word as the lead-in's own verb (the lead should:), not inside a clause that names the list (Questions you should ask:). */
|
|
120
|
+
const dutyAsVerb = (sentence: Sentence, entry: LexiconEntry): boolean => {
|
|
121
|
+
const tokens = sentence.tokens;
|
|
122
|
+
if (tokens === undefined || tokens.length === 0) return entryIn(sentence, entry);
|
|
123
|
+
return entryRanges(sentence, entry).some((range) => !opensInnerClause(tokens, range.start));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/** The lead-in without its colon and bold marks at the end. */
|
|
127
|
+
const withoutColon = (text: string): string => {
|
|
128
|
+
const chars = Array.from(text);
|
|
129
|
+
return chars.slice(0, chars.findLastIndex((char) => !LABEL_END.test(char)) + 1).join("");
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/** The duty at the lead-in's end, where a predicate-final language puts it (行わなければならない:). */
|
|
133
|
+
const closesLeadIn = (sentence: Sentence, entry: LexiconEntry): boolean => withoutColon(comparableText(sentence)).endsWith(comparableWords(entry.pattern));
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* A lead-in that states a duty (the lead should:, Each entry must include:) hands over what has to be done or contained:
|
|
137
|
+
* the steps or parts of a procedure, a real list that can stay. A word with position: after counts only at the lead-in's end.
|
|
138
|
+
*/
|
|
139
|
+
const statesDuty = (sentence: Sentence, duties: readonly LexiconEntry[]): boolean =>
|
|
140
|
+
duties.some((entry) => (entry.position === "after" ? closesLeadIn(sentence, entry) : dutyAsVerb(sentence, entry)));
|
|
141
|
+
|
|
96
142
|
/**
|
|
97
143
|
* Prose sentences ending in a colon that hand the reader straight to a list (「以下の通りです:」, "Here's what you need:").
|
|
98
144
|
* An item of a list is not prose: 「例: `code`」 ends in a colon once its code is masked, and it leads nowhere.
|
|
@@ -103,6 +149,7 @@ export const colonLeadIns = (
|
|
|
103
149
|
listSpans: readonly Span[],
|
|
104
150
|
source: string,
|
|
105
151
|
markdown = true,
|
|
152
|
+
duties: readonly LexiconEntry[] = [],
|
|
106
153
|
): Sentence[] => {
|
|
107
154
|
const leadIns = lists.flatMap((list) => {
|
|
108
155
|
if (isValueList(list, source)) return [];
|
|
@@ -111,7 +158,7 @@ export const colonLeadIns = (
|
|
|
111
158
|
});
|
|
112
159
|
const fields = fieldLabels(leadIns);
|
|
113
160
|
const namesItsList = (sentence: Sentence): boolean => (markdown ? isBoldNounLabel(sentence, source) : isPlainNounLabel(sentence, source));
|
|
114
|
-
return leadIns.filter((sentence) => !fields.has(labelWords(sentence.text)) && !namesItsList(sentence));
|
|
161
|
+
return leadIns.filter((sentence) => !fields.has(labelWords(sentence.text)) && !namesItsList(sentence) && !statesDuty(sentence, duties));
|
|
115
162
|
};
|
|
116
163
|
|
|
117
164
|
/** Lead-ins in one or two sections are a how-to's procedure or a pair of lists; the rule's why is section after section. */
|
|
@@ -135,7 +182,8 @@ const sectionsHolding = (doc: ProseDocument, leadIns: readonly Sentence[]): numb
|
|
|
135
182
|
*/
|
|
136
183
|
export const colonLeadIn: Detector = (doc, options): Finding[] => {
|
|
137
184
|
const inDocumentLanguage = doc.sentences.filter((sentence) => sentence.embeddedLanguage === undefined);
|
|
138
|
-
const
|
|
185
|
+
const duties = doc.lexicons[DUTY_LEXICON] ?? [];
|
|
186
|
+
const leadIns = colonLeadIns(inDocumentLanguage, doc.lists, doc.listSpans, doc.source, isMarkdownPath(doc.path), duties);
|
|
139
187
|
if (sectionsHolding(doc, leadIns) < minSections(options)) return [];
|
|
140
188
|
const hits = leadIns.map((sentence) => ({ sentence, matched: sentence.text.trim() }));
|
|
141
189
|
return densityFindings(doc, hits, options.limit);
|
|
@@ -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;
|