From fd3dac04439a8f25ee1ce78545565a3e4eeed103 Mon Sep 17 00:00:00 2001 From: isamu Date: Mon, 30 Nov 2026 15:31:59 +0900 Subject: [PATCH] feat: unbalanced-bracket pairs the brackets of each heading on its own line Heading lines are masked in the body text, so a bracket left unopened or unclosed in a heading was never read. Each heading is now paired by itself, over its visible text only (no # marks, inline code or link text). Co-Authored-By: Claude Opus 5.5 --- text/corpus/expected/unbalanced-bracket.txt | 2 +- text/docs/samples/en/unbalanced-bracket.md | 4 +-- text/docs/samples/ja/unbalanced-bracket.md | 6 ++-- .../chaff/rules/unbalanced-bracket.yaml | 4 +-- .../chaff/src/detectors/unbalanced-bracket.ts | 26 +++++++++++++++-- text/test/test_unbalanced_bracket.ts | 29 ++++++++++++++++++- 6 files changed, 59 insertions(+), 12 deletions(-) diff --git a/text/corpus/expected/unbalanced-bracket.txt b/text/corpus/expected/unbalanced-bracket.txt index e1b54920c..00461a975 100644 --- a/text/corpus/expected/unbalanced-bracket.txt +++ b/text/corpus/expected/unbalanced-bracket.txt @@ -16,7 +16,7 @@ mhlw-icd-committee-26-minutes 2 mhlw-kikan-gyomu-shokuin-boshu 1 mhlw-norovirus-qa 2 nps-yellowstone-faqs 1 -ppc-guidelines-tsusoku 22 +ppc-guidelines-tsusoku 23 rfc9293-tcp 1 saikosai-h26-09-02-nagano 1 tokyoapp-service-terms 1 diff --git a/text/docs/samples/en/unbalanced-bracket.md b/text/docs/samples/en/unbalanced-bracket.md index 715ef17ae..d59fac072 100644 --- a/text/docs/samples/en/unbalanced-bracket.md +++ b/text/docs/samples/en/unbalanced-bracket.md @@ -33,7 +33,7 @@ return-laptop.md docs/manual · English genre from --genre · Unbalanced bracket "(" is never closed - A "(" never closed, or a ")" never opened, leaves the reader unsure where the aside ends. A full-width "(" closed by a half-width ")" is left over from an edit too. Brackets are paired within each section under a heading ((), [], “”, and the Japanese 「」『』()[]【】〔〕〈〉《》), across paragraphs, since a stanza or a long quotation may span several. A short label of up to three characters such as "1)", "a)" or "iv)" at the start of a line or after a space, a ")" right after a number, a numbered label inside an aside ("A3)" in "(see the notes; A3) and more)", when the same label is used at the start of a line and later closes on the same line still close the aside), code, URLs, and an opening bracket quoted as the last character of a quotation (「取締役(」, as statutes quote the text they amend) are not counted. An opening quotation mark carried into a paragraph that opens with the same mark, as in fiction, is not counted, nor is a quotation of verse that reopens with the same mark at the start of two or more lines in a row and closes once at the end. + A "(" never closed, or a ")" never opened, leaves the reader unsure where the aside ends. A full-width "(" closed by a half-width ")" is left over from an edit too. Brackets are paired within each section under a heading ((), [], “”, and the Japanese 「」『』()[]【】〔〕〈〉《》), across paragraphs, since a stanza or a long quotation may span several. A heading is paired on its own line. A short label of up to three characters such as "1)", "a)" or "iv)" at the start of a line or after a space, a ")" right after a number, a numbered label inside an aside ("A3)" in "(see the notes; A3) and more)", when the same label is used at the start of a line and later closes on the same line still close the aside), code, URLs, and an opening bracket quoted as the last character of a quotation (「取締役(」, as statutes quote the text they amend) are not counted. An opening quotation mark carried into a paragraph that opens with the same mark, as in fiction, is not counted, nor is a quotation of verse that reopens with the same mark at the start of two or more lines in a row and closes once at the end. → Add the missing bracket or remove the extra one. Make the opening and closing marks the same width. @@ -86,7 +86,7 @@ $ npx chaffjs explain unbalanced-bracket --genre docs/manual Unbalanced bracket (unbalanced-bracket) - A "(" never closed, or a ")" never opened, leaves the reader unsure where the aside ends. A full-width "(" closed by a half-width ")" is left over from an edit too. Brackets are paired within each section under a heading ((), [], “”, and the Japanese 「」『』()[]【】〔〕〈〉《》), across paragraphs, since a stanza or a long quotation may span several. A short label of up to three characters such as "1)", "a)" or "iv)" at the start of a line or after a space, a ")" right after a number, a numbered label inside an aside ("A3)" in "(see the notes; A3) and more)", when the same label is used at the start of a line and later closes on the same line still close the aside), code, URLs, and an opening bracket quoted as the last character of a quotation (「取締役(」, as statutes quote the text they amend) are not counted. An opening quotation mark carried into a paragraph that opens with the same mark, as in fiction, is not counted, nor is a quotation of verse that reopens with the same mark at the start of two or more lines in a row and closes once at the end. + A "(" never closed, or a ")" never opened, leaves the reader unsure where the aside ends. A full-width "(" closed by a half-width ")" is left over from an edit too. Brackets are paired within each section under a heading ((), [], “”, and the Japanese 「」『』()[]【】〔〕〈〉《》), across paragraphs, since a stanza or a long quotation may span several. A heading is paired on its own line. A short label of up to three characters such as "1)", "a)" or "iv)" at the start of a line or after a space, a ")" right after a number, a numbered label inside an aside ("A3)" in "(see the notes; A3) and more)", when the same label is used at the start of a line and later closes on the same line still close the aside), code, URLs, and an opening bracket quoted as the last character of a quotation (「取締役(」, as statutes quote the text they amend) are not counted. An opening quotation mark carried into a paragraph that opens with the same mark, as in fiction, is not counted, nor is a quotation of verse that reopens with the same mark at the start of two or more lines in a row and closes once at the end. How to fix: Add the missing bracket or remove the extra one. Make the opening and closing marks the same width. diff --git a/text/docs/samples/ja/unbalanced-bracket.md b/text/docs/samples/ja/unbalanced-bracket.md index 56a78cc47..71ea5c599 100644 --- a/text/docs/samples/ja/unbalanced-bracket.md +++ b/text/docs/samples/ja/unbalanced-bracket.md @@ -41,7 +41,7 @@ minutes.md business/meeting-notes · 日本語 ジャンルは --genre か · 括弧が組になっていない 「(」が閉じていません - 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 + 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。見出しの行は、見出しごとに一行の中で組にします。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 → 足りない括弧を足すか、余分な括弧を消してください。全角と半角は、開きと閉じをそろえてください。 @@ -55,7 +55,7 @@ minutes.md business/meeting-notes · 日本語 ジャンルは --genre か · 括弧が組になっていない 「」」に対応する開きがありません - 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 + 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。見出しの行は、見出しごとに一行の中で組にします。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 → 足りない括弧を足すか、余分な括弧を消してください。全角と半角は、開きと閉じをそろえてください。 @@ -114,7 +114,7 @@ $ npx chaffjs explain unbalanced-bracket --genre business/meeting-notes 括弧が組になっていない (unbalanced-bracket) - 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 + 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。見出しの行は、見出しごとに一行の中で組にします。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 直しかた: 足りない括弧を足すか、余分な括弧を消してください。全角と半角は、開きと閉じをそろえてください。 diff --git a/text/packages/chaff/rules/unbalanced-bracket.yaml b/text/packages/chaff/rules/unbalanced-bracket.yaml index 400b0cc9a..271498879 100644 --- a/text/packages/chaff/rules/unbalanced-bracket.yaml +++ b/text/packages/chaff/rules/unbalanced-bracket.yaml @@ -9,8 +9,8 @@ name: en: Unbalanced bracket why: - ja: 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 - en: A "(" never closed, or a ")" never opened, leaves the reader unsure where the aside ends. A full-width "(" closed by a half-width ")" is left over from an edit too. Brackets are paired within each section under a heading ((), [], “”, and the Japanese 「」『』()[]【】〔〕〈〉《》), across paragraphs, since a stanza or a long quotation may span several. A short label of up to three characters such as "1)", "a)" or "iv)" at the start of a line or after a space, a ")" right after a number, a numbered label inside an aside ("A3)" in "(see the notes; A3) and more)", when the same label is used at the start of a line and later closes on the same line still close the aside), code, URLs, and an opening bracket quoted as the last character of a quotation (「取締役(」, as statutes quote the text they amend) are not counted. An opening quotation mark carried into a paragraph that opens with the same mark, as in fiction, is not counted, nor is a quotation of verse that reopens with the same mark at the start of two or more lines in a row and closes once at the end. + ja: 閉じ忘れた「(」や、開きの無い「」」があると、読み手はどこまでが括弧の中なのか分からなくなります。全角の「(」を半角の「)」で閉じるのも、書き換えの途中で残った印です。見出しで区切った節ごとに、「」『』()()[][]【】〔〕〈〉《》“” と半角の 「」 の組を数えます。見出しの行は、見出しごとに一行の中で組にします。詩の連や長い引用は段落をまたぐので、段落を越えて組にします。行の頭や空白の後ろの「1)」「a)」「事例)」のような三文字までの番号の印と数の後ろの「)」、括弧の中の番号の印(「(…以下、事例5)まで同じ。)」の「事例5)」。同じ字の印を行の頭でも使っていて、同じ行の後ろの閉じで括弧が閉じるときに限る)、コードと URL、引用の閉じのすぐ前の開き(法令の読替えの「取締役(」とあるのは)は数えません。英語の小説のように、次の段落も同じ開きで始まるなら、閉じない開きは続きとして数えません。詩の引用のように二行以上を続けて行の頭の同じ引用符で開き直し、終わりで一度だけ閉じるものも、続きとして数えません。 + en: A "(" never closed, or a ")" never opened, leaves the reader unsure where the aside ends. A full-width "(" closed by a half-width ")" is left over from an edit too. Brackets are paired within each section under a heading ((), [], “”, and the Japanese 「」『』()[]【】〔〕〈〉《》), across paragraphs, since a stanza or a long quotation may span several. A heading is paired on its own line. A short label of up to three characters such as "1)", "a)" or "iv)" at the start of a line or after a space, a ")" right after a number, a numbered label inside an aside ("A3)" in "(see the notes; A3) and more)", when the same label is used at the start of a line and later closes on the same line still close the aside), code, URLs, and an opening bracket quoted as the last character of a quotation (「取締役(」, as statutes quote the text they amend) are not counted. An opening quotation mark carried into a paragraph that opens with the same mark, as in fiction, is not counted, nor is a quotation of verse that reopens with the same mark at the start of two or more lines in a row and closes once at the end. message: ja: 「{mark}」が組になっていません diff --git a/text/packages/chaff/src/detectors/unbalanced-bracket.ts b/text/packages/chaff/src/detectors/unbalanced-bracket.ts index 76ba445b9..0b41b8e48 100644 --- a/text/packages/chaff/src/detectors/unbalanced-bracket.ts +++ b/text/packages/chaff/src/detectors/unbalanced-bracket.ts @@ -196,12 +196,32 @@ const findingOf = (text: string, problem: BracketProblem): Finding => ({ variant: problem.kind, }); -/** 見出しで区切った節ごとに読む。見出しの行は本文で覆ってある。 */ +/** 見出しの行のうち、字のまま見える範囲(texts)だけを残し、ほかの字(# の印、コード、リンク)を空白にしたもの。位置は見出しの始まりから。 */ +export const visibleHeading = (source: string, heading: Span, texts: readonly Span[]): string => { + const parts: string[] = []; + const cursor = { at: heading.start }; + clippedTo(mergeSpans(texts, true), heading).forEach((span) => { + parts.push(" ".repeat(span.start - cursor.at), source.slice(span.start, span.end)); + cursor.at = span.end; + }); + parts.push(" ".repeat(Math.max(0, heading.end - cursor.at))); + return parts.join(""); +}; + +/** 見出しは一行で閉じる言葉なので、見出しごとに組にする。 */ +const headingFindings = (source: string, headings: readonly Span[], texts: readonly Span[]): Finding[] => + headings.flatMap((heading) => + bracketProblems(visibleHeading(source, heading, texts)).map((problem) => findingOf(source, { ...problem, offset: heading.start + problem.offset })), + ); + +/** 見出しで区切った節ごとに読む。見出しの行は本文で覆ってあるので、見出しは一つずつ読む。 */ export const unbalancedBracket: Detector = (doc): Finding[] => { - const text = withRunOnRestored(doc.prose ?? doc.source, doc.source, doc.markup?.texts ?? []); - return doc.sections.flatMap((section) => + const texts = doc.markup?.texts ?? []; + const text = withRunOnRestored(doc.prose ?? doc.source, doc.source, texts); + const inSections = doc.sections.flatMap((section) => bracketProblems(text.slice(section.span.start, section.span.end)).map((problem) => findingOf(text, { ...problem, offset: section.span.start + problem.offset }), ), ); + return [...inSections, ...headingFindings(doc.source, doc.markup?.headings ?? [], texts)]; }; diff --git a/text/test/test_unbalanced_bracket.ts b/text/test/test_unbalanced_bracket.ts index c54688110..7dd655606 100644 --- a/text/test/test_unbalanced_bracket.ts +++ b/text/test/test_unbalanced_bracket.ts @@ -3,7 +3,7 @@ import assert from "node:assert/strict"; import { namedRuleRun } from "./rule-run.ts"; import { adapter as ja } from "../packages/lang-ja/src/index.ts"; import { adapter as en } from "../packages/lang-en/src/index.ts"; -import { bracketProblems, withRunOnRestored } from "../packages/chaff/src/detectors/unbalanced-bracket.ts"; +import { bracketProblems, visibleHeading, withRunOnRestored } from "../packages/chaff/src/detectors/unbalanced-bracket.ts"; import { isLabelClose, isLabelInAside } from "../packages/chaff/src/detectors/bracket-label.ts"; import { isInchMark } from "../packages/chaff/src/detectors/inch-mark.ts"; import { isEmoticonMouth } from "../packages/chaff/src/detectors/emoticon.ts"; @@ -93,6 +93,33 @@ describe("unbalanced-bracket: 括弧が組になっていない", () => { assert.deepEqual(findingsOf("## A\n\n(open here.\n\n## B\n\nclosed here).\n", en), ['"(" is never closed', '")" closes nothing that was opened']); }); + it("見出しの中の括弧も、見出しごとに組にする", () => { + assert.deepEqual(findingsOf("# 会議\n\n## (1) 計画について (2) 未来の学校 -教育ビジョン-」(素案)について\n\n本文です。\n"), [ + "「」」に対応する開きがありません", + ]); + assert.deepEqual(findingsOf("# 会議\n\n## 資料(別紙\n\n本文です。\n"), ["「(」が閉じていません"]); + assert.deepEqual(findingsOf("## The plan (draft\n\nBody text.\n", en), ['"(" is never closed']); + }); + + it("組になった見出し、番号の印、コードとリンクの中の括弧は言わない", () => { + assert.deepEqual(findingsOf("# 会議\n\n## (1) 「未来の学校」(素案)について\n\n本文です。\n"), []); + assert.deepEqual(findingsOf("## Calling `run(` here\n\nBody text.\n", en), []); + assert.deepEqual(findingsOf("## See [the guide (v2](https://example.com/)\n\nBody text.\n", en), []); + assert.deepEqual(findingsOf("見出しの無い文書(です。\n"), ["「(」が閉じていません"]); + }); + + it("visibleHeading: 見える字だけを残し、位置を見出しの始まりからに保つ", () => { + const source = "## A `b(` c"; + assert.equal( + visibleHeading(source, { start: 0, end: source.length }, [ + { start: 3, end: 5 }, + { start: 9, end: 11 }, + ]), + " A c", + ); + assert.equal(visibleHeading(source, { start: 0, end: source.length }, []), " ".repeat(source.length)); + }); + it("次の段落を同じ引用符で始める台詞は、続きとして数えない", () => { assert.deepEqual(findingsOf("“I went out,\n\n“and came back.”\n", en), []); });