chaffjs 0.39.0 → 0.40.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/README.ja.md +3 -2
- package/README.md +2 -1
- 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/alternative-words.d.ts +6 -0
- package/dist/alternative-words.d.ts.map +1 -1
- package/dist/alternative-words.js +10 -0
- package/dist/alternative-words.js.map +1 -1
- package/dist/closing-code.d.ts +8 -0
- package/dist/closing-code.d.ts.map +1 -0
- package/dist/closing-code.js +18 -0
- package/dist/closing-code.js.map +1 -0
- package/dist/date-format.d.ts +19 -1
- package/dist/date-format.d.ts.map +1 -1
- package/dist/date-format.js +41 -5
- package/dist/date-format.js.map +1 -1
- package/dist/detectors/acronym-context.d.ts +1 -1
- package/dist/detectors/acronym-context.d.ts.map +1 -1
- package/dist/detectors/acronym-context.js +3 -2
- package/dist/detectors/acronym-context.js.map +1 -1
- package/dist/detectors/acronym-mention.d.ts +5 -0
- package/dist/detectors/acronym-mention.d.ts.map +1 -1
- package/dist/detectors/acronym-mention.js +16 -0
- package/dist/detectors/acronym-mention.js.map +1 -1
- package/dist/detectors/acronym-name.d.ts +5 -0
- package/dist/detectors/acronym-name.d.ts.map +1 -1
- package/dist/detectors/acronym-name.js +15 -0
- package/dist/detectors/acronym-name.js.map +1 -1
- package/dist/detectors/blank-runs.d.ts +12 -0
- package/dist/detectors/blank-runs.d.ts.map +1 -0
- package/dist/detectors/blank-runs.js +27 -0
- package/dist/detectors/blank-runs.js.map +1 -0
- package/dist/detectors/bold-label.d.ts +3 -1
- package/dist/detectors/bold-label.d.ts.map +1 -1
- package/dist/detectors/bold-label.js +16 -6
- package/dist/detectors/bold-label.js.map +1 -1
- package/dist/detectors/capitalised-term.d.ts.map +1 -1
- package/dist/detectors/capitalised-term.js +28 -9
- package/dist/detectors/capitalised-term.js.map +1 -1
- package/dist/detectors/citation-string.d.ts +6 -0
- package/dist/detectors/citation-string.d.ts.map +1 -1
- package/dist/detectors/citation-string.js +52 -0
- package/dist/detectors/citation-string.js.map +1 -1
- package/dist/detectors/contraction-emphasis.d.ts +3 -0
- package/dist/detectors/contraction-emphasis.d.ts.map +1 -0
- package/dist/detectors/contraction-emphasis.js +24 -0
- package/dist/detectors/contraction-emphasis.js.map +1 -0
- package/dist/detectors/contraction-mention.d.ts +12 -1
- package/dist/detectors/contraction-mention.d.ts.map +1 -1
- package/dist/detectors/contraction-mention.js +39 -6
- package/dist/detectors/contraction-mention.js.map +1 -1
- package/dist/detectors/contraction-mix.d.ts +3 -0
- package/dist/detectors/contraction-mix.d.ts.map +1 -0
- package/dist/detectors/contraction-mix.js +69 -0
- package/dist/detectors/contraction-mix.js.map +1 -0
- package/dist/detectors/date-format.d.ts.map +1 -1
- package/dist/detectors/date-format.js +13 -1
- package/dist/detectors/date-format.js.map +1 -1
- package/dist/detectors/duplicate-definition.d.ts +3 -0
- package/dist/detectors/duplicate-definition.d.ts.map +1 -0
- package/dist/detectors/duplicate-definition.js +16 -0
- package/dist/detectors/duplicate-definition.js.map +1 -0
- package/dist/detectors/final-period-captions.d.ts +15 -0
- package/dist/detectors/final-period-captions.d.ts.map +1 -0
- package/dist/detectors/final-period-captions.js +61 -0
- package/dist/detectors/final-period-captions.js.map +1 -0
- package/dist/detectors/heading-sentences.d.ts +10 -0
- package/dist/detectors/heading-sentences.d.ts.map +1 -0
- package/dist/detectors/heading-sentences.js +32 -0
- package/dist/detectors/heading-sentences.js.map +1 -0
- package/dist/detectors/lexicon.d.ts +1 -2
- package/dist/detectors/lexicon.d.ts.map +1 -1
- package/dist/detectors/lexicon.js +5 -55
- package/dist/detectors/lexicon.js.map +1 -1
- package/dist/detectors/list-lead-in.d.ts +1 -1
- package/dist/detectors/list-lead-in.d.ts.map +1 -1
- package/dist/detectors/list-lead-in.js +31 -10
- package/dist/detectors/list-lead-in.js.map +1 -1
- package/dist/detectors/missing-final-period.d.ts.map +1 -1
- package/dist/detectors/missing-final-period.js +3 -1
- package/dist/detectors/missing-final-period.js.map +1 -1
- package/dist/detectors/numbering-readings.d.ts +2 -2
- package/dist/detectors/numbering-readings.d.ts.map +1 -1
- package/dist/detectors/numbering-readings.js +7 -0
- package/dist/detectors/numbering-readings.js.map +1 -1
- package/dist/detectors/open-end-anaphor.d.ts +8 -0
- package/dist/detectors/open-end-anaphor.d.ts.map +1 -0
- package/dist/detectors/open-end-anaphor.js +22 -0
- package/dist/detectors/open-end-anaphor.js.map +1 -0
- package/dist/detectors/open-end-lead.d.ts +4 -0
- package/dist/detectors/open-end-lead.d.ts.map +1 -0
- package/dist/detectors/open-end-lead.js +34 -0
- package/dist/detectors/open-end-lead.js.map +1 -0
- package/dist/detectors/open-end-scope.d.ts.map +1 -1
- package/dist/detectors/open-end-scope.js +7 -1
- package/dist/detectors/open-end-scope.js.map +1 -1
- package/dist/detectors/ordinal-run-gaps.d.ts +1 -0
- package/dist/detectors/ordinal-run-gaps.d.ts.map +1 -1
- package/dist/detectors/ordinal-run-gaps.js +1 -1
- package/dist/detectors/ordinal-run-gaps.js.map +1 -1
- package/dist/detectors/orthographic-variant-open.d.ts +4 -2
- package/dist/detectors/orthographic-variant-open.d.ts.map +1 -1
- package/dist/detectors/orthographic-variant-open.js +5 -3
- package/dist/detectors/orthographic-variant-open.js.map +1 -1
- package/dist/detectors/orthography.d.ts.map +1 -1
- package/dist/detectors/orthography.js +14 -5
- package/dist/detectors/orthography.js.map +1 -1
- package/dist/detectors/placeholder-text.d.ts.map +1 -1
- package/dist/detectors/placeholder-text.js +14 -2
- package/dist/detectors/placeholder-text.js.map +1 -1
- package/dist/detectors/quote-without-source.d.ts +6 -2
- package/dist/detectors/quote-without-source.d.ts.map +1 -1
- package/dist/detectors/quote-without-source.js +33 -8
- package/dist/detectors/quote-without-source.js.map +1 -1
- package/dist/detectors/register-separate-part.d.ts +7 -0
- package/dist/detectors/register-separate-part.d.ts.map +1 -0
- package/dist/detectors/register-separate-part.js +32 -0
- package/dist/detectors/register-separate-part.js.map +1 -0
- package/dist/detectors/registry/contraction-mix.js +1 -1
- package/dist/detectors/registry/contraction-mix.js.map +1 -1
- package/dist/detectors/registry/duplicate-definition.js +1 -1
- package/dist/detectors/registry/duplicate-definition.js.map +1 -1
- package/dist/detectors/reproduced-text.d.ts +5 -0
- package/dist/detectors/reproduced-text.d.ts.map +1 -0
- package/dist/detectors/reproduced-text.js +19 -0
- package/dist/detectors/reproduced-text.js.map +1 -0
- package/dist/detectors/request-owner.d.ts +2 -1
- package/dist/detectors/request-owner.d.ts.map +1 -1
- package/dist/detectors/request-owner.js +31 -7
- package/dist/detectors/request-owner.js.map +1 -1
- package/dist/detectors/requirement-smell.d.ts.map +1 -1
- package/dist/detectors/requirement-smell.js +16 -5
- package/dist/detectors/requirement-smell.js.map +1 -1
- package/dist/detectors/sentence-counts.d.ts +2 -1
- package/dist/detectors/sentence-counts.d.ts.map +1 -1
- package/dist/detectors/sentence-counts.js +23 -7
- package/dist/detectors/sentence-counts.js.map +1 -1
- package/dist/detectors/sentence-ending.d.ts +1 -0
- package/dist/detectors/sentence-ending.d.ts.map +1 -1
- package/dist/detectors/sentence-ending.js +5 -2
- package/dist/detectors/sentence-ending.js.map +1 -1
- package/dist/detectors/sentence-length.d.ts.map +1 -1
- package/dist/detectors/sentence-length.js +13 -3
- package/dist/detectors/sentence-length.js.map +1 -1
- package/dist/detectors/sentence-parts.d.ts +2 -0
- package/dist/detectors/sentence-parts.d.ts.map +1 -1
- package/dist/detectors/sentence-parts.js +3 -1
- package/dist/detectors/sentence-parts.js.map +1 -1
- package/dist/detectors/signals.d.ts.map +1 -1
- package/dist/detectors/signals.js +6 -4
- package/dist/detectors/signals.js.map +1 -1
- package/dist/detectors/spelling-variety.d.ts.map +1 -1
- package/dist/detectors/spelling-variety.js +20 -2
- package/dist/detectors/spelling-variety.js.map +1 -1
- package/dist/detectors/structure-tree.d.ts +2 -1
- package/dist/detectors/structure-tree.d.ts.map +1 -1
- package/dist/detectors/structure-tree.js +2 -7
- package/dist/detectors/structure-tree.js.map +1 -1
- package/dist/detectors/structure.d.ts.map +1 -1
- package/dist/detectors/structure.js +19 -5
- package/dist/detectors/structure.js.map +1 -1
- package/dist/detectors/token-shape.d.ts.map +1 -1
- package/dist/detectors/token-shape.js +12 -6
- package/dist/detectors/token-shape.js.map +1 -1
- package/dist/detectors/undefined-term.d.ts +5 -0
- package/dist/detectors/undefined-term.d.ts.map +1 -1
- package/dist/detectors/undefined-term.js +22 -1
- package/dist/detectors/undefined-term.js.map +1 -1
- package/dist/detectors/unfilled-placeholder.d.ts.map +1 -1
- package/dist/detectors/unfilled-placeholder.js +15 -1
- package/dist/detectors/unfilled-placeholder.js.map +1 -1
- package/dist/detectors/unknown-word-context.d.ts +7 -0
- package/dist/detectors/unknown-word-context.d.ts.map +1 -1
- package/dist/detectors/unknown-word-context.js +11 -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 +5 -4
- package/dist/detectors/unknown-word.js.map +1 -1
- package/dist/latin-spacing-source.d.ts +12 -0
- package/dist/latin-spacing-source.d.ts.map +1 -0
- package/dist/latin-spacing-source.js +25 -0
- package/dist/latin-spacing-source.js.map +1 -0
- package/dist/name-separator.d.ts.map +1 -1
- package/dist/name-separator.js +4 -1
- package/dist/name-separator.js.map +1 -1
- package/dist/paragraph-length.d.ts +14 -0
- package/dist/paragraph-length.d.ts.map +1 -1
- package/dist/paragraph-length.js +25 -0
- package/dist/paragraph-length.js.map +1 -1
- package/dist/paragraph-runs.d.ts +2 -2
- package/dist/paragraph-runs.d.ts.map +1 -1
- package/dist/paragraph-runs.js +65 -14
- package/dist/paragraph-runs.js.map +1 -1
- package/dist/quote-echo.d.ts +6 -0
- package/dist/quote-echo.d.ts.map +1 -0
- package/dist/quote-echo.js +14 -0
- package/dist/quote-echo.js.map +1 -0
- package/dist/semantic.d.ts.map +1 -1
- package/dist/semantic.js +2 -2
- package/dist/semantic.js.map +1 -1
- package/dist/set-phrase-particles.d.ts +4 -0
- package/dist/set-phrase-particles.d.ts.map +1 -0
- package/dist/set-phrase-particles.js +26 -0
- package/dist/set-phrase-particles.js.map +1 -0
- package/dist/structure/alternative-clause.d.ts +7 -0
- package/dist/structure/alternative-clause.d.ts.map +1 -1
- package/dist/structure/alternative-clause.js +23 -8
- package/dist/structure/alternative-clause.js.map +1 -1
- package/dist/structure/cited-document-section.d.ts +2 -0
- package/dist/structure/cited-document-section.d.ts.map +1 -1
- package/dist/structure/cited-document-section.js +8 -0
- package/dist/structure/cited-document-section.js.map +1 -1
- package/dist/structure/issues.d.ts +3 -1
- package/dist/structure/issues.d.ts.map +1 -1
- package/dist/structure/issues.js +12 -8
- package/dist/structure/issues.js.map +1 -1
- package/dist/structure/numbering.d.ts +10 -1
- package/dist/structure/numbering.d.ts.map +1 -1
- package/dist/structure/numbering.js +41 -4
- package/dist/structure/numbering.js.map +1 -1
- package/dist/structure/unread-label.d.ts +13 -0
- package/dist/structure/unread-label.d.ts.map +1 -0
- package/dist/structure/unread-label.js +70 -0
- package/dist/structure/unread-label.js.map +1 -0
- package/dist/topic-clause.d.ts.map +1 -1
- package/dist/topic-clause.js +15 -2
- package/dist/topic-clause.js.map +1 -1
- package/package.json +3 -3
- package/rules/ai-generated-composite.yaml +1 -0
- package/rules/ai-tell.yaml +6 -2
- package/rules/bold-label-list.yaml +6 -6
- package/rules/closing-cliche.yaml +2 -2
- package/rules/colon-lead-in.yaml +2 -2
- package/rules/contraction-consistency.yaml +2 -2
- package/rules/date-format-consistency.yaml +2 -2
- package/rules/duplicate-definition.yaml +2 -2
- package/rules/honesty-marker.yaml +84 -0
- package/rules/latin-spacing.yaml +6 -4
- package/rules/max-paragraph-length.yaml +2 -2
- package/rules/max-sentence-length.yaml +4 -2
- package/rules/missing-final-period.yaml +2 -2
- package/rules/no-doubled-joshi.yaml +2 -2
- package/rules/no-mixed-desumasu.yaml +2 -2
- package/rules/nominalization.yaml +1 -1
- package/rules/numbering-gap.yaml +2 -2
- package/rules/one-sentence-paragraph-run.yaml +2 -2
- package/rules/orthographic-variant.yaml +2 -2
- package/rules/quote-without-source.yaml +2 -2
- package/rules/request-without-deadline.yaml +2 -2
- package/rules/requirement-smell.yaml +2 -2
- package/rules/spelling-consistency.yaml +4 -4
- package/rules/teki-overuse.yaml +2 -2
- package/rules/topic-predicate-distance.yaml +2 -2
- package/rules/undefined-acronym.yaml +3 -2
- package/rules/undefined-term.yaml +2 -2
- package/rules/unfilled-placeholder.yaml +2 -2
- package/rules/unknown-word.yaml +2 -2
- package/skills/chaff/SKILL.md +2 -1
- package/src/ai-score/human-shares.ts +1 -0
- package/src/alternative-words.ts +15 -0
- package/src/closing-code.ts +21 -0
- package/src/date-format.ts +54 -7
- package/src/detectors/acronym-context.ts +3 -2
- package/src/detectors/acronym-mention.ts +17 -0
- package/src/detectors/acronym-name.ts +18 -0
- package/src/detectors/blank-runs.ts +30 -0
- package/src/detectors/bold-label.ts +22 -6
- package/src/detectors/capitalised-term.ts +36 -9
- package/src/detectors/citation-string.ts +57 -0
- package/src/detectors/contraction-emphasis.ts +46 -0
- package/src/detectors/contraction-mention.ts +52 -6
- package/src/detectors/contraction-mix.ts +94 -0
- package/src/detectors/date-format.ts +18 -2
- package/src/detectors/duplicate-definition.ts +21 -0
- package/src/detectors/final-period-captions.ts +78 -0
- package/src/detectors/heading-sentences.ts +36 -0
- package/src/detectors/lexicon.ts +5 -61
- package/src/detectors/list-lead-in.ts +44 -10
- package/src/detectors/missing-final-period.ts +9 -1
- package/src/detectors/numbering-readings.ts +10 -2
- package/src/detectors/open-end-anaphor.ts +31 -0
- package/src/detectors/open-end-lead.ts +46 -0
- package/src/detectors/open-end-scope.ts +8 -1
- package/src/detectors/ordinal-run-gaps.ts +1 -1
- package/src/detectors/orthographic-variant-open.ts +8 -3
- package/src/detectors/orthography.ts +20 -7
- package/src/detectors/placeholder-text.ts +21 -4
- package/src/detectors/quote-without-source.ts +52 -10
- package/src/detectors/register-separate-part.ts +42 -0
- package/src/detectors/registry/contraction-mix.ts +1 -1
- package/src/detectors/registry/duplicate-definition.ts +1 -1
- package/src/detectors/reproduced-text.ts +33 -0
- package/src/detectors/request-owner.ts +31 -8
- package/src/detectors/requirement-smell.ts +21 -5
- package/src/detectors/sentence-counts.ts +27 -7
- package/src/detectors/sentence-ending.ts +6 -2
- package/src/detectors/sentence-length.ts +18 -3
- package/src/detectors/sentence-parts.ts +4 -1
- package/src/detectors/signals.ts +7 -4
- package/src/detectors/spelling-variety.ts +26 -2
- package/src/detectors/structure-tree.ts +2 -8
- package/src/detectors/structure.ts +22 -6
- package/src/detectors/token-shape.ts +16 -6
- package/src/detectors/undefined-term.ts +26 -1
- package/src/detectors/unfilled-placeholder.ts +20 -2
- package/src/detectors/unknown-word-context.ts +17 -0
- package/src/detectors/unknown-word.ts +15 -4
- package/src/latin-spacing-source.ts +33 -0
- package/src/name-separator.ts +4 -1
- package/src/paragraph-length.ts +31 -0
- package/src/paragraph-runs.ts +71 -13
- package/src/quote-echo.ts +18 -0
- package/src/semantic.ts +2 -3
- package/src/set-phrase-particles.ts +32 -0
- package/src/structure/alternative-clause.ts +32 -7
- package/src/structure/cited-document-section.ts +8 -0
- package/src/structure/issues.ts +19 -9
- package/src/structure/numbering.ts +50 -3
- package/src/structure/unread-label.ts +74 -0
- package/src/topic-clause.ts +16 -2
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** A fenced block: code the closing shows, never only a mention of code the body gave. */
|
|
2
|
+
const FENCE = /^[ \t]*(?:```|~~~)/mu;
|
|
3
|
+
/** Inline code (`pip freeze`) and a bare or linked address (https://…). */
|
|
4
|
+
const CODE_OR_LINK = /`([^`\n]+)`|(https?:\/\/[^\s)>\]]+)/gu;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Whether the closing gives code or a link the text before it did not. A closing that repeats the body's commands
|
|
8
|
+
* (`python -m venv .venv` again) restates the body, the same as one that repeats its numbers.
|
|
9
|
+
*/
|
|
10
|
+
export const hasNewCodeOrLink = (closing: string, earlier: string): boolean =>
|
|
11
|
+
FENCE.test(closing) || [...closing.matchAll(CODE_OR_LINK)].some((match) => !earlier.includes((match[1] ?? match[2] ?? "").trim()));
|
|
12
|
+
|
|
13
|
+
const HEADING = /^#{1,6}[ \t]/u;
|
|
14
|
+
const ITEM_MARKER = /^[ \t]*(?:[-*+]|\d{1,9}[.)])[ \t]/u;
|
|
15
|
+
const ADDRESS = /https?:\/\//u;
|
|
16
|
+
|
|
17
|
+
/** Whether a section, its heading aside, is only list items that each carry a link: references, not a closing that could restate anything. */
|
|
18
|
+
export const isLinkList = (section: string): boolean => {
|
|
19
|
+
const lines = section.split("\n").filter((line) => line.trim() !== "" && !HEADING.test(line));
|
|
20
|
+
return lines.length > 0 && lines.every((line) => ITEM_MARKER.test(line) && ADDRESS.test(line));
|
|
21
|
+
};
|
package/src/date-format.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// 日付の書き方(2026-10-02、2026年10月2日、Oct 2, 2026)を読む。純粋な関数だけを置く。
|
|
2
2
|
// 月の名前と元号は言語パッケージの語彙表が渡す。年・月・日のそろった日付だけを読む(年だけ、月日だけは書き方を比べられない)。
|
|
3
3
|
|
|
4
|
+
import type { Span } from "./plugin.ts";
|
|
5
|
+
|
|
4
6
|
/** 見つけた日付 1 つ。offset は渡した文字列の中の位置、style は書き方の名前。 */
|
|
5
7
|
export type WrittenDate = { readonly offset: number; readonly written: string; readonly style: string };
|
|
6
8
|
|
|
@@ -123,8 +125,8 @@ const withoutGap = (text: string): string => {
|
|
|
123
125
|
return chars.slice(0, chars.findLastIndex((char) => !STAMP_GAP.test(char)) + 1).join("");
|
|
124
126
|
};
|
|
125
127
|
|
|
126
|
-
/** 刻印のラベルの前に来てよい字。行の頭か、ページの部品を区切る空白と縦線(ページID:0014616
|
|
127
|
-
const STAMP_LEAD = /[\s||]/u;
|
|
128
|
+
/** 刻印のラベルの前に来てよい字。行の頭か、ページの部品を区切る空白と縦線(ページID:0014616 更新日:)と、刻印を囲む開き括弧([更新日 2021年10月1日])。 */
|
|
129
|
+
const STAMP_LEAD = /[\s||[[【((]/u;
|
|
128
130
|
|
|
129
131
|
/**
|
|
130
132
|
* text が刻印のラベル word で終わり、ラベルが行の頭か区切りの後ろから始まる。文の中の語(この日付は公開:、The date:、
|
|
@@ -145,18 +147,63 @@ export const stampedDate = (text: string, offset: number, stampLabels: readonly
|
|
|
145
147
|
return stampLabels.some((label) => endsWithLabel(before, label));
|
|
146
148
|
};
|
|
147
149
|
|
|
148
|
-
|
|
150
|
+
/** 見出しの行。 */
|
|
151
|
+
const HEADING_LINE = /^ {0,3}#{1,6}(?:\s|$)/u;
|
|
152
|
+
/** 日付を除いた行に残ってよいもの: 空白と強調の印。 */
|
|
153
|
+
const DATE_ONLY_REST = /^[\s*_]*$/u;
|
|
154
|
+
|
|
155
|
+
const lineAround = (text: string, offset: number): Span => {
|
|
156
|
+
const end = text.indexOf("\n", offset);
|
|
157
|
+
return { start: text.lastIndexOf("\n", offset - 1) + 1, end: end === -1 ? text.length : end };
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 文書の頭(空行と見出しのほかに何も無い所)の、日付だけの行。題のすぐ下に CMS が付けるページの日付(# 講師募集 / 2026年10月7日)で、
|
|
162
|
+
* 書いた人が本文で選んだ書き方(令和8年…)とは別のもの。source は記法を覆う前の文書で、位置は text と同じ。
|
|
163
|
+
*/
|
|
164
|
+
export const headerDate = (source: string, date: WrittenDate): boolean => {
|
|
165
|
+
const line = lineAround(source, date.offset);
|
|
166
|
+
const rest = source.slice(line.start, date.offset) + source.slice(date.offset + date.written.length, line.end);
|
|
167
|
+
if (!DATE_ONLY_REST.test(rest)) return false;
|
|
168
|
+
return source
|
|
169
|
+
.slice(0, line.start)
|
|
170
|
+
.split("\n")
|
|
171
|
+
.every((above) => above.trim() === "" || HEADING_LINE.test(above));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
/** 括弧一つで行の全部を囲んだ行([受理:2020-11-18]、[更新日 2021年10月1日])。 */
|
|
175
|
+
const BRACKETED_LINE = /^\s*[[[【((][^\]]】))\n]*[\]]】))]\s*$/u;
|
|
176
|
+
|
|
177
|
+
/** 日付が、括弧一つで囲んだ行の中にある。論文の受理日や頁の更新日を付け足した刻印で、本文の日付の書き方とは別のもの。 */
|
|
178
|
+
export const bracketedDate = (text: string, date: WrittenDate): boolean => {
|
|
179
|
+
const line = lineAround(text, date.offset);
|
|
180
|
+
return BRACKETED_LINE.test(text.slice(line.start, line.end));
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 日付の置き場所の手がかり。stampLabels は刻印のラベル(語彙表 date-stamp-label)、cited は文献一覧と注の並びの範囲、
|
|
185
|
+
* source は記法を覆う前の文書(文書の頭の日付の行を見分ける)。
|
|
186
|
+
*/
|
|
187
|
+
export type DatePlaces = { readonly stampLabels?: readonly string[]; readonly cited?: readonly Span[]; readonly source?: string };
|
|
188
|
+
|
|
189
|
+
type DateGroup = "head" | "body" | "stamp" | "cited";
|
|
190
|
+
|
|
191
|
+
const isWithin = (spans: readonly Span[], offset: number): boolean => spans.some((span) => offset >= span.start && offset < span.end);
|
|
149
192
|
|
|
150
|
-
const groupOf = (text: string, date: WrittenDate,
|
|
151
|
-
if (
|
|
193
|
+
const groupOf = (text: string, date: WrittenDate, places: DatePlaces): DateGroup => {
|
|
194
|
+
if (isWithin(places.cited ?? [], date.offset)) return "cited";
|
|
195
|
+
if (stampedDate(text, date.offset, places.stampLabels ?? []) || bracketedDate(text, date)) return "stamp";
|
|
196
|
+
if (places.source !== undefined && headerDate(places.source, date)) return "stamp";
|
|
152
197
|
return opensLine(text, date.offset) ? "head" : "body";
|
|
153
198
|
};
|
|
154
199
|
|
|
200
|
+
/** 比べる組。文献一覧と注の日付(cited)は比べない。題の中の日付は引いた文書の書き方で、参照日は文献の書き方の決まりに従う。 */
|
|
155
201
|
const GROUP_ORDER: readonly DateGroup[] = ["head", "body", "stamp"];
|
|
156
202
|
|
|
157
203
|
/**
|
|
158
204
|
* 行の頭の日付と、文の中の日付と、更新日・公開日の刻印の日付を分ける。予定表を 2026-10-16 で並べ、文では 14 October 2026 と
|
|
159
205
|
* 書くのは使い分けで、混ざりではない。刻印のラベルは言語パッケージの語彙表(date-stamp-label)が渡す。書き方はそれぞれの組の中でだけ比べる。
|
|
206
|
+
* 文献一覧と注の並びの中の日付は、どの組にも入れない。
|
|
160
207
|
*/
|
|
161
|
-
export const dateGroups = (text: string, dates: readonly WrittenDate[],
|
|
162
|
-
GROUP_ORDER.map((group) => dates.filter((date) => groupOf(text, date,
|
|
208
|
+
export const dateGroups = (text: string, dates: readonly WrittenDate[], places: DatePlaces = {}): WrittenDate[][] =>
|
|
209
|
+
GROUP_ORDER.map((group) => dates.filter((date) => groupOf(text, date, places) === group)).filter((group) => group.length > 0);
|
|
@@ -6,7 +6,7 @@ import { loneNumeralSpans, nameNumeralSpans } from "./name-numeral.ts";
|
|
|
6
6
|
import { placeholderSpans } from "./placeholder.ts";
|
|
7
7
|
import { numeralWordSpans } from "./numeral-word.ts";
|
|
8
8
|
import { seriesLabelSpans } from "./series-label.ts";
|
|
9
|
-
import { nameSpansOf, oneOf } from "./acronym-name.ts";
|
|
9
|
+
import { nameSpansOf, oneOf, postalAddressSpans } from "./acronym-name.ts";
|
|
10
10
|
import { grammarSpans } from "./abnf-grammar.ts";
|
|
11
11
|
import { outlineLabelSpans } from "./outline-label.ts";
|
|
12
12
|
import { referenceListSpans } from "../reference-lists.ts";
|
|
@@ -180,8 +180,9 @@ export const lexiconNotationOf = (wordsOf: (id: string) => string[]): NotAcronym
|
|
|
180
180
|
return (text) => [...notation(text), ...names(text)];
|
|
181
181
|
};
|
|
182
182
|
|
|
183
|
-
/** 文書全体で読む、略語として数えない範囲。文献一覧の誌名や会議名、番号の付いた区切りの見出し、ABNF
|
|
183
|
+
/** 文書全体で読む、略語として数えない範囲。文献一覧の誌名や会議名、番号の付いた区切りの見出し、ABNF の文法、文に切れ目の入る所在地(Rd. NE)。 */
|
|
184
184
|
export const sourceNotAcronymSpans = (source: string, wordsOf: (id: string) => string[]): Span[] => [
|
|
185
|
+
...postalAddressSpans(source, wordsOf("street-suffix"), wordsOf("us-state-code")),
|
|
185
186
|
...referenceListSpans(source, wordsOf("reference-list-heading")),
|
|
186
187
|
...outlineLabelSpans(source, wordsOf("numbered-division")),
|
|
187
188
|
...grammarSpans(source),
|
|
@@ -42,3 +42,20 @@ export const caseVariantsIn = (text: string, acronym: string): string[] => {
|
|
|
42
42
|
const variants = [...text.matchAll(written)].map((match) => match[0]);
|
|
43
43
|
return [...new Set(variants)].filter((variant) => variant !== acronym && (variant.match(CAPITAL) ?? []).length >= MIN_CAPITALS);
|
|
44
44
|
};
|
|
45
|
+
|
|
46
|
+
/** A name in capitals that the text also writes as a word is that word set in capitals; short capitals (IT, It) are left alone. */
|
|
47
|
+
const MIN_WORD_LETTERS = 4;
|
|
48
|
+
/** Before a word inside a sentence: a lower-case word, a number, a comma or a slash, not a sentence's or a line's start (Its, It). */
|
|
49
|
+
const INSIDE_SENTENCE = /(?:[\p{Ll}\p{N},;&)][ \t]+|[/(][ \t]*)$/u;
|
|
50
|
+
const LOOK_BEHIND = 4;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Whether the text writes the acronym's letters as a word with only its first letter a capital, inside a sentence (AIRBNB
|
|
54
|
+
* beside "a hotel / Airbnb"). The capitals are then the name written loud, not an acronym with an expansion.
|
|
55
|
+
*/
|
|
56
|
+
export const writtenAsWordIn = (text: string, acronym: string): boolean => {
|
|
57
|
+
if (!/^\p{Lu}+$/u.test(acronym) || Array.from(acronym).length < MIN_WORD_LETTERS) return false;
|
|
58
|
+
const word = `${acronym.slice(0, 1)}${acronym.slice(1).toLowerCase()}`;
|
|
59
|
+
const written = new RegExp(String.raw`(?<![\p{L}\p{N}])${escapeRegExp(word)}(?![\p{L}\p{N}])`, "gu");
|
|
60
|
+
return [...text.matchAll(written)].some((match) => INSIDE_SENTENCE.test(text.slice(Math.max(0, match.index - LOOK_BEHIND), match.index)));
|
|
61
|
+
};
|
|
@@ -134,6 +134,24 @@ export const streetSuffixSpans = (text: string, suffixes: readonly string[]): Sp
|
|
|
134
134
|
? []
|
|
135
135
|
: spansOf(text, new RegExp(String.raw`(?<=${EDGE_BEFORE}\d+[A-Z]?[ \t]+(?:\p{Lu}\p{Ll}[\p{L}'’.-]*[ \t]+){1,3})${oneOf(suffixes)}${EDGE_AFTER}`, "gu"));
|
|
136
136
|
|
|
137
|
+
/** 通りの種類は、語彙表の大文字の書き方と頭だけ大文字の書き方(DR と Dr)、略さずに書いた語(Road)。 */
|
|
138
|
+
const streetTypesOf = (suffixes: readonly string[]): string[] =>
|
|
139
|
+
suffixes.flatMap((suffix) => [suffix, `${suffix.slice(0, 1)}${suffix.slice(1).toLowerCase()}`]);
|
|
140
|
+
|
|
141
|
+
/** 番地から州と郵便番号までの、一行の所在地の長さの上限。 */
|
|
142
|
+
const MAX_ADDRESS_TAIL = 80;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* 番地・通り・市・州と郵便番号まで揃った一行の所在地(1600 Clifton Rd. NE, MS H21-8, Atlanta, GA 30329)。その中の大文字
|
|
146
|
+
* (通りの方角 NE、局内の宛先 MS)は所在地の部品で、略語ではない。州と郵便番号で終わらないもの(3 AI CT)は所在地と読まない。
|
|
147
|
+
*/
|
|
148
|
+
export const postalAddressSpans = (text: string, suffixes: readonly string[], states: readonly string[]): Span[] => {
|
|
149
|
+
if (suffixes.length === 0 || states.length === 0) return [];
|
|
150
|
+
const street = String.raw`${EDGE_BEFORE}\d+[A-Z]?[ \t]+(?:\p{Lu}\p{Ll}[\p{L}'’.-]*[ \t]+){1,3}(?:${oneOf(streetTypesOf(suffixes))}\.?|\p{Lu}\p{Ll}+)(?=[ \t,])`;
|
|
151
|
+
const city = String.raw`,[ \t]+\p{Lu}[\p{L}'’.-]*(?:[ \t]+\p{Lu}[\p{L}'’.-]*){0,2},[ \t]+${oneOf(states)}[ \t]+\d{5}(?:-\d{4})?(?!\d)`;
|
|
152
|
+
return spansOf(text, new RegExp(`${street}[^\n]{0,${MAX_ADDRESS_TAIL}}?${city}`, "gu"));
|
|
153
|
+
};
|
|
154
|
+
|
|
137
155
|
/** 大文字で書く決まり文句(NOW, THEREFORE、AND/OR)。 */
|
|
138
156
|
export const capitalPhraseSpans = (text: string, phrases: readonly string[]): Span[] =>
|
|
139
157
|
phrases.length === 0 ? [] : spansOf(text, new RegExp(`${EDGE_BEFORE}${oneOf(phrases)}${EDGE_AFTER}`, "gu"));
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type Span = { readonly start: number; readonly end: number };
|
|
2
|
+
|
|
3
|
+
/** Between two pieces of one blank: marks, spaces (\s takes in the full-width one) and a number's hyphens, on one line (〇〇区〇〇 〇丁目, 03-〇〇〇〇-〇〇〇〇). */
|
|
4
|
+
const JOINER = /^(?:(?!\n)[\s○〇◯△\-‐-])*$/u;
|
|
5
|
+
const JOINER_CHAR = /(?!\n)[\s○〇◯△\-‐-]/u;
|
|
6
|
+
const MARK = /[○〇◯△]/u;
|
|
7
|
+
|
|
8
|
+
/** How far the joiner characters reach from `from` in one direction, ending on a blank mark (〇〇区〇〇 takes in the last 〇〇). */
|
|
9
|
+
const reach = (text: string, from: number, step: 1 | -1): number => {
|
|
10
|
+
const chars = step === 1 ? Array.from(text.slice(from)) : Array.from(text.slice(0, from)).reverse();
|
|
11
|
+
const run = chars.findIndex((char) => !JOINER_CHAR.test(char));
|
|
12
|
+
const within = (run === -1 ? chars : chars.slice(0, run)).map((char) => char.length);
|
|
13
|
+
const lastMark = (run === -1 ? chars : chars.slice(0, run)).findLastIndex((char) => MARK.test(char));
|
|
14
|
+
return from + step * within.slice(0, lastMark + 1).reduce((sum, length) => sum + length, 0);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The spans as the blanks a reader sees: pieces that overlap, or that only blank marks, spaces and hyphens on one line
|
|
19
|
+
* separate, are one blank, and so are the marks right beside them (〒〇〇〇-〇〇〇〇, 〇丁目〇番〇号 〇〇ビル, 東京都〇〇区〇〇).
|
|
20
|
+
* A filled word between two pieces (株式会社〇〇の〇〇様) keeps them two.
|
|
21
|
+
*/
|
|
22
|
+
export const joinedBlanks = (text: string, spans: readonly Span[]): Span[] =>
|
|
23
|
+
spans
|
|
24
|
+
.toSorted((left, right) => left.start - right.start || left.end - right.end)
|
|
25
|
+
.reduce<Span[]>((joined, span) => {
|
|
26
|
+
const last = joined.at(-1);
|
|
27
|
+
if (last === undefined || (span.start > last.end && !JOINER.test(text.slice(last.end, span.start)))) return [...joined, span];
|
|
28
|
+
return [...joined.slice(0, -1), { start: last.start, end: Math.max(last.end, span.end) }];
|
|
29
|
+
}, [])
|
|
30
|
+
.map((span) => ({ start: reach(text, span.start, -1), end: reach(text, span.end, 1) }));
|
|
@@ -32,16 +32,32 @@ export const opensWithBoldLabel = (item: string): boolean => BOLD_LABEL.test(ite
|
|
|
32
32
|
/** Whether the line holding `offset` is quoted (`> - **a**:…`): someone else's words, not the writer's to fix. */
|
|
33
33
|
export const isQuotedAt = (source: string, offset: number): boolean => source.slice(source.lastIndexOf("\n", offset - 1) + 1, offset).includes(">");
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
35
|
+
/** A bold run longer than this is a paragraph in bold, not a lead sentence. */
|
|
36
|
+
const MAX_LEAD_CHARS = 80;
|
|
37
|
+
|
|
38
|
+
/** `**Label.** text`: a bold lead sentence, its stop inside the bold, then the item goes on. The same label, closed by a stop instead of a colon. */
|
|
39
|
+
const BOLD_LEAD = new RegExp(String.raw`^(\*\*|__)(?!\s)((?:(?!\1)[^\n]){1,${MAX_LEAD_CHARS}}?[.!?。!?])\1[ \t]*\S`, "u");
|
|
40
|
+
|
|
41
|
+
/** The lead sentence a list item opens with in bold, its stop left off, or undefined. `item` is the item as written, marker and all. */
|
|
42
|
+
export const boldLeadOf = (item: string): string | undefined =>
|
|
43
|
+
BOLD_LEAD.exec(item.replace(ITEM_MARKER, "").replace(TASK_BOX, ""))?.[2]
|
|
44
|
+
?.trim()
|
|
45
|
+
.replace(/[.!?。!?]$/u, "");
|
|
46
|
+
|
|
47
|
+
const labelOrLeadOf = (item: string): string | undefined => boldLabelOf(item) ?? boldLeadOf(item);
|
|
48
|
+
|
|
49
|
+
const unquotedItems = (doc: ProseDocument, opens: (item: string) => string | undefined): Span[] =>
|
|
37
50
|
doc.lists
|
|
38
51
|
.flatMap((list) => list.itemSpans)
|
|
39
|
-
.filter((span) => !isQuotedAt(doc.source, span.start) &&
|
|
52
|
+
.filter((span) => !isQuotedAt(doc.source, span.start) && opens(doc.source.slice(span.start, span.end)) !== undefined)
|
|
40
53
|
.toSorted((left, right) => left.start - right.start);
|
|
41
54
|
|
|
42
|
-
/**
|
|
55
|
+
/** The list items, in document order, that open with a bold label and are not quoted. */
|
|
56
|
+
export const boldLabelItems = (doc: ProseDocument): Span[] => unquotedItems(doc, boldLabelOf);
|
|
57
|
+
|
|
58
|
+
/** A count, not a density: a person writes one or two labels even in a long article. A bold lead sentence counts as a label. */
|
|
43
59
|
export const boldLabelList: Detector = (doc, options): Finding[] => {
|
|
44
|
-
const items =
|
|
60
|
+
const items = unquotedItems(doc, labelOrLeadOf);
|
|
45
61
|
const first = items[0];
|
|
46
62
|
if (first === undefined || items.length < options.limit) return [];
|
|
47
63
|
const written = doc.source.slice(first.start, first.end);
|
|
@@ -52,7 +68,7 @@ export const boldLabelList: Detector = (doc, options): Finding[] => {
|
|
|
52
68
|
line: 0,
|
|
53
69
|
column: 0,
|
|
54
70
|
quote: (written.split("\n")[0] ?? "").trim(),
|
|
55
|
-
values: { label:
|
|
71
|
+
values: { label: labelOrLeadOf(written) ?? "", count: items.length, limit: options.limit, offset: first.start },
|
|
56
72
|
},
|
|
57
73
|
];
|
|
58
74
|
};
|
|
@@ -73,27 +73,52 @@ const formsOf = (term: string): string[] => {
|
|
|
73
73
|
return lasts.map((form) => (head === "" ? form : `${head} ${form}`));
|
|
74
74
|
};
|
|
75
75
|
|
|
76
|
+
/** Whether two tokens are next to each other in a run: at most one character apart, or only spaces apart on one line (Normal Business Hours). */
|
|
77
|
+
type Near = (left: Token, right: Token) => boolean;
|
|
78
|
+
|
|
79
|
+
const nearIn =
|
|
80
|
+
(source: string): Near =>
|
|
81
|
+
(left, right) =>
|
|
82
|
+
adjacent(left, right) || /^[ \t]+$/u.test(source.slice(left.span.end, right.span.start));
|
|
83
|
+
|
|
76
84
|
/** How many tokens the capitalised run starting at index takes: words that continue it, and a joiner or hyphen between two. */
|
|
77
|
-
const runLength = (tokens: readonly Token[], index: number, joiners: ReadonlySet<string
|
|
85
|
+
const runLength = (tokens: readonly Token[], index: number, joiners: ReadonlySet<string>, near: Near): number => {
|
|
78
86
|
const word = tokens[index];
|
|
79
87
|
const next = tokens[index + 1];
|
|
80
88
|
const after = tokens[index + 2];
|
|
81
|
-
if (next === undefined || word === undefined || !
|
|
82
|
-
if (continues(next)) return 1 + runLength(tokens, index + 1, joiners);
|
|
89
|
+
if (next === undefined || word === undefined || !near(word, next)) return 1;
|
|
90
|
+
if (continues(next)) return 1 + runLength(tokens, index + 1, joiners, near);
|
|
83
91
|
const hyphened = next.surface === HYPHEN && touching(word, next) && after !== undefined && touching(next, after);
|
|
84
|
-
const joined = hyphened || (joiners.has(next.surface) && after !== undefined &&
|
|
85
|
-
return joined && continues(after) ? 2 + runLength(tokens, index + 2, joiners) : 1;
|
|
92
|
+
const joined = hyphened || (joiners.has(next.surface) && after !== undefined && near(next, after));
|
|
93
|
+
return joined && continues(after) ? 2 + runLength(tokens, index + 2, joiners, near) : 1;
|
|
86
94
|
};
|
|
87
95
|
|
|
88
96
|
type Run = { readonly start: number; readonly end: number };
|
|
89
97
|
|
|
90
98
|
/** The capitalised runs of one sentence, as [start, end) token indexes; a run never starts inside the one before. */
|
|
91
|
-
const runsOf = (tokens: readonly Token[], joiners: ReadonlySet<string
|
|
99
|
+
const runsOf = (tokens: readonly Token[], joiners: ReadonlySet<string>, near: Near): Run[] =>
|
|
92
100
|
tokens.reduce<Run[]>((runs, token, index) => {
|
|
93
101
|
if ((runs.at(-1)?.end ?? 0) > index || !isCapitalised(token)) return runs;
|
|
94
|
-
return [...runs, { start: index, end: index + runLength(tokens, index, joiners) }];
|
|
102
|
+
return [...runs, { start: index, end: index + runLength(tokens, index, joiners, near) }];
|
|
95
103
|
}, []);
|
|
96
104
|
|
|
105
|
+
/** Marks that glue a word to the next with no space (Borrowmoney.com, Leased/rent): one written form, not a term. */
|
|
106
|
+
const WORD_GLUE = new Set([".", "/"]);
|
|
107
|
+
const LETTER_START = /^\p{L}/u;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* The run is not written as a term: its last word is glued to more letters (a web address, a slash pair), or a small
|
|
111
|
+
* word inside it is capitalised too (Maintenance And Deployment), which is capitalising every word, not naming a term.
|
|
112
|
+
*/
|
|
113
|
+
const notWrittenAsTerm = (tokens: readonly Token[], start: number, end: number, small: ReadonlySet<string>): boolean => {
|
|
114
|
+
const last = tokens[end - 1];
|
|
115
|
+
const glue = tokens[end];
|
|
116
|
+
const more = tokens[end + 1];
|
|
117
|
+
const glued = last !== undefined && glue !== undefined && more !== undefined && WORD_GLUE.has(glue.surface) && touching(last, glue) && touching(glue, more);
|
|
118
|
+
if (glued && LETTER_START.test(more.surface)) return true;
|
|
119
|
+
return tokens.slice(start + 1, end - 1).some((token) => isCapitalised(token) && small.has(token.surface.toLowerCase()));
|
|
120
|
+
};
|
|
121
|
+
|
|
97
122
|
const inside = (offset: number, spans: readonly Span[]): boolean => spans.some((span) => span.start <= offset && offset < span.end);
|
|
98
123
|
|
|
99
124
|
/** Used like a defined term: after a lower-case word, and not a modifier of a lower-case noun after it (the Premium plan). */
|
|
@@ -273,18 +298,20 @@ export const capitalisedTerms = (input: CapitalisedInput): CapitalisedTerm[] =>
|
|
|
273
298
|
const defining = input.defined.filter((definition) => definition.bare !== true && DEFINED_FORM.test(definition.term));
|
|
274
299
|
if (defining.length < MIN_DEFINED) return [];
|
|
275
300
|
const joiners = new Set(input.words.joiners);
|
|
301
|
+
const small = new Set([...input.words.joiners, ...input.words.articles].map((word) => word.toLowerCase()));
|
|
302
|
+
const near = nearIn(input.source);
|
|
276
303
|
const isName = nameCues(input, joiners);
|
|
277
304
|
const divisions = new Set(input.words.divisions.flatMap(formsOf));
|
|
278
305
|
const known = new Set([...input.defined.map((definition) => definition.term), ...input.named, ...input.words.names].flatMap(formsOf));
|
|
279
306
|
const isKnown = (term: string): boolean => partsOf(term).some((part) => formsOf(part).some((form) => known.has(form)));
|
|
280
307
|
const uses = input.sentences.flatMap((tokens) =>
|
|
281
|
-
runsOf(tokens, joiners).flatMap(({ start, end }): CapitalisedTerm[] => {
|
|
308
|
+
runsOf(tokens, joiners, near).flatMap(({ start, end }): CapitalisedTerm[] => {
|
|
282
309
|
const first = tokens[start];
|
|
283
310
|
const last = tokens[end - 1];
|
|
284
311
|
if (first === undefined || last === undefined || !placedLikeATerm(tokens[start - 1], tokens[end])) return [];
|
|
285
312
|
if (inside(first.span.start, input.marked) || formsOf(first.surface).some((form) => divisions.has(form))) return [];
|
|
286
313
|
const term = input.source.slice(first.span.start, last.span.end);
|
|
287
|
-
if (isKnown(term) || isName(tokens, start, end, term)) return [];
|
|
314
|
+
if (isKnown(term) || isName(tokens, start, end, term) || notWrittenAsTerm(tokens, start, end, small)) return [];
|
|
288
315
|
return writtenInLowerCase(input.readable, term) ? [] : [{ offset: first.span.start, term }];
|
|
289
316
|
}),
|
|
290
317
|
);
|
|
@@ -34,3 +34,60 @@ export const isCitationString = (text: string, signals: readonly string[]): bool
|
|
|
34
34
|
const cited = afterOpeningSignal(text, signals);
|
|
35
35
|
return cited !== undefined && ((SECTION_SIGN.test(cited) && CITATION_DETAIL.test(cited)) || isTitleSeries(cited));
|
|
36
36
|
};
|
|
37
|
+
|
|
38
|
+
/** A quoted title (“Frequently Asked Questions …,”), cited as it is published. */
|
|
39
|
+
const QUOTED_TITLE = /“[^“”]*”|"[^"]*"/gu;
|
|
40
|
+
const PARENTHESIS = /\(([^()]*)\)/gu;
|
|
41
|
+
const PARENTHESIS_WORD_BREAK = /[\s,]+/u;
|
|
42
|
+
const STARTS_CAPITAL_OR_DIGIT = /^[\p{Lu}\p{N}]/u;
|
|
43
|
+
/** A capitalised word or a number, as the words of a cited name are written (Act, 2001, U.S., Questions:). */
|
|
44
|
+
const NAME_WORD = /^(?:\p{Lu}[\p{L}\p{N}.'’&-]*:?|\p{N}[\p{N}.-]*)$/u;
|
|
45
|
+
/** Short words a name keeps in lower case between its capitalised words (Act of 2001, Tools Required to Intercept). */
|
|
46
|
+
const NAME_JOINERS: ReadonlySet<string> = new Set(["of", "and", "the", "for", "to", "by", "in", "on", "at", "with", "or", "from", "a", "an"]);
|
|
47
|
+
const CAPITALISED = /^\p{Lu}/u;
|
|
48
|
+
/** Fewer capitalised words than this are the writer's own (the CIP Rule), not the name of a cited document or body. */
|
|
49
|
+
const MIN_NAME_WORDS = 3;
|
|
50
|
+
|
|
51
|
+
/** A parenthesis of names, codes and dates only ((FEMA), (DR 4339), (January 18, 2023)): every word in it starts with a capital or a digit. */
|
|
52
|
+
const isNameParenthesis = (inside: string): boolean => {
|
|
53
|
+
const words = inside.split(PARENTHESIS_WORD_BREAK).filter((word) => word !== "");
|
|
54
|
+
return words.length > 0 && words.every((word) => STARTS_CAPITAL_OR_DIGIT.test(word));
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/** The words in runs: a run is name words, with joiners only between two of them. */
|
|
58
|
+
type Runs = { readonly runs: string[][]; readonly joiners: string[] };
|
|
59
|
+
|
|
60
|
+
const addWord = (state: Runs, word: string): Runs => {
|
|
61
|
+
const current = state.runs.at(-1);
|
|
62
|
+
if (NAME_WORD.test(word)) {
|
|
63
|
+
if (current !== undefined && current.length > 0 && NAME_WORD.test(current.at(-1) ?? "")) current.push(...state.joiners, word);
|
|
64
|
+
else state.runs.push(...(state.joiners.length > 0 ? [state.joiners.slice()] : []), [word]);
|
|
65
|
+
return { runs: state.runs, joiners: [] };
|
|
66
|
+
}
|
|
67
|
+
if (NAME_JOINERS.has(word) && current !== undefined && NAME_WORD.test(current.at(-1) ?? "")) return { runs: state.runs, joiners: [...state.joiners, word] };
|
|
68
|
+
state.runs.push([...state.joiners, word]);
|
|
69
|
+
return { runs: state.runs, joiners: [] };
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const isCitedName = (run: readonly string[]): boolean => NAME_WORD.test(run[0] ?? "") && run.filter((word) => CAPITALISED.test(word)).length >= MIN_NAME_WORDS;
|
|
73
|
+
|
|
74
|
+
/** The words left once every run of three capitalised words or more (Act of 2001, Hurricane Maria) is taken out. */
|
|
75
|
+
const withoutNames = (cited: string): string => {
|
|
76
|
+
const { runs, joiners } = cited
|
|
77
|
+
.split(/\s+/u)
|
|
78
|
+
.filter((word) => word !== "")
|
|
79
|
+
.reduce<Runs>(addWord, { runs: [], joiners: [] });
|
|
80
|
+
return [...runs.filter((run) => !isCitedName(run)).flat(), ...joiners].join(" ");
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The writer's words in a footnote that opens with a citation signal: the signal and what is left once the cited names
|
|
85
|
+
* (Uniting and Strengthening America … Act of 2001), quoted titles and parentheses of codes and dates are taken out
|
|
86
|
+
* (See also SR letter 05-9, “…,” for interagency guidance on …). Undefined when the text does not open with a signal.
|
|
87
|
+
*/
|
|
88
|
+
export const wordsAroundCitation = (text: string, signals: readonly string[]): string | undefined => {
|
|
89
|
+
const cited = afterOpeningSignal(text, signals);
|
|
90
|
+
if (cited === undefined) return undefined;
|
|
91
|
+
const signal = text.trim().slice(0, text.trim().length - cited.length);
|
|
92
|
+
return `${signal} ${withoutNames(cited.replace(QUOTED_TITLE, " ").replace(PARENTHESIS, (whole, inside: string) => (isNameParenthesis(inside) ? " " : whole)))}`;
|
|
93
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Span } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
// A word emphasised alone is stressed: "does **not**" keeps the full form because a contraction cannot carry the stress,
|
|
4
|
+
// so it does not say which form the writer's prose uses. A longer emphasised stretch (a bold label, an italic note) is
|
|
5
|
+
// still the writer's prose and is counted.
|
|
6
|
+
|
|
7
|
+
/** A run of emphasis marks in the source that the prose masks out (chrome), with its place. */
|
|
8
|
+
type Marks = { readonly start: number; readonly end: number };
|
|
9
|
+
|
|
10
|
+
const MARK_RUN = /[*_]+/gu;
|
|
11
|
+
const BLANK_LINE = /\n[ \t]*\n/u;
|
|
12
|
+
const ONE_WORD = /^[\p{L}'’]+$/u;
|
|
13
|
+
|
|
14
|
+
const isMasked = (prose: string, start: number, end: number): boolean => prose.slice(start, end).trim() === "";
|
|
15
|
+
|
|
16
|
+
const isText = (char: string | undefined): boolean => char !== undefined && char.trim() !== "";
|
|
17
|
+
|
|
18
|
+
/** Emphasis marks the parser took as markup: masked in the prose and touching unmasked text on one side. */
|
|
19
|
+
const markRuns = (source: string, prose: string): (Marks & { readonly opens: boolean; readonly closes: boolean })[] =>
|
|
20
|
+
[...source.matchAll(MARK_RUN)].flatMap((match) => {
|
|
21
|
+
const start = match.index;
|
|
22
|
+
const end = start + match[0].length;
|
|
23
|
+
if (!isMasked(prose, start, end)) return [];
|
|
24
|
+
const opens = isText(prose[end]);
|
|
25
|
+
const closes = isText(prose[start - 1]);
|
|
26
|
+
return opens || closes ? [{ start, end, opens, closes }] : [];
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The words emphasised alone in the document, without their marks, in source coordinates. Emphasis does not cross a
|
|
31
|
+
* blank line, so an opening mark left without its closing one in its paragraph covers nothing.
|
|
32
|
+
*/
|
|
33
|
+
type Pairing = { readonly open: Marks | undefined; readonly words: readonly Span[] };
|
|
34
|
+
|
|
35
|
+
const pairRun =
|
|
36
|
+
(source: string) =>
|
|
37
|
+
(state: Pairing, run: Marks & { readonly opens: boolean; readonly closes: boolean }): Pairing => {
|
|
38
|
+
const { open } = state;
|
|
39
|
+
if (open === undefined || !run.closes || BLANK_LINE.test(source.slice(open.end, run.start)))
|
|
40
|
+
return { open: run.opens ? run : undefined, words: state.words };
|
|
41
|
+
const inside = { start: open.end, end: run.start };
|
|
42
|
+
return { open: undefined, words: ONE_WORD.test(source.slice(inside.start, inside.end)) ? [...state.words, inside] : state.words };
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const stressedWords = (source: string, prose: string): readonly Span[] =>
|
|
46
|
+
markRuns(source, prose).reduce<Pairing>(pairRun(source), { open: undefined, words: [] }).words;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import { quotedSpans } from "../quoted-span.ts";
|
|
2
2
|
|
|
3
|
+
import type { Span } from "../plugin.ts";
|
|
4
|
+
|
|
3
5
|
// A short phrase in quotation marks ("Do Not Disturb", ‘we’re going to x’) is a name or an example: the form is mentioned,
|
|
4
6
|
// not used, so it says nothing about the document's register. Spoken words carry punctuation ("We don't know," she
|
|
5
|
-
// said) and stay
|
|
7
|
+
// said) and stay, unless they are quoted inside another quotation (a platform's label in a quoted sentence), which is
|
|
8
|
+
// someone else's wording twice over. The text must already have straight apostrophes, so a ’ left in it closes a quotation.
|
|
6
9
|
|
|
7
10
|
const MENTION_MARKS: Readonly<Record<string, string>> = { "“": "”", '"': '"', "‘": "’" };
|
|
8
11
|
/** More words than this in quotation marks is a quoted sentence, not a phrase named as an example. */
|
|
@@ -14,11 +17,54 @@ const isMention = (quoted: string): boolean => {
|
|
|
14
17
|
return PHRASE.test(phrase) && phrase.split(/\s+/u).length <= MAX_MENTION_WORDS;
|
|
15
18
|
};
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
const isNested = (span: Span, spans: readonly Span[]): boolean => spans.some((outer) => outer !== span && outer.start < span.start && span.end < outer.end);
|
|
21
|
+
|
|
22
|
+
/** text with the characters in each span replaced by spaces, every other character at its place. */
|
|
23
|
+
const blankSpans = (text: string, spans: readonly Span[]): string => {
|
|
24
|
+
if (spans.length === 0) return text;
|
|
21
25
|
const units = text.split("");
|
|
22
|
-
|
|
26
|
+
spans.forEach((span) => units.fill(" ", Math.max(0, span.start), Math.min(text.length, span.end)));
|
|
23
27
|
return units.join("");
|
|
24
28
|
};
|
|
29
|
+
|
|
30
|
+
/** The text with each quoted short phrase, and each quotation inside another, blanked out. */
|
|
31
|
+
export const withoutMentions = (text: string): string => {
|
|
32
|
+
const quoted = quotedSpans(text, MENTION_MARKS);
|
|
33
|
+
return blankSpans(
|
|
34
|
+
text,
|
|
35
|
+
quoted.filter((span) => isMention(text.slice(span.start, span.end)) || isNested(span, quoted)),
|
|
36
|
+
);
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The text (which starts at offset in the document) with the document ranges that fall in it blanked out.
|
|
41
|
+
* Ranges are in document coordinates; those outside the text change nothing.
|
|
42
|
+
*/
|
|
43
|
+
export const blankRanges = (text: string, offset: number, ranges: readonly Span[]): string =>
|
|
44
|
+
blankSpans(
|
|
45
|
+
text,
|
|
46
|
+
ranges
|
|
47
|
+
.filter((range) => range.end > offset && range.start < offset + text.length)
|
|
48
|
+
.map((range) => ({ start: range.start - offset, end: range.end - offset })),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
/** A list of search terms or keywords runs to at least this many comma-separated items. */
|
|
52
|
+
const MIN_LIST_TERMS = 8;
|
|
53
|
+
/** A term is a few words; a longer item is a clause of prose. */
|
|
54
|
+
const MAX_TERM_WORDS = 4;
|
|
55
|
+
/** Share of the items that must be terms: a list's label ("(i) Functional errors (k1=50): error") may run longer. */
|
|
56
|
+
const MIN_TERM_SHARE = 0.9;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A sentence that is a list of terms (keywords, search strings: "won't load, wont load, can't login, …") names its
|
|
60
|
+
* contractions as items; it does not write in a register.
|
|
61
|
+
*/
|
|
62
|
+
export const isTermList = (text: string): boolean => {
|
|
63
|
+
const items = text
|
|
64
|
+
.split(/[,;]/u)
|
|
65
|
+
.map((item) => item.trim())
|
|
66
|
+
.filter((item) => item !== "");
|
|
67
|
+
if (items.length < MIN_LIST_TERMS) return false;
|
|
68
|
+
const terms = items.filter((item) => item.split(/\s+/u).length <= MAX_TERM_WORDS);
|
|
69
|
+
return terms.length >= items.length * MIN_TERM_SHARE;
|
|
70
|
+
};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { pairMinority, type PairUses } from "./contraction-minority.ts";
|
|
2
|
+
import { blankRanges, isTermList, withoutMentions } from "./contraction-mention.ts";
|
|
3
|
+
import { stressedWords } from "./contraction-emphasis.ts";
|
|
4
|
+
import { escapeRegExp, straightApostrophes } from "../orthography.ts";
|
|
5
|
+
import type { Detector, Finding, ProseDocument, Sentence, Span } from "../plugin.ts";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 同じことを言う 2 つの書きかたが、1 つの文書で混ざっているか。
|
|
9
|
+
*
|
|
10
|
+
* **どちらが正しいかは決めない。** 短縮形を使うかどうかは文体の選択で、
|
|
11
|
+
* 立場を取ると方針の違う書き手に rule ごと無視される。spec §12.3。
|
|
12
|
+
*
|
|
13
|
+
* 語彙表は対を持つ(`pattern` と `instead_of`)。比べるのは対の中だけで、別の語どうしは比べない。
|
|
14
|
+
* GOV.UK の文体は「you're」を使い否定は「do not」と書く。語ごとに決めた書きかたで、混在ではない。
|
|
15
|
+
* 否定と代名詞+動詞の族にもまとめない。「don't」と「does not」を書き分ける文書もある。
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
type Pair = { readonly short: string; readonly long: string };
|
|
19
|
+
type MixedPair = { readonly pair: Pair; readonly side: keyof PairUses; readonly minority: string; readonly majority: string; readonly count: number };
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 語の境界で照合する。部分一致だと「it isn't」が「it is」を含んでしまい、
|
|
23
|
+
* 短縮形を使っている文が「使っていない」側に数えられる。
|
|
24
|
+
*/
|
|
25
|
+
const wordUses = (text: string, word: string): number => [...text.matchAll(new RegExp(`\\b${escapeRegExp(word)}\\b`, "gu"))].length;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 書き手が選んだ形として数える文の字。引用で挙げた語句と語の一覧の中の形は、書き手の文体を語らないので空ける。
|
|
29
|
+
* アポストロフィの形(don’t と don't)は短縮形かどうかを変えない。折り返した行も、空白 1 つと同じに読む。
|
|
30
|
+
*/
|
|
31
|
+
const comparableText = (text: string): string => {
|
|
32
|
+
const counted = withoutMentions(straightApostrophes(text));
|
|
33
|
+
return isTermList(counted) ? "" : counted.replace(/\s+/gu, " ").toLowerCase();
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 指してよい字。一語だけ強調した形(does **not**)は強勢のために選んだ形なので指さない。
|
|
38
|
+
* 書き手がその形を書いたことには変わりないので、どちらが多いかを数えるときは数える。
|
|
39
|
+
*/
|
|
40
|
+
const pointableText = (sentence: Sentence, stressed: readonly Span[]): string => comparableText(blankRanges(sentence.text, sentence.span.start, stressed));
|
|
41
|
+
|
|
42
|
+
/** 語のすぐ後ろが句読点か文の終わり。名前として挙げた語("Use cannot, instead of can't.")はそう並ぶ。 */
|
|
43
|
+
const bareUses = (text: string, word: string): number => [...text.matchAll(new RegExp(`\\b${escapeRegExp(word)}\\b(?=\\s*(?:\\p{P}|$))`, "gu"))].length;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 1 文の中の、対の 2 つの形の数。両方の形をそれぞれ単独で挙げた文("Use cannot, instead of can't.")は語を比べていて、
|
|
47
|
+
* どちらも使っていない。両方を文の中で使う文("It is important -- it's the most important")はそのまま数える。
|
|
48
|
+
*/
|
|
49
|
+
const usesIn = (text: string, pair: Pair): PairUses => {
|
|
50
|
+
const named = bareUses(text, pair.short) > 0 && bareUses(text, pair.long) > 0;
|
|
51
|
+
return named ? { short: 0, long: 0 } : { short: wordUses(text, pair.short), long: wordUses(text, pair.long) };
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const mixedPair = (texts: readonly string[], pair: Pair): MixedPair | undefined => {
|
|
55
|
+
const uses = texts.reduce<PairUses>(
|
|
56
|
+
(sum, text) => {
|
|
57
|
+
const here = usesIn(text, pair);
|
|
58
|
+
return { short: sum.short + here.short, long: sum.long + here.long };
|
|
59
|
+
},
|
|
60
|
+
{ short: 0, long: 0 },
|
|
61
|
+
);
|
|
62
|
+
const side = pairMinority(uses);
|
|
63
|
+
if (side === undefined) return undefined;
|
|
64
|
+
return side === "short"
|
|
65
|
+
? { pair, side, minority: pair.short, majority: pair.long, count: uses.short }
|
|
66
|
+
: { pair, side, minority: pair.long, majority: pair.short, count: uses.long };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const usesMinority = (text: string, mixed: MixedPair): boolean => usesIn(text, mixed.pair)[mixed.side] > 0;
|
|
70
|
+
|
|
71
|
+
const emphasisOf = (doc: ProseDocument): readonly Span[] => (doc.prose === undefined ? [] : stressedWords(doc.source, doc.prose));
|
|
72
|
+
|
|
73
|
+
export const contractionMix: Detector = (doc, options): Finding[] => {
|
|
74
|
+
const stressed = emphasisOf(doc);
|
|
75
|
+
// 文ごとに数える。文をつないで数えると、箇条書きの「What we do」と次の項目「not every…」が「do not」と数えられる。
|
|
76
|
+
const texts = doc.sentences.map((sentence) => comparableText(sentence.text));
|
|
77
|
+
const mixed = (options.lexicon ?? []).flatMap((entry) => {
|
|
78
|
+
const pair = entry.instead_of === undefined ? undefined : mixedPair(texts, { short: entry.pattern.toLowerCase(), long: entry.instead_of.toLowerCase() });
|
|
79
|
+
return pair === undefined || pair.count > options.limit ? [] : [pair];
|
|
80
|
+
});
|
|
81
|
+
const hits = doc.sentences.flatMap((sentence) => {
|
|
82
|
+
const pointable = pointableText(sentence, stressed);
|
|
83
|
+
const pair = mixed.find((candidate) => usesMinority(pointable, candidate));
|
|
84
|
+
return pair === undefined ? [] : [{ sentence, pair }];
|
|
85
|
+
});
|
|
86
|
+
return hits.map(({ sentence, pair }) => ({
|
|
87
|
+
rule: "contraction-consistency",
|
|
88
|
+
severity: "info",
|
|
89
|
+
line: 0,
|
|
90
|
+
column: 0,
|
|
91
|
+
quote: sentence.text.trim(),
|
|
92
|
+
values: { matched: pair.minority, majority: pair.majority, count: pair.count, limit: options.limit, offset: sentence.span.start },
|
|
93
|
+
}));
|
|
94
|
+
};
|