chaffjs 0.47.0 → 0.48.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/bound-word-particles.d.ts +1 -0
- package/dist/bound-word-particles.d.ts.map +1 -1
- package/dist/bound-word-particles.js +1 -1
- package/dist/bound-word-particles.js.map +1 -1
- package/dist/clause-particles.d.ts +11 -0
- package/dist/clause-particles.d.ts.map +1 -0
- package/dist/clause-particles.js +20 -0
- package/dist/clause-particles.js.map +1 -0
- package/dist/detectors/acronym-definition.d.ts.map +1 -1
- package/dist/detectors/acronym-definition.js +14 -6
- package/dist/detectors/acronym-definition.js.map +1 -1
- package/dist/detectors/acronym-expansion.d.ts +2 -0
- package/dist/detectors/acronym-expansion.d.ts.map +1 -1
- package/dist/detectors/acronym-expansion.js +18 -7
- package/dist/detectors/acronym-expansion.js.map +1 -1
- package/dist/detectors/acronym-mention.d.ts.map +1 -1
- package/dist/detectors/acronym-mention.js +3 -1
- package/dist/detectors/acronym-mention.js.map +1 -1
- package/dist/detectors/acronym-parent-item.d.ts +6 -0
- package/dist/detectors/acronym-parent-item.d.ts.map +1 -0
- package/dist/detectors/acronym-parent-item.js +50 -0
- package/dist/detectors/acronym-parent-item.js.map +1 -0
- package/dist/detectors/broken-link.d.ts.map +1 -1
- package/dist/detectors/broken-link.js +14 -0
- package/dist/detectors/broken-link.js.map +1 -1
- package/dist/detectors/citation-sentence.d.ts +8 -0
- package/dist/detectors/citation-sentence.d.ts.map +1 -1
- package/dist/detectors/citation-sentence.js +24 -0
- package/dist/detectors/citation-sentence.js.map +1 -1
- package/dist/detectors/concrete-evidence-density.d.ts.map +1 -1
- package/dist/detectors/concrete-evidence-density.js +13 -6
- package/dist/detectors/concrete-evidence-density.js.map +1 -1
- package/dist/detectors/concrete-evidence.d.ts +7 -1
- package/dist/detectors/concrete-evidence.d.ts.map +1 -1
- package/dist/detectors/concrete-evidence.js +45 -0
- package/dist/detectors/concrete-evidence.js.map +1 -1
- package/dist/detectors/doubled-punctuation.d.ts.map +1 -1
- package/dist/detectors/doubled-punctuation.js +14 -4
- package/dist/detectors/doubled-punctuation.js.map +1 -1
- package/dist/detectors/en-shape.d.ts.map +1 -1
- package/dist/detectors/en-shape.js +8 -5
- package/dist/detectors/en-shape.js.map +1 -1
- package/dist/detectors/law-governed.d.ts +10 -0
- package/dist/detectors/law-governed.d.ts.map +1 -0
- package/dist/detectors/law-governed.js +41 -0
- package/dist/detectors/law-governed.js.map +1 -0
- package/dist/detectors/name-variant-two-things.d.ts +2 -1
- package/dist/detectors/name-variant-two-things.d.ts.map +1 -1
- package/dist/detectors/name-variant-two-things.js +9 -3
- package/dist/detectors/name-variant-two-things.js.map +1 -1
- package/dist/detectors/open-end-names.d.ts +7 -2
- package/dist/detectors/open-end-names.d.ts.map +1 -1
- package/dist/detectors/open-end-names.js +14 -4
- package/dist/detectors/open-end-names.js.map +1 -1
- package/dist/detectors/requirement-smell.d.ts.map +1 -1
- package/dist/detectors/requirement-smell.js +6 -2
- package/dist/detectors/requirement-smell.js.map +1 -1
- package/dist/detectors/sentence-length.js +5 -5
- package/dist/detectors/sentence-length.js.map +1 -1
- package/dist/detectors/sentence-parts.d.ts +2 -0
- package/dist/detectors/sentence-parts.d.ts.map +1 -1
- package/dist/detectors/sentence-parts.js +10 -0
- package/dist/detectors/sentence-parts.js.map +1 -1
- package/dist/detectors/signals.d.ts.map +1 -1
- package/dist/detectors/signals.js +3 -0
- package/dist/detectors/signals.js.map +1 -1
- package/dist/detectors/space-before-punctuation.js +3 -3
- package/dist/detectors/space-before-punctuation.js.map +1 -1
- package/dist/detectors/token-shape.d.ts.map +1 -1
- package/dist/detectors/token-shape.js +21 -5
- package/dist/detectors/token-shape.js.map +1 -1
- package/dist/detectors/topic-predicate-distance.d.ts.map +1 -1
- package/dist/detectors/topic-predicate-distance.js +3 -1
- package/dist/detectors/topic-predicate-distance.js.map +1 -1
- package/dist/detectors/unbalanced-bracket.d.ts.map +1 -1
- package/dist/detectors/unbalanced-bracket.js +31 -6
- package/dist/detectors/unbalanced-bracket.js.map +1 -1
- package/dist/detectors/vague-density.d.ts +4 -2
- package/dist/detectors/vague-density.d.ts.map +1 -1
- package/dist/detectors/vague-density.js +17 -9
- package/dist/detectors/vague-density.js.map +1 -1
- package/dist/document.js +2 -2
- package/dist/document.js.map +1 -1
- package/dist/name-variant-shapes.d.ts +22 -0
- package/dist/name-variant-shapes.d.ts.map +1 -1
- package/dist/name-variant-shapes.js +63 -0
- package/dist/name-variant-shapes.js.map +1 -1
- package/dist/paragraph-length.d.ts.map +1 -1
- package/dist/paragraph-length.js +5 -6
- package/dist/paragraph-length.js.map +1 -1
- package/dist/sentence-load.d.ts.map +1 -1
- package/dist/sentence-load.js +20 -6
- package/dist/sentence-load.js.map +1 -1
- package/dist/structure/definition-use.d.ts.map +1 -1
- package/dist/structure/definition-use.js +13 -0
- package/dist/structure/definition-use.js.map +1 -1
- package/dist/structure/universal.d.ts.map +1 -1
- package/dist/structure/universal.js +9 -1
- package/dist/structure/universal.js.map +1 -1
- package/dist/structure/unread-label.d.ts.map +1 -1
- package/dist/structure/unread-label.js +3 -1
- package/dist/structure/unread-label.js.map +1 -1
- package/dist/topic-clause.d.ts +2 -0
- package/dist/topic-clause.d.ts.map +1 -1
- package/dist/topic-clause.js.map +1 -1
- package/package.json +3 -3
- package/rules/adverb-overuse.yaml +2 -2
- package/rules/bracket-nesting.yaml +2 -2
- package/rules/broken-link.yaml +2 -2
- package/rules/concrete-evidence-density.yaml +2 -2
- package/rules/doubled-punctuation.yaml +2 -2
- package/rules/max-sentence-length.yaml +2 -2
- package/rules/name-variant.yaml +2 -2
- package/rules/no-doubled-joshi.yaml +2 -2
- package/rules/requirement-smell.yaml +2 -2
- package/rules/space-before-punctuation.yaml +4 -4
- package/rules/taigen-dome-in-prose.yaml +2 -2
- package/rules/topic-predicate-distance.yaml +3 -3
- package/rules/unbalanced-bracket.yaml +4 -4
- package/rules/undefined-acronym.yaml +2 -0
- package/rules/vague-word-density.yaml +2 -2
- package/src/bound-word-particles.ts +2 -1
- package/src/clause-particles.ts +35 -0
- package/src/detectors/acronym-definition.ts +14 -6
- package/src/detectors/acronym-expansion.ts +26 -7
- package/src/detectors/acronym-mention.ts +6 -1
- package/src/detectors/acronym-parent-item.ts +60 -0
- package/src/detectors/broken-link.ts +16 -0
- package/src/detectors/citation-sentence.ts +30 -0
- package/src/detectors/concrete-evidence-density.ts +18 -7
- package/src/detectors/concrete-evidence.ts +56 -1
- package/src/detectors/doubled-punctuation.ts +20 -4
- package/src/detectors/en-shape.ts +8 -5
- package/src/detectors/law-governed.ts +50 -0
- package/src/detectors/name-variant-two-things.ts +18 -3
- package/src/detectors/open-end-names.ts +21 -4
- package/src/detectors/requirement-smell.ts +6 -2
- package/src/detectors/sentence-length.ts +5 -5
- package/src/detectors/sentence-parts.ts +12 -0
- package/src/detectors/signals.ts +3 -0
- package/src/detectors/space-before-punctuation.ts +3 -3
- package/src/detectors/token-shape.ts +27 -7
- package/src/detectors/topic-predicate-distance.ts +3 -1
- package/src/detectors/unbalanced-bracket.ts +39 -7
- package/src/detectors/vague-density.ts +25 -10
- package/src/document.ts +2 -2
- package/src/name-variant-shapes.ts +74 -0
- package/src/paragraph-length.ts +8 -9
- package/src/sentence-load.ts +29 -6
- package/src/structure/definition-use.ts +15 -0
- package/src/structure/universal.ts +10 -1
- package/src/structure/unread-label.ts +3 -1
- package/src/topic-clause.ts +2 -0
|
@@ -42,12 +42,28 @@ const PAGE_ROMAN = /^x{0,3}(?:ix|iv|v?i{0,3})$/u;
|
|
|
42
42
|
|
|
43
43
|
const isPageNumber = (page: string): boolean => PAGE_DIGITS.test(page) || (page !== "" && PAGE_ROMAN.test(page));
|
|
44
44
|
|
|
45
|
-
/**
|
|
45
|
+
/** 点線の後ろの、揃えのための二つ以上の空白と次の欄(配役表の「NORA.. Emma Vernon」)。 */
|
|
46
|
+
const LEADER_THEN_GAP = /^[ \t]{2,}\S/u;
|
|
47
|
+
/** 同じ段落の、ほかの長い点線と揃えの空白(「MAURYA...... Honor Lavelle」)。 */
|
|
48
|
+
const LONG_LEADER_THEN_GAP = /\.{3}[ \t]{2,}\S/u;
|
|
49
|
+
|
|
50
|
+
const paragraphAround = (text: string, offset: number): string => {
|
|
51
|
+
const before = text.lastIndexOf("\n\n", offset);
|
|
52
|
+
const after = text.indexOf("\n\n", offset);
|
|
53
|
+
return text.slice(before === -1 ? 0 : before + 2, after === -1 ? text.length : after);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
/** 点線の後ろが揃えの空白で、同じ段落にほかの長い点線も揃えの空白に続く(点線で欄を揃えた表)。 */
|
|
57
|
+
const isLeaderColumn = (text: string, leaderEnd: number): boolean =>
|
|
58
|
+
LEADER_THEN_GAP.test(text.slice(leaderEnd)) && LONG_LEADER_THEN_GAP.test(paragraphAround(text, leaderEnd));
|
|
59
|
+
|
|
60
|
+
const isLeaderBefore = (text: string, leaderEnd: number): boolean =>
|
|
61
|
+
isPageNumber(LEADER_THEN_PAGE.exec(text.slice(leaderEnd))?.groups?.["page"] ?? "") || isLeaderColumn(text, leaderEnd);
|
|
62
|
+
|
|
63
|
+
/** 並びの終わりの点がページ番号の前の点線か、点線で欄を揃えた表の点線なら、その点は点線で、並びの印ではない。 */
|
|
46
64
|
const withoutLeader = (text: string, found: DoubledMarks): DoubledMarks => {
|
|
47
65
|
const kept = found.run.slice(0, Array.from(found.run).findLastIndex((mark) => mark !== ".") + 1);
|
|
48
|
-
return kept !== found.run &&
|
|
49
|
-
? { start: found.start, run: kept }
|
|
50
|
-
: found;
|
|
66
|
+
return kept !== found.run && isLeaderBefore(text, found.start + found.run.length) ? { start: found.start, run: kept } : found;
|
|
51
67
|
};
|
|
52
68
|
|
|
53
69
|
/**
|
|
@@ -21,12 +21,15 @@ const CAPITALISED = /^\p{Lu}\p{Ll}/u;
|
|
|
21
21
|
const LETTER = /\p{L}/u;
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
24
|
+
* 頭だけ大文字の -ly の語は、文の途中なら解析器が語尾で副詞と当てた名前(July、Kelly)か題の語、文頭でも行全体が題の書き方なら
|
|
25
|
+
* 題の語(Frequently Asked Questions)。どちらも動詞に畳める副詞ではない。書き手の確かさを言う副詞(probably、apparently)も、畳む先の動詞が無い。
|
|
26
26
|
*/
|
|
27
|
-
const lyAdverbsOf = (
|
|
27
|
+
const lyAdverbsOf = (sentence: Sentence, stance: ReadonlySet<string>): Token[] => {
|
|
28
|
+
const tokens = sentence.tokens ?? [];
|
|
28
29
|
const first = tokens.findIndex((token) => LETTER.test(token.surface));
|
|
29
|
-
|
|
30
|
+
const titleLine = isTitleCase(sentence.text) === true;
|
|
31
|
+
const isTitleWord = (token: Token, at: number): boolean => CAPITALISED.test(token.surface) && (at > first || titleLine);
|
|
32
|
+
return tokens.filter((token, at) => isLyAdverb(token) && !stance.has(token.surface.toLowerCase()) && !isTitleWord(token, at));
|
|
30
33
|
};
|
|
31
34
|
|
|
32
35
|
/**
|
|
@@ -36,7 +39,7 @@ const lyAdverbsOf = (tokens: readonly Token[], stance: ReadonlySet<string>): Tok
|
|
|
36
39
|
export const adverbDensity: Detector = (doc, options): Finding[] => {
|
|
37
40
|
const words = wordsIn(doc);
|
|
38
41
|
const stance = new Set((doc.lexicons["stance-adverb"] ?? []).map((entry) => entry.pattern));
|
|
39
|
-
const hits = doc.sentences.flatMap((sentence) => lyAdverbsOf(sentence
|
|
42
|
+
const hits = doc.sentences.flatMap((sentence) => lyAdverbsOf(sentence, stance).map((token) => ({ sentence, token })));
|
|
40
43
|
const density = words === 0 ? 0 : Math.round((hits.length / words) * PER);
|
|
41
44
|
const first = hits[0];
|
|
42
45
|
if (words < MIN_WORDS || first === undefined || density <= options.limit) return [];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { escapeRegExp } from "../orthography.ts";
|
|
2
|
+
import type { Lexicon } from "../plugin.ts";
|
|
3
|
+
|
|
4
|
+
// A vague word that a law or a section governs: "as required by applicable law", "as required under 45 CFR §164.528".
|
|
5
|
+
// What the law requires is fixed by the law it names, so the word leaves nothing for the reader to decide. Pure: the
|
|
6
|
+
// governing words (by, under) and the words that name a law (law, Act, §, HIPAA) come from the language's lexicon.
|
|
7
|
+
|
|
8
|
+
/** The lexicon's groups: the words that govern a law, and the words that name one. */
|
|
9
|
+
const GOVERNOR = "governor";
|
|
10
|
+
const REFERENCE = "reference";
|
|
11
|
+
/** How many words after the governing word may hold the law's name: "the Food Safety Modernization Act". */
|
|
12
|
+
const MAX_OBJECT_WORDS = 6;
|
|
13
|
+
/** Where the governed phrase ends: a clause's punctuation or a bracket. A full stop counts only before a space. */
|
|
14
|
+
const OBJECT_END = /[,;:()()、。\n]|\.(?=\s|$)/u;
|
|
15
|
+
|
|
16
|
+
export type LawReference = { readonly governors: readonly string[]; readonly references: readonly string[] };
|
|
17
|
+
|
|
18
|
+
export const NO_LAW_REFERENCE: LawReference = { governors: [], references: [] };
|
|
19
|
+
|
|
20
|
+
export const lawReferenceOf = (lexicon: Lexicon): LawReference => ({
|
|
21
|
+
governors: lexicon.filter((entry) => entry.group === GOVERNOR).map((entry) => entry.pattern.toLowerCase()),
|
|
22
|
+
references: lexicon.filter((entry) => entry.group === REFERENCE).map((entry) => entry.pattern.toLowerCase()),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/** A word written whole: no letter or digit right before it, no letter right after it (§164 holds §, laws does not hold law). */
|
|
26
|
+
const wholeWord = (word: string): RegExp => new RegExp(`(?<![\\p{L}\\p{N}])${escapeRegExp(word)}(?!\\p{L})`, "u");
|
|
27
|
+
|
|
28
|
+
/** The governing word the text opens with, and the text after it; undefined when it opens with none. */
|
|
29
|
+
const afterGovernor = (text: string, governors: readonly string[]): string | undefined => {
|
|
30
|
+
const lower = text.trimStart().toLowerCase();
|
|
31
|
+
const governor = governors.find((word) => word !== "" && new RegExp(`^${escapeRegExp(word)}(?!\\p{L})`, "u").test(lower));
|
|
32
|
+
return governor === undefined ? undefined : lower.slice(governor.length);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** The words the governing word takes, up to the clause's end or the next governing word ("the customer under the law" stops at under). */
|
|
36
|
+
const objectOf = (text: string, governors: readonly string[]): string => {
|
|
37
|
+
const end = text.search(OBJECT_END);
|
|
38
|
+
const words = (end === -1 ? text : text.slice(0, end)).split(/\s+/u).filter((word) => word !== "");
|
|
39
|
+
const starts = new Set(governors.map((governor) => governor.split(/\s+/u)[0] ?? ""));
|
|
40
|
+
const next = words.findIndex((word) => starts.has(word));
|
|
41
|
+
return (next === -1 ? words : words.slice(0, next)).slice(0, MAX_OBJECT_WORDS).join(" ");
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/** Whether the words right after end (an offset in text) govern the word by a law or a section: "… by applicable law". */
|
|
45
|
+
export const governedByLaw = (text: string, end: number, law: LawReference): boolean => {
|
|
46
|
+
const rest = afterGovernor(text.slice(end), law.governors);
|
|
47
|
+
if (rest === undefined) return false;
|
|
48
|
+
const object = objectOf(rest, law.governors);
|
|
49
|
+
return law.references.some((word) => word !== "" && wholeWord(word).test(object));
|
|
50
|
+
};
|
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
import type { ProseDocument, Span, Token } from "../plugin.ts";
|
|
2
2
|
import { escapeRegExp } from "../orthography.ts";
|
|
3
3
|
import { isKnownWord, type Speller } from "../spelling.ts";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
isAcronymPair,
|
|
6
|
+
isContractionPair,
|
|
7
|
+
isInFileName,
|
|
8
|
+
isInverseOrder,
|
|
9
|
+
isListedPair,
|
|
10
|
+
isNameAndWord,
|
|
11
|
+
isOtherEnding,
|
|
12
|
+
type WordEnding,
|
|
13
|
+
} from "../name-variant-shapes.ts";
|
|
5
14
|
import { titleSlipsIn } from "../name-title-slip.ts";
|
|
6
15
|
|
|
7
16
|
// name-variant が、似た二つの形を一つの名前の書き分けではなく別の二つと見る所。中黒で並べた二つの語、ある向きとその逆、語の別の
|
|
@@ -89,7 +98,8 @@ const inOneSentence = (sentences: readonly Span[], offset: number, other: number
|
|
|
89
98
|
/**
|
|
90
99
|
* A one-letter variant (near) that is two names: another form of the word (Texan), the usual form in the same sentence
|
|
91
100
|
* (Constantinus and Constantius, two sons), the differing word written twice or more (Bandle, of Richard Bandle and
|
|
92
|
-
* Mrs. Bandle),
|
|
101
|
+
* Mrs. Bandle), a word the document also writes in small letters (Stain, Vigil), two plural acronyms (SBLCs, SBDCs), or a
|
|
102
|
+
* word capitalised by its place (Sprang).
|
|
93
103
|
*/
|
|
94
104
|
const isNearPair = (reading: Reading, found: Found): boolean => {
|
|
95
105
|
const word = differingWord(found.name, found.usual);
|
|
@@ -97,13 +107,16 @@ const isNearPair = (reading: Reading, found: Found): boolean => {
|
|
|
97
107
|
isOtherEnding(found.name, found.usual, reading.endings) ||
|
|
98
108
|
offsetsOf(reading.text, found.usual).some((offset) => inOneSentence(reading.sentences, found.offset, offset)) ||
|
|
99
109
|
(word !== undefined && offsetsOf(reading.text, word).length >= 2) ||
|
|
110
|
+
isNameAndWord(found.name, found.usual, reading.text, (word) => reading.speller !== undefined && isKnownWord(word, reading.speller)) ||
|
|
111
|
+
isAcronymPair(found.name, found.usual) ||
|
|
100
112
|
isCapitalisedByPlace(found, reading.text, reading.sentenceStarts, reading.speller, reading.ordinary)
|
|
101
113
|
);
|
|
102
114
|
};
|
|
103
115
|
|
|
104
116
|
/**
|
|
105
117
|
* The finding is not two forms of one name: a one-letter variant that is two names (isNearPair), ・ listing two words, a
|
|
106
|
-
* direction and its inverse, a title quoted in capitals, a
|
|
118
|
+
* direction and its inverse, a title quoted in capitals, a contraction printed with and without its apostrophe, a piece of
|
|
119
|
+
* a file name or URL, a verb stem. ordinary holds the words read as names that are
|
|
107
120
|
* ordinary words in small letters (name-ordinary-word.ts).
|
|
108
121
|
*/
|
|
109
122
|
export const twoThingsIn = (
|
|
@@ -128,6 +141,8 @@ export const twoThingsIn = (
|
|
|
128
141
|
isListedPair(found.name, found.usual) ||
|
|
129
142
|
(found.kind === "order" && isInverseOrder(found.name, found.usual, reading.directions)) ||
|
|
130
143
|
isQuotedTitleCase(found, reading.text) ||
|
|
144
|
+
(found.kind === "spelling" && isContractionPair(found.name, found.usual, reading.text)) ||
|
|
145
|
+
isInFileName(reading.text, found.offset, found.name.length) ||
|
|
131
146
|
(found.kind === "reading" && isVerbStem(found, reading.tokenAt, reading.copulas));
|
|
132
147
|
};
|
|
133
148
|
|
|
@@ -52,6 +52,8 @@ export type OpenEndNames = {
|
|
|
52
52
|
readonly terms: ReadonlySet<string>;
|
|
53
53
|
/** The document's headings: a noun a heading names with the word (## 出願書類等の送付先) is the page's subject, not an open list. */
|
|
54
54
|
readonly headings: readonly string[];
|
|
55
|
+
/** Terms that law or usage fixes with the word (消費税等, 免許等), from the language's lexicon: no open list. */
|
|
56
|
+
readonly fixed: ReadonlySet<string>;
|
|
55
57
|
};
|
|
56
58
|
|
|
57
59
|
const attached = (left: Token | undefined, right: Token | undefined): boolean =>
|
|
@@ -145,13 +147,28 @@ const definedTerms = (doc: ProseDocument): Set<string> =>
|
|
|
145
147
|
: inDocumentOrder(doc.structure).flatMap((node) => (node.kind === "definition" && typeof node.attrs["term"] === "string" ? [node.attrs["term"]] : [])),
|
|
146
148
|
);
|
|
147
149
|
|
|
148
|
-
/**
|
|
149
|
-
|
|
150
|
+
/**
|
|
151
|
+
* The names a document gives that contain an open-end word. statements: the language's words of a definition (をいう, means);
|
|
152
|
+
* fixed: the terms law or usage fixes with the word (消費税等).
|
|
153
|
+
*/
|
|
154
|
+
export const openEndNames = (
|
|
155
|
+
doc: ProseDocument,
|
|
156
|
+
words: readonly LexiconEntry[],
|
|
157
|
+
statements: readonly string[],
|
|
158
|
+
fixed: readonly string[] = [],
|
|
159
|
+
): OpenEndNames => {
|
|
150
160
|
const occurrences = doc.sentences.flatMap((sentence) => words.flatMap((word) => occurrencesIn(sentence, word)));
|
|
151
161
|
const defined = occurrences.filter((occurrence) => definedInAside(doc.source, occurrence, statements)).map((occurrence) => nounOf(doc.source, occurrence));
|
|
152
162
|
const heads = occurrences.filter((occurrence) => headsItem(doc.source, occurrence, statements)).map((occurrence) => nounOf(doc.source, occurrence));
|
|
153
163
|
const headings = doc.sections.map((section) => section.heading).filter((heading) => heading.trim() !== "");
|
|
154
|
-
return {
|
|
164
|
+
return {
|
|
165
|
+
quoted: quotedNames(doc.source, words),
|
|
166
|
+
defined: new Set(defined),
|
|
167
|
+
heads: [...new Set(heads)],
|
|
168
|
+
terms: definedTerms(doc),
|
|
169
|
+
headings,
|
|
170
|
+
fixed: new Set(fixed),
|
|
171
|
+
};
|
|
155
172
|
};
|
|
156
173
|
|
|
157
174
|
/** Whether a quoted name holds this occurrence: the text around it reads the whole name (当該責任追及等 holds 「責任追及等」). */
|
|
@@ -183,7 +200,7 @@ const isNamed = (source: string, occurrence: Occurrence, names: OpenEndNames): b
|
|
|
183
200
|
inQuotedName(source, occurrence, names.quoted) ||
|
|
184
201
|
namedByHeading(source, occurrence, names.headings) ||
|
|
185
202
|
followsTerm(source, occurrence, names.terms) ||
|
|
186
|
-
partsHoldingWord(source, occurrence).some((part) => names.defined.has(part) || names.heads.some((head) => head.startsWith(part)));
|
|
203
|
+
partsHoldingWord(source, occurrence).some((part) => names.defined.has(part) || names.fixed.has(part) || names.heads.some((head) => head.startsWith(part)));
|
|
187
204
|
|
|
188
205
|
/** Whether the words around an occurrence bound its list; range: the tokens the word covers. */
|
|
189
206
|
export type BoundedAfter = (tokens: readonly Token[], range: TokenRange) => boolean;
|
|
@@ -20,6 +20,8 @@ const LOOPHOLE = "requirement-loophole";
|
|
|
20
20
|
const OPEN_QUESTION = "requirement-open-question";
|
|
21
21
|
/** The words of a condition whose bracketed examples an open list may give. */
|
|
22
22
|
const OPEN_CONDITION = "requirement-open-condition";
|
|
23
|
+
/** The terms law or usage fixes with an open-end word (消費税等), which close their own list. The language may have none. */
|
|
24
|
+
const FIXED_TERMS = "requirement-open-end-fixed-term";
|
|
23
25
|
const TIME = "time";
|
|
24
26
|
const CONCRETE_DEADLINE = "concrete-deadline";
|
|
25
27
|
|
|
@@ -77,7 +79,8 @@ const smellWords = (doc: ProseDocument, lexicon: string, genre: string | undefin
|
|
|
77
79
|
/**
|
|
78
80
|
* Where the word stands as a smell. An open-end word inside a name the document gives (業務執行取締役等), giving examples
|
|
79
81
|
* of a class named right after it (印刷等、二次的に利用), ending a list a condition, a permission or a prohibition holds
|
|
80
|
-
* (転載等をしてはならない),
|
|
82
|
+
* (転載等をしてはならない), ending a list a pointing word opens (当該改良、改善等), or part of a term law fixes (消費税等), is
|
|
83
|
+
* none; without tokens none of these can be told apart.
|
|
81
84
|
*/
|
|
82
85
|
const smellAt = (doc: ProseDocument, sentence: Sentence, entry: LexiconEntry, lexicon: string, names: () => OpenEndNames): number | undefined => {
|
|
83
86
|
if (!entryIn(sentence, entry) || boundedInTime(doc, sentence, entry, lexicon)) return undefined;
|
|
@@ -126,7 +129,8 @@ export const requirementSmell: Detector = (doc, options): Finding[] => {
|
|
|
126
129
|
const statements = (doc.lexicons[STATEMENTS] ?? []).map((entry) => entry.pattern);
|
|
127
130
|
// Read once, and only when a requirement has an open-end word to tell apart from a name.
|
|
128
131
|
const cache: { names?: OpenEndNames } = {};
|
|
129
|
-
const
|
|
132
|
+
const fixed = (doc.lexicons[FIXED_TERMS] ?? []).map((entry) => entry.pattern);
|
|
133
|
+
const names = (): OpenEndNames => (cache.names ??= openEndNames(doc, smellWords(doc, OPEN_END, options.genre), statements, fixed));
|
|
130
134
|
const onPurpose = scopeWordsOf(doc.lexicons[OPEN_END] ?? []).onPurpose;
|
|
131
135
|
const openItems = itemsOpenOnPurpose(doc.source, doc.sentences, doc.lists, onPurpose);
|
|
132
136
|
return doc.sentences.flatMap((sentence, index) => {
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { lengthOf, proseText } from "../measure.ts";
|
|
2
2
|
import type { Detector, DetectorOptions, Finding, LengthUnit, Sentence } from "../plugin.ts";
|
|
3
|
-
import { citationLead } from "./citation-sentence.ts";
|
|
3
|
+
import { authorListSaving, citationLead, withoutEarlierReference } from "./citation-sentence.ts";
|
|
4
4
|
import { isCitationString, wordsAroundCitation } from "./citation-string.ts";
|
|
5
5
|
import { isNameList } from "./name-list-sentence.ts";
|
|
6
6
|
import { pageMarkerPattern, pageMarkerSize } from "./page-marker.ts";
|
|
7
7
|
import { quotedSentenceParts } from "./quoted-sentences.ts";
|
|
8
8
|
import { isReproduced, reproducedSpans } from "./reproduced-text.ts";
|
|
9
|
-
import { colonQuotationParts, flattenedTableParts, isGrammarBlock, labelLineParts } from "./sentence-parts.ts";
|
|
9
|
+
import { colonQuotationParts, flattenedTableParts, isGrammarBlock, labelLineParts, parentheticalSentenceParts } from "./sentence-parts.ts";
|
|
10
10
|
import { isInListItem, isReferenceLine, isTitleLine, quotedTitleWords, remarkAfterTitle, termSeriesSaving } from "./sentence-title.ts";
|
|
11
11
|
|
|
12
12
|
type Measure = { readonly size: number; readonly limit: number; readonly variant?: string };
|
|
@@ -25,7 +25,7 @@ type SentenceWords = {
|
|
|
25
25
|
* Only where words are the unit: the shapes are read from capitals and spaces.
|
|
26
26
|
*/
|
|
27
27
|
const unwrittenWords = (sentence: Sentence, text: string, listItems: SentenceWords["listItems"]): number =>
|
|
28
|
-
termSeriesSaving(text) + (isInListItem(sentence.span, listItems) || isReferenceLine(text) ? quotedTitleWords(text) : 0);
|
|
28
|
+
termSeriesSaving(text) + authorListSaving(text) + (isInListItem(sentence.span, listItems) || isReferenceLine(text) ? quotedTitleWords(text) : 0);
|
|
29
29
|
|
|
30
30
|
const sizeOf = (text: string, unit: LengthUnit): number =>
|
|
31
31
|
unit === "word" ? text.split(/\s+/u).filter((word) => word.length > 0).length : text.replace(/\s+/gu, "").length;
|
|
@@ -43,7 +43,7 @@ const longestQuotedPart = (text: string, size: number, markers: RegExp | undefin
|
|
|
43
43
|
*/
|
|
44
44
|
const measureOf = (sentence: Sentence, unit: LengthUnit, options: DetectorOptions, words: SentenceWords): Measure => {
|
|
45
45
|
const lead = citationLead(proseText(sentence), words.citationSignals);
|
|
46
|
-
if (lead !== undefined) return { size: sizeOf(lead, unit), limit: options.limit };
|
|
46
|
+
if (lead !== undefined) return { size: sizeOf(withoutEarlierReference(lead), unit), limit: options.limit };
|
|
47
47
|
const embedded = sentence.embeddedLanguage;
|
|
48
48
|
const limit = embedded === undefined ? undefined : options.embeddedLimits?.[embedded.id];
|
|
49
49
|
if (embedded === undefined || limit === undefined) return { size: ownSize(sentence, unit, words), limit: options.limit };
|
|
@@ -56,7 +56,7 @@ const isUnwritten = (sentence: Sentence, text: string, unit: LengthUnit, words:
|
|
|
56
56
|
|
|
57
57
|
/** The parts measured one by one when the splitter ran several things into one sentence (label lines, table cells, a quotation). */
|
|
58
58
|
const partsOf = (sentence: Sentence, text: string): readonly string[] | undefined =>
|
|
59
|
-
labelLineParts(sentence.text) ?? flattenedTableParts(text) ?? colonQuotationParts(text);
|
|
59
|
+
labelLineParts(sentence.text) ?? flattenedTableParts(text) ?? colonQuotationParts(text) ?? parentheticalSentenceParts(text);
|
|
60
60
|
|
|
61
61
|
/** The length of a sentence in the document's language, without what the writer did not write as one. */
|
|
62
62
|
const ownSize = (sentence: Sentence, unit: LengthUnit, words: SentenceWords): number => {
|
|
@@ -78,6 +78,18 @@ export const colonQuotationParts = (text: string): readonly string[] | undefined
|
|
|
78
78
|
return nonEmpty([text.replace(COLON_QUOTATION, ":"), ...quotes]);
|
|
79
79
|
};
|
|
80
80
|
|
|
81
|
+
/**
|
|
82
|
+
* A sentence that opens the text inside parentheses and closes there ("(Don't bother … now.)"), then the next sentence: a
|
|
83
|
+
* splitter that does not cut after ".)" runs them together. Only at the start, where the splitter already cut before "(".
|
|
84
|
+
*/
|
|
85
|
+
const PARENTHETICAL_SENTENCE_HEAD = /^\([^()]*[.?!]\)(?=\s+["“]?\p{Lu})/u;
|
|
86
|
+
|
|
87
|
+
/** A parenthesised sentence and the sentence after it, measured apart. */
|
|
88
|
+
export const parentheticalSentenceParts = (text: string): readonly string[] | undefined => {
|
|
89
|
+
const head = PARENTHETICAL_SENTENCE_HEAD.exec(text)?.[0];
|
|
90
|
+
return head === undefined ? undefined : nonEmpty([head, text.slice(head.length)]);
|
|
91
|
+
};
|
|
92
|
+
|
|
81
93
|
/** A header cell closed by its unit in brackets (所有割合(%), 従業員数(人), 資本金(百万円)). */
|
|
82
94
|
const UNIT_HEADER = /[((](?:%|%|人|名|株|件|台|社|円|千円|百万円)[))]/gu;
|
|
83
95
|
/** Fewer unit headers than this are a sentence that names a unit or two, not a table's header row. */
|
package/src/detectors/signals.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { dateSpans, wordsOf } from "./structure.ts";
|
|
|
3
3
|
import { lexiconNotationOf, sourceNotAcronymSpans, unreportedAcronymSpans, type NotAcronymSpans } from "./acronym-context.ts";
|
|
4
4
|
import { titledCodeSpans } from "./titled-code-citation.ts";
|
|
5
5
|
import { abbreviates, expansionAt, termEntryAcronyms, type DefinitionWords, type ExpandedAt } from "./acronym-expansion.ts";
|
|
6
|
+
import { parentItemAcronyms } from "./acronym-parent-item.ts";
|
|
6
7
|
import { isExplained } from "./acronym-compound.ts";
|
|
7
8
|
import { statedAcronyms } from "./acronym-definition.ts";
|
|
8
9
|
import { isBracketed, isInHeading, isNamePart } from "./acronym-lone.ts";
|
|
@@ -268,12 +269,14 @@ export const undefinedAcronym: Detector = (doc, options): Finding[] => {
|
|
|
268
269
|
// 略語のすぐ後ろのダッシュで挟んだ同格(the VIX—a measure of …—)、同じ文の前に書いた名前(Small Business Administration … SBA)、
|
|
269
270
|
// 2 つ以上の番号で引く続き物の名前(BCP 78、BCP 14)も、略語の説明と読む。
|
|
270
271
|
// 文献一覧の見出し語([UNIX] The Open Group, "The Single UNIX Specification")と、見出しに番号で立てた書簡の続き物(### SR 26-8: …)も。
|
|
272
|
+
// 入れ子の箇条書きで、親の項目が書き出した名前(- No product owner: の下の - Unavailable PO)も。
|
|
271
273
|
const headings = doc.sections.map((section) => halfWidth(section.heading));
|
|
272
274
|
const counted = countedHitsOf(doc);
|
|
273
275
|
// ファイルの種類の名前としてだけ書いた大文字(the MAP files、MAPファイル)も、展開を待つ略語ではない。
|
|
274
276
|
const entries = new Set([
|
|
275
277
|
...fileTypeNames(counted.map(fileUseOf), patternsOf(doc, "file-noun")),
|
|
276
278
|
...termEntryAcronyms(doc.source),
|
|
279
|
+
...parentItemAcronyms(doc.source),
|
|
277
280
|
...stated,
|
|
278
281
|
...linkedListTerms(doc.source),
|
|
279
282
|
...spelled,
|
|
@@ -8,10 +8,10 @@ import { quoteAround } from "./quote-around.ts";
|
|
|
8
8
|
*/
|
|
9
9
|
const SPACED_MARK = /(?<=\p{L}[)\]}"'”’]?) (?=[.,;?!](?:[\s)\]"'”’]|$))/gu;
|
|
10
10
|
|
|
11
|
-
/** 空白で区切った点の並び(". . ."
|
|
12
|
-
const SPACED_DOTS = /^\. ?\./u;
|
|
11
|
+
/** 空白で区切った点の並び(". . .")。省略の印と、目次の点線。行の折り返しが点の間に入ってもよい("vibrations .\n. . I")。 */
|
|
12
|
+
const SPACED_DOTS = /^\.(?: |\r?\n)?\./u;
|
|
13
13
|
|
|
14
|
-
const DOTS_REACH =
|
|
14
|
+
const DOTS_REACH = 4;
|
|
15
15
|
|
|
16
16
|
const isSpacedEllipsis = (text: string, space: number): boolean => SPACED_DOTS.test(text.slice(space + 1, space + 1 + DOTS_REACH));
|
|
17
17
|
|
|
@@ -6,6 +6,7 @@ import type { Detector, Finding, ProseDocument, Sentence, Span, Token } from "..
|
|
|
6
6
|
import { countedTokens, uncountedWordsOf, type UncountedWords } from "../uncounted-particles.ts";
|
|
7
7
|
import { boundWordParticles } from "../bound-word-particles.ts";
|
|
8
8
|
import { setPhraseParticles } from "../set-phrase-particles.ts";
|
|
9
|
+
import { clauseEnds, determinerParticles, longestClausePart } from "../clause-particles.ts";
|
|
9
10
|
import { noteSentences } from "../note-paragraphs.ts";
|
|
10
11
|
import { labelledNoteSentences } from "../labelled-note-paragraphs.ts";
|
|
11
12
|
import { listLeadSentences } from "../list-lead-sentence.ts";
|
|
@@ -40,12 +41,20 @@ const clippedByForm = (doc: ProseDocument): ReadonlySet<Sentence> => {
|
|
|
40
41
|
]);
|
|
41
42
|
};
|
|
42
43
|
|
|
44
|
+
/** 問いを閉じる助詞の語彙表(か)。 */
|
|
45
|
+
const QUESTION_PARTICLE_LEXICON = "question-particle";
|
|
46
|
+
|
|
47
|
+
/** 最後の語の後ろに問いの助詞がある文(どのような見直しが必要か。)。問いで、名詞で止めた文ではない。 */
|
|
48
|
+
const asksAfter = (sentence: Sentence, last: Token, questionParticles: ReadonlySet<string>): boolean =>
|
|
49
|
+
(sentence.tokens ?? []).some((token) => token.span.start >= last.span.end && token.pos === "PART" && questionParticles.has(token.surface));
|
|
50
|
+
|
|
43
51
|
export const nounEnding: Detector = (doc, options): Finding[] => {
|
|
44
52
|
const clipped = clippedByForm(doc);
|
|
53
|
+
const questionParticles = new Set((doc.lexicons[QUESTION_PARTICLE_LEXICON] ?? []).map((entry) => entry.pattern));
|
|
45
54
|
const ended = doc.sentences.filter((sentence) => {
|
|
46
55
|
if (inside(sentence.span, doc.listSpans) || clipped.has(sentence) || !isClosed(sentence) || !hasParticle(sentence)) return false;
|
|
47
56
|
const last = lastContent(sentence);
|
|
48
|
-
return last !== undefined && NOMINAL.has(last.pos) && !isDependent(last);
|
|
57
|
+
return last !== undefined && NOMINAL.has(last.pos) && !isDependent(last) && !asksAfter(sentence, last, questionParticles);
|
|
49
58
|
});
|
|
50
59
|
const first = ended[0];
|
|
51
60
|
// 文書全体の数についての指摘なので、1 文ずつ出さない。13 件並べても直す順番が分からない。
|
|
@@ -121,12 +130,19 @@ const POSITION_LEXICON = "particle-run-position";
|
|
|
121
130
|
/** 助詞を挟んで一語のように読む決まった言い回し(おそれのある)。中の助詞は入れ子の輪に数えない。 */
|
|
122
131
|
const SET_PHRASE_LEXICON = "particle-run-set-phrase";
|
|
123
132
|
|
|
124
|
-
/**
|
|
125
|
-
|
|
133
|
+
/** どれを指すかだけを言う語(他の)。直後の「の」は名詞を名詞の中に入れない。 */
|
|
134
|
+
const DETERMINER_LEXICON = "particle-run-determiner";
|
|
126
135
|
|
|
127
|
-
/**
|
|
128
|
-
|
|
129
|
-
|
|
136
|
+
/** 前の語と組んで一つの名前になる語(皆様、下)、決まった言い回し(おそれのある)、どれを指すかだけを言う語(他)。 */
|
|
137
|
+
type Phrases = { readonly boundWords: readonly string[]; readonly setPhrases: readonly string[]; readonly determiners: readonly string[] };
|
|
138
|
+
|
|
139
|
+
/** 数えない助詞(番号をつなぐ「の」、敬って呼ぶ語や置き場所を言う語の前の「の」、決まった言い回しの中の「の」、他の)を除いた語。 */
|
|
140
|
+
const chainTokens = (tokens: readonly Token[], numbering: UncountedWords, { boundWords, setPhrases, determiners }: Phrases): readonly Token[] => {
|
|
141
|
+
const uncounted = new Set([
|
|
142
|
+
...boundWordParticles(tokens, numbering.particles, boundWords),
|
|
143
|
+
...setPhraseParticles(tokens, numbering.particles, setPhrases),
|
|
144
|
+
...determinerParticles(tokens, numbering.particles, determiners),
|
|
145
|
+
]);
|
|
130
146
|
const counted = countedTokens(tokens, numbering);
|
|
131
147
|
return uncounted.size === 0 ? counted : counted.filter((token) => !uncounted.has(token));
|
|
132
148
|
};
|
|
@@ -139,6 +155,7 @@ export const doubledParticle: Detector = (doc, options): Finding[] => {
|
|
|
139
155
|
const phrases: Phrases = {
|
|
140
156
|
boundWords: [HONORIFIC_GROUP_LEXICON, POSITION_LEXICON].flatMap((id) => (doc.lexicons[id] ?? []).map((entry) => entry.pattern)),
|
|
141
157
|
setPhrases: (doc.lexicons[SET_PHRASE_LEXICON] ?? []).map((entry) => entry.pattern),
|
|
158
|
+
determiners: (doc.lexicons[DETERMINER_LEXICON] ?? []).map((entry) => entry.pattern),
|
|
142
159
|
};
|
|
143
160
|
const prose = doc.prose ?? doc.source;
|
|
144
161
|
const namingMarkers = (doc.lexicons[NAMING_LEXICON] ?? []).map((entry) => entry.pattern);
|
|
@@ -155,7 +172,10 @@ export const doubledParticle: Detector = (doc, options): Finding[] => {
|
|
|
155
172
|
...namedLongForms(sentence.text, sentence.span.start, sentence.tokens ?? [], namingMarkers, continuesName),
|
|
156
173
|
...quotedNameSpans(sentence.text, sentence.span.start, names),
|
|
157
174
|
];
|
|
158
|
-
|
|
175
|
+
// 「の」で主語を示した節(番号のついた版)の前と後ろは別の句。どちらかの句の中で上限を超える連なりだけを、連なり全体で言う。
|
|
176
|
+
const ends = clauseEnds(tokens, nesting);
|
|
177
|
+
const isLong = (run: Run): boolean => run.tokens.length > options.limit && longestClausePart(run.tokens, ends) > options.limit;
|
|
178
|
+
const longRuns = (skipped: readonly Span[]): Run[] => runsOf(tokens, nesting, breaks, skipped).filter(isLong);
|
|
159
179
|
const found = longRuns(quoted);
|
|
160
180
|
// 文書の名前を読むのは長い連なりのある文だけ。名前は位置ごとに読むので、全部の文で読むと遅い。
|
|
161
181
|
const runs = found.length === 0 ? found : longRuns([...quoted, ...documentNameSpans(prose, sentence.span, doc.namedDocument)]);
|
|
@@ -6,6 +6,7 @@ import { overLimit, ownTokens } from "./sentence-load.ts";
|
|
|
6
6
|
|
|
7
7
|
const CLOSER_LEXICON = "topic-clause-close";
|
|
8
8
|
const ARTICLE_LEXICON = "statute-article-opener";
|
|
9
|
+
const ADVERB_LEXICON = "topic-adverb";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Whether the sentence is an article of a statute quoted in another kind of document (第7条 … in a manual): the law's wording,
|
|
@@ -19,7 +20,8 @@ const quotesArticle = (doc: ProseDocument, genre: string | undefined): ((sentenc
|
|
|
19
20
|
|
|
20
21
|
/** The distance from the sentence's topic (〜は) to the end, where its predicate is. Needs the language's tokens. */
|
|
21
22
|
export const topicPredicateDistance: Detector = (doc, options) => {
|
|
22
|
-
const
|
|
23
|
+
const patterns = (lexicon: string): string[] => (doc.lexicons[lexicon] ?? []).map((entry) => entry.pattern);
|
|
24
|
+
const words: TopicClauseWords = { closers: patterns(CLOSER_LEXICON), adverbs: patterns(ADVERB_LEXICON) };
|
|
23
25
|
const quoted = quotesArticle(doc, options.genre);
|
|
24
26
|
return overLimit((sentence) => (sentence.tokens === undefined || quoted(sentence) ? undefined : topicDistance(ownTokens(sentence), words)))(doc, options);
|
|
25
27
|
};
|
|
@@ -49,7 +49,8 @@ export type BracketProblem = {
|
|
|
49
49
|
readonly partner?: string;
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
/** continues は、行の頭で同じ引用符を開き直したときの、その前の開き(詩の引用は行ごとに開き直し、終わりで一度だけ閉じる)。 */
|
|
53
|
+
type Open = { readonly bracket: Bracket; readonly offset: number; readonly continues?: Open };
|
|
53
54
|
type Scan = { readonly open: Open[]; readonly problems: BracketProblem[] };
|
|
54
55
|
|
|
55
56
|
/** 半角の「「」」。幅の違いは hankaku-kana が指すので、全角と組んでも幅の違いとしては指さない。 */
|
|
@@ -78,6 +79,22 @@ const closeWith = (scan: Scan, text: string, bracket: Bracket, offset: number):
|
|
|
78
79
|
.forEach((inner) => scan.problems.push(unclosedOf(inner)));
|
|
79
80
|
if (opened !== undefined && isWidthSlip(opened.bracket, bracket))
|
|
80
81
|
scan.problems.push({ kind: "mismatch", offset, mark: bracket.close, partner: opened.bracket.open });
|
|
82
|
+
if (opened !== undefined && reopenedLines(opened) >= MIN_REOPENED_LINES) dropContinued(scan, opened.continues);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 行の頭で開き直した数がこれより少なければ、行の頭の引用符は中に引いた言葉(“I told her\n“go away” and left.”)と読む。
|
|
87
|
+
* 詩の引用は二行以上を続けて開き直す。
|
|
88
|
+
*/
|
|
89
|
+
const MIN_REOPENED_LINES = 2;
|
|
90
|
+
|
|
91
|
+
const reopenedLines = (entry: Open | undefined): number => (entry?.continues === undefined ? 0 : 1 + reopenedLines(entry.continues));
|
|
92
|
+
|
|
93
|
+
/** 閉じた引用が開き直しで続けてきた前の開きを、閉じたものとして外す。 */
|
|
94
|
+
const dropContinued = (scan: Scan, prior: Open | undefined): void => {
|
|
95
|
+
if (prior === undefined || scan.open.at(-1) !== prior) return;
|
|
96
|
+
scan.open.pop();
|
|
97
|
+
dropContinued(scan, prior.continues);
|
|
81
98
|
};
|
|
82
99
|
|
|
83
100
|
/** 段落(空行で区切った塊)の最初の字。 */
|
|
@@ -85,14 +102,29 @@ const PARAGRAPH_HEAD = /(?:^|\n[ \t\u3000]*\n)[ \t\u3000]*(?=\S)/gu;
|
|
|
85
102
|
|
|
86
103
|
const paragraphHeads = (text: string): ReadonlySet<number> => new Set([...text.matchAll(PARAGRAPH_HEAD)].map((match) => match.index + match[0].length));
|
|
87
104
|
|
|
105
|
+
/** 行の最初の字(字下げの後ろ)。 */
|
|
106
|
+
const LINE_HEAD = /(?:^|\n)[ \t\u3000]*(?=\S)/gu;
|
|
107
|
+
|
|
108
|
+
const lineHeads = (text: string): ReadonlySet<number> => new Set([...text.matchAll(LINE_HEAD)].map((match) => match.index + match[0].length));
|
|
109
|
+
|
|
110
|
+
type Heads = { readonly paragraphs: ReadonlySet<number>; readonly lines: ReadonlySet<number> };
|
|
111
|
+
|
|
88
112
|
/** 人の言葉を引く括弧。続く段落を同じ開きで始めて、引用が続くことを示す書き方がある。 */
|
|
89
113
|
const QUOTATIONS: ReadonlySet<string> = new Set(["quote", "kagi", "double-kagi"]);
|
|
90
114
|
|
|
91
|
-
|
|
115
|
+
/** 空行。詩の連や、詩の一つ一つの区切り。 */
|
|
116
|
+
const BLANK_LINE = /\n[ \t\u3000]*\n/u;
|
|
117
|
+
|
|
118
|
+
const openWith = (scan: Scan, text: string, bracket: Bracket, offset: number, heads: Heads): void => {
|
|
119
|
+
const top = scan.open.at(-1);
|
|
120
|
+
const reopens = QUOTATIONS.has(bracket.family) && top?.bracket === bracket;
|
|
92
121
|
// 段落をまたぐ台詞・引用(英語の小説)は、閉じずに次の段落を同じ開きで始める。前の開きはここで続く。
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
122
|
+
if (reopens && heads.paragraphs.has(offset)) scan.open.pop();
|
|
123
|
+
// 行の頭では、閉じるまで続きかどうか決められない。折り返した台詞の行の頭の開きは、中に引いた別の言葉のこともある。
|
|
124
|
+
// 続きと読むのは同じ連の中だけ。前の詩で閉じ忘れた開きを、次の詩の引用の続きにはしない。
|
|
125
|
+
const sameStanza = top !== undefined && !BLANK_LINE.test(text.slice(top.offset, offset));
|
|
126
|
+
const continues = reopens && sameStanza && !heads.paragraphs.has(offset) && heads.lines.has(offset) ? top : undefined;
|
|
127
|
+
scan.open.push(continues === undefined ? { bracket, offset } : { bracket, offset, continues });
|
|
96
128
|
};
|
|
97
129
|
|
|
98
130
|
/**
|
|
@@ -101,13 +133,13 @@ const openWith = (scan: Scan, bracket: Bracket, offset: number, opensParagraph:
|
|
|
101
133
|
*/
|
|
102
134
|
export const bracketProblems = (text: string): BracketProblem[] => {
|
|
103
135
|
const scan: Scan = { open: [], problems: [] };
|
|
104
|
-
const heads = paragraphHeads(text);
|
|
136
|
+
const heads: Heads = { paragraphs: paragraphHeads(text), lines: lineHeads(text) };
|
|
105
137
|
[...text.matchAll(MARKS)].forEach((match) => {
|
|
106
138
|
const quoteOpen = scan.open.some((entry) => entry.bracket.family === "quote");
|
|
107
139
|
if (isInchMark(text, match.index, quoteOpen) || isEmoticonMouth(text, match.index)) return;
|
|
108
140
|
const opening = BY_OPEN.get(match[0]);
|
|
109
141
|
const closing = BY_CLOSE.get(match[0]);
|
|
110
|
-
if (opening !== undefined) openWith(scan, opening, match.index, heads
|
|
142
|
+
if (opening !== undefined) openWith(scan, text, opening, match.index, heads);
|
|
111
143
|
else if (closing !== undefined) closeWith(scan, text, closing, match.index);
|
|
112
144
|
});
|
|
113
145
|
return [...scan.problems, ...scan.open.map(unclosedOf)].toSorted((left, right) => left.offset - right.offset);
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
// document's genre ("reasonably" in a contract, "adequately compensate" as the test for an injunction) is not vague
|
|
4
4
|
// there. The rule speaks once per sentence, about the document's density. Pure: the words come from the language's
|
|
5
5
|
// lexicons vague-word and vague-word-term-of-art, the genre from the options. The lines of a table of contents are the
|
|
6
|
-
// sections' names, not the writer's sentences, and are not read.
|
|
6
|
+
// sections' names, not the writer's sentences, and are not read. A word a law governs ("as required by applicable law")
|
|
7
|
+
// is fixed by that law; the governing words and the law's names come from the lexicon vague-word-law-reference.
|
|
7
8
|
import { entriesForGenre } from "../lexicon-genres.ts";
|
|
8
9
|
import { contentsLineNumbers } from "../structure/contents-lines.ts";
|
|
9
10
|
import { lineNumberAt, linesOf } from "../structure/lines.ts";
|
|
@@ -12,10 +13,12 @@ import type { Detector, Finding, Lexicon, LexiconEntry, ProseDocument, Sentence
|
|
|
12
13
|
import { comparableText, comparableWords, entryRanges } from "./lexicon-match.ts";
|
|
13
14
|
import { rateOver } from "./lexicon.ts";
|
|
14
15
|
import { specifiedByBracket } from "./bracket-specified.ts";
|
|
16
|
+
import { governedByLaw, lawReferenceOf, NO_LAW_REFERENCE, type LawReference } from "./law-governed.ts";
|
|
15
17
|
|
|
16
18
|
const TERMS_OF_ART = "vague-word-term-of-art";
|
|
17
19
|
/** The words that start an aside giving a name (以下「HR」という, hereinafter): such an aside pins nothing down. */
|
|
18
20
|
const NAME_MARKERS = "definition-marker";
|
|
21
|
+
const LAW_REFERENCE = "vague-word-law-reference";
|
|
19
22
|
|
|
20
23
|
type Place = { readonly start: number; readonly end: number };
|
|
21
24
|
|
|
@@ -42,17 +45,28 @@ const covers = (outer: Place, inner: Place): boolean => outer.start <= inner.sta
|
|
|
42
45
|
|
|
43
46
|
const isLonger = (outer: Match, inner: Match): boolean => outer.place.end - outer.place.start > inner.place.end - inner.place.start;
|
|
44
47
|
|
|
45
|
-
/**
|
|
46
|
-
const
|
|
47
|
-
byTokens
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
/** The text the match stands in and where it ends there: token places are in the document, text places in the comparable text. */
|
|
49
|
+
const textAround = (sentence: Sentence, match: Match, byTokens: boolean): { readonly text: string; readonly end: number } =>
|
|
50
|
+
byTokens ? { text: sentence.text, end: match.place.end - sentence.span.start } : { text: comparableText(sentence), end: match.place.end };
|
|
51
|
+
|
|
52
|
+
/** Whether the words right after the match pin the word down: a bracket that says what it leaves open, or a law that governs it. */
|
|
53
|
+
const pinnedDown = (sentence: Sentence, match: Match, byTokens: boolean, nameMarkers: readonly string[], law: LawReference): boolean => {
|
|
54
|
+
const { text, end } = textAround(sentence, match, byTokens);
|
|
55
|
+
return specifiedByBracket(text, end, nameMarkers) || governedByLaw(text, end, law);
|
|
56
|
+
};
|
|
50
57
|
|
|
51
58
|
/**
|
|
52
59
|
* The vague words of one sentence, in the order written, each once: none inside a longer vague word or a term of art,
|
|
53
|
-
* and none pinned down by the bracket right after it (一定の事項(…))
|
|
60
|
+
* and none pinned down by the bracket right after it (一定の事項(…)) or by a law that governs it (as required by law).
|
|
61
|
+
* nameMarkers: the words that start a name in an aside.
|
|
54
62
|
*/
|
|
55
|
-
export const vagueWordsIn = (
|
|
63
|
+
export const vagueWordsIn = (
|
|
64
|
+
sentence: Sentence,
|
|
65
|
+
vague: Lexicon,
|
|
66
|
+
termsOfArt: Lexicon,
|
|
67
|
+
nameMarkers: readonly string[] = [],
|
|
68
|
+
law: LawReference = NO_LAW_REFERENCE,
|
|
69
|
+
): string[] => {
|
|
56
70
|
const byTokens = readsTokens(sentence, [...vague, ...termsOfArt]);
|
|
57
71
|
const matches = matchesOf(sentence, vague, byTokens);
|
|
58
72
|
const terms = matchesOf(sentence, termsOfArt, byTokens);
|
|
@@ -60,7 +74,7 @@ export const vagueWordsIn = (sentence: Sentence, vague: Lexicon, termsOfArt: Lex
|
|
|
60
74
|
(match) =>
|
|
61
75
|
!matches.some((other) => isLonger(other, match) && covers(other.place, match.place)) &&
|
|
62
76
|
!terms.some((term) => covers(term.place, match.place)) &&
|
|
63
|
-
!pinnedDown(sentence, match, byTokens, nameMarkers),
|
|
77
|
+
!pinnedDown(sentence, match, byTokens, nameMarkers, law),
|
|
64
78
|
);
|
|
65
79
|
return [...new Set(kept.toSorted((left, right) => left.place.start - right.place.start).map((match) => match.pattern))];
|
|
66
80
|
};
|
|
@@ -78,10 +92,11 @@ const contentsSentences = (doc: ProseDocument): ReadonlySet<Sentence> => {
|
|
|
78
92
|
export const vagueDensity: Detector = (doc, options): Finding[] => {
|
|
79
93
|
const termsOfArt = entriesForGenre(doc.lexicons[TERMS_OF_ART] ?? [], options.genre);
|
|
80
94
|
const nameMarkers = (doc.lexicons[NAME_MARKERS] ?? []).map((entry) => entry.pattern);
|
|
95
|
+
const law = lawReferenceOf(doc.lexicons[LAW_REFERENCE] ?? []);
|
|
81
96
|
const contents = contentsSentences(doc);
|
|
82
97
|
const hits: SentenceHits[] = doc.sentences
|
|
83
98
|
.filter((sentence) => !contents.has(sentence))
|
|
84
|
-
.map((sentence) => ({ sentence, words: vagueWordsIn(sentence, options.lexicon ?? [], termsOfArt, nameMarkers) }))
|
|
99
|
+
.map((sentence) => ({ sentence, words: vagueWordsIn(sentence, options.lexicon ?? [], termsOfArt, nameMarkers, law) }))
|
|
85
100
|
.filter((hit) => hit.words.length > 0);
|
|
86
101
|
const count = hits.reduce((sum, hit) => sum + hit.words.length, 0);
|
|
87
102
|
const density = rateOver(doc, count, options.limit);
|