From 26361102db77e856bb598a4d0220db6094c0b75f Mon Sep 17 00:00:00 2001 From: isamu Date: Mon, 30 Nov 2026 12:08:22 +0900 Subject: [PATCH] fix: ngram-repetition leaves the phrase each named entry of a specification carries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sibling sections that each quote their own heading in their first sentence (2.5.3. Contact / The "Contact" field indicates …) define one named item each and fill one template per item (If this field indicates …). They join the repeated forms whose phrase, carried once or twice per section, is not counted; plain-text numbered sections are read from the structure tree. Uses outside those sections still count against the limit. Co-Authored-By: Claude Opus 5.5 --- .../chaff/rules/ngram-repetition.yaml | 4 +- .../chaff/src/detectors/gram-repetition.ts | 3 +- .../chaff/src/detectors/gram-siblings.ts | 39 ++++++++++++++++++- text/test/test_ngram_formulas.ts | 16 ++++++++ 4 files changed, 58 insertions(+), 4 deletions(-) diff --git a/text/packages/chaff/rules/ngram-repetition.yaml b/text/packages/chaff/rules/ngram-repetition.yaml index 0b62aded8..b9e24c717 100644 --- a/text/packages/chaff/rules/ngram-repetition.yaml +++ b/text/packages/chaff/rules/ngram-repetition.yaml @@ -52,8 +52,8 @@ example: We listen to our customers. Regulars try every prototype, and store staff ask shoppers what they thought. not_flagged: - ja: 一覧の飾り(各項目に付くタグやリンクの文字)、欄の名前(Cited by:)、かぎ括弧で引いた題や言葉、文書が定義した語と、名前にかかる語句は数えません。英語の、名詞と形容詞と略語だけが並ぶ用語(the federal funds rate、extended AI assistant)は言い回しではないので数えません。一つの文の中の繰り返しは 1 回と数えます。文書の題と見出しにある言い回しは主題なので数えません。よくある質問の答えは、問いの見出しごとに別々に数えます。節の最初の文にある言い回し(節ごとのひな形の書き出し)、番号を振った項目の見出し((1)…に係る規定に関する改正)、参照の項目の説明の段落(`max-age` の節、定義の並び)、句点の無い名詞で終わる一行(表の欄)に出たぶんは数えません。項目ごとの決まった結び(〜ものとすること。原案のとおり決定する。が表示されます。を記載してください。表の前の次のとおりであります。)も、文の終わりにあれば数えません。文書が引いた題の後ろで決まって使う短い結び(詳しくは「…」を確認してください。)、文書の部分を指す名前(Section 3.1、表2)にかかる語句、表の欄が 2 つ続いた後ろの一行の文(表の行の最後の欄)も数えません。枠ごとにまったく同じ形で付けるコロンで終わる一行の札(The following rule has the force of law:)、平文の仕様書の定義の並びの項目(行の頭の短い名前とコロンのあとに空白を 2 つ置いて書く定義)、リンクのすぐ後ろで決まって使う結び([…](…)を参照してください。)、数字だけを入れ替えた報告の行(There are currently 17 committers …)も、書き手の言い回しではないので数えません。英語で、名詞の入る所の -ing 形の句(what occupying the anticipatory mode requires)と、用語の後ろの be(the anticipatory mode is)は用語で、言い回しではありません。手順書が手順ごとに役割を名指しする、名詞の主語と助動詞だけの語句(the manager should)も、何をするかが毎回違うので数えません。長い文書の中で何千語も離れて使う語句(報告が測る出来事の名前 adopted a budget resolution)は、前の出現を覚えているうちに次が来ないので数えません。見出しが名付けた句に前後の語が 1 つ付いただけの語句(見出し Create a ConfigMap の下の to create a ConfigMap、見出し 旧氏を併記した場合 の下の 旧氏を併記した氏名)も主題で、数えません。角括弧でくくった画面の部品の名前に付いた語句([許可]をクリックすると、[ブックマーク]をクリックすると)は、手順ごとに違う部品を指す書き方で、数えません。中黒で並べた名詞を飾る節(育成すべき資質・能力)も、文書の決まった用語で、言い回しではありません。短い文書は測りません。見出しが一つの複合語に書いた主題を、本文が助詞を入れて書いた語句(見出しの「旧氏併記」に対する「旧氏を併記した氏」)も、主題なので数えません。数を入れた短い括弧の注に触れる語句(売上高は307,814千円(前期比99.6%)となりました。の)となりました。)も、報告が数ごとに添える書式で、数えません。同じ形で終わる文が続く所の、文の結びだけの語句(〜を強化してまいります。の してまいります。)も数えません。並んだ結びの繰り返しは repeated-sentence-ending が見ます。離れた所で何度も出る結びと、中身の動詞が入る結び(取り組んでまいります。)は言い回しとして数えます。番号を振った例の節(Example 1、事例 2 …。手順や施策の番号の節は入れません)がそれぞれ一度か二度ずつ持つ語句(例ごとの結び)、引用符でくくったファイルの名前に触れる語句(A "security.txt" file)、英語の議事録が報告ごとに置く決まった一行(No report was submitted.)も数えません。中身を持つ語が文書の題の語だけの語句(題 Record keeping の下の you can record the)も、主題を扱うたびに出る言い方で、数えません。大文字だけの語と読点で始まる議決の条項(RESOLVED, that the office of … be and hereby is created)も、条項ごとに同じ頭と言い回しで書く書式で、数えません。同じ形を繰り返す節(コロンで終わる同じ欄の名前の見出しの節、すぐ下に同じ小見出しを並べた節)がそれぞれ一度か二度ずつ持つ語句(報告ごとの Description の節の書き出し)も、項目ごとに一つのひな形を埋めたもので、数えません。並びの外の出現が上限を超えれば数えます。 - en: Tags, link text and symbols that a listing repeats on every entry, field labels (Cited by:), titles and words quoted in Japanese brackets, terms the document defines, and the words around a repeated name are not counted. A term made only of nouns, adjectives and acronyms ("the federal funds rate", "extended AI assistant") is not a phrasing and is not counted. A phrase repeated inside one sentence counts once, and one in the document's title or headings is its subject and is not counted. The answers in a FAQ are counted one question at a time. Uses in the first sentence of sections (each section's template opening), in numbered item titles ((1) ... amendment of the provision on ...), in the first paragraph of a reference entry (a section headed `max-age`, a definition list) and in a line without a full stop that ends in a noun (a table cell) are not counted, nor a fixed closing line repeated per item at the end of a Japanese sentence (〜ものとすること。, 原案のとおり決定する。, が表示されます。, を記載してください。, 次のとおりであります。 before a table). Nor are the short closing a document uses mostly right after a quoted title (詳しくは「…」を確認してください。), the words around a reference to a numbered part (set out in Section 3.1, 表2), or a one-line sentence after two table cells (the last cell of a table run into lines). Nor are a one-line label ending in a colon repeated verbatim before each box (The following rule has the force of law:), an entry of a plain-text definition list (a short name, a colon and two spaces, then the definition), the short closing a document uses mostly right after a link ([…](…)を参照してください。), or a report line filled in with different numbers (There are currently 17 committers …). In English, an -ing phrase in a noun's place (what occupying the anticipatory mode requires) and a term followed by be (the anticipatory mode is) are terms, not phrasing. Nor is a noun subject followed only by a modal (the manager should), the way a process manual names the role for each step, since what the role must do follows and differs each time. A phrase whose uses lie thousands of words apart in a long document (the event a report times, adopted a budget resolution) is not counted, since the reader has forgotten one use before the next comes. A phrase that is a heading's phrase with a word attached (to create a ConfigMap under the heading Create a ConfigMap, 旧氏を併記した氏名 under 旧氏を併記した場合) is the subject too, and is not counted. In Japanese, the words attached to a bracketed screen label ([許可]をクリックすると) are the step wording of a screen manual, each naming a different button, and are not counted. In Japanese, a clause modifying nouns joined by a nakaguro (育成すべき資質・能力) is part of one fixed term, not phrasing. Short documents are not measured. Nor is a Japanese phrase that spells out with particles a compound a heading names (旧氏を併記した氏 under a heading holding 旧氏併記), since it is the subject. Nor are the words touching a short Japanese aside holding a figure (the )となりました。 of 売上高は307,814千円(前期比99.6%)となりました。), the frame a report puts around each number. Nor is a Japanese phrase made only of a sentence ending (the してまいります。 of 〜を強化してまいります。) where sentences in a row end the same way, since repeated-sentence-ending reports that run; the same ending scattered through a document, or an ending that holds a verb with content (取り組んでまいります。), is still a phrasing. Nor is a phrase each numbered example (Example 1, Example 2, …; not numbered steps or measures) carries once or twice, such as the closing of each worked example, the words touching a quoted file name (A "security.txt" file), or the fixed line board minutes give each report (No report was submitted.). Nor is a phrase whose only content words are words of the document's title (you can record the under the title Record keeping), the way a document speaks of its subject each time it treats it. Nor are the clauses of a resolution, opening with a word in capitals and a comma (RESOLVED, that the office of … be and hereby is created), which repeat one head and one wording by form. Nor is a phrase each section of a repeated form carries once or twice (sections under the same field heading ending in a colon, or sections with the same sub-headings right below them, such as the opening of each report's Description section), since each fills in one template; it is counted when its uses outside those sections pass the limit. + ja: 一覧の飾り(各項目に付くタグやリンクの文字)、欄の名前(Cited by:)、かぎ括弧で引いた題や言葉、文書が定義した語と、名前にかかる語句は数えません。英語の、名詞と形容詞と略語だけが並ぶ用語(the federal funds rate、extended AI assistant)は言い回しではないので数えません。一つの文の中の繰り返しは 1 回と数えます。文書の題と見出しにある言い回しは主題なので数えません。よくある質問の答えは、問いの見出しごとに別々に数えます。節の最初の文にある言い回し(節ごとのひな形の書き出し)、番号を振った項目の見出し((1)…に係る規定に関する改正)、参照の項目の説明の段落(`max-age` の節、定義の並び)、句点の無い名詞で終わる一行(表の欄)に出たぶんは数えません。項目ごとの決まった結び(〜ものとすること。原案のとおり決定する。が表示されます。を記載してください。表の前の次のとおりであります。)も、文の終わりにあれば数えません。文書が引いた題の後ろで決まって使う短い結び(詳しくは「…」を確認してください。)、文書の部分を指す名前(Section 3.1、表2)にかかる語句、表の欄が 2 つ続いた後ろの一行の文(表の行の最後の欄)も数えません。枠ごとにまったく同じ形で付けるコロンで終わる一行の札(The following rule has the force of law:)、平文の仕様書の定義の並びの項目(行の頭の短い名前とコロンのあとに空白を 2 つ置いて書く定義)、リンクのすぐ後ろで決まって使う結び([…](…)を参照してください。)、数字だけを入れ替えた報告の行(There are currently 17 committers …)も、書き手の言い回しではないので数えません。英語で、名詞の入る所の -ing 形の句(what occupying the anticipatory mode requires)と、用語の後ろの be(the anticipatory mode is)は用語で、言い回しではありません。手順書が手順ごとに役割を名指しする、名詞の主語と助動詞だけの語句(the manager should)も、何をするかが毎回違うので数えません。長い文書の中で何千語も離れて使う語句(報告が測る出来事の名前 adopted a budget resolution)は、前の出現を覚えているうちに次が来ないので数えません。見出しが名付けた句に前後の語が 1 つ付いただけの語句(見出し Create a ConfigMap の下の to create a ConfigMap、見出し 旧氏を併記した場合 の下の 旧氏を併記した氏名)も主題で、数えません。角括弧でくくった画面の部品の名前に付いた語句([許可]をクリックすると、[ブックマーク]をクリックすると)は、手順ごとに違う部品を指す書き方で、数えません。中黒で並べた名詞を飾る節(育成すべき資質・能力)も、文書の決まった用語で、言い回しではありません。短い文書は測りません。見出しが一つの複合語に書いた主題を、本文が助詞を入れて書いた語句(見出しの「旧氏併記」に対する「旧氏を併記した氏」)も、主題なので数えません。数を入れた短い括弧の注に触れる語句(売上高は307,814千円(前期比99.6%)となりました。の)となりました。)も、報告が数ごとに添える書式で、数えません。同じ形で終わる文が続く所の、文の結びだけの語句(〜を強化してまいります。の してまいります。)も数えません。並んだ結びの繰り返しは repeated-sentence-ending が見ます。離れた所で何度も出る結びと、中身の動詞が入る結び(取り組んでまいります。)は言い回しとして数えます。番号を振った例の節(Example 1、事例 2 …。手順や施策の番号の節は入れません)がそれぞれ一度か二度ずつ持つ語句(例ごとの結び)、引用符でくくったファイルの名前に触れる語句(A "security.txt" file)、英語の議事録が報告ごとに置く決まった一行(No report was submitted.)も数えません。中身を持つ語が文書の題の語だけの語句(題 Record keeping の下の you can record the)も、主題を扱うたびに出る言い方で、数えません。大文字だけの語と読点で始まる議決の条項(RESOLVED, that the office of … be and hereby is created)も、条項ごとに同じ頭と言い回しで書く書式で、数えません。同じ形を繰り返す節(コロンで終わる同じ欄の名前の見出しの節、すぐ下に同じ小見出しを並べた節)がそれぞれ一度か二度ずつ持つ語句(報告ごとの Description の節の書き出し)も、項目ごとに一つのひな形を埋めたもので、数えません。並びの外の出現が上限を超えれば数えます。頭の文で自分の見出しを引用符で引く節が並ぶ所(2.5.3. Contact の下の The "Contact" field indicates …)も、仕様書が名前のある項目を節ごとに一つずつ定めた並びで、項目ごとに一度か二度ずつ持つ語句(If this field indicates …)は数えません。 + en: Tags, link text and symbols that a listing repeats on every entry, field labels (Cited by:), titles and words quoted in Japanese brackets, terms the document defines, and the words around a repeated name are not counted. A term made only of nouns, adjectives and acronyms ("the federal funds rate", "extended AI assistant") is not a phrasing and is not counted. A phrase repeated inside one sentence counts once, and one in the document's title or headings is its subject and is not counted. The answers in a FAQ are counted one question at a time. Uses in the first sentence of sections (each section's template opening), in numbered item titles ((1) ... amendment of the provision on ...), in the first paragraph of a reference entry (a section headed `max-age`, a definition list) and in a line without a full stop that ends in a noun (a table cell) are not counted, nor a fixed closing line repeated per item at the end of a Japanese sentence (〜ものとすること。, 原案のとおり決定する。, が表示されます。, を記載してください。, 次のとおりであります。 before a table). Nor are the short closing a document uses mostly right after a quoted title (詳しくは「…」を確認してください。), the words around a reference to a numbered part (set out in Section 3.1, 表2), or a one-line sentence after two table cells (the last cell of a table run into lines). Nor are a one-line label ending in a colon repeated verbatim before each box (The following rule has the force of law:), an entry of a plain-text definition list (a short name, a colon and two spaces, then the definition), the short closing a document uses mostly right after a link ([…](…)を参照してください。), or a report line filled in with different numbers (There are currently 17 committers …). In English, an -ing phrase in a noun's place (what occupying the anticipatory mode requires) and a term followed by be (the anticipatory mode is) are terms, not phrasing. Nor is a noun subject followed only by a modal (the manager should), the way a process manual names the role for each step, since what the role must do follows and differs each time. A phrase whose uses lie thousands of words apart in a long document (the event a report times, adopted a budget resolution) is not counted, since the reader has forgotten one use before the next comes. A phrase that is a heading's phrase with a word attached (to create a ConfigMap under the heading Create a ConfigMap, 旧氏を併記した氏名 under 旧氏を併記した場合) is the subject too, and is not counted. In Japanese, the words attached to a bracketed screen label ([許可]をクリックすると) are the step wording of a screen manual, each naming a different button, and are not counted. In Japanese, a clause modifying nouns joined by a nakaguro (育成すべき資質・能力) is part of one fixed term, not phrasing. Short documents are not measured. Nor is a Japanese phrase that spells out with particles a compound a heading names (旧氏を併記した氏 under a heading holding 旧氏併記), since it is the subject. Nor are the words touching a short Japanese aside holding a figure (the )となりました。 of 売上高は307,814千円(前期比99.6%)となりました。), the frame a report puts around each number. Nor is a Japanese phrase made only of a sentence ending (the してまいります。 of 〜を強化してまいります。) where sentences in a row end the same way, since repeated-sentence-ending reports that run; the same ending scattered through a document, or an ending that holds a verb with content (取り組んでまいります。), is still a phrasing. Nor is a phrase each numbered example (Example 1, Example 2, …; not numbered steps or measures) carries once or twice, such as the closing of each worked example, the words touching a quoted file name (A "security.txt" file), or the fixed line board minutes give each report (No report was submitted.). Nor is a phrase whose only content words are words of the document's title (you can record the under the title Record keeping), the way a document speaks of its subject each time it treats it. Nor are the clauses of a resolution, opening with a word in capitals and a comma (RESOLVED, that the office of … be and hereby is created), which repeat one head and one wording by form. Nor is a phrase each section of a repeated form carries once or twice (sections under the same field heading ending in a colon, or sections with the same sub-headings right below them, such as the opening of each report's Description section), since each fills in one template; it is counted when its uses outside those sections pass the limit. The same holds for sibling sections that each quote their own heading in their first sentence (The "Contact" field indicates … under 2.5.3. Contact), a specification defining one named item per section, where a phrase each item carries once or twice (If this field indicates …) is not counted. level_meaning: ja: 同じ言い回し {limit} 回まで en: up to {limit} {limit|use|uses} of the same phrase diff --git a/text/packages/chaff/src/detectors/gram-repetition.ts b/text/packages/chaff/src/detectors/gram-repetition.ts index aca2dad2c..85aef5983 100644 --- a/text/packages/chaff/src/detectors/gram-repetition.ts +++ b/text/packages/chaff/src/detectors/gram-repetition.ts @@ -20,7 +20,7 @@ import { isSentenceEndingOnly, sentencesInEndingRuns } from "./gram-ending.ts"; import { gramPassagesOf } from "./gram-passages.ts"; import { narrowestStretch, sentencePositions } from "./gram-spread.ts"; import { namedByHeading } from "./gram-heading.ts"; -import { carriedBySiblings, numberedSiblingsOf, repeatedBlocksOf } from "./gram-siblings.ts"; +import { carriedBySiblings, namedEntriesOf, numberedSiblingsOf, repeatedBlocksOf } from "./gram-siblings.ts"; import { enactingClauses, entryOpeners, @@ -213,6 +213,7 @@ const repeatsOf = (doc: ProseDocument, limit: number): Repeat[] => { const positions = sentencePositions(doc.sentences, doc.lengthUnit); const siblings = new Map([ ...repeatedBlocksOf(doc.sections), + ...namedEntriesOf(doc.structure, doc.sentences, doc.source), ...numberedSiblingsOf( doc.sections, (doc.lexicons["ngram-example-heading"] ?? []).map((entry) => entry.pattern), diff --git a/text/packages/chaff/src/detectors/gram-siblings.ts b/text/packages/chaff/src/detectors/gram-siblings.ts index 5076dd022..06b2bebf1 100644 --- a/text/packages/chaff/src/detectors/gram-siblings.ts +++ b/text/packages/chaff/src/detectors/gram-siblings.ts @@ -1,4 +1,5 @@ -import type { Section, Sentence } from "../plugin.ts"; +import type { Section, Sentence, StructureNode } from "../plugin.ts"; +import { inDocumentOrder } from "../structure/issues.ts"; const DIGITS = new Set("01234567890123456789"); const NUMBER_CLOSERS = new Set("..::"); @@ -92,6 +93,42 @@ export const repeatedBlocksOf = (sections: readonly Outlined[]): ReadonlyMap = new Set(["section", "chapter", "article"]); +/** 項目の名前を引く、見出しの行の後ろの頭の文の数。 */ +const NAMING_SENTENCES = 2; + +const quotesName = (name: string, text: string): boolean => [`"${name}"`, `“${name}”`].some((quoted) => text.includes(quoted)); + +/** 見出しの付いた節点の見出しと、その範囲の文。 */ +const entryOf = (node: StructureNode, sentences: readonly Sentence[]): Heading => ({ + heading: String(node.attrs["heading"] ?? "").trim(), + sentences: sentences.filter((sentence) => node.span.start <= sentence.span.start && sentence.span.start < node.span.end), +}); + +/** 節の見出しの行の後ろの頭の文が、その見出しを引用符で引くか。 */ +const namesItself = (node: StructureNode, sentences: readonly Sentence[], source: string): boolean => { + const entry = entryOf(node, sentences); + const lineEnd = source.indexOf("\n", node.span.start); + const body = entry.sentences.filter((sentence) => lineEnd !== -1 && sentence.span.start > lineEnd); + return entry.heading !== "" && body.slice(0, NAMING_SENTENCES).some((sentence) => quotesName(entry.heading, sentence.text)); +}; + +/** + * 並んだ節のうち、頭の文で自分の見出しを引用符で引く節(2.5.3. Contact / The "Contact" field indicates …)の文と、その節。 + * 仕様書は節ごとに名前のある項目を一つ定め、項目ごとに同じひな形を埋める(If this field indicates …)。読み手は項目を一つずつ引く。 + */ +export const namedEntriesOf = (tree: StructureNode | undefined, sentences: readonly Sentence[], source: string): ReadonlyMap => { + const owners = new Map(); + (tree === undefined ? [] : inDocumentOrder(tree)).forEach((parent) => { + const named = parent.children + .filter((child) => HEADED_KINDS.has(child.kind) && namesItself(child, sentences, source)) + .map((child) => entryOf(child, sentences)); + if (named.length >= MIN_SIBLINGS) named.forEach((entry) => entry.sentences.forEach((sentence) => owners.set(sentence, entry))); + }); + return owners; +}; + /** * ひな形の節(番号を振った例、同じ形を繰り返す節)がそれぞれ一度か二度ずつ持つ語句。例ごとに同じ結び(The digital link requirement * starts from …)を置き、報告ごとに同じ書き出し(The mission of … is the creation and maintenance of software related to …)で欄を diff --git a/text/test/test_ngram_formulas.ts b/text/test/test_ngram_formulas.ts index a4dae7f63..37ba1f5b2 100644 --- a/text/test/test_ngram_formulas.ts +++ b/text/test/test_ngram_formulas.ts @@ -179,4 +179,20 @@ describe("ngram-repetition: a fixed formula an item carries", () => { assert.match(worst(en, `# Bookkeeping basics\n\n${body("record")}\n`) ?? "", /record/u); assert.notEqual(worst(en, `# Record keeping\n\n${body("file")}\n`), undefined); }); + it("does not count the rule each section that quotes its own name carries, and counts it where sections do not name themselves", () => { + const fields = ["Archive", "Badge", "Calendar", "Digest", "Mirror", "Roster", "Ticket"]; + const entry = (field: string, index: number, opening: string): string => + `## ${field}\n\n${opening} a place where ${SUBJECTS[index] ?? ""} are kept.\n\nIf this field indicates a web URI, then it must use a secure scheme.\n`; + const named = fields.map((field, index) => entry(field, index, `The "${field}" field indicates`)); + assert.equal(worst(en, `# Field Definitions\n\n${named.join("\n")}`), undefined); + const plain = fields.map((field, index) => entry(field, index, `${field} points to`)); + assert.match(worst(en, `# Field Definitions\n\n${plain.join("\n")}`) ?? "", /this field indicates/u); + }); + + it("reads a plain-text specification's numbered sections the same way", () => { + const fields = ["Archive", "Badge", "Calendar", "Digest", "Mirror", "Roster", "Ticket"]; + const entry = (field: string, index: number): string => + `3.${String(index + 1)}. ${field}\n\n The "${field}" field indicates a place where ${SUBJECTS[index] ?? ""} are kept.\n\n If this field indicates a web URI, then it must use a secure scheme.\n`; + assert.equal(worst(en, `3. Field Definitions\n\n${fields.map(entry).join("\n")}`), undefined); + }); });