chaffjs 0.13.0 → 0.15.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/address-chain.d.ts +4 -1
- package/dist/address-chain.d.ts.map +1 -1
- package/dist/address-chain.js +5 -2
- package/dist/address-chain.js.map +1 -1
- package/dist/cli-text.d.ts +4 -1
- package/dist/cli-text.d.ts.map +1 -1
- package/dist/cli-text.js +14 -2
- package/dist/cli-text.js.map +1 -1
- package/dist/cli.d.ts.map +1 -1
- package/dist/cli.js +21 -21
- package/dist/cli.js.map +1 -1
- package/dist/commands/eval.d.ts +4 -0
- package/dist/commands/eval.d.ts.map +1 -1
- package/dist/commands/eval.js +20 -6
- package/dist/commands/eval.js.map +1 -1
- package/dist/commands/feedback.d.ts.map +1 -1
- package/dist/commands/feedback.js +2 -1
- package/dist/commands/feedback.js.map +1 -1
- package/dist/commands/test.d.ts.map +1 -1
- package/dist/commands/test.js +3 -2
- package/dist/commands/test.js.map +1 -1
- package/dist/commands/tree.d.ts.map +1 -1
- package/dist/commands/tree.js +2 -1
- package/dist/commands/tree.js.map +1 -1
- package/dist/detectors/acronym-compound.d.ts +8 -0
- package/dist/detectors/acronym-compound.d.ts.map +1 -0
- package/dist/detectors/acronym-compound.js +8 -0
- package/dist/detectors/acronym-compound.js.map +1 -0
- package/dist/detectors/acronym-context.d.ts +6 -0
- package/dist/detectors/acronym-context.d.ts.map +1 -1
- package/dist/detectors/acronym-context.js +24 -1
- package/dist/detectors/acronym-context.js.map +1 -1
- package/dist/detectors/acronym-expansion.d.ts +10 -0
- package/dist/detectors/acronym-expansion.d.ts.map +1 -1
- package/dist/detectors/acronym-expansion.js +63 -5
- package/dist/detectors/acronym-expansion.js.map +1 -1
- package/dist/detectors/char-shape.d.ts.map +1 -1
- package/dist/detectors/char-shape.js +38 -16
- package/dist/detectors/char-shape.js.map +1 -1
- package/dist/detectors/citation-key.d.ts +13 -0
- package/dist/detectors/citation-key.d.ts.map +1 -0
- package/dist/detectors/citation-key.js +9 -0
- package/dist/detectors/citation-key.js.map +1 -0
- package/dist/detectors/cited-name.d.ts +11 -0
- package/dist/detectors/cited-name.d.ts.map +1 -0
- package/dist/detectors/cited-name.js +15 -0
- package/dist/detectors/cited-name.js.map +1 -0
- package/dist/detectors/concrete-evidence.d.ts +7 -0
- package/dist/detectors/concrete-evidence.d.ts.map +1 -0
- package/dist/detectors/concrete-evidence.js +21 -0
- package/dist/detectors/concrete-evidence.js.map +1 -0
- package/dist/detectors/conjugated-form.d.ts +3 -0
- package/dist/detectors/conjugated-form.d.ts.map +1 -0
- package/dist/detectors/conjugated-form.js +29 -0
- package/dist/detectors/conjugated-form.js.map +1 -0
- package/dist/detectors/counter-edge.d.ts +8 -0
- package/dist/detectors/counter-edge.d.ts.map +1 -0
- package/dist/detectors/counter-edge.js +27 -0
- package/dist/detectors/counter-edge.js.map +1 -0
- package/dist/detectors/dotted-name.d.ts +13 -0
- package/dist/detectors/dotted-name.d.ts.map +1 -0
- package/dist/detectors/dotted-name.js +10 -0
- package/dist/detectors/dotted-name.js.map +1 -0
- package/dist/detectors/doubled-word.d.ts +6 -1
- package/dist/detectors/doubled-word.d.ts.map +1 -1
- package/dist/detectors/doubled-word.js +22 -2
- package/dist/detectors/doubled-word.js.map +1 -1
- package/dist/detectors/en-shape.d.ts +1 -1
- package/dist/detectors/en-shape.d.ts.map +1 -1
- package/dist/detectors/en-shape.js +11 -22
- package/dist/detectors/en-shape.js.map +1 -1
- package/dist/detectors/enumerated-runs.d.ts +22 -0
- package/dist/detectors/enumerated-runs.d.ts.map +1 -0
- package/dist/detectors/enumerated-runs.js +53 -0
- package/dist/detectors/enumerated-runs.js.map +1 -0
- package/dist/detectors/gram-place.d.ts +0 -4
- package/dist/detectors/gram-place.d.ts.map +1 -1
- package/dist/detectors/gram-place.js +6 -1
- package/dist/detectors/gram-place.js.map +1 -1
- package/dist/detectors/gram-predicate.d.ts +7 -0
- package/dist/detectors/gram-predicate.d.ts.map +1 -0
- package/dist/detectors/gram-predicate.js +44 -0
- package/dist/detectors/gram-predicate.js.map +1 -0
- package/dist/detectors/heading-case.d.ts +21 -0
- package/dist/detectors/heading-case.d.ts.map +1 -0
- package/dist/detectors/heading-case.js +38 -0
- package/dist/detectors/heading-case.js.map +1 -0
- package/dist/detectors/heading-echo.d.ts.map +1 -1
- package/dist/detectors/heading-echo.js +4 -1
- package/dist/detectors/heading-echo.js.map +1 -1
- package/dist/detectors/lexicon.d.ts.map +1 -1
- package/dist/detectors/lexicon.js +7 -4
- package/dist/detectors/lexicon.js.map +1 -1
- package/dist/detectors/middle-dot.d.ts +6 -0
- package/dist/detectors/middle-dot.d.ts.map +1 -0
- package/dist/detectors/middle-dot.js +9 -0
- package/dist/detectors/middle-dot.js.map +1 -0
- package/dist/detectors/name-numeral.d.ts +16 -0
- package/dist/detectors/name-numeral.d.ts.map +1 -0
- package/dist/detectors/name-numeral.js +30 -0
- package/dist/detectors/name-numeral.js.map +1 -0
- package/dist/detectors/name-part.d.ts +8 -0
- package/dist/detectors/name-part.d.ts.map +1 -0
- package/dist/detectors/name-part.js +20 -0
- package/dist/detectors/name-part.js.map +1 -0
- package/dist/detectors/name-run.d.ts +8 -0
- package/dist/detectors/name-run.d.ts.map +1 -0
- package/dist/detectors/name-run.js +13 -0
- package/dist/detectors/name-run.js.map +1 -0
- package/dist/detectors/orthography.d.ts.map +1 -1
- package/dist/detectors/orthography.js +18 -6
- package/dist/detectors/orthography.js.map +1 -1
- package/dist/detectors/placeholder.d.ts +14 -0
- package/dist/detectors/placeholder.d.ts.map +1 -0
- package/dist/detectors/placeholder.js +24 -0
- package/dist/detectors/placeholder.js.map +1 -0
- package/dist/detectors/polite-word.d.ts +4 -0
- package/dist/detectors/polite-word.d.ts.map +1 -0
- package/dist/detectors/polite-word.js +10 -0
- package/dist/detectors/polite-word.js.map +1 -0
- package/dist/detectors/quoted-variant.d.ts +7 -0
- package/dist/detectors/quoted-variant.d.ts.map +1 -0
- package/dist/detectors/quoted-variant.js +18 -0
- package/dist/detectors/quoted-variant.js.map +1 -0
- package/dist/detectors/register.d.ts +7 -5
- package/dist/detectors/register.d.ts.map +1 -1
- package/dist/detectors/register.js +6 -4
- package/dist/detectors/register.js.map +1 -1
- package/dist/detectors/roman-numeral.d.ts +6 -0
- package/dist/detectors/roman-numeral.d.ts.map +1 -0
- package/dist/detectors/roman-numeral.js +6 -0
- package/dist/detectors/roman-numeral.js.map +1 -0
- package/dist/detectors/sentence-ending.d.ts.map +1 -1
- package/dist/detectors/sentence-ending.js +14 -4
- package/dist/detectors/sentence-ending.js.map +1 -1
- package/dist/detectors/series-label.d.ts +13 -0
- package/dist/detectors/series-label.d.ts.map +1 -0
- package/dist/detectors/series-label.js +26 -0
- package/dist/detectors/series-label.js.map +1 -0
- package/dist/detectors/signals.d.ts.map +1 -1
- package/dist/detectors/signals.js +37 -25
- package/dist/detectors/signals.js.map +1 -1
- package/dist/detectors/structure-tree.js +1 -1
- package/dist/detectors/structure-tree.js.map +1 -1
- package/dist/detectors/structure.d.ts.map +1 -1
- package/dist/detectors/structure.js +2 -1
- package/dist/detectors/structure.js.map +1 -1
- package/dist/detectors/superlative-name.d.ts +9 -0
- package/dist/detectors/superlative-name.d.ts.map +1 -0
- package/dist/detectors/superlative-name.js +24 -0
- package/dist/detectors/superlative-name.js.map +1 -0
- package/dist/detectors/token-shape.d.ts.map +1 -1
- package/dist/detectors/token-shape.js +10 -2
- package/dist/detectors/token-shape.js.map +1 -1
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +10 -4
- package/dist/document.js.map +1 -1
- package/dist/genre-check.d.ts +34 -0
- package/dist/genre-check.d.ts.map +1 -0
- package/dist/genre-check.js +21 -0
- package/dist/genre-check.js.map +1 -0
- package/dist/genre.d.ts +8 -1
- package/dist/genre.d.ts.map +1 -1
- package/dist/genre.js +29 -5
- package/dist/genre.js.map +1 -1
- package/dist/init.d.ts +0 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +0 -12
- package/dist/init.js.map +1 -1
- package/dist/joined-view.d.ts +25 -0
- package/dist/joined-view.d.ts.map +1 -0
- package/dist/joined-view.js +76 -0
- package/dist/joined-view.js.map +1 -0
- package/dist/line-continues.d.ts +6 -0
- package/dist/line-continues.d.ts.map +1 -0
- package/dist/line-continues.js +33 -0
- package/dist/line-continues.js.map +1 -0
- package/dist/measure.d.ts +1 -0
- package/dist/measure.d.ts.map +1 -1
- package/dist/measure.js +5 -1
- package/dist/measure.js.map +1 -1
- package/dist/number-name.d.ts +7 -3
- package/dist/number-name.d.ts.map +1 -1
- package/dist/number-name.js +51 -15
- package/dist/number-name.js.map +1 -1
- package/dist/orthography.d.ts +4 -0
- package/dist/orthography.d.ts.map +1 -1
- package/dist/orthography.js +11 -9
- package/dist/orthography.js.map +1 -1
- package/dist/plain-source.d.ts +6 -0
- package/dist/plain-source.d.ts.map +1 -0
- package/dist/plain-source.js +6 -0
- package/dist/plain-source.js.map +1 -0
- package/dist/plugin.d.ts +18 -1
- package/dist/plugin.d.ts.map +1 -1
- package/dist/preamble-paragraphs.d.ts +7 -0
- package/dist/preamble-paragraphs.d.ts.map +1 -0
- package/dist/preamble-paragraphs.js +7 -0
- package/dist/preamble-paragraphs.js.map +1 -0
- package/dist/quoted-span.d.ts +10 -0
- package/dist/quoted-span.d.ts.map +1 -0
- package/dist/quoted-span.js +20 -0
- package/dist/quoted-span.js.map +1 -0
- package/dist/render/eval.d.ts +2 -1
- package/dist/render/eval.d.ts.map +1 -1
- package/dist/render/eval.js +84 -43
- package/dist/render/eval.js.map +1 -1
- package/dist/resolve-genre.d.ts +11 -0
- package/dist/resolve-genre.d.ts.map +1 -0
- package/dist/resolve-genre.js +18 -0
- package/dist/resolve-genre.js.map +1 -0
- package/dist/soft-break.d.ts +19 -0
- package/dist/soft-break.d.ts.map +1 -0
- package/dist/soft-break.js +78 -0
- package/dist/soft-break.js.map +1 -0
- package/dist/structure/build.d.ts.map +1 -1
- package/dist/structure/build.js +23 -10
- package/dist/structure/build.js.map +1 -1
- package/dist/structure/date-order.d.ts +3 -0
- package/dist/structure/date-order.d.ts.map +1 -1
- package/dist/structure/date-order.js +9 -1
- package/dist/structure/date-order.js.map +1 -1
- package/dist/structure/heading-leaves.d.ts +7 -0
- package/dist/structure/heading-leaves.d.ts.map +1 -0
- package/dist/structure/heading-leaves.js +6 -0
- package/dist/structure/heading-leaves.js.map +1 -0
- package/dist/structure/issues.d.ts +1 -1
- package/dist/structure/issues.d.ts.map +1 -1
- package/dist/structure/issues.js +14 -3
- package/dist/structure/issues.js.map +1 -1
- package/dist/structure/listed-tags.d.ts +3 -0
- package/dist/structure/listed-tags.d.ts.map +1 -0
- package/dist/structure/listed-tags.js +6 -0
- package/dist/structure/listed-tags.js.map +1 -0
- package/dist/structure/number-in-sentence.d.ts +15 -0
- package/dist/structure/number-in-sentence.d.ts.map +1 -0
- package/dist/structure/number-in-sentence.js +13 -0
- package/dist/structure/number-in-sentence.js.map +1 -0
- package/dist/structure/unit-word.d.ts +8 -0
- package/dist/structure/unit-word.d.ts.map +1 -0
- package/dist/structure/unit-word.js +10 -0
- package/dist/structure/unit-word.js.map +1 -0
- package/dist/structure/wrapped-tail.d.ts +18 -0
- package/dist/structure/wrapped-tail.d.ts.map +1 -0
- package/dist/structure/wrapped-tail.js +11 -0
- package/dist/structure/wrapped-tail.js.map +1 -0
- package/package.json +3 -3
- package/rules/dangling-reference.yaml +2 -2
- package/rules/date-order.yaml +2 -2
- package/rules/duplicate-definition.yaml +2 -2
- package/rules/latin-spacing.yaml +4 -2
- package/rules/max-kanji-continuous.yaml +1 -1
- package/rules/no-doubled-joshi.yaml +2 -2
- package/rules/no-nakaguro-parallel.yaml +2 -2
- package/rules/title-case-consistency.yaml +2 -2
- package/rules/undefined-acronym.yaml +15 -1
- package/rules/unqualified-superlative.yaml +1 -1
- package/src/address-chain.ts +5 -2
- package/src/cli-text.ts +19 -3
- package/src/cli.ts +22 -19
- package/src/commands/eval.ts +26 -5
- package/src/commands/feedback.ts +2 -1
- package/src/commands/test.ts +3 -2
- package/src/commands/tree.ts +2 -1
- package/src/detectors/acronym-compound.ts +9 -0
- package/src/detectors/acronym-context.ts +32 -1
- package/src/detectors/acronym-expansion.ts +72 -5
- package/src/detectors/char-shape.ts +44 -17
- package/src/detectors/citation-key.ts +13 -0
- package/src/detectors/cited-name.ts +23 -0
- package/src/detectors/concrete-evidence.ts +31 -0
- package/src/detectors/conjugated-form.ts +40 -0
- package/src/detectors/counter-edge.ts +29 -0
- package/src/detectors/dotted-name.ts +14 -0
- package/src/detectors/doubled-word.ts +25 -2
- package/src/detectors/en-shape.ts +17 -30
- package/src/detectors/enumerated-runs.ts +68 -0
- package/src/detectors/gram-place.ts +8 -1
- package/src/detectors/gram-predicate.ts +55 -0
- package/src/detectors/heading-case.ts +41 -0
- package/src/detectors/heading-echo.ts +5 -1
- package/src/detectors/lexicon.ts +8 -5
- package/src/detectors/middle-dot.ts +10 -0
- package/src/detectors/name-numeral.ts +37 -0
- package/src/detectors/name-part.ts +24 -0
- package/src/detectors/name-run.ts +16 -0
- package/src/detectors/orthography.ts +20 -9
- package/src/detectors/placeholder.ts +38 -0
- package/src/detectors/polite-word.ts +14 -0
- package/src/detectors/quoted-variant.ts +20 -0
- package/src/detectors/register.ts +11 -7
- package/src/detectors/roman-numeral.ts +5 -0
- package/src/detectors/sentence-ending.ts +23 -6
- package/src/detectors/series-label.ts +33 -0
- package/src/detectors/signals.ts +44 -31
- package/src/detectors/structure-tree.ts +2 -2
- package/src/detectors/structure.ts +2 -1
- package/src/detectors/superlative-name.ts +30 -0
- package/src/detectors/token-shape.ts +12 -3
- package/src/document.ts +12 -5
- package/src/genre-check.ts +46 -0
- package/src/genre.ts +34 -5
- package/src/init.ts +0 -13
- package/src/joined-view.ts +96 -0
- package/src/line-continues.ts +43 -0
- package/src/measure.ts +9 -1
- package/src/number-name.ts +65 -15
- package/src/orthography.ts +15 -9
- package/src/plain-source.ts +5 -0
- package/src/plugin.ts +23 -2
- package/src/preamble-paragraphs.ts +9 -0
- package/src/quoted-span.ts +25 -0
- package/src/render/eval.ts +101 -38
- package/src/resolve-genre.ts +20 -0
- package/src/soft-break.ts +94 -0
- package/src/structure/build.ts +29 -17
- package/src/structure/date-order.ts +16 -1
- package/src/structure/heading-leaves.ts +7 -0
- package/src/structure/issues.ts +18 -3
- package/src/structure/listed-tags.ts +7 -0
- package/src/structure/number-in-sentence.ts +20 -0
- package/src/structure/unit-word.ts +11 -0
- package/src/structure/wrapped-tail.ts +22 -0
|
@@ -9,7 +9,9 @@ import { escapeRegExp } from "../orthography.ts";
|
|
|
9
9
|
* Continuous Integration (CI) 括弧の中が略語
|
|
10
10
|
* Tax Cuts and Jobs Act [TCJA] 角括弧の中が略語で、直前の語の頭文字と揃う
|
|
11
11
|
* Relief Act (RA; P.L. 112-240) 括弧の最初の項目が略語で、直前の語か区切りの後の語の頭文字と揃う
|
|
12
|
+
* (single nucleotide polymorphism:SNP) 括弧の最後の項目が略語で、コロンの前の語の頭文字と揃う
|
|
12
13
|
* 人事部(以下「HR」という。) 括弧の中が定義の語と略語だけ
|
|
14
|
+
* overnight reverse repurchase agreement (ON RRP) 括弧の中が空白で繋いだ略語だけで、前の名前から文字が順に拾える
|
|
13
15
|
* 括弧と略語の間には、引用符((“MNDA”)、(「MNDA」))と空白だけを許す。
|
|
14
16
|
*/
|
|
15
17
|
const WRAP = String.raw`[\s"“”'‘’「」『』]*`;
|
|
@@ -20,7 +22,9 @@ const SQUARE_CLOSES = new RegExp(String.raw`^${WRAP}\]`, "u");
|
|
|
20
22
|
const SQUARE_OPENED = new RegExp(String.raw`\[${WRAP}$`, "u");
|
|
21
23
|
const ANY_OPENED = new RegExp(String.raw`[(([]${WRAP}$`, "u");
|
|
22
24
|
/** 括弧の最初の項目のあとの区切りと、括弧が閉じるまでの残り((ARRA; P.L. 111-5)、(OP、Originator Profile))。 */
|
|
23
|
-
const SEPARATED = new RegExp(String.raw`^${WRAP}[
|
|
25
|
+
const SEPARATED = new RegExp(String.raw`^${WRAP}[;;,,、::](?<rest>[^()()]*)`, "u");
|
|
26
|
+
/** 括弧の中の名前とコロンのあと、略語の直前まで((single nucleotide polymorphism:SNP))。名前に区切りがあれば列挙なので外す。 */
|
|
27
|
+
const NAMED_BEFORE = new RegExp(String.raw`[((](?<name>[^()();;,,、::]*)[::]${WRAP}$`, "u");
|
|
24
28
|
const QUOTES = /["“”'‘’「」『』]/gu;
|
|
25
29
|
|
|
26
30
|
/** 括弧と略語の間の幅。空白は 1 つに畳んであるので、(“ MNDA ”) まで収まる。 */
|
|
@@ -36,7 +40,7 @@ const MARKER_GAP = String.raw`(?![A-Za-z])[\s、,]*`;
|
|
|
36
40
|
* 角括弧は引用の印にも使う([IANA]、[1])。直前の語のうち大文字で始まる語の頭文字が
|
|
37
41
|
* 略語と揃うときだけ展開と見なす。見る語は略語の文字数の 2 倍まで(and や of を挟むため)。
|
|
38
42
|
*/
|
|
39
|
-
const lettersOf = (acronym: string): string => acronym.replaceAll(
|
|
43
|
+
const lettersOf = (acronym: string): string => acronym.replaceAll(/[&-]/gu, "");
|
|
40
44
|
|
|
41
45
|
const initialsOf = (words: readonly string[]): string =>
|
|
42
46
|
words
|
|
@@ -54,6 +58,17 @@ const spellsOut = (before: string, acronym: string): boolean => {
|
|
|
54
58
|
return initialsOf(words).endsWith(letters);
|
|
55
59
|
};
|
|
56
60
|
|
|
61
|
+
/**
|
|
62
|
+
* 括弧の中で略語と組になる名前の頭文字が、略語とちょうど揃うか。大文字で始まる語だけの頭文字(Data Retention and Reuse Act)か、
|
|
63
|
+
* すべての語の頭文字(single nucleotide polymorphism)で比べる。後者は大文字小文字を問わない。
|
|
64
|
+
*/
|
|
65
|
+
export const namesAcronym = (name: string, acronym: string): boolean => {
|
|
66
|
+
const words = name.replace(QUOTES, " ").trim().split(/\s+/u);
|
|
67
|
+
const letters = lettersOf(acronym);
|
|
68
|
+
const allInitials = words.map((word) => word.charAt(0)).join("");
|
|
69
|
+
return initialsOf(words) === letters || allInitials.toUpperCase() === letters.toUpperCase();
|
|
70
|
+
};
|
|
71
|
+
|
|
57
72
|
/**
|
|
58
73
|
* 括弧の最初の項目が略語で、区切りのあとに注記が続く形((ARRA; P.L. 111-5)、(OP、Originator Profile))。
|
|
59
74
|
* 同じ形で列挙も書く((MR, handbook, etc.)、(EPA, FDIC, GSA))ので、直前の語か、区切りから括弧が閉じるまでの語の
|
|
@@ -66,7 +81,14 @@ const isSeparatedAt = (body: string, acronym: string, at: number): boolean => {
|
|
|
66
81
|
const rest = separated.groups?.["rest"] ?? "";
|
|
67
82
|
const end = at + acronym.length + separated[0].length;
|
|
68
83
|
const closed = CLOSES.test(body.slice(end, end + NEAR));
|
|
69
|
-
return spellsOut(body.slice(0, at), acronym) || (closed &&
|
|
84
|
+
return spellsOut(body.slice(0, at), acronym) || (closed && namesAcronym(rest, acronym));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** 括弧の最後の項目が略語で、コロンの前に名前を書く形((Information-technology Promotion Agency:IPA))。 */
|
|
88
|
+
const isNamedBeforeAt = (body: string, acronym: string, at: number): boolean => {
|
|
89
|
+
const named = NAMED_BEFORE.exec(body.slice(Math.max(0, at - DEFINITION_REACH), at));
|
|
90
|
+
if (named === null || !CLOSES.test(body.slice(at + acronym.length, at + acronym.length + NEAR))) return false;
|
|
91
|
+
return namesAcronym(named.groups?.["name"] ?? "", acronym);
|
|
70
92
|
};
|
|
71
93
|
|
|
72
94
|
/** 言語パッケージの語彙表から読む、括弧の中で略語の前に書く語(以下、hereinafter)と後ろに書く語(という)。 */
|
|
@@ -92,18 +114,63 @@ const isDefinedAt = (patterns: DefinitionPatterns, body: string, acronym: string
|
|
|
92
114
|
return (opened.groups?.["markers"] ?? "") !== "" || (closes.groups?.["verb"] ?? "") !== "";
|
|
93
115
|
};
|
|
94
116
|
|
|
117
|
+
/** 空白 1 つで繋いだ略語の並びのうち、at の略語の前と後ろに続く分((ON RRP) の ON の後ろの " RRP")。 */
|
|
118
|
+
const CAPITALS = "[A-Z][A-Z&-]*[A-Z]";
|
|
119
|
+
const JOINT_BEFORE = new RegExp(String.raw`(?:(?<![A-Za-z0-9_&-])${CAPITALS} )+$`, "u");
|
|
120
|
+
const JOINT_AFTER = new RegExp(String.raw`^(?: ${CAPITALS}(?![A-Za-z0-9_&-]))+`, "u");
|
|
121
|
+
|
|
122
|
+
const LOWER_WORDS = /[a-z]+/gu;
|
|
123
|
+
|
|
124
|
+
const isSubsequence = (letters: string, text: string): boolean =>
|
|
125
|
+
[...text].reduce((matched, char) => (char === letters.charAt(matched) ? matched + 1 : matched), 0) === letters.length;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 略語の文字が、名前のどれかの語の頭から始まって、名前の文字の中に順に拾えるか。1 つの略語を空白で分けて書くときは、
|
|
129
|
+
* 語の頭文字ではなく語の途中の文字も使う(overnight reverse repurchase agreement → ON RRP)。見る語は略語の文字数の 2 倍まで。
|
|
130
|
+
*/
|
|
131
|
+
export const abbreviates = (name: string, acronym: string): boolean => {
|
|
132
|
+
const letters = acronym.toLowerCase().replaceAll(/[^a-z]/gu, "");
|
|
133
|
+
const words = (name.toLowerCase().match(LOWER_WORDS) ?? []).slice(-letters.length * 2);
|
|
134
|
+
return letters !== "" && words.some((word, index) => word.startsWith(letters.charAt(0)) && isSubsequence(letters, words.slice(index).join("")));
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 括弧の中身が、空白で繋いだ略語の並びだけの形((ON RRP))。1 つの略語を 2 語以上で書いたもの。並びの文字が括弧の前の名前から
|
|
139
|
+
* 順に拾えるときだけ展開と見なす。拾えなければ、空白で並べた略語の列挙((SEC FINRA))。
|
|
140
|
+
* 括弧の外の並び(AWS KMS (Key Management Service))は、括弧の直前の語だけが展開されるので、ここでは見ない。
|
|
141
|
+
*/
|
|
142
|
+
const isJointBracketedAt = (body: string, acronym: string, at: number): boolean => {
|
|
143
|
+
const end = at + acronym.length;
|
|
144
|
+
const before = JOINT_BEFORE.exec(body.slice(Math.max(0, at - DEFINITION_REACH), at))?.[0] ?? "";
|
|
145
|
+
const after = JOINT_AFTER.exec(body.slice(end, end + DEFINITION_REACH))?.[0] ?? "";
|
|
146
|
+
const start = at - before.length;
|
|
147
|
+
const close = end + after.length;
|
|
148
|
+
if (!OPENED.test(body.slice(Math.max(0, start - NEAR), start)) || !CLOSES.test(body.slice(close, close + NEAR))) return false;
|
|
149
|
+
return abbreviates(body.slice(0, start), body.slice(start, close));
|
|
150
|
+
};
|
|
151
|
+
|
|
95
152
|
const isBracketedAt = (body: string, acronym: string, at: number): boolean => {
|
|
96
153
|
const after = body.slice(at + acronym.length, at + acronym.length + NEAR);
|
|
97
154
|
const before = body.slice(Math.max(0, at - NEAR), at);
|
|
98
155
|
if (OPENS.test(after) || (OPENED.test(before) && CLOSES.test(after))) return true;
|
|
99
156
|
if (SQUARE_OPENED.test(before) && SQUARE_CLOSES.test(after) && spellsOut(body.slice(0, at), acronym)) return true;
|
|
100
|
-
return isSeparatedAt(body, acronym, at);
|
|
157
|
+
return isSeparatedAt(body, acronym, at) || isNamedBeforeAt(body, acronym, at) || isJointBracketedAt(body, acronym, at);
|
|
101
158
|
};
|
|
102
159
|
|
|
160
|
+
/**
|
|
161
|
+
* 記号を = で定義する書き方(where N = number of cases, EH = total hours worked)。定義は記号の後ろの語。
|
|
162
|
+
* 値(EH = 2,000)、比べる・矢印(==、=>)、別の略語だけ(EH = SRE)は定義ではない。定義の語は小文字か、大小の無い文字を含む。
|
|
163
|
+
* 語の値(SRE = enabled)は 1 語の定義と見分けられないので、定義と読む。値を入れる大文字の名前は設定の名前で、展開するものが無い。
|
|
164
|
+
*/
|
|
165
|
+
const EQUALS_DEFINITION = /^\s*[==]\s*(?=\p{L})\S*?[\p{Ll}\p{Lo}]/u;
|
|
166
|
+
|
|
167
|
+
const isEqualsDefinedAt = (body: string, acronym: string, at: number): boolean =>
|
|
168
|
+
EQUALS_DEFINITION.test(body.slice(at + acronym.length, at + acronym.length + DEFINITION_REACH));
|
|
169
|
+
|
|
103
170
|
export type ExpandedAt = (body: string, acronym: string, at: number) => boolean;
|
|
104
171
|
|
|
105
172
|
/** body の at にある略語が、その場で展開・定義されているか。語彙表から一度だけ組み立てて、略語ごとに呼ぶ。 */
|
|
106
173
|
export const expansionAt = (words: DefinitionWords): ExpandedAt => {
|
|
107
174
|
const patterns = definitionPatterns(words);
|
|
108
|
-
return (body, acronym, at) => isBracketedAt(body, acronym, at) || isDefinedAt(patterns, body, acronym, at);
|
|
175
|
+
return (body, acronym, at) => isBracketedAt(body, acronym, at) || isDefinedAt(patterns, body, acronym, at) || isEqualsDefinedAt(body, acronym, at);
|
|
109
176
|
};
|
|
@@ -1,29 +1,58 @@
|
|
|
1
|
-
import type { Detector, DocumentProfile, Finding, Sentence } from "../plugin.ts";
|
|
2
|
-
import { compacted
|
|
1
|
+
import type { Detector, DocumentProfile, Finding, Sentence, Span, Token } from "../plugin.ts";
|
|
2
|
+
import { compacted } from "./gram-place.ts";
|
|
3
|
+
import { leadingCounter } from "./counter-edge.ts";
|
|
3
4
|
import { maskAddresses } from "../address-chain.ts";
|
|
4
5
|
import { isAddressRun } from "./place-run.ts";
|
|
6
|
+
import { isOneName } from "./name-run.ts";
|
|
5
7
|
import { proseText } from "../measure.ts";
|
|
8
|
+
import { parallelDotCount } from "./middle-dot.ts";
|
|
6
9
|
|
|
7
10
|
/**
|
|
8
|
-
*
|
|
11
|
+
* 文字の並びを見る検出。漢字の連なりが住所か名前かだけは、形態素解析の固有名詞と数で決める。
|
|
9
12
|
* 覆った箇所は空白になっているので、proseText で潰してから数える。
|
|
10
13
|
*/
|
|
11
14
|
const KANJI_RUN = /[一-鿿]+/gu;
|
|
12
15
|
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
16
|
+
/** 漢字の連なりと、その文書全体の座標での範囲。 */
|
|
17
|
+
type KanjiRun = { readonly text: string; readonly span: Span | undefined };
|
|
18
|
+
|
|
19
|
+
const SPACE = /\s/gu;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 同じ連なりが文に二度出ても、それぞれの位置で読む。proseText は空白を詰めたり除いたりするだけなので、
|
|
23
|
+
* 空白でない k 文字目は元の文でも空白でない k 文字目。番地の覆いは長さを保つので、proseText の位置がそのまま使える。
|
|
24
|
+
*/
|
|
25
|
+
const runsOf = (sentence: Sentence, profile: DocumentProfile | undefined): KanjiRun[] => {
|
|
26
|
+
const prose = proseText(sentence);
|
|
27
|
+
const printed = compacted(sentence.text, "char").offsets;
|
|
28
|
+
return [...maskAddresses(prose, profile).matchAll(KANJI_RUN)].map((match) => {
|
|
29
|
+
const before = prose.slice(0, match.index).replace(SPACE, "").length;
|
|
30
|
+
const [start, last] = [printed[before], printed[before + match[0].length - 1]];
|
|
31
|
+
const span = start === undefined || last === undefined ? undefined : { start: sentence.span.start + start, end: sentence.span.start + last + 1 };
|
|
32
|
+
return { text: match[0], span };
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** 漢字の連なりが住所(東京都港区新橋二丁目)か 1 つの名前(日本銀行)か。形態素解析の地名・人名・組織名と数で決める。 */
|
|
37
|
+
const isPlaceName = (tokens: readonly Token[], span: Span, topUnits: ReadonlySet<string>): boolean => {
|
|
38
|
+
const covering = tokens.flatMap((token, index) => (token.span.start < span.end && span.start < token.span.end ? [index] : []));
|
|
39
|
+
return isAddressRun(tokens, covering, topUnits) || isOneName(tokens, covering);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 数えない連なりは空。頭の、数に付いた助数詞(2日日本弁護士連合会の「日」)は数えない。
|
|
44
|
+
* 住所か名前かは、助数詞を外した残りで決める(1日日本銀行の「日本銀行」)。品詞が無ければ判定しない。
|
|
45
|
+
*/
|
|
46
|
+
const measuredRun = (tokens: readonly Token[] | undefined, run: KanjiRun, topUnits: ReadonlySet<string>): string => {
|
|
47
|
+
if (tokens === undefined || run.span === undefined) return run.text;
|
|
48
|
+
const counter = leadingCounter(tokens, run.span.start, run.text);
|
|
49
|
+
const rest = counter === undefined ? run.span : { start: counter.span.end, end: run.span.end };
|
|
50
|
+
return isPlaceName(tokens, rest, topUnits) ? "" : run.text.slice(counter?.surface.length ?? 0);
|
|
21
51
|
};
|
|
22
52
|
|
|
23
53
|
const longestKanji = (sentence: Sentence, profile: DocumentProfile | undefined, topUnits: ReadonlySet<string>): string =>
|
|
24
|
-
|
|
25
|
-
.map((
|
|
26
|
-
.filter((run) => !isPlaceName(sentence, run, topUnits))
|
|
54
|
+
runsOf(sentence, profile)
|
|
55
|
+
.map((run) => measuredRun(sentence.tokens, run, topUnits))
|
|
27
56
|
.reduce((longest, run) => (run.length > longest.length ? run : longest), "");
|
|
28
57
|
|
|
29
58
|
/**
|
|
@@ -45,15 +74,13 @@ export const kanjiRun: Detector = (doc, options): Finding[] => {
|
|
|
45
74
|
}));
|
|
46
75
|
};
|
|
47
76
|
|
|
48
|
-
const MIDDLE_DOT = /・/gu;
|
|
49
|
-
|
|
50
77
|
/**
|
|
51
78
|
* 中黒で並べると、どこまでが 1 つの項目か分からなくなる。
|
|
52
79
|
* 「企画・開発・運用・保守体制」は、保守体制が 1 つなのか保守と体制なのか読めない。
|
|
53
80
|
*/
|
|
54
81
|
export const middleDot: Detector = (doc, options): Finding[] =>
|
|
55
82
|
doc.sentences
|
|
56
|
-
.map((sentence) => ({ sentence, count:
|
|
83
|
+
.map((sentence) => ({ sentence, count: parallelDotCount(sentence.text) }))
|
|
57
84
|
.filter(({ count }) => count > options.limit)
|
|
58
85
|
.map(({ sentence, count }) => ({
|
|
59
86
|
rule: "no-nakaguro-parallel",
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 仕様書が参考文献を指す角括弧の見出し語([RFC9110]、[HPACK]、[SECURING-WEB])。中身は文献一覧の見出しで、読み手はそこへ
|
|
3
|
+
* 飛ぶので、略語として展開を求めない。空白を含まない 1 語だけを見る。Markdown のリンク([DRI](/url)、[DRI][ref])と
|
|
4
|
+
* 参照の定義([DRI]: /url)は括弧の後ろの記号で外し、その中の語は略語のまま数える。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
type Span = { readonly start: number; readonly end: number };
|
|
8
|
+
|
|
9
|
+
const CITATION_KEY = /(?<![\p{L}\p{N}_!\]])\[[A-Za-z][A-Za-z0-9.-]*[A-Za-z0-9]\](?![([:])/gu;
|
|
10
|
+
|
|
11
|
+
/** 文の中の、参考文献の見出し語の範囲(括弧を含む)。 */
|
|
12
|
+
export const citationKeySpans = (text: string): Span[] =>
|
|
13
|
+
[...text.matchAll(CITATION_KEY)].map((match) => ({ start: match.index, end: match.index + match[0].length }));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { StructureNode, Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
/** 他の文書を指す参照(民法第709条の第709条)の始まりの位置から、その文書の名前(民法)へ。 */
|
|
4
|
+
export type CitedNames = ReadonlyMap<number, string>;
|
|
5
|
+
|
|
6
|
+
const namedReferences = (node: StructureNode): (readonly [number, string])[] => {
|
|
7
|
+
const document = node.attrs["document"];
|
|
8
|
+
const own: (readonly [number, string])[] = node.kind === "reference" && typeof document === "string" ? [[node.span.start, document]] : [];
|
|
9
|
+
return [...own, ...node.children.flatMap(namedReferences)];
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const citedNamesOf = (structure: StructureNode | undefined): CitedNames => new Map(structure === undefined ? [] : namedReferences(structure));
|
|
13
|
+
|
|
14
|
+
/** 略した名前は、頭の一字と種類の一字(所法・法法・措令)。二字の種類の語の重なり(規則規則)は略さない書き方。 */
|
|
15
|
+
const ABBREVIATED_KIND_LENGTH = 1;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 重なった二語が、番地のすぐ前に書いた文書の名前の全部で、頭の一字と種類の一字か。通達の「法法第64条」は法人税法の略で、
|
|
19
|
+
* 略した頭(法)と種類の語(法)がたまたま同じ字になる。名前の一部だけの重なり(就業規則規則第3条)や、種類の語でない語の
|
|
20
|
+
* 重なり(民法民法第709条)は書き損じのまま。
|
|
21
|
+
*/
|
|
22
|
+
export const endsCitedName = (first: Token, second: Token, cited: CitedNames, kinds: ReadonlySet<string>): boolean =>
|
|
23
|
+
kinds.has(second.surface) && [...second.surface].length === ABBREVIATED_KIND_LENGTH && cited.get(second.span.end) === `${first.surface}${second.surface}`;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Span, StructureKind, StructureNode, Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 形態素解析が数と読んだ語(UD の NumType=Card)。漢数字の「二割」「十五分」も、算用数字と同じ具体的な数。
|
|
5
|
+
* 言い回しの「二人三脚」「一人ひとり」は、辞書が一語として持つので数にならない。
|
|
6
|
+
*/
|
|
7
|
+
const isCardinal = (token: Token): boolean => token.features?.["NumType"] === "Card";
|
|
8
|
+
|
|
9
|
+
const HYPHEN = /^[-‐‑]$/u;
|
|
10
|
+
|
|
11
|
+
/** ハイフンで前の語に続く数(one-on-one の後ろの one)は、合成語の部品。 */
|
|
12
|
+
const isCompoundPart = (token: Token, previous: Token | undefined): boolean =>
|
|
13
|
+
previous !== undefined && previous.span.end === token.span.start && HYPHEN.test(previous.surface);
|
|
14
|
+
|
|
15
|
+
/** 数の語(UPOS の NUM)が、すぐ後ろの複数形の名詞を数えている(five minutes)。one of や two-way は量ではない。 */
|
|
16
|
+
const countsPlural = (tokens: readonly Token[], index: number): boolean => {
|
|
17
|
+
const token = tokens[index];
|
|
18
|
+
return token?.pos === "NUM" && tokens[index + 1]?.features?.["Number"] === "Plur" && !isCompoundPart(token, tokens[index - 1]);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** 品詞が無ければ何も言わない。数字そのものは呼ぶ側が文字で見る。 */
|
|
22
|
+
export const hasNumeral = (tokens: readonly Token[]): boolean => tokens.some((token, index) => isCardinal(token) || countsPlural(tokens, index));
|
|
23
|
+
|
|
24
|
+
/** 節の中で始まるものがある。節は見出しの直後から次の見出しの前まで。 */
|
|
25
|
+
export const startsWithin = (section: Span, spans: readonly Span[]): boolean => spans.some((span) => span.start >= section.start && span.start < section.end);
|
|
26
|
+
|
|
27
|
+
/** 構造の木が読んだ参照(第3条・Section VI)・数量・日付。どれも読み手が確かめに行ける具体物。 */
|
|
28
|
+
const EVIDENCE: ReadonlySet<StructureKind> = new Set(["reference", "quantity", "date"]);
|
|
29
|
+
|
|
30
|
+
export const evidenceSpans = (node: StructureNode | undefined): Span[] =>
|
|
31
|
+
node === undefined ? [] : [...(EVIDENCE.has(node.kind) ? [node.span] : []), ...node.children.flatMap((child) => evidenceSpans(child))];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Sentence, Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 語彙表が原形で書いた語句(という、と呼ぶ)が、文の中で活用して書かれた形(といいます、と呼びました)。
|
|
5
|
+
* 語句の最後の動詞を原形(lemma)で照らし、その前の部分は字面で照らす。語句を語に分けて並びで照らさないのは、
|
|
6
|
+
* 解析器が単独の「という」を 1 つの助詞に、「といいます」を と・いい・ます に分け、「称し」の原形を 称す とするから。
|
|
7
|
+
* 活用した形は、動詞に続く助動詞(ます、た)まで。品詞が無い文からは何も出ない。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
type Run = { readonly start: number; readonly end: number };
|
|
11
|
+
|
|
12
|
+
/** 動詞と、そのすぐ後ろに続く助動詞の、文の中の範囲。 */
|
|
13
|
+
const inflectedRun = (sentence: Sentence, tokens: readonly Token[], index: number, verb: Token): Run => {
|
|
14
|
+
const rest = tokens.slice(index + 1);
|
|
15
|
+
const stop = rest.findIndex((token) => token.pos !== "AUX");
|
|
16
|
+
const last = (stop === -1 ? rest : rest.slice(0, stop)).at(-1) ?? verb;
|
|
17
|
+
return { start: verb.span.start - sentence.span.start, end: last.span.end - sentence.span.start };
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const formsAt = (sentence: Sentence, tokens: readonly Token[], index: number, bases: readonly string[]): string[] => {
|
|
21
|
+
const verb = tokens[index];
|
|
22
|
+
const lemma = verb?.lemma;
|
|
23
|
+
if (verb?.pos !== "VERB" || lemma === undefined || lemma === "") return [];
|
|
24
|
+
const run = inflectedRun(sentence, tokens, index, verb);
|
|
25
|
+
const before = sentence.text.slice(0, run.start);
|
|
26
|
+
const written = sentence.text.slice(run.start, run.end);
|
|
27
|
+
return bases
|
|
28
|
+
.filter((base) => base.endsWith(lemma))
|
|
29
|
+
.map((base) => base.slice(0, base.length - lemma.length))
|
|
30
|
+
.filter((head) => before.endsWith(head))
|
|
31
|
+
.map((head) => `${head}${written}`);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const conjugatedForms = (sentences: readonly Sentence[], bases: readonly string[]): string[] => {
|
|
35
|
+
const forms = sentences.flatMap((sentence) => {
|
|
36
|
+
const tokens = sentence.tokens ?? [];
|
|
37
|
+
return tokens.flatMap((_token, index) => formsAt(sentence, tokens, index, bases));
|
|
38
|
+
});
|
|
39
|
+
return [...new Set(forms)];
|
|
40
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
const BLANK = /^\s+$/u;
|
|
4
|
+
|
|
5
|
+
const isNumber = (token: Token | undefined): boolean => token?.features?.["NumType"] === "Card";
|
|
6
|
+
const isCounter = (token: Token | undefined): boolean => token?.features?.["NounType"] === "Class";
|
|
7
|
+
|
|
8
|
+
const touches = (left: Token, right: Token): boolean => left.span.end === right.span.start;
|
|
9
|
+
|
|
10
|
+
/** at の語のすぐ前の語。空白一つを挟んでもよい(「2 日」)。 */
|
|
11
|
+
const wordBefore = (tokens: readonly Token[], at: number): Token | undefined => {
|
|
12
|
+
const [previous, token] = [tokens[at - 1], tokens[at]];
|
|
13
|
+
if (previous === undefined || token === undefined || !touches(previous, token)) return undefined;
|
|
14
|
+
if (!BLANK.test(previous.surface)) return previous;
|
|
15
|
+
const beyond = tokens[at - 2];
|
|
16
|
+
return beyond !== undefined && touches(beyond, previous) ? beyond : undefined;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 漢字の連なり run(文書全体の座標で start から始まる)の頭が、直前の数に付いた助数詞(2日、第76回、24時間)なら、その助数詞の語。
|
|
21
|
+
* 数字が区切りを見せるので、助数詞は数と読まれ、後ろの複合語の一部ではない。そうでなければ undefined。
|
|
22
|
+
* 折り返しをまたぐ語(年\n度)は span が surface より長いので、連なりの残りは span の終わりから始まる。
|
|
23
|
+
*/
|
|
24
|
+
export const leadingCounter = (tokens: readonly Token[], start: number, run: string): Token | undefined => {
|
|
25
|
+
const at = tokens.findIndex((token) => token.span.start === start);
|
|
26
|
+
const counter = tokens[at];
|
|
27
|
+
if (counter === undefined || !isCounter(counter) || !isNumber(wordBefore(tokens, at))) return undefined;
|
|
28
|
+
return run.startsWith(counter.surface) ? counter : undefined;
|
|
29
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* . で空白を挟まずに繋いだ語(GOV.UK、SAM.gov、README.md、ASP.NET)。ドメイン名やファイル名で、中の大文字は略語ではない。
|
|
3
|
+
* 文の終わりの . は後ろに空白か行末が来る(…by the IRS. Then)ので、ここには当たらない。空白を落とした文の境目
|
|
4
|
+
* (the IRS.Then、Mr.HAWLEY)は頭だけ大文字の部品で分かるので、部品は全部大文字か全部小文字(と数字)に限る。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
type Span = { readonly start: number; readonly end: number };
|
|
8
|
+
|
|
9
|
+
const LABEL = "(?:[A-Z0-9][A-Z0-9-]*|[a-z0-9][a-z0-9-]*)";
|
|
10
|
+
const DOTTED_NAME = new RegExp(String.raw`(?<![A-Za-z0-9_&.-])${LABEL}(?:\.${LABEL})+(?![A-Za-z0-9_&])`, "gu");
|
|
11
|
+
|
|
12
|
+
/** 文の中の、. で繋いだ名前の範囲。 */
|
|
13
|
+
export const dottedNameSpans = (text: string): Span[] =>
|
|
14
|
+
[...text.matchAll(DOTTED_NAME)].map((match) => ({ start: match.index, end: match.index + match[0].length }));
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Detector, Finding, Lexicon, Sentence, Token } from "../plugin.ts";
|
|
2
|
+
import { citedNamesOf, endsCitedName } from "./cited-name.ts";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 語を書き損じて二度書いた(the the / 資料をを)。消し忘れと、書き換えの途中で残った語(our the platform)の二つ。
|
|
@@ -99,11 +100,22 @@ const repeatsPrevious = (tokens: readonly Token[], at: number): boolean => {
|
|
|
99
100
|
/** 語彙表の重なり(had had)も、三つ続けば(had had had)書き損じ。 */
|
|
100
101
|
const allowedAt = (tokens: readonly Token[], at: number, allowed: Lexicon): Lexicon => (repeatsPrevious(tokens, at) ? [] : allowed);
|
|
101
102
|
|
|
102
|
-
|
|
103
|
+
/**
|
|
104
|
+
* tokens の at 語目から、語彙表の決まった句(a priori・a la carte)が始まるか。句の頭の a は冠詞ではなく句の一部なので、
|
|
105
|
+
* 前の違う冠詞と並んでも(the a priori approach)書き損じではない。同じ語(a a priori)は書き損じのまま。
|
|
106
|
+
*/
|
|
107
|
+
export const opensPhrase = (tokens: readonly Token[], at: number, phrases: Lexicon): boolean =>
|
|
108
|
+
phrases.some((entry) => {
|
|
109
|
+
const words = entry.tokens ?? [];
|
|
110
|
+
return words.length > 1 && surfacesOf(tokens.slice(at, at + words.length)) === surfacesOf(words);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
export const doubledIn = (source: string, tokens: readonly Token[], spaced: boolean, allowed: Lexicon, phrases: Lexicon = []): Doubled[] =>
|
|
103
114
|
tokens.flatMap((second, index) => {
|
|
104
115
|
const first = tokens[index - 1];
|
|
105
116
|
if (first === undefined || !doubledAt(source, first, second, spaced)) return [];
|
|
106
117
|
if (isAllowed(tokens, index - 1, allowedAt(tokens, index - 1, allowed))) return [];
|
|
118
|
+
if (!sameWord(first, second) && opensPhrase(tokens, index, phrases)) return [];
|
|
107
119
|
return isNameBefore(first, second, opensSentence(tokens, index - 1)) ? [] : [{ first, second }];
|
|
108
120
|
});
|
|
109
121
|
|
|
@@ -119,10 +131,21 @@ const findingOf = (sentence: Sentence, doubled: Doubled, spaced: boolean): Findi
|
|
|
119
131
|
values: { word: wordOf(doubled, spaced), offset: doubled.second.span.start },
|
|
120
132
|
});
|
|
121
133
|
|
|
134
|
+
/** 文書の種類の語(法・規則・契約書)。語彙表はアダプタが持つ。 */
|
|
135
|
+
const DOCUMENT_KIND = "document-kind";
|
|
136
|
+
|
|
137
|
+
/** 冠詞のような語で始まる決まった句。語彙表はアダプタが持つ。冠詞の無い言語(日本語)は持たない。 */
|
|
138
|
+
const FIXED_PHRASE = "fixed-phrase";
|
|
139
|
+
|
|
122
140
|
export const doubledWord: Detector = (doc, options): Finding[] => {
|
|
123
141
|
const spaced = doc.lengthUnit === "word";
|
|
124
142
|
const allowed = options.lexicon ?? [];
|
|
143
|
+
const cited = citedNamesOf(doc.structure);
|
|
144
|
+
const kinds = new Set((doc.lexicons[DOCUMENT_KIND] ?? []).map((entry) => entry.pattern));
|
|
145
|
+
const phrases = doc.lexicons[FIXED_PHRASE] ?? [];
|
|
125
146
|
return doc.sentences.flatMap((sentence) =>
|
|
126
|
-
doubledIn(doc.source, sentence.tokens ?? [], spaced, allowed
|
|
147
|
+
doubledIn(doc.source, sentence.tokens ?? [], spaced, allowed, phrases)
|
|
148
|
+
.filter((doubled) => !endsCitedName(doubled.first, doubled.second, cited, kinds))
|
|
149
|
+
.map((doubled) => findingOf(sentence, doubled, spaced)),
|
|
127
150
|
);
|
|
128
151
|
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { lengthOf } from "../measure.ts";
|
|
2
2
|
import { isClosed } from "../sentence-shape.ts";
|
|
3
|
+
import { isTitleCase, minorityCase, pageTitleOf } from "./heading-case.ts";
|
|
3
4
|
import type { Detector, Finding, ProseDocument, Section, Sentence, Token } from "../plugin.ts";
|
|
4
5
|
|
|
5
6
|
const PER = 1000;
|
|
@@ -106,19 +107,6 @@ export const conjunctionRun: Detector = (doc, options): Finding[] => {
|
|
|
106
107
|
});
|
|
107
108
|
};
|
|
108
109
|
|
|
109
|
-
const WORD = /[A-Za-z][A-Za-z'-]*/gu;
|
|
110
|
-
|
|
111
|
-
/** 小さい語は Title Case でも小文字のままなので、大文字化の判定から外す。 */
|
|
112
|
-
const MINOR = new Set(["a", "an", "the", "and", "or", "but", "of", "in", "on", "at", "to", "for", "with", "as", "by", "from", "is"]);
|
|
113
|
-
|
|
114
|
-
const isTitleCase = (heading: string): boolean | undefined => {
|
|
115
|
-
const words = [...heading.matchAll(WORD)].map((match) => match[0]).filter((word) => !MINOR.has(word.toLowerCase()));
|
|
116
|
-
// 1 語の見出しは、どちらの流儀でも先頭が大文字になる。判定できない。
|
|
117
|
-
if (words.length < 2) return undefined;
|
|
118
|
-
const capitalized = words.filter((word) => word[0] === word[0]?.toUpperCase()).length;
|
|
119
|
-
return capitalized === words.length;
|
|
120
|
-
};
|
|
121
|
-
|
|
122
110
|
const headingsOf = (sections: readonly Section[]): { readonly section: Section; readonly title: boolean }[] =>
|
|
123
111
|
sections.flatMap((section) => {
|
|
124
112
|
const title = isTitleCase(section.heading);
|
|
@@ -127,25 +115,24 @@ const headingsOf = (sections: readonly Section[]): { readonly section: Section;
|
|
|
127
115
|
|
|
128
116
|
/**
|
|
129
117
|
* 見出しの大文字化が混ざっている。どちらの流儀が正しいかは決めない。spec §12.3。
|
|
130
|
-
*
|
|
118
|
+
* 見るのは文書の中で揃っているかだけで、少数派のほうを指摘する。題名は指摘しない(minorityCase)。
|
|
131
119
|
*/
|
|
132
120
|
export const titleCaseMix: Detector = (doc, options): Finding[] => {
|
|
133
|
-
const
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
const
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}));
|
|
121
|
+
const pageTitle = pageTitleOf(doc.sections);
|
|
122
|
+
const judged = headingsOf(doc.sections.filter((section) => section !== pageTitle));
|
|
123
|
+
const titleCase = judged.filter((entry) => entry.title).length;
|
|
124
|
+
const pageTitleCase = pageTitle === undefined ? undefined : isTitleCase(pageTitle.heading);
|
|
125
|
+
const minorityIsTitle = minorityCase({ titleCase, sentenceCase: judged.length - titleCase }, pageTitleCase);
|
|
126
|
+
const few = minorityIsTitle === undefined ? [] : judged.filter((entry) => entry.title === minorityIsTitle);
|
|
127
|
+
if (few.length > options.limit) return [];
|
|
128
|
+
return few.map(({ section }) => ({
|
|
129
|
+
rule: "title-case-consistency",
|
|
130
|
+
severity: "info",
|
|
131
|
+
line: 0,
|
|
132
|
+
column: 0,
|
|
133
|
+
quote: section.heading,
|
|
134
|
+
values: { count: few.length, limit: options.limit, offset: section.span.start },
|
|
135
|
+
}));
|
|
149
136
|
};
|
|
150
137
|
|
|
151
138
|
const LIST_CONJUNCTION = new Set(["and", "or"]);
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Sentence, Span, StructureNode, Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
/** 番号の付いた行。start は行の始まり(字下げの前)、label は言語パッケージが読んだ番号(「(1)」)。 */
|
|
4
|
+
export type NumberedStart = { readonly start: number; readonly label: string };
|
|
5
|
+
|
|
6
|
+
const labelOf = (node: StructureNode): string => {
|
|
7
|
+
const label = node.attrs["label"];
|
|
8
|
+
return typeof label === "string" ? label : "";
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 条の外で、番号の付いた行(言語パッケージが項目と読んだ「(1)」など)。
|
|
13
|
+
* 条の中の番号付きの行は、条の項(条の本文の続き)でもありうるので数えない。
|
|
14
|
+
*/
|
|
15
|
+
export const numberedStarts = (node: StructureNode, inArticle = false): NumberedStart[] => [
|
|
16
|
+
...(node.kind === "item" && !inArticle ? [{ start: node.span.start, label: labelOf(node) }] : []),
|
|
17
|
+
...node.children.flatMap((child) => numberedStarts(child, inArticle || node.kind === "article")),
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
const INDENT = /^[ \t\u3000]*/u;
|
|
21
|
+
|
|
22
|
+
const lineAt = (source: string, start: number): string => {
|
|
23
|
+
const end = source.indexOf("\n", start);
|
|
24
|
+
return source.slice(start, end === -1 ? source.length : end);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/** 番号の直後の語。字下げと番号と空白を飛ばした先の、最初の語。 */
|
|
28
|
+
const wordAfter = (numbered: NumberedStart, sentences: readonly Sentence[], source: string): Token | undefined => {
|
|
29
|
+
const after = numbered.start + (INDENT.exec(lineAt(source, numbered.start))?.[0].length ?? 0) + numbered.label.length;
|
|
30
|
+
const sentence = sentences.find((candidate) => candidate.span.end > after);
|
|
31
|
+
return sentence?.tokens?.find((token) => token.span.start >= after && token.surface.trim() !== "");
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 並びの項目として番号を振った行の始まり。番号の直後が助詞(「(1)の金額は」「(2)は対象外」)なら、
|
|
36
|
+
* 番号は項目の印ではなく、項目を指す本文の主語や修飾なので除く。
|
|
37
|
+
*/
|
|
38
|
+
export const enumeratorStarts = (numbered: readonly NumberedStart[], sentences: readonly Sentence[], source: string): number[] =>
|
|
39
|
+
numbered.filter((line) => wordAfter(line, sentences, source)?.pos !== "ADP").map((line) => line.start);
|
|
40
|
+
|
|
41
|
+
/** 番号の付いた行の始まりと段落の始まりのあいだが字下げだけなら、段落はその行から始まる。 */
|
|
42
|
+
const opensWithItem = (paragraph: Span, starts: readonly number[], source: string): boolean =>
|
|
43
|
+
starts.some((start) => start <= paragraph.start && source.slice(start, paragraph.start).trim() === "");
|
|
44
|
+
|
|
45
|
+
const isBlank = (source: string, from: number, to: number): boolean => source.slice(from, to).trim() === "";
|
|
46
|
+
|
|
47
|
+
/** 並びと読むのに要る、番号の付いた行の数。一つだけなら、並びではなく番号で始まる本文の段落かもしれない。 */
|
|
48
|
+
const MIN_ITEMS = 2;
|
|
49
|
+
|
|
50
|
+
/** 番号の行の始まりは字下げの前なので、並びの最初の行の頭から数える。 */
|
|
51
|
+
const itemsIn = (run: Span, starts: readonly number[], source: string): number => {
|
|
52
|
+
const lineStart = source.lastIndexOf("\n", run.start - 1) + 1;
|
|
53
|
+
return starts.filter((start) => start >= lineStart && start < run.end).length;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 番号で始まる段落が、空行だけを挟んで続く範囲。番号の付いた行を二つ以上含むものだけ。
|
|
58
|
+
* 要件や条件を「(1)…であること。」と並べるのは、段落でも箇条書きと同じ書き方で、本文の調子とは別に揃える。
|
|
59
|
+
*/
|
|
60
|
+
export const enumeratedRuns = (paragraphs: readonly Span[], starts: readonly number[], source: string): Span[] => {
|
|
61
|
+
const enumerated = paragraphs.filter((paragraph) => opensWithItem(paragraph, starts, source));
|
|
62
|
+
const runs = enumerated.reduce<Span[]>((joined, paragraph) => {
|
|
63
|
+
const last = joined.at(-1);
|
|
64
|
+
if (last === undefined || !isBlank(source, last.end, paragraph.start)) return [...joined, paragraph];
|
|
65
|
+
return [...joined.slice(0, -1), { start: last.start, end: paragraph.end }];
|
|
66
|
+
}, []);
|
|
67
|
+
return runs.filter((run) => itemsIn(run, starts, source) >= MIN_ITEMS);
|
|
68
|
+
};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { softBreaks } from "../soft-break.ts";
|
|
1
2
|
import type { LengthUnit, Span } from "../plugin.ts";
|
|
2
3
|
|
|
3
4
|
/** 数えるために詰めた文と、詰めた文の各文字が元の文のどこにあったか。 */
|
|
@@ -7,15 +8,21 @@ export type Compacted = { readonly text: string; readonly offsets: readonly numb
|
|
|
7
8
|
* n-gram を数えるときと同じ詰め方。word 単位は空白の並びを 1 つにして前後を削り、char 単位は空白を全部除く。
|
|
8
9
|
* 語句が元の文のどこにあるかを戻せるよう、残した文字ごとに元の位置を持つ。
|
|
9
10
|
*/
|
|
11
|
+
/** spans が覆う位置(UTF-16 の単位)。 */
|
|
12
|
+
const offsetsIn = (spans: readonly Span[]): Set<number> =>
|
|
13
|
+
new Set(spans.flatMap((span) => Array.from({ length: span.end - span.start }, (_, step) => span.start + step)));
|
|
14
|
+
|
|
10
15
|
export const compacted = (text: string, unit: LengthUnit): Compacted => {
|
|
11
16
|
const chars: string[] = [];
|
|
12
17
|
const offsets: number[] = [];
|
|
13
18
|
const state = { pendingSpace: -1, at: 0 };
|
|
19
|
+
// 読み手に見えない改行は空白 1 つにもしない。proseText がつないだ語句を、ここでも同じ形で探せるように。
|
|
20
|
+
const hidden = offsetsIn(unit === "word" ? softBreaks(text) : []);
|
|
14
21
|
Array.from(text).forEach((char) => {
|
|
15
22
|
const at = state.at;
|
|
16
23
|
state.at += char.length;
|
|
17
24
|
if (/\s/u.test(char)) {
|
|
18
|
-
if (unit === "word" && chars.length > 0 && state.pendingSpace === -1) state.pendingSpace = at;
|
|
25
|
+
if (unit === "word" && chars.length > 0 && state.pendingSpace === -1 && !hidden.has(at)) state.pendingSpace = at;
|
|
19
26
|
return;
|
|
20
27
|
}
|
|
21
28
|
if (state.pendingSpace !== -1) {
|