diff --git a/text/packages/chaff/rules/max-sentence-length.yaml b/text/packages/chaff/rules/max-sentence-length.yaml index a67a59d78..4567d6146 100644 --- a/text/packages/chaff/rules/max-sentence-length.yaml +++ b/text/packages/chaff/rules/max-sentence-length.yaml @@ -104,9 +104,9 @@ example: not_flagged: ja: >- - 見出し、表のセル、コードは数えません。メールの件名の欄の値(「件名:」「SUBJECT:」の行や、その下の行に書いた件名)も、文ではないので数えません(件名の長さは email-subject-length が見ます)。句点の無い、名前や肩書きを「、」で並べただけの行(出席者の一覧)も数えません。印刷版を文字にした文書が文の途中に残すページの印(「( printed page 52044)」「- 12 -」)も数えません。注の頭の札と番号(「(注)1.」「※3:」)も数えません。脚注の文献の記載(著者 (2025), "題," 出版者)は、その前に書き手が置いた言葉(For example, see …)だけを数え、前の文献の終わり((Washington: …); and)から始まる文は、そこまでを数えません。姓と頭文字で並べた文献の著者(Kreimer AR, Porras C, …)は一語として数えます。括弧の中で閉じた文(「….)」)は、後ろの文と別に測ります。句点で閉じた文を二つ以上引いた「」(「…転落。…入院。」)は、引いた文を一つずつ、括弧の外を別に測ります。すぐ前か、すぐ後ろの括弧に条を挙げて引いた一文(第13条「…ものとする。」)も同じです。句点の無い「項目:値」の行が続くもの(日時:…/場所:…)は一行ずつ、変換で区切りが消えて一続きになった表(桁区切りの崩れた数字の列、行頭の年月が前のセルに続く年表、数の後ろに括弧書きの数を添えたセルが続く行(201(16)…831(-)))はセルごとに測ります。行の頭に置いたリンクの題名に紹介文が空白一つで続く行(「法令」×「デジタル」ハッカソン 「法令」×…を開催しました。)は、題名が括弧で始まるか、後ろの文にもう一度出てくるとき、題名と紹介文を別に測ります。コロンの後に引いた「"…"」は括弧の外と別に測ります。仕様書の文法(name = a / b)、引用記号で始まる法令や文書の列挙(See, e.g., 12 C.F.R. § …; …)は数えません。英文の題名の後の括弧書きは括弧の中だけを、文書の参照(番号、"題名"、(日付))は題名を除いて数えます。引用記号で始まる脚注(See …)は、引いた法令や文書の名前・"題名"・略称や番号の括弧を除いた言葉だけを数えます。他の文書をそのまま写した文(文書がほかで名前を挙げて引いている法律を見出しにした節の条、「reads as follows:」のような前置きの次の段落)は数えません。法令や論文のように長い一文が普通の種類では、ジャンルが上限を長くします。 + 見出し、表のセル、コードは数えません。メールの件名の欄の値(「件名:」「SUBJECT:」の行や、その下の行に書いた件名)も、文ではないので数えません(件名の長さは email-subject-length が見ます)。句点の無い、名前や肩書きを「、」で並べただけの行(出席者の一覧)も数えません。印刷版を文字にした文書が文の途中に残すページの印(「( printed page 52044)」「- 12 -」)も数えません。注の頭の札と番号(「(注)1.」「※3:」)も数えません。脚注の文献の記載(著者 (2025), "題," 出版者)は、その前に書き手が置いた言葉(For example, see …)だけを数え、前の文献の終わり((Washington: …); and)から始まる文は、そこまでを数えません。姓と頭文字で並べた文献の著者(Kreimer AR, Porras C, …)は一語として数えます。コロンの後に読点で並べた、頭だけ大文字の三つ以上の題名(New books include: The lighthouse keeper, A short history of tea, …)も、一つずつを一語として数えます。語ごとに大文字で始める名前の並びは数えます。括弧の中で閉じた文(「….)」)は、後ろの文と別に測ります。句点で閉じた文を二つ以上引いた「」(「…転落。…入院。」)は、引いた文を一つずつ、括弧の外を別に測ります。すぐ前か、すぐ後ろの括弧に条を挙げて引いた一文(第13条「…ものとする。」)も同じです。句点の無い「項目:値」の行が続くもの(日時:…/場所:…)は一行ずつ、変換で区切りが消えて一続きになった表(桁区切りの崩れた数字の列、行頭の年月が前のセルに続く年表、数の後ろに括弧書きの数を添えたセルが続く行(201(16)…831(-)))はセルごとに測ります。行の頭に置いたリンクの題名に紹介文が空白一つで続く行(「法令」×「デジタル」ハッカソン 「法令」×…を開催しました。)は、題名が括弧で始まるか、後ろの文にもう一度出てくるとき、題名と紹介文を別に測ります。コロンの後に引いた「"…"」は括弧の外と別に測ります。仕様書の文法(name = a / b)、引用記号で始まる法令や文書の列挙(See, e.g., 12 C.F.R. § …; …)は数えません。英文の題名の後の括弧書きは括弧の中だけを、文書の参照(番号、"題名"、(日付))は題名を除いて数えます。引用記号で始まる脚注(See …)は、引いた法令や文書の名前・"題名"・略称や番号の括弧を除いた言葉だけを数えます。他の文書をそのまま写した文(文書がほかで名前を挙げて引いている法律を見出しにした節の条、「reads as follows:」のような前置きの次の段落)は数えません。法令や論文のように長い一文が普通の種類では、ジャンルが上限を長くします。 en: >- - Headings, table cells and code are not counted, nor is the value of an email's subject field (on the "Subject:" line, or on the line under a bare "SUBJECT:"), which email-subject-length measures, nor is a line that only lists names or titles with 、 and has no full stop (an attendee list), nor are the page markers a converted print edition leaves inside a sentence ("( printed page 52044)", "[[Page 52044]]"), nor, in Japanese, the label and number that open a note ((注)1., ※3:). A footnote that is a bibliographic citation (authors (2025), "Title," publisher) counts only the words the writer puts before it ("For example, see …"), and not the end of an earlier reference it begins inside ("(Washington: …); and"). The authors of a reference written as surname and initials (Kreimer AR, Porras C, …) count as one word. A sentence closed inside parentheses ("….)") is measured apart from the sentence after it. A 「」 quotation holding two or more sentences closed by 。 is measured sentence by sentence, apart from the words around it, and so is one sentence quoted with the article it comes from named right before it or in the bracket after it (第13条「…ものとする。」). Lines of a label and a value with no full stop (When: … / Where: …) are measured one line at a time, and a table a converter ran together (numbers whose digit groups broke, a chronology whose dates run into the cell before, rows of a figure with another in brackets after it: 201(16) … 831(-)) cell by cell. In Japanese, a link title at the head of a line that runs into its blurb after one space (「法令」×「デジタル」ハッカソン 「法令」×…を開催しました。) is measured apart from the blurb, when the title opens with a bracket or comes back in the sentence after it. A quotation opened after a colon (has observed: "…") is measured apart from the writer's words. A grammar in a specification (name = a / b), a citation string after a signal (See, e.g., 12 C.F.R. § …; …), the title before a remark in parentheses and the quoted title of a reference line (Letter 21-8, "Title" (April 9, 2021).) are not counted. A footnote that opens with a signal (See …) counts only its words outside the names, "titles" and parentheses of codes it cites. Text copied word for word from another document is not counted: the articles of a statute under a heading that names it, where the document cites that statute elsewhere, and the paragraph after a lead-in such as "reads as follows:". Genres where long sentences are normal (statutes, papers) raise the limit. + Headings, table cells and code are not counted, nor is the value of an email's subject field (on the "Subject:" line, or on the line under a bare "SUBJECT:"), which email-subject-length measures, nor is a line that only lists names or titles with 、 and has no full stop (an attendee list), nor are the page markers a converted print edition leaves inside a sentence ("( printed page 52044)", "[[Page 52044]]"), nor, in Japanese, the label and number that open a note ((注)1., ※3:). A footnote that is a bibliographic citation (authors (2025), "Title," publisher) counts only the words the writer puts before it ("For example, see …"), and not the end of an earlier reference it begins inside ("(Washington: …); and"). The authors of a reference written as surname and initials (Kreimer AR, Porras C, …) count as one word. So does each of three or more titles in sentence case listed after a colon (New books include: The lighthouse keeper, A short history of tea, …); names written in title case are still counted. A sentence closed inside parentheses ("….)") is measured apart from the sentence after it. A 「」 quotation holding two or more sentences closed by 。 is measured sentence by sentence, apart from the words around it, and so is one sentence quoted with the article it comes from named right before it or in the bracket after it (第13条「…ものとする。」). Lines of a label and a value with no full stop (When: … / Where: …) are measured one line at a time, and a table a converter ran together (numbers whose digit groups broke, a chronology whose dates run into the cell before, rows of a figure with another in brackets after it: 201(16) … 831(-)) cell by cell. In Japanese, a link title at the head of a line that runs into its blurb after one space (「法令」×「デジタル」ハッカソン 「法令」×…を開催しました。) is measured apart from the blurb, when the title opens with a bracket or comes back in the sentence after it. A quotation opened after a colon (has observed: "…") is measured apart from the writer's words. A grammar in a specification (name = a / b), a citation string after a signal (See, e.g., 12 C.F.R. § …; …), the title before a remark in parentheses and the quoted title of a reference line (Letter 21-8, "Title" (April 9, 2021).) are not counted. A footnote that opens with a signal (See …) counts only its words outside the names, "titles" and parentheses of codes it cites. Text copied word for word from another document is not counted: the articles of a statute under a heading that names it, where the document cites that statute elsewhere, and the paragraph after a lead-in such as "reads as follows:". Genres where long sentences are normal (statutes, papers) raise the limit. level_meaning: ja: 一文 {limit} 字まで en: up to {limit} {limit|word|words} in a sentence diff --git a/text/packages/chaff/src/detectors/sentence-length.ts b/text/packages/chaff/src/detectors/sentence-length.ts index 1c5fddd74..c4c0e0ee9 100644 --- a/text/packages/chaff/src/detectors/sentence-length.ts +++ b/text/packages/chaff/src/detectors/sentence-length.ts @@ -13,6 +13,7 @@ import { isReproduced, reproducedSpans } from "./reproduced-text.ts"; import { runInTitleParts } from "./run-in-title.ts"; import { colonQuotationParts, figureCellParts, flattenedTableParts, isGrammarBlock, labelLineParts, parentheticalSentenceParts } from "./sentence-parts.ts"; import { isInListItem, isReferenceLine, isTitleLine, quotedTitleWords, remarkAfterTitle, termSeriesSaving } from "./sentence-title.ts"; +import { titleSeriesSaving } from "./title-series.ts"; type Measure = { readonly size: number; readonly limit: number; readonly variant?: string }; @@ -42,7 +43,10 @@ const noteLabelSize = (text: string, unit: LengthUnit, words: SentenceWords): nu * Only where words are the unit: the shapes are read from capitals and spaces. */ const unwrittenWords = (sentence: Sentence, text: string, listItems: SentenceWords["listItems"]): number => - termSeriesSaving(text) + authorListSaving(text) + (isInListItem(sentence.span, listItems) || isReferenceLine(text) ? quotedTitleWords(text) : 0); + termSeriesSaving(text) + + authorListSaving(text) + + titleSeriesSaving(text) + + (isInListItem(sentence.span, listItems) || isReferenceLine(text) ? quotedTitleWords(text) : 0); /** * A comma or a stop typed with one space before it ("the same brick , and"). It is no word of its own. One space only: diff --git a/text/packages/chaff/src/detectors/title-series.ts b/text/packages/chaff/src/detectors/title-series.ts new file mode 100644 index 000000000..c0478a439 --- /dev/null +++ b/text/packages/chaff/src/detectors/title-series.ts @@ -0,0 +1,38 @@ +// A run of titles written in sentence case after a colon (New books include: The lighthouse keeper, A short history of +// tea, and …), the way a catalogue writes them: each title, however many words it holds, is one entry of the list, not +// words the writer built into a sentence. Names in title case (Agua Caliente Band of Cahuilla Indians, …) are the +// writer's own long sentence and stay counted. Pure. + +const WORD = /[\p{L}\p{N}][\p{L}\p{N}'’&.-]*/gu; +/** Fewer titles in a row than this are a sentence going on after its colon, not a list. */ +const MIN_ITEMS = 3; +const ITEM_SEPARATOR = /,\s+(?:and\s+|or\s+)?/u; +const STARTS_UPPER = /^\p{Lu}/u; +/** A lower-case word this long is a content word (bridge, story), not one a title-case name keeps small (of, and, the). */ +const CONTENT_WORD_LENGTH = 4; +const COLON = /:\s+/gu; + +const wordsOf = (text: string): string[] => text.match(WORD) ?? []; + +/** A title in sentence case: a capital first word, and a content word after it in lower case. */ +const isSentenceCaseTitle = (item: string): boolean => + STARTS_UPPER.test(item) && + wordsOf(item) + .slice(1) + .some((word) => word.length >= CONTENT_WORD_LENGTH && /^\p{Ll}/u.test(word)); + +/** The titles after one colon, up to the first item that is not one. */ +const leadingTitles = (rest: string): string[] => { + const items = rest.split(ITEM_SEPARATOR); + const stop = items.findIndex((item) => !isSentenceCaseTitle(item)); + return stop === -1 ? items : items.slice(0, stop); +}; + +const savingOf = (titles: readonly string[]): number => titles.reduce((saving, title) => saving + Math.max(0, wordsOf(title).length - 1), 0); + +/** How many words fewer the sentence is when each title listed after a colon counts as one word. */ +export const titleSeriesSaving = (text: string): number => + [...text.matchAll(COLON)] + .map((match) => leadingTitles(text.slice(match.index + match[0].length))) + .filter((titles) => titles.length >= MIN_ITEMS) + .reduce((most, titles) => Math.max(most, savingOf(titles)), 0); diff --git a/text/test/test_sentence_length_titles.ts b/text/test/test_sentence_length_titles.ts index ca8e7498c..03023ad97 100644 --- a/text/test/test_sentence_length_titles.ts +++ b/text/test/test_sentence_length_titles.ts @@ -3,6 +3,7 @@ import assert from "node:assert/strict"; import { namedRuleRun } from "./rule-run.ts"; import { adapter as en } from "../packages/lang-en/src/index.ts"; import { isTitleCase, isTitleLine, quotedTitleWords, termSeriesSaving } from "../packages/chaff/src/detectors/sentence-title.ts"; +import { titleSeriesSaving } from "../packages/chaff/src/detectors/title-series.ts"; // max-sentence-length and words the writer did not build into a sentence: a title line with no full stop, a quoted title // in a list item, and quoted terms listed one after another. A long sentence beside them is still reported. Every text is @@ -71,3 +72,26 @@ describe("max-sentence-length: titles and quoted term runs", () => { assert.equal(findingsOf(`Notes:\n\nThe desk wrote ${subject}.`).length, 1); }); }); + +describe("max-sentence-length: titles listed after a colon", () => { + const TITLES = + "New books this month include: The lighthouse keeper of the northern bay, A short history of tea in the old ports, The garden that grew back after the long winter, Notes from a quiet river town in the mountains, and Letters to a young printer on the craft of setting type."; + const NAMES = + "The valley is home to many groups today: North Ridge Band of River Peoples, South Ridge Band of River Peoples, East Valley Tribe of Hill Peoples, West Valley Tribe of Hill Peoples, Lake Shore Nation of Fishing Peoples, and the Desert Springs Nation."; + const PROSE = + "The library added new books this month: the staff chose them after a long survey of readers in the northern towns, and they hope that the readers will borrow them often during the winter when the roads are closed and the reading rooms are full."; + + it("titleSeriesSaving: each listed title counts as one word", () => { + assert.equal(titleSeriesSaving("New: The lighthouse keeper, A short history of tea, and The garden plan."), 8); + assert.equal(titleSeriesSaving("New: The lighthouse keeper and A short history of tea."), 0); + assert.equal(titleSeriesSaving("Members: Ann Lee of Leeds, Bo Chan of York, and Cy Moss of Hull."), 0); + assert.equal(titleSeriesSaving("Note: the parser, the renderer and the exporter ship."), 0); + assert.equal(titleSeriesSaving(""), 0); + }); + + it("a sentence that is mostly a list of titles is not reported; long prose or names in title case after a colon are", () => { + assert.deepEqual(findingsOf(TITLES), []); + assert.equal(findingsOf(PROSE).length, 1); + assert.equal(findingsOf(NAMES).length, 1); + }); +});