chaffjs 0.14.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 +4 -0
- package/dist/detectors/acronym-context.d.ts.map +1 -1
- package/dist/detectors/acronym-context.js +17 -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 +36 -14
- 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/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/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/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 +15 -5
- 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/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/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 +29 -24
- 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/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 +6 -3
- package/dist/number-name.d.ts.map +1 -1
- package/dist/number-name.js +31 -14
- 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 +16 -1
- package/dist/plugin.d.ts.map +1 -1
- 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 +19 -9
- 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/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/latin-spacing.yaml +4 -2
- package/rules/max-kanji-continuous.yaml +1 -1
- package/rules/no-doubled-joshi.yaml +2 -2
- package/rules/title-case-consistency.yaml +2 -2
- package/rules/undefined-acronym.yaml +14 -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 +22 -1
- package/src/detectors/acronym-expansion.ts +72 -5
- package/src/detectors/char-shape.ts +42 -14
- 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/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/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/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 +17 -8
- package/src/detectors/placeholder.ts +38 -0
- package/src/detectors/quoted-variant.ts +20 -0
- package/src/detectors/series-label.ts +33 -0
- package/src/detectors/signals.ts +35 -30
- package/src/detectors/structure-tree.ts +2 -2
- 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 +35 -13
- package/src/orthography.ts +15 -9
- package/src/plain-source.ts +5 -0
- package/src/plugin.ts +21 -2
- 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 +19 -10
- package/src/structure/date-order.ts +16 -1
- 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,30 +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";
|
|
6
8
|
import { parallelDotCount } from "./middle-dot.ts";
|
|
7
9
|
|
|
8
10
|
/**
|
|
9
|
-
*
|
|
11
|
+
* 文字の並びを見る検出。漢字の連なりが住所か名前かだけは、形態素解析の固有名詞と数で決める。
|
|
10
12
|
* 覆った箇所は空白になっているので、proseText で潰してから数える。
|
|
11
13
|
*/
|
|
12
14
|
const KANJI_RUN = /[一-鿿]+/gu;
|
|
13
15
|
|
|
14
|
-
/**
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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);
|
|
22
51
|
};
|
|
23
52
|
|
|
24
53
|
const longestKanji = (sentence: Sentence, profile: DocumentProfile | undefined, topUnits: ReadonlySet<string>): string =>
|
|
25
|
-
|
|
26
|
-
.map((
|
|
27
|
-
.filter((run) => !isPlaceName(sentence, run, topUnits))
|
|
54
|
+
runsOf(sentence, profile)
|
|
55
|
+
.map((run) => measuredRun(sentence.tokens, run, topUnits))
|
|
28
56
|
.reduce((longest, run) => (run.length > longest.length ? run : longest), "");
|
|
29
57
|
|
|
30
58
|
/**
|
|
@@ -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,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"]);
|
|
@@ -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) {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { isNamePart } from "./name-part.ts";
|
|
2
|
+
import type { LengthUnit, Span, Token } from "../plugin.ts";
|
|
3
|
+
|
|
4
|
+
/** 動詞と助動詞。言い回し(ることができます、it is important to)にはこれが入り、主題の名前(state and local tax)には入らない。 */
|
|
5
|
+
const PREDICATE_POS = new Set(["VERB", "AUX"]);
|
|
6
|
+
|
|
7
|
+
const NOUNS = new Set(["NOUN", "PROPN", "ADJ"]);
|
|
8
|
+
|
|
9
|
+
/** 中身を持たない語。名前の前後に付いても、名前の繰り返しを言い回しに変えない。 */
|
|
10
|
+
const FUNCTION_POS = new Set(["ADP", "DET", "CCONJ", "SCONJ", "PART", "PUNCT", "SYM", "X"]);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 英語で名詞のすぐ前の動詞は、名詞を飾る語(形容詞を挟んでも同じ)(the upcoming fiscal year、the Disclosing Party、the borrow checker)で、
|
|
14
|
+
* 述語ではない。日本語の名詞の前の動詞(〜にあるコンポーネント)は言い回しの一部になるので、word 単位だけで見る。
|
|
15
|
+
*/
|
|
16
|
+
const modifiesNoun = (tokens: readonly Token[], index: number, unit: LengthUnit): boolean =>
|
|
17
|
+
unit === "word" && tokens[index]?.pos === "VERB" && NOUNS.has(tokens[index + 1]?.pos ?? "");
|
|
18
|
+
|
|
19
|
+
/** 英語で大文字の名前の一部になった動詞(Procedures Guide、the Location Object)も、述語ではない。 */
|
|
20
|
+
const namePartAt = (tokens: readonly Token[], index: number, unit: LengthUnit): boolean => unit === "word" && isNamePart(tokens, index);
|
|
21
|
+
|
|
22
|
+
const isName = (tokens: readonly Token[], index: number, unit: LengthUnit): boolean => {
|
|
23
|
+
const token = tokens[index];
|
|
24
|
+
return token !== undefined && (token.pos === "PROPN" || token.features?.["NameType"] !== undefined || namePartAt(tokens, index, unit));
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const overlaps = (token: Token, window: Span): boolean => token.span.start < window.end && window.start < token.span.end;
|
|
28
|
+
|
|
29
|
+
const isCut = (token: Token, window: Span): boolean => token.span.start < window.start || window.end < token.span.end;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 窓が名前だけを繰り返しているか。predicate(添字)のほかに窓が触れる語が、名前と中身の無い語だけで、名前が 1 つはある。
|
|
33
|
+
* 窓の端で切れた動詞("ed in the NSF Propos" の identified、"ed by Applicable Law" の prohibited)は語尾しか入っておらず、
|
|
34
|
+
* 動詞は出てくるたびに違ってよい。繰り返されているのは名前のほう。
|
|
35
|
+
*/
|
|
36
|
+
const onlyNamesBeside = (tokens: readonly Token[], predicate: number, window: Span, unit: LengthUnit): boolean => {
|
|
37
|
+
const touched = tokens.flatMap((token, index) => (index !== predicate && overlaps(token, window) ? [index] : []));
|
|
38
|
+
const names = touched.filter((index) => isName(tokens, index, unit));
|
|
39
|
+
const rest = touched.filter((index) => !isName(tokens, index, unit));
|
|
40
|
+
return names.length > 0 && rest.every((index) => FUNCTION_POS.has(tokens[index]?.pos ?? ""));
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const countsAsPredicate = (tokens: readonly Token[], index: number, window: Span, unit: LengthUnit): boolean => {
|
|
44
|
+
const token = tokens[index];
|
|
45
|
+
if (token === undefined || !PREDICATE_POS.has(token.pos) || !overlaps(token, window)) return false;
|
|
46
|
+
if (modifiesNoun(tokens, index, unit) || namePartAt(tokens, index, unit)) return false;
|
|
47
|
+
return !isCut(token, window) || !onlyNamesBeside(tokens, index, window, unit);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 語句が文の中で占める範囲(window、文書の中の位置)に、述語になる動詞か助動詞があるか。
|
|
52
|
+
* 範囲で見る。文字で探すと、同じ文の別の所の動詞「use」が、語句の中の名詞「use」に当たる。
|
|
53
|
+
*/
|
|
54
|
+
export const hasPredicateIn = (tokens: readonly Token[], window: Span, unit: LengthUnit): boolean =>
|
|
55
|
+
tokens.some((_, index) => countsAsPredicate(tokens, index, window, unit));
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const WORD = /[A-Za-z][A-Za-z'-]*/gu;
|
|
2
|
+
|
|
3
|
+
/** 小さい語は Title Case でも小文字のままなので、大文字化の判定から外す。 */
|
|
4
|
+
const MINOR = new Set(["a", "an", "the", "and", "or", "but", "of", "in", "on", "at", "to", "for", "with", "as", "by", "from", "is"]);
|
|
5
|
+
|
|
6
|
+
const TITLE_DEPTH = 1;
|
|
7
|
+
|
|
8
|
+
/** 見出しが Title Case か。判定できなければ undefined。 */
|
|
9
|
+
export const isTitleCase = (heading: string): boolean | undefined => {
|
|
10
|
+
const words = [...heading.matchAll(WORD)].map((match) => match[0]).filter((word) => !MINOR.has(word.toLowerCase()));
|
|
11
|
+
// 1 語の見出しは、どちらの流儀でも先頭が大文字になる。判定できない。
|
|
12
|
+
if (words.length < 2) return undefined;
|
|
13
|
+
const capitalized = words.filter((word) => word[0] === word[0]?.toUpperCase()).length;
|
|
14
|
+
return capitalized === words.length;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 文書の題名。最初の見出しが深さ 1 で、深さ 1 の見出しがほかに無いときの、その見出し。
|
|
19
|
+
* 深さ 1 が複数あると、最初のものが題名なのか章なのかを構造からは決められないので、題名とは呼ばない。
|
|
20
|
+
* 見出しより前の導入部(深さ 0)は見出しではないので数えない。
|
|
21
|
+
*/
|
|
22
|
+
export const pageTitleOf = <T extends { readonly depth: number }>(sections: readonly T[]): T | undefined => {
|
|
23
|
+
const headed = sections.filter((section) => section.depth > 0);
|
|
24
|
+
const first = headed[0];
|
|
25
|
+
if (first?.depth !== TITLE_DEPTH) return undefined;
|
|
26
|
+
return headed.filter((section) => section.depth === TITLE_DEPTH).length === 1 ? first : undefined;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type CaseCounts = { readonly titleCase: number; readonly sentenceCase: number };
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* 節の見出しのうち少数派の流儀(true なら Title Case)。少数派が無ければ undefined。
|
|
33
|
+
* 題名は数に入れない。題名は名前なので、節と流儀を分けるのはよくある揃えかた。
|
|
34
|
+
* 同数のときだけ題名の流儀で決める。題名と同じ流儀のほうが、書き手の選んだ流儀である見込みが高い。
|
|
35
|
+
*/
|
|
36
|
+
export const minorityCase = (sections: CaseCounts, pageTitleCase: boolean | undefined): boolean | undefined => {
|
|
37
|
+
if (sections.titleCase === sections.sentenceCase) return sections.titleCase === 0 || pageTitleCase === undefined ? undefined : !pageTitleCase;
|
|
38
|
+
const minorityIsTitle = sections.titleCase < sections.sentenceCase;
|
|
39
|
+
const few = minorityIsTitle ? sections.titleCase : sections.sentenceCase;
|
|
40
|
+
return few === 0 ? undefined : minorityIsTitle;
|
|
41
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { lengthOf } from "../measure.ts";
|
|
2
2
|
import { handsOver } from "./lead-in.ts";
|
|
3
|
+
import { withoutQuotedVariants } from "./quoted-variant.ts";
|
|
3
4
|
import type { Detector, Finding, LengthUnit, ProseDocument, Section } from "../plugin.ts";
|
|
4
5
|
|
|
5
6
|
/** 見出しが短すぎると、偶然の一致で 100% になる。これ未満の見出しは見ない。 */
|
|
@@ -58,12 +59,15 @@ const leadsIn = (doc: ProseDocument, section: Section, phrases: readonly string[
|
|
|
58
59
|
return first !== undefined && handsOver(first.text, doc.source.slice(first.span.end, section.span.end), phrases);
|
|
59
60
|
};
|
|
60
61
|
|
|
62
|
+
/** 見出しとの重なりを測る文。用語集や表記の手引きは見出しの語の別の書き方を引用する(Not “datacentre”)ので、それは数えない。 */
|
|
63
|
+
const echoedText = (section: Section): string => withoutQuotedVariants(section.firstSentence?.text ?? "", section.heading);
|
|
64
|
+
|
|
61
65
|
export const headingEcho: Detector = (doc, options): Finding[] => {
|
|
62
66
|
const leadIns = (doc.lexicons["lead-in"] ?? []).map((entry) => entry.pattern);
|
|
63
67
|
return doc.sections
|
|
64
68
|
.filter((section) => section.heading.length > 0 && section.firstSentence !== undefined && addsLittle(section, doc.lengthUnit))
|
|
65
69
|
.filter((section) => !leadsIn(doc, section, leadIns))
|
|
66
|
-
.map((section) => ({ section, overlap: Math.round(containment(trigrams(section.heading), trigrams(section
|
|
70
|
+
.map((section) => ({ section, overlap: Math.round(containment(trigrams(section.heading), trigrams(echoedText(section))) * 100) }))
|
|
67
71
|
.filter(({ overlap }) => overlap >= options.limit)
|
|
68
72
|
.map(({ section, overlap }) => ({
|
|
69
73
|
rule: "heading-echo",
|