chaffjs 0.12.0 → 0.13.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/detectors/acronym-context.d.ts +15 -3
- package/dist/detectors/acronym-context.d.ts.map +1 -1
- package/dist/detectors/acronym-context.js +18 -32
- package/dist/detectors/acronym-context.js.map +1 -1
- package/dist/detectors/acronym-expansion.d.ts.map +1 -1
- package/dist/detectors/acronym-expansion.js +33 -6
- 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 +20 -34
- package/dist/detectors/char-shape.js.map +1 -1
- package/dist/detectors/orthography.d.ts.map +1 -1
- package/dist/detectors/orthography.js +17 -1
- package/dist/detectors/orthography.js.map +1 -1
- package/dist/detectors/place-run.d.ts +8 -0
- package/dist/detectors/place-run.d.ts.map +1 -0
- package/dist/detectors/place-run.js +54 -0
- package/dist/detectors/place-run.js.map +1 -0
- package/dist/detectors/register.d.ts +32 -0
- package/dist/detectors/register.d.ts.map +1 -0
- package/dist/detectors/register.js +62 -0
- package/dist/detectors/register.js.map +1 -0
- package/dist/detectors/sentence-ending.d.ts.map +1 -1
- package/dist/detectors/sentence-ending.js +17 -28
- package/dist/detectors/sentence-ending.js.map +1 -1
- package/dist/detectors/signals.d.ts.map +1 -1
- package/dist/detectors/signals.js +13 -6
- package/dist/detectors/signals.js.map +1 -1
- package/dist/number-name.d.ts +9 -0
- package/dist/number-name.d.ts.map +1 -0
- package/dist/number-name.js +57 -0
- package/dist/number-name.js.map +1 -0
- package/dist/orthography.d.ts +6 -2
- package/dist/orthography.d.ts.map +1 -1
- package/dist/orthography.js +78 -17
- package/dist/orthography.js.map +1 -1
- package/package.json +3 -3
- package/rules/latin-spacing.yaml +4 -2
- package/rules/max-kanji-continuous.yaml +2 -0
- package/rules/undefined-acronym.yaml +1 -0
- package/src/detectors/acronym-context.ts +30 -33
- package/src/detectors/acronym-expansion.ts +34 -6
- package/src/detectors/char-shape.ts +12 -30
- package/src/detectors/orthography.ts +20 -2
- package/src/detectors/place-run.ts +63 -0
- package/src/detectors/register.ts +83 -0
- package/src/detectors/sentence-ending.ts +24 -28
- package/src/detectors/signals.ts +15 -6
- package/src/number-name.ts +68 -0
- package/src/orthography.ts +76 -15
|
@@ -4,10 +4,11 @@ import { escapeRegExp } from "../orthography.ts";
|
|
|
4
4
|
* 展開は略語の**すぐ隣**にあるときだけ認める。
|
|
5
5
|
* 60 文字も見ると、同じ文のどこかに括弧があるだけで「説明済み」になり、1 件も出なくなる。
|
|
6
6
|
*
|
|
7
|
-
*
|
|
7
|
+
* 認めるのは次の形。どれも実際によく書かれる。
|
|
8
8
|
* CI(継続的インテグレーション) 略語のあとに括弧
|
|
9
9
|
* Continuous Integration (CI) 括弧の中が略語
|
|
10
10
|
* Tax Cuts and Jobs Act [TCJA] 角括弧の中が略語で、直前の語の頭文字と揃う
|
|
11
|
+
* Relief Act (RA; P.L. 112-240) 括弧の最初の項目が略語で、直前の語か区切りの後の語の頭文字と揃う
|
|
11
12
|
* 人事部(以下「HR」という。) 括弧の中が定義の語と略語だけ
|
|
12
13
|
* 括弧と略語の間には、引用符((“MNDA”)、(「MNDA」))と空白だけを許す。
|
|
13
14
|
*/
|
|
@@ -17,6 +18,10 @@ const CLOSES = new RegExp(String.raw`^${WRAP}[))]`, "u");
|
|
|
17
18
|
const OPENED = new RegExp(String.raw`[((]${WRAP}$`, "u");
|
|
18
19
|
const SQUARE_CLOSES = new RegExp(String.raw`^${WRAP}\]`, "u");
|
|
19
20
|
const SQUARE_OPENED = new RegExp(String.raw`\[${WRAP}$`, "u");
|
|
21
|
+
const ANY_OPENED = new RegExp(String.raw`[(([]${WRAP}$`, "u");
|
|
22
|
+
/** 括弧の最初の項目のあとの区切りと、括弧が閉じるまでの残り((ARRA; P.L. 111-5)、(OP、Originator Profile))。 */
|
|
23
|
+
const SEPARATED = new RegExp(String.raw`^${WRAP}[;;,,、](?<rest>[^()()]*)`, "u");
|
|
24
|
+
const QUOTES = /["“”'‘’「」『』]/gu;
|
|
20
25
|
|
|
21
26
|
/** 括弧と略語の間の幅。空白は 1 つに畳んであるので、(“ MNDA ”) まで収まる。 */
|
|
22
27
|
const NEAR = 3;
|
|
@@ -31,15 +36,37 @@ const MARKER_GAP = String.raw`(?![A-Za-z])[\s、,]*`;
|
|
|
31
36
|
* 角括弧は引用の印にも使う([IANA]、[1])。直前の語のうち大文字で始まる語の頭文字が
|
|
32
37
|
* 略語と揃うときだけ展開と見なす。見る語は略語の文字数の 2 倍まで(and や of を挟むため)。
|
|
33
38
|
*/
|
|
39
|
+
const lettersOf = (acronym: string): string => acronym.replaceAll("&", "");
|
|
40
|
+
|
|
41
|
+
const initialsOf = (words: readonly string[]): string =>
|
|
42
|
+
words
|
|
43
|
+
.filter((word) => /^[A-Z]/u.test(word))
|
|
44
|
+
.map((word) => word.charAt(0))
|
|
45
|
+
.join("");
|
|
46
|
+
|
|
34
47
|
const spellsOut = (before: string, acronym: string): boolean => {
|
|
35
|
-
const letters = acronym
|
|
48
|
+
const letters = lettersOf(acronym);
|
|
36
49
|
const words = before
|
|
37
|
-
.replace(
|
|
50
|
+
.replace(ANY_OPENED, "")
|
|
38
51
|
.trim()
|
|
39
52
|
.split(/\s+/u)
|
|
40
53
|
.slice(-letters.length * 2);
|
|
41
|
-
|
|
42
|
-
|
|
54
|
+
return initialsOf(words).endsWith(letters);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 括弧の最初の項目が略語で、区切りのあとに注記が続く形((ARRA; P.L. 111-5)、(OP、Originator Profile))。
|
|
59
|
+
* 同じ形で列挙も書く((MR, handbook, etc.)、(EPA, FDIC, GSA))ので、直前の語か、区切りから括弧が閉じるまでの語の
|
|
60
|
+
* 頭文字が略語と揃うときだけ展開と見なす。区切りの後ろは閉じた括弧の中だけを見て、頭文字がちょうど略語になることを求める。
|
|
61
|
+
*/
|
|
62
|
+
const isSeparatedAt = (body: string, acronym: string, at: number): boolean => {
|
|
63
|
+
if (!OPENED.test(body.slice(Math.max(0, at - NEAR), at))) return false;
|
|
64
|
+
const separated = SEPARATED.exec(body.slice(at + acronym.length, at + acronym.length + DEFINITION_REACH));
|
|
65
|
+
if (separated === null) return false;
|
|
66
|
+
const rest = separated.groups?.["rest"] ?? "";
|
|
67
|
+
const end = at + acronym.length + separated[0].length;
|
|
68
|
+
const closed = CLOSES.test(body.slice(end, end + NEAR));
|
|
69
|
+
return spellsOut(body.slice(0, at), acronym) || (closed && initialsOf(rest.replace(QUOTES, " ").trim().split(/\s+/u)) === lettersOf(acronym));
|
|
43
70
|
};
|
|
44
71
|
|
|
45
72
|
/** 言語パッケージの語彙表から読む、括弧の中で略語の前に書く語(以下、hereinafter)と後ろに書く語(という)。 */
|
|
@@ -69,7 +96,8 @@ const isBracketedAt = (body: string, acronym: string, at: number): boolean => {
|
|
|
69
96
|
const after = body.slice(at + acronym.length, at + acronym.length + NEAR);
|
|
70
97
|
const before = body.slice(Math.max(0, at - NEAR), at);
|
|
71
98
|
if (OPENS.test(after) || (OPENED.test(before) && CLOSES.test(after))) return true;
|
|
72
|
-
|
|
99
|
+
if (SQUARE_OPENED.test(before) && SQUARE_CLOSES.test(after) && spellsOut(body.slice(0, at), acronym)) return true;
|
|
100
|
+
return isSeparatedAt(body, acronym, at);
|
|
73
101
|
};
|
|
74
102
|
|
|
75
103
|
export type ExpandedAt = (body: string, acronym: string, at: number) => boolean;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type { Detector, DocumentProfile, Finding, Sentence
|
|
1
|
+
import type { Detector, DocumentProfile, Finding, Sentence } from "../plugin.ts";
|
|
2
2
|
import { compacted, placeOf } from "./gram-place.ts";
|
|
3
3
|
import { maskAddresses } from "../address-chain.ts";
|
|
4
|
+
import { isAddressRun } from "./place-run.ts";
|
|
4
5
|
import { proseText } from "../measure.ts";
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -9,50 +10,30 @@ import { proseText } from "../measure.ts";
|
|
|
9
10
|
*/
|
|
10
11
|
const KANJI_RUN = /[一-鿿]+/gu;
|
|
11
12
|
|
|
12
|
-
/**
|
|
13
|
-
const
|
|
14
|
-
const isGeo = (token: Token | undefined): boolean => isGeoName(token) || token?.features?.["NameType"] === "GeoUnit";
|
|
15
|
-
const isNumber = (token: Token | undefined): boolean => token?.features?.["NumType"] === "Card";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 住所の語: 地名と地名の単位(NameType=Geo / GeoUnit)、数(NumType=Card)、数のすぐ後ろの助数詞(丁目、NounType=Class)。
|
|
19
|
-
* 辞書に無い町名(新橋)は、地名の後ろで数の前に来るときだけ住所の一部とする。
|
|
20
|
-
*/
|
|
21
|
-
const isPlacePart = (tokens: readonly Token[], index: number): boolean =>
|
|
22
|
-
isGeo(tokens[index]) ||
|
|
23
|
-
isNumber(tokens[index]) ||
|
|
24
|
-
(isNumber(tokens[index - 1]) && tokens[index]?.features?.["NounType"] === "Class") ||
|
|
25
|
-
(isGeo(tokens[index - 1]) && isNumber(tokens[index + 1]));
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 漢字の連なりが住所か(東京都港区新橋二丁目)。形態素解析が地名と読んだ語で始まり、住所の語だけでできている。
|
|
29
|
-
* 住所は決まった形で、ひらがなを挟んで書けない。組織名(日本経済団体連合会)は地名の後ろに普通の語が続くので住所ではない。品詞が無ければ判定しない。
|
|
30
|
-
*/
|
|
31
|
-
const isPlaceName = (sentence: Sentence, run: string): boolean => {
|
|
13
|
+
/** 漢字の連なりが住所か(東京都港区新橋二丁目)。形態素解析の地名と数で決める。品詞が無ければ判定しない。 */
|
|
14
|
+
const isPlaceName = (sentence: Sentence, run: string, topUnits: ReadonlySet<string>): boolean => {
|
|
32
15
|
const tokens = sentence.tokens;
|
|
33
16
|
const place = tokens === undefined ? undefined : placeOf(compacted(sentence.text, "word"), run);
|
|
34
17
|
if (tokens === undefined || place === undefined) return false;
|
|
35
18
|
const [start, end] = [sentence.span.start + place.start, sentence.span.start + place.end];
|
|
36
19
|
const covering = tokens.flatMap((token, index) => (token.span.start < end && start < token.span.end ? [index] : []));
|
|
37
|
-
|
|
38
|
-
// 地名が単位を挟まずに続けば(東京大阪名古屋福岡)、住所ではなく地名の並び。
|
|
39
|
-
const listed = covering.some((index) => isGeoName(tokens[index]) && isGeoName(tokens[index + 1]));
|
|
40
|
-
return first !== undefined && isGeo(tokens[first]) && !listed && covering.every((index) => isPlacePart(tokens, index));
|
|
20
|
+
return isAddressRun(tokens, covering, topUnits);
|
|
41
21
|
};
|
|
42
22
|
|
|
43
|
-
const longestKanji = (sentence: Sentence, profile: DocumentProfile | undefined): string =>
|
|
23
|
+
const longestKanji = (sentence: Sentence, profile: DocumentProfile | undefined, topUnits: ReadonlySet<string>): string =>
|
|
44
24
|
[...maskAddresses(proseText(sentence), profile).matchAll(KANJI_RUN)]
|
|
45
25
|
.map((match) => match[0])
|
|
46
|
-
.filter((run) => !isPlaceName(sentence, run))
|
|
26
|
+
.filter((run) => !isPlaceName(sentence, run, topUnits))
|
|
47
27
|
.reduce((longest, run) => (run.length > longest.length ? run : longest), "");
|
|
48
28
|
|
|
49
29
|
/**
|
|
50
30
|
* 漢字が続くと、どこで語が切れるのか読み手が探すことになる。
|
|
51
31
|
* 「情報処理推進機構認定試験」は 12 字。ひらがなを 1 つ挟むだけで読める。
|
|
52
32
|
*/
|
|
53
|
-
export const kanjiRun: Detector = (doc, options): Finding[] =>
|
|
54
|
-
doc.
|
|
55
|
-
|
|
33
|
+
export const kanjiRun: Detector = (doc, options): Finding[] => {
|
|
34
|
+
const topUnits = new Set((doc.lexicons["prefecture-unit"] ?? []).map((entry) => entry.pattern));
|
|
35
|
+
return doc.sentences
|
|
36
|
+
.map((sentence) => ({ sentence, run: longestKanji(sentence, doc.profile, topUnits) }))
|
|
56
37
|
.filter(({ run }) => run.length > options.limit)
|
|
57
38
|
.map(({ sentence, run }) => ({
|
|
58
39
|
rule: "max-kanji-continuous",
|
|
@@ -62,6 +43,7 @@ export const kanjiRun: Detector = (doc, options): Finding[] =>
|
|
|
62
43
|
quote: sentence.text.trim(),
|
|
63
44
|
values: { word: run, count: run.length, limit: options.limit, offset: sentence.span.start },
|
|
64
45
|
}));
|
|
46
|
+
};
|
|
65
47
|
|
|
66
48
|
const MIDDLE_DOT = /・/gu;
|
|
67
49
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Detector, Finding, Sentence } from "../plugin.ts";
|
|
2
|
-
import { latinBoundaries, minorityStyle, occurrencesOutside, type SpacingKind } from "../orthography.ts";
|
|
2
|
+
import { latinBoundaries, minorityStyle, occurrencesOutside, type Boundary, type SpacingKind } from "../orthography.ts";
|
|
3
|
+
import { digitRunAround, isNumberName } from "../number-name.ts";
|
|
3
4
|
|
|
4
5
|
/** チームが chaff.yaml の prefer に書いた「使わない書き方」。書いていなければ何も言わない。 */
|
|
5
6
|
export const preferredTerm: Detector = (doc, options): Finding[] => {
|
|
@@ -23,12 +24,29 @@ const KINDS: readonly SpacingKind[] = ["letter", "before-digit", "after-digit"];
|
|
|
23
24
|
|
|
24
25
|
type Located = { readonly sentence: Sentence; readonly offset: number; readonly kind: SpacingKind; readonly spaced: boolean };
|
|
25
26
|
|
|
27
|
+
/** 境目の隣の数字の、並びの中の 1 字の位置。数字の後ろなら左の字、数字の前なら(空白を越えた)右の字。 */
|
|
28
|
+
const digitBeside = (boundary: Boundary): number => {
|
|
29
|
+
if (boundary.kind === "after-digit") return boundary.offset - 1;
|
|
30
|
+
return boundary.spaced ? boundary.offset + 1 : boundary.offset;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/** 番号・識別子として書かれた数の境目は、空け方の好みではないので数えない(number-name.ts)。 */
|
|
34
|
+
const isCounted = (sentence: Sentence, boundary: Boundary): boolean => {
|
|
35
|
+
if (boundary.kind === "letter") return true;
|
|
36
|
+
const run = digitRunAround(sentence.text, digitBeside(boundary));
|
|
37
|
+
return run === undefined || !isNumberName(sentence.text, run, sentence.tokens, sentence.span.start);
|
|
38
|
+
};
|
|
39
|
+
|
|
26
40
|
/**
|
|
27
41
|
* 日本語と英字・数字のあいだを、空けるか詰めるか。文書の中で混ざっていたら、少ないほうを指摘する。
|
|
28
42
|
* どちらが正しいかは決めない。決めるのはチームで、chaff はそろっているかだけを見る。
|
|
29
43
|
*/
|
|
30
44
|
export const latinSpacing: Detector = (doc, options): Finding[] => {
|
|
31
|
-
const located: Located[] = doc.sentences.flatMap((sentence) =>
|
|
45
|
+
const located: Located[] = doc.sentences.flatMap((sentence) =>
|
|
46
|
+
latinBoundaries(sentence.text, doc.source.slice(sentence.span.start, sentence.span.end))
|
|
47
|
+
.filter((boundary) => isCounted(sentence, boundary))
|
|
48
|
+
.map((boundary) => ({ sentence, ...boundary })),
|
|
49
|
+
);
|
|
32
50
|
return KINDS.flatMap((kind) => {
|
|
33
51
|
const ofKind = located.filter((entry) => entry.kind === kind);
|
|
34
52
|
const minority = minorityStyle(ofKind);
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
/** 地名(Geo)と、地名に付く単位(GeoUnit: 都・県・郡・市・区・町・村)。 */
|
|
4
|
+
const isGeoName = (token: Token | undefined): boolean => token?.features?.["NameType"] === "Geo";
|
|
5
|
+
const isGeoUnit = (token: Token | undefined): boolean => token?.features?.["NameType"] === "GeoUnit";
|
|
6
|
+
const isGeo = (token: Token | undefined): boolean => isGeoName(token) || isGeoUnit(token);
|
|
7
|
+
const isNumber = (token: Token | undefined): boolean => token?.features?.["NumType"] === "Card";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 辞書に無い町名は、形態素解析が固有名詞か 1 字ずつに割る(紀美野町は 紀+美野、南伊勢町は 南+伊勢、久米島町は固有名詞,一般)。
|
|
11
|
+
* 2 字以上の普通の語(政策・総務)は地名のかけらにしない。
|
|
12
|
+
*/
|
|
13
|
+
const isNamePiece = (token: Token | undefined): boolean => token !== undefined && (token.pos === "PROPN" || [...token.surface].length === 1);
|
|
14
|
+
|
|
15
|
+
/** 連なりの語と、閉じても 1 つの地名にならない単位(都道府県。住所の先頭にしか来ない。語彙表 prefecture-unit)。 */
|
|
16
|
+
type Run = { readonly tokens: readonly Token[]; readonly last: number; readonly topUnits: ReadonlySet<string> };
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* index から地名のかけらだけを読み進めて、連なりの終わりまでに都道府県より下の単位(郡・市・町)で閉じるか。
|
|
20
|
+
* 閉じれば、そこまでが 1 つの地名。連なりの外(「に」「の」も 1 字)までは読まない。
|
|
21
|
+
*/
|
|
22
|
+
const closedByUnit = (run: Run, index: number): boolean => {
|
|
23
|
+
const token = run.tokens[index];
|
|
24
|
+
if (index > run.last || token === undefined) return false;
|
|
25
|
+
if (isGeoUnit(token)) return !run.topUnits.has(token.surface);
|
|
26
|
+
return isNamePiece(token) && closedByUnit(run, index + 1);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 住所の語: 地名と地名の単位(NameType=Geo / GeoUnit)、数(NumType=Card)、数のすぐ後ろの助数詞(丁目、NounType=Class)。
|
|
31
|
+
* 辞書に無い町名(新橋)は、地名の後ろで数の前に来るときだけ住所の一部とする。
|
|
32
|
+
* 割られた町名(紀美野町)は、単位の後ろで、地名のかけらだけを挟んで次の単位で閉じるときだけ住所の一部とする。
|
|
33
|
+
*/
|
|
34
|
+
const isPlacePart = (run: Run, index: number): boolean => {
|
|
35
|
+
const tokens = run.tokens;
|
|
36
|
+
return (
|
|
37
|
+
isGeo(tokens[index]) ||
|
|
38
|
+
isNumber(tokens[index]) ||
|
|
39
|
+
(isNumber(tokens[index - 1]) && tokens[index]?.features?.["NounType"] === "Class") ||
|
|
40
|
+
(isGeo(tokens[index - 1]) && isNumber(tokens[index + 1])) ||
|
|
41
|
+
(isGeo(tokens[index - 1]) && isNamePiece(tokens[index]) && closedByUnit(run, index + 1))
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 地名が単位を挟まずに続けば地名の並び(東京大阪名古屋福岡)。ただし 2 つだけで郡・市・町が閉じれば、割られた 1 つの地名
|
|
47
|
+
* (北海道虻田郡、富士河口湖町)。3 つ続けば、単位が付いても並び(京都奈良大阪神戸市)。
|
|
48
|
+
* 名古屋大阪市は北海道札幌市と品詞が同じ形なので、ここでは分けられない。
|
|
49
|
+
*/
|
|
50
|
+
const isListed = (run: Run, index: number): boolean =>
|
|
51
|
+
isGeoName(run.tokens[index]) && isGeoName(run.tokens[index + 1]) && (isGeoName(run.tokens[index + 2]) || !closedByUnit(run, index + 1));
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 漢字の連なりの語(covering: tokens の添字)が住所か(東京都港区新橋二丁目)。地名で始まり、住所の語だけでできている。
|
|
55
|
+
* 住所は決まった形で、ひらがなを挟んで書けない。組織名(日本経済団体連合会)は地名の後ろに普通の語が続くので住所ではない。
|
|
56
|
+
* topUnits は、住所の先頭にしか来ない単位(都道府県)。言語の語彙表から渡す。
|
|
57
|
+
*/
|
|
58
|
+
export const isAddressRun = (tokens: readonly Token[], covering: readonly number[], topUnits: ReadonlySet<string>): boolean => {
|
|
59
|
+
const [first, last] = [covering[0], covering.at(-1)];
|
|
60
|
+
if (first === undefined || last === undefined) return false;
|
|
61
|
+
const run: Run = { tokens, last, topUnits };
|
|
62
|
+
return isGeo(tokens[first]) && !covering.some((index) => isListed(run, index)) && covering.every((index) => isPlacePart(run, index));
|
|
63
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { Sentence, Span, Token } from "../plugin.ts";
|
|
2
|
+
|
|
3
|
+
export type Register = "polite" | "plain";
|
|
4
|
+
|
|
5
|
+
const PREDICATE = new Set(["VERB", "ADJ", "AUX"]);
|
|
6
|
+
|
|
7
|
+
const isPredicate = (token: Token): boolean => PREDICATE.has(token.pos);
|
|
8
|
+
|
|
9
|
+
const isDependent = (token: Token | undefined): boolean => token?.features?.["NounType"] === "Dependent";
|
|
10
|
+
|
|
11
|
+
/** preceding の後ろの述語の連なり: 助動詞の連なりと、その前の語一つ。「おかけしましたこと」なら「し・まし・た」。 */
|
|
12
|
+
const chainAtEnd = (preceding: readonly Token[]): readonly Token[] => {
|
|
13
|
+
const head = preceding.findLastIndex((token) => token.pos !== "AUX");
|
|
14
|
+
return preceding.slice(Math.max(head, 0));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 非自立名詞で終わる文末は、手前の述語の連なりまで見る。「予約できること。」「おかけしましたこと。」は手前の述語が調子を持つ。
|
|
19
|
+
* 要件や規程のである調は「〜こと。」で書く。「以下のとおり。」は手前も述語でないので、調子を持たないまま。
|
|
20
|
+
*/
|
|
21
|
+
const withPreceding = (ending: readonly Token[], preceding: readonly Token[]): readonly Token[] =>
|
|
22
|
+
isDependent(ending[0]) ? [...chainAtEnd(preceding), ...ending] : ending;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 文末の語の調子。preceding は文末の語より前の語。
|
|
26
|
+
* 文末に述語(動詞・形容詞・助動詞)が無ければ、ですます調でもである調でもないので undefined。
|
|
27
|
+
* 「以下の通り。」「円錐形の麦わら帽子。」のような名詞で終わる文を数えると、そのままである調の少数派になっていた。
|
|
28
|
+
* 書いた形でも原形でも当てる。「ください」の原形は「くださる」で、原形だけを見ると丁寧な文末を見落とす。
|
|
29
|
+
*/
|
|
30
|
+
export const registerOf = (ending: readonly Token[], preceding: readonly Token[], polite: readonly string[]): Register | undefined => {
|
|
31
|
+
const judged = withPreceding(ending, preceding);
|
|
32
|
+
if (!judged.some(isPredicate)) return undefined;
|
|
33
|
+
const isPolite = judged.some((token) => polite.includes(token.surface) || (token.lemma !== undefined && polite.includes(token.lemma)));
|
|
34
|
+
return isPolite ? "polite" : "plain";
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const EXCLAIMED = /[!?!?]$/u;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 「!」「?」で切れた文が、間を置かずに助詞で始まる次の文へ続くか。「作っていた!!という人」「導入しませんか?が断られた」は、
|
|
41
|
+
* 分割器が切っても一つの文で、切れ目の手前は書き手の文末ではない。「。」で切れた文は続きとは見ない。
|
|
42
|
+
*/
|
|
43
|
+
export const continuesInto = (sentence: Sentence, next: Sentence | undefined): boolean =>
|
|
44
|
+
EXCLAIMED.test(sentence.text) && next !== undefined && next.span.start === sentence.span.end && next.tokens?.[0]?.pos === "ADP";
|
|
45
|
+
|
|
46
|
+
/** 位置 offset を含む一番外側の箇条書きの始まり。本文なら undefined。入れ子の項目は外側の箇条書きと一緒に見る。 */
|
|
47
|
+
export const outermostList = (offset: number, lists: readonly Span[]): number | undefined =>
|
|
48
|
+
lists
|
|
49
|
+
.filter((list) => offset >= list.start && offset < list.end)
|
|
50
|
+
.reduce<number | undefined>((outer, list) => (outer === undefined || list.start < outer ? list.start : outer), undefined);
|
|
51
|
+
|
|
52
|
+
/** group: 文が入っている箇条書き(outermostList の値)。本文なら undefined。 */
|
|
53
|
+
export type Judged = { readonly register: Register; readonly group: number | undefined };
|
|
54
|
+
|
|
55
|
+
/** 混ざった文と、その文が属する本文または箇条書きの中の少数派の数。 */
|
|
56
|
+
export type Slip<T extends Judged> = { readonly entry: T; readonly count: number };
|
|
57
|
+
|
|
58
|
+
const countIn = (judged: readonly Judged[], register: Register): number => judged.filter((entry) => entry.register === register).length;
|
|
59
|
+
|
|
60
|
+
/** 少ないほう。同数なら文書全体で少ないほう、それも同数なら丁寧体。 */
|
|
61
|
+
const minorityOf = (group: readonly Judged[], whole: readonly Judged[]): Register => {
|
|
62
|
+
const difference = countIn(group, "polite") - countIn(group, "plain");
|
|
63
|
+
if (difference !== 0) return difference < 0 ? "polite" : "plain";
|
|
64
|
+
return countIn(whole, "polite") <= countIn(whole, "plain") ? "polite" : "plain";
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const slipsInGroup = <T extends Judged>(judged: readonly T[], group: number | undefined, limit: number): Slip<T>[] => {
|
|
68
|
+
const members = judged.filter((entry) => entry.group === group);
|
|
69
|
+
const minority = minorityOf(members, judged);
|
|
70
|
+
const slips = members.filter((entry) => entry.register === minority);
|
|
71
|
+
// 片方しか無ければ揃っている。少数派が閾値を超えて多ければ、混在ではなく別の文体。
|
|
72
|
+
if (slips.length === 0 || slips.length > limit) return [];
|
|
73
|
+
return slips.map((entry) => ({ entry, count: slips.length }));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 調子が混ざった文を、judged の順で。本文は本文どうし、箇条書きは 1 つずつ、その中で揃っているかを見る。
|
|
78
|
+
* ですます調の本文に常体の箇条書きを置くのはよくある書き方で、箇条書きが丸ごと揃っていれば混在ではない。
|
|
79
|
+
*/
|
|
80
|
+
export const slipsOf = <T extends Judged>(judged: readonly T[], limit: number): Slip<T>[] => {
|
|
81
|
+
const slips = [...new Set(judged.map((entry) => entry.group))].flatMap((group) => slipsInGroup(judged, group, limit));
|
|
82
|
+
return judged.flatMap((entry) => slips.filter((slip) => slip.entry === entry));
|
|
83
|
+
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { endingTokens,
|
|
1
|
+
import { endingTokens, isClosed } from "../sentence-shape.ts";
|
|
2
|
+
import { continuesInto, outermostList, registerOf, slipsOf, type Register } from "./register.ts";
|
|
2
3
|
import type { Detector, Finding, Sentence } from "../plugin.ts";
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -6,36 +7,31 @@ import type { Detector, Finding, Sentence } from "../plugin.ts";
|
|
|
6
7
|
*
|
|
7
8
|
* どちらが正しいかは決めない。決めると、方針の違う書き手にそのまま無視される。
|
|
8
9
|
* 見るのは文書の中での一貫性だけで、少数派のほうを指摘する。spec §12.3 の方針を日本語にも。
|
|
10
|
+
* 終止符で終わらないもの(見出しの下の `MaaSサービス` のような名前だけの行)は文として数えない。
|
|
9
11
|
*/
|
|
10
|
-
|
|
11
|
-
* 述語を持たないものは文として数えない。見出しの下の `MaaSサービス` のような
|
|
12
|
-
* 名前だけの行がそのまま「である調」の少数派になり、実文書の誤検知はすべてこれだった。
|
|
13
|
-
*/
|
|
14
|
-
const isSentence = (sentence: Sentence): boolean => isClosed(sentence) && hasPredicate(sentence) && endingTokens(sentence).length > 0;
|
|
12
|
+
type Entry = { readonly sentence: Sentence; readonly register: Register; readonly group: number | undefined };
|
|
15
13
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
14
|
+
const registerOfSentence = (sentence: Sentence, polite: readonly string[]): Register | undefined => {
|
|
15
|
+
const ending = endingTokens(sentence);
|
|
16
|
+
const tokens = sentence.tokens ?? [];
|
|
17
|
+
const head = ending[0] === undefined ? -1 : tokens.indexOf(ending[0]);
|
|
18
|
+
return registerOf(ending, head > 0 ? tokens.slice(0, head) : [], polite);
|
|
19
|
+
};
|
|
22
20
|
|
|
23
21
|
export const sentenceEnding: Detector = (doc, options): Finding[] => {
|
|
24
22
|
const polite = (options.lexicon ?? []).map((entry) => entry.pattern);
|
|
25
|
-
const
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return judged
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
values: { count: few, limit: options.limit, offset: sentence.span.start },
|
|
40
|
-
}));
|
|
23
|
+
const lists = doc.lists.map((list) => list.span);
|
|
24
|
+
const judged = doc.sentences.flatMap((sentence, index): Entry[] => {
|
|
25
|
+
if (!isClosed(sentence) || continuesInto(sentence, doc.sentences[index + 1])) return [];
|
|
26
|
+
const register = registerOfSentence(sentence, polite);
|
|
27
|
+
return register === undefined ? [] : [{ sentence, register, group: outermostList(sentence.span.start, lists) }];
|
|
28
|
+
});
|
|
29
|
+
return slipsOf(judged, options.limit).map(({ entry: { sentence }, count }) => ({
|
|
30
|
+
rule: "no-mixed-desumasu",
|
|
31
|
+
severity: "warning",
|
|
32
|
+
line: 0,
|
|
33
|
+
column: 0,
|
|
34
|
+
quote: sentence.text.trim(),
|
|
35
|
+
values: { count, limit: options.limit, offset: sentence.span.start },
|
|
36
|
+
}));
|
|
41
37
|
};
|
package/src/detectors/signals.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { proseText } from "../measure.ts";
|
|
2
2
|
import { wordsOf } from "./structure.ts";
|
|
3
3
|
import { compacted, placeOf } from "./gram-place.ts";
|
|
4
|
-
import {
|
|
4
|
+
import { notAcronymSpansOf, type NotAcronymSpans } from "./acronym-context.ts";
|
|
5
5
|
import { expansionAt, type ExpandedAt } from "./acronym-expansion.ts";
|
|
6
6
|
import type { Detector, Finding, ProseDocument, Section, Sentence, Token } from "../plugin.ts";
|
|
7
7
|
|
|
@@ -183,18 +183,18 @@ type Span = { readonly start: number; readonly end: number };
|
|
|
183
183
|
|
|
184
184
|
const spanOf = (match: RegExpExecArray): Span => ({ start: match.index, end: match.index + match[0].length });
|
|
185
185
|
|
|
186
|
-
const spansOf = (text: string): Span[] => [
|
|
186
|
+
const spansOf = (text: string, notation: NotAcronymSpans): Span[] => [
|
|
187
187
|
...[...text.matchAll(UNCASED_STRETCH)].filter((match) => SHOUTED_RUN.test(match[0])).map(spanOf),
|
|
188
188
|
...[...text.matchAll(IDENTIFIER)].filter((match) => /\d/u.test(match[0])).map(spanOf),
|
|
189
189
|
...[QUOTED_CAPS, REQUIREMENT_WORD, LICENCE].flatMap((pattern) => [...text.matchAll(pattern)].map(spanOf)),
|
|
190
|
-
...
|
|
190
|
+
...notation(text),
|
|
191
191
|
];
|
|
192
192
|
|
|
193
193
|
type AcronymHit = { readonly word: string; readonly hit: Hit };
|
|
194
194
|
|
|
195
|
-
const acronymsOf = (doc: ProseDocument): AcronymHit[] =>
|
|
195
|
+
const acronymsOf = (doc: ProseDocument, notation: NotAcronymSpans): AcronymHit[] =>
|
|
196
196
|
doc.sentences.flatMap((sentence) => {
|
|
197
|
-
const excluded = spansOf(sentence.text);
|
|
197
|
+
const excluded = spansOf(sentence.text, notation);
|
|
198
198
|
return [...sentence.text.matchAll(ACRONYM)]
|
|
199
199
|
.filter((match) => !excluded.some((span) => span.start <= match.index && match.index + match[0].length <= span.end))
|
|
200
200
|
.map((match) => ({ word: match[0], hit: { sentence, offset: sentence.span.start + match.index } }));
|
|
@@ -209,11 +209,20 @@ const isExpanded = (body: string, acronym: string, expandedAt: ExpandedAt): bool
|
|
|
209
209
|
|
|
210
210
|
const patternsOf = (doc: ProseDocument, id: string): string[] => (doc.lexicons[id] ?? []).map((entry) => entry.pattern);
|
|
211
211
|
|
|
212
|
+
const notationOf = (doc: ProseDocument): NotAcronymSpans =>
|
|
213
|
+
notAcronymSpansOf({
|
|
214
|
+
meridiem: patternsOf(doc, "meridiem"),
|
|
215
|
+
timeZones: patternsOf(doc, "time-zone"),
|
|
216
|
+
currencies: patternsOf(doc, "currency-code"),
|
|
217
|
+
usStates: patternsOf(doc, "us-state-code"),
|
|
218
|
+
emphasis: patternsOf(doc, "emphasis-word"),
|
|
219
|
+
});
|
|
220
|
+
|
|
212
221
|
export const undefinedAcronym: Detector = (doc, options): Finding[] => {
|
|
213
222
|
const body = bodyOf(doc);
|
|
214
223
|
const common = new Set((options.lexicon ?? []).map((entry) => entry.pattern));
|
|
215
224
|
const seen = new Map<string, Hit>();
|
|
216
|
-
acronymsOf(doc).forEach(({ word, hit }) => {
|
|
225
|
+
acronymsOf(doc, notationOf(doc)).forEach(({ word, hit }) => {
|
|
217
226
|
if (!common.has(word) && !seen.has(word)) seen.set(word, hit);
|
|
218
227
|
});
|
|
219
228
|
const expandedAt = expansionAt({ markers: patternsOf(doc, "definition-marker"), verbs: patternsOf(doc, "definition-verb") });
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Span, Token } from "./plugin.ts";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 数量ではなく名前として書かれた数(番号・識別子)。鉤括弧で引いた「3.1 リサーチの原則」の節番号、※1 のような注の番号、
|
|
5
|
+
* 「〒102-0094 東京都」の郵便番号は、後ろの空白が番号と題・項目の区切りで、「3回」「3 回」のような空け方の好みではない。
|
|
6
|
+
* 空け方を数える材料にしない。
|
|
7
|
+
*
|
|
8
|
+
* 形で名前と読むのは三つだけ。0 で始まる組を含むハイフンつなぎの番号、※ や鉤括弧のすぐ後ろの番号、文頭の階層つきの節番号(2.1、3.1.2)。
|
|
9
|
+
* 文頭でも「223 言語」「1ターン」は数量なので、階層の無い数は文頭というだけでは名前と読まない。
|
|
10
|
+
* そのうえで、すぐ後ろ(空白 1 つまで)の語が数につく語(助数詞・数・助詞・助動詞)なら数量として数え続ける(「3-5 日」「26.7 万行」)。
|
|
11
|
+
* 品詞が無ければ判断せず、名前とは読まない。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** 数と結びついて読まれる語。助数詞は NounType=Class、数は NumType=Card で渡る。 */
|
|
15
|
+
const BOUND_TO_NUMBER = new Set(["ADP", "PART", "AUX", "SCONJ", "CCONJ"]);
|
|
16
|
+
|
|
17
|
+
/** 番号が立つ位置の直前: 題を包む鉤括弧、注の印。 */
|
|
18
|
+
const LABEL_LEAD = /[「『【※]$/u;
|
|
19
|
+
|
|
20
|
+
/** 文頭(前に空白と箇条書きの印だけ)。 */
|
|
21
|
+
const LINE_HEAD = /^[\s\-*+・•]*$/u;
|
|
22
|
+
|
|
23
|
+
/** 階層つきの節番号(2.1、3.1.2)。 */
|
|
24
|
+
const SECTION_NUMBER = /^\d+(?:\.\d+)+$/u;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 0 で始まる組を含む、ハイフンでつないだ番号(〒102-0094、03-3501)。二つの数だけの「3-5」は範囲(3-5 営業日)とも読めるので
|
|
28
|
+
* 数え続ける。三つ以上つないだもの(073-489-5909)は、orthography.ts が境目を作る前に外している。
|
|
29
|
+
*/
|
|
30
|
+
const isHyphenIdentifier = (digits: string): boolean => {
|
|
31
|
+
const parts = digits.split("-").filter((part) => part !== "");
|
|
32
|
+
return parts.length > 1 && parts.some((part) => part.startsWith("0"));
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const RUN_CHAR = /[\d.-]/u;
|
|
36
|
+
|
|
37
|
+
/** at を含む、数字・小数点・ハイフンの並び。at が並びの中に無ければ undefined。 */
|
|
38
|
+
export const digitRunAround = (text: string, at: number): Span | undefined => {
|
|
39
|
+
if (!RUN_CHAR.test(text[at] ?? "")) return undefined;
|
|
40
|
+
const startAt = (index: number): number => (index > 0 && RUN_CHAR.test(text[index - 1] ?? "") ? startAt(index - 1) : index);
|
|
41
|
+
const endAt = (index: number): number => (index < text.length && RUN_CHAR.test(text[index] ?? "") ? endAt(index + 1) : index);
|
|
42
|
+
return { start: startAt(at), end: endAt(at) };
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/** 並びの後ろ(空白 1 つまで)で始まる語。 */
|
|
46
|
+
const wordAfter = (tokens: readonly Token[], text: string, run: Span, base: number): Token | undefined => {
|
|
47
|
+
const next = text[run.end] === " " ? run.end + 1 : run.end;
|
|
48
|
+
return tokens.find((token) => token.span.start === base + next);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const isBoundToNumber = (token: Token): boolean =>
|
|
52
|
+
token.features?.["NounType"] === "Class" || token.features?.["NumType"] === "Card" || BOUND_TO_NUMBER.has(token.pos);
|
|
53
|
+
|
|
54
|
+
/** ハイフンでつないだ識別子か、番号の立つ位置の番号か、文頭の節番号か。 */
|
|
55
|
+
const isNameShaped = (text: string, run: Span): boolean => {
|
|
56
|
+
const [before, digits] = [text.slice(0, run.start), text.slice(run.start, run.end)];
|
|
57
|
+
return isHyphenIdentifier(digits) || LABEL_LEAD.test(before) || (LINE_HEAD.test(before) && SECTION_NUMBER.test(digits));
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* text の run が名前として書かれた数か。tokens は文書全体の座標で、base は text の先頭の位置。
|
|
62
|
+
* 後ろの語が読めない(品詞が無い、語の切れ目が合わない)ときは、数量として数え続ける。
|
|
63
|
+
*/
|
|
64
|
+
export const isNumberName = (text: string, run: Span, tokens: readonly Token[] | undefined, base: number): boolean => {
|
|
65
|
+
if (tokens === undefined || !isNameShaped(text, run)) return false;
|
|
66
|
+
const next = wordAfter(tokens, text, run, base);
|
|
67
|
+
return next !== undefined && !isBoundToNumber(next);
|
|
68
|
+
};
|