diff --git a/text/corpus/rules-measure.json b/text/corpus/rules-measure.json index 2ea9202c3..4bd295bd9 100644 --- a/text/corpus/rules-measure.json +++ b/text/corpus/rules-measure.json @@ -5033,6 +5033,42 @@ "falseAlarms": 0 } }, + "inline-triad": { + "groups": { + "academic": { + "documents": 5, + "fired": 0 + }, + "blog": { + "documents": 9, + "fired": 0 + }, + "business": { + "documents": 41, + "fired": 0 + }, + "docs": { + "documents": 11, + "fired": 0 + }, + "legal": { + "documents": 14, + "fired": 0 + }, + "literature": { + "documents": 9, + "fired": 0 + }, + "speech": { + "documents": 3, + "fired": 0 + }, + "technical": { + "documents": 27, + "fired": 0 + } + } + }, "installment-total-mismatch": { "groups": { "academic": { diff --git a/text/docs/samples/en/README.md b/text/docs/samples/en/README.md index 7a30ec379..b82fe3627 100644 --- a/text/docs/samples/en/README.md +++ b/text/docs/samples/en/README.md @@ -11,7 +11,7 @@ Or paste a draft into the [playground](https://isamu.github.io/lab/en/playground 日本語の見本: [../ja/README.md](../ja/README.md) -Rules with a sample: 268 / 268 +Rules with a sample: 269 / 269 ## Readability @@ -314,6 +314,7 @@ Traits common in generated text. None of them alone says the text was generated; | [Headings decorated with emoji](emoji-heading.md) (`emoji-heading`) | Headings with an emoji in them ("## 🚀 Getting started"), one after another | | [Too many decorative figures](figurative-density.md) (`figurative-density`) | Figures such as "like magic" or "secret sauce" piled up instead of the mechanism | | [An honesty marker](honesty-marker.md) (`honesty-marker`) | A sentence flagged as candid ("honestly", "if I'm being honest") | +| [Threes strung on commas](inline-triad.md) (`inline-triad`) | Three short phrases strung on commas with no "and", again and again | | [Too many dashes](no-em-dash.md) (`no-em-dash`) | Too many dashes, which is also one of the better-known marks of generated text | | [Too many sweeping claims about people](over-general-claim.md) (`over-general-claim`) | Sweeping claims about people ("most people", "nobody") piled up | | [Paragraph lengths are too uniform](paragraph-length-variance.md) (`paragraph-length-variance`) | Paragraphs all of about the same length, as if poured into a mould | diff --git a/text/docs/samples/en/inline-triad.md b/text/docs/samples/en/inline-triad.md new file mode 100644 index 000000000..305c03408 --- /dev/null +++ b/text/docs/samples/en/inline-triad.md @@ -0,0 +1,190 @@ + + +# Threes strung on commas (`inline-triad`) + +**Where it helps:** essays, blog posts, newsletters. Any prose drafted with an AI that keeps reaching for three. + +## Original + +```markdown chaff-before +# Learning to cook late + +For years my idea of dinner was pasta with sauce from a jar. +At thirty I decided that had to change, and I bought a pan that was not already scratched. + +I started with simple things: scrambled eggs, roasted vegetables, a basic tomato soup. +I burned the garlic more often than I can count, and the smoke alarm learned my schedule. +Most dishes, I noticed, begin the same way: an onion, some garlic, a little oil. +After a month I stopped reading the recipe line by line and began to taste as I went. + +What changed most was the evening itself. +Instead of scrolling while I waited for a delivery, I was chopping, stirring, paying attention. +I forgot about work. +I thought about whether the onions were ready, and whether the soup needed more salt. + +Later I started asking friends over for dinner, which I had always avoided. +The meals were not perfect. +But people do not really come for the food. +They come for the table, the conversation, the feeling of being cared for. +Cooking gave me a way to offer that, and it still does on most weeknights. + +I am still not a great cook. +The list of failures is much longer, and I keep adding to it every month. +But I no longer feel that cooking is something I missed. +It is something I am learning slowly, one meal at a time, in a small kitchen. +If you think it is too late to learn something new, I would gently suggest that it is not. +Being a beginner as an adult is uncomfortable, but there is a quiet pleasure in it as well. +``` + +## What chaff says + +```text chaff-output +$ npx chaffjs --no-guide --genre blog/essay cooking.md + +cooking.md blog/essay · English genre from --genre + +─── line 6 ─────────────────────────────────────────────────── + + I started with simple things: scrambled eggs, roasted vegetables, a basic tomato soup. + + · Threes strung on commas + + "scrambled eggs, roasted vegetables, a basic tomato soup" and other threes strung on commas: 14 per 1000 words (limit 3) + Generated prose keeps stringing three short phrases on commas with no "and" between them: "the table, the conversation, the feeling of being cared for". Three feels complete, so two things become three and four become three. Once is a turn of phrase; again and again, the pattern shows more than the content. + + → Keep three only where there really are three. Drop the item added to make three, and join the rest with "and". + + Relax this rule: npx chaffjs relax inline-triad + + +─── line 8 ─────────────────────────────────────────────────── + + Most dishes, I noticed, begin the same way: an onion, some garlic, a little oil. + + · Threes strung on commas + + "an onion, some garlic, a little oil" and other threes strung on commas: 14 per 1000 words (limit 3) + Generated prose keeps stringing three short phrases on commas with no "and" between them: "the table, the conversation, the feeling of being cared for". Three feels complete, so two things become three and four become three. Once is a turn of phrase; again and again, the pattern shows more than the content. + + → Keep three only where there really are three. Drop the item added to make three, and join the rest with "and". + + Relax this rule: npx chaffjs relax inline-triad + + +─── line 12 ────────────────────────────────────────────────── + + Instead of scrolling while I waited for a delivery, I was chopping, stirring, paying attention. + + · Threes strung on commas + + "I was chopping, stirring, paying attention" and other threes strung on commas: 14 per 1000 words (limit 3) + Generated prose keeps stringing three short phrases on commas with no "and" between them: "the table, the conversation, the feeling of being cared for". Three feels complete, so two things become three and four become three. Once is a turn of phrase; again and again, the pattern shows more than the content. + + → Keep three only where there really are three. Drop the item added to make three, and join the rest with "and". + + Relax this rule: npx chaffjs relax inline-triad + + +─── line 19 ────────────────────────────────────────────────── + + They come for the table, the conversation, the feeling of being cared for. + + · Threes strung on commas + + "They come for the table, the conversation, the feeling of being cared for" and other threes strung on commas: 14 per 1000 words (limit 3) + Generated prose keeps stringing three short phrases on commas with no "and" between them: "the table, the conversation, the feeling of being cared for". Three feels complete, so two things become three and four become three. Once is a turn of phrase; again and again, the pattern shows more than the content. + + → Keep three only where there really are three. Drop the item added to make three, and join the rest with "and". + + Relax this rule: npx chaffjs relax inline-triad + + +──────────────────────────────────────────────────────────── + + 4 notes All judged by machine + (the same text gives the same result every time) + + The text was not changed. Fixing it is the writer's job. +``` + +## Corrected + +```markdown chaff-after +# Learning to cook late + +For years my idea of dinner was pasta with sauce from a jar. +At thirty I decided that had to change, and I bought a pan that was not already scratched. + +I started with scrambled eggs and a basic tomato soup. +I burned the garlic more often than I can count, and the smoke alarm learned my schedule. +Most dishes, I noticed, begin the same way, with an onion and a little oil. +After a month I stopped reading the recipe line by line and began to taste as I went. + +What changed most was the evening itself. +Instead of scrolling while I waited for a delivery, I was chopping and paying attention. +I forgot about work. +I thought about whether the onions were ready, and whether the soup needed more salt. + +Later I started asking friends over for dinner, which I had always avoided. +The meals were not perfect. +But people do not really come for the food. +They come for the conversation and the feeling of being cared for. +Cooking gave me a way to offer that, and it still does on most weeknights. + +I am still not a great cook. +The list of failures is much longer, and I keep adding to it every month. +But I no longer feel that cooking is something I missed. +It is something I am learning slowly, one meal at a time, in a small kitchen. +If you think it is too late to learn something new, I would gently suggest that it is not. +Being a beginner as an adult is uncomfortable, but there is a quiet pleasure in it as well. +``` + +## How it was fixed + +The draft lists things in threes again and again, each time with commas and no "and": three first dishes, three things in the pan, three things done at the stove, three reasons friends come over. +One such run is a turn of phrase. Four in a short essay make the pattern louder than what the writer is saying. + +The fix asks of each run whether there really are three things. +Where one item only filled the slot (the roasted vegetables, the garlic beside the onion, the stirring beside the chopping, the table beside the conversation), it goes, and the two that are left are joined with "and". + +## Settings + +The rule counts three short phrases strung on commas with no "and" or "or", per 1000 words. +A list joined with "and" is not counted, since human prose writes those as often. +Runs holding a figure, capitalised names and titles, one word said again (old, old, old), runs inside a list or quotation marks, and runs of four or more are left out. +A document shorter than 200 words, or with fewer than three runs, is not measured. + +```yaml chaff-settings +rules: + inline-triad: relaxed +``` + +The rule runs only on English. + +```text chaff-explain +$ npx chaffjs explain inline-triad --genre blog/essay + + Threes strung on commas (inline-triad) + + Generated prose keeps stringing three short phrases on commas with no "and" between them: "the table, the conversation, the feeling of being cared for". Three feels complete, so two things become three and four become three. Once is a turn of phrase; again and again, the pattern shows more than the content. + + How to fix: Keep three only where there really are three. Drop the item added to make three, and join the rest with "and". + + Rewrite depth: light (words and sentences; the structure and the voice stay). With a shallower chaff fix-plan --depth, the plan names this rule without fixing it. + + Levels: + strict up to 2 runs of three per 1000 words + → normal up to 3 runs of three per 1000 words + relaxed up to 5 runs of three per 1000 words + off not checked + + Now: normal. + + Change it: npx chaffjs relax inline-triad --why "reason" +``` + +[All samples](README.md) diff --git a/text/packages/chaff/README.ja.md b/text/packages/chaff/README.ja.md index 344bdda5e..5fcc56b18 100644 --- a/text/packages/chaff/README.ja.md +++ b/text/packages/chaff/README.ja.md @@ -256,7 +256,7 @@ npx chaffjs ai-score article.md 低・中・高と、出てい npx chaffjs ai-score article.md --format json 同じものを JSON で ``` -短すぎる文書は測らず、そう言います。目印の一つが `email-stock-courtesy` で、英文のメールに「I hope this email finds you well」「I apologize for any inconvenience」のような決まり文句が 2 つ以上重なった所を言います。`honesty-marker` は、「正直、」「正直に言うと」「if I'm being honest」のように本音だと前置きする言い回しを、ブログと業務文書では 1 つでも言います。もう一つの例が `closing-exhortation` で、「ぜひ〜してみてください」「きっと〜はずです」のように結びで読み手を励ます文が 2 つ以上重なった所を言います。`cold-open` は、状況も例もないまま、短い願い(「〜に任せたい。」)、前の文の語の引き直し(この「だいたい」が)、難所の予告(「それだけのことに見えて」)が書き出しに重なった所を言います。`unprompted-denial` は、「〜の話ではありません」「〜の材料になりません」のように、誰も言っていない前提を打ち消して終わる文が多い所を言います。 +短すぎる文書は測らず、そう言います。目印の一つが `email-stock-courtesy` で、英文のメールに「I hope this email finds you well」「I apologize for any inconvenience」のような決まり文句が 2 つ以上重なった所を言います。`honesty-marker` は、「正直、」「正直に言うと」「if I'm being honest」のように本音だと前置きする言い回しを、ブログと業務文書では 1 つでも言います。`inline-triad` は、「the table, the conversation, the feeling of being cared for」のように、短い句を三つ、and を入れずにカンマだけで並べる形が何度も出る所を言います(英語の文書だけ)。もう一つの例が `closing-exhortation` で、「ぜひ〜してみてください」「きっと〜はずです」のように結びで読み手を励ます文が 2 つ以上重なった所を言います。`cold-open` は、状況も例もないまま、短い願い(「〜に任せたい。」)、前の文の語の引き直し(この「だいたい」が)、難所の予告(「それだけのことに見えて」)が書き出しに重なった所を言います。`unprompted-denial` は、「〜の話ではありません」「〜の材料になりません」のように、誰も言っていない前提を打ち消して終わる文が多い所を言います。 ## AI の評価に使う(chaff grade) @@ -367,7 +367,8 @@ rule the rewrite raised; `npx chaffjs test --checks ai-tells` asks about **AI score.** `chaff ai-score ` — a quick AI-likeness score (low, medium, high): counts the signs of generated text that human documents of the same genre rarely show. It is not a verdict on whether AI wrote it. One sign is `email-stock-courtesy`: two or more stock email courtesies in one English email. `honesty-marker`: a sentence flagged as -candid ("Honestly,", 「正直、」), reported alone in blog posts and business documents. +candid ("Honestly,", 「正直、」), reported alone in blog posts and business documents. `inline-triad`: three short +phrases strung on commas with no "and", again and again (English only). `closing-exhortation`: a closing that cheers the reader on two or more times. `unprompted-denial`: many sentences that deny a premise nobody stated ("This isn't about tooling."). diff --git a/text/packages/chaff/README.md b/text/packages/chaff/README.md index 51fbd71bc..8540e50c6 100644 --- a/text/packages/chaff/README.md +++ b/text/packages/chaff/README.md @@ -208,7 +208,8 @@ A quick AI-likeness reading: low, medium or high. It counts the signs of generat human documents of the same genre rarely show. It is not a verdict on whether AI wrote it, and it is not meant to be one. Among the signs: `email-stock-courtesy`, two or more stock email courtesies ("I hope this email finds you well", "I apologize for any inconvenience") in one English email; `honesty-marker`, a sentence flagged -as candid ("Honestly,", "if I'm being honest", 「正直、」), reported alone in blog posts and business documents; `closing-exhortation`, a closing that cheers +as candid ("Honestly,", "if I'm being honest", 「正直、」), reported alone in blog posts and business documents; `inline-triad`, three short phrases strung on commas with no "and", +again and again ("the table, the conversation, the feeling of being cared for"; English only); `closing-exhortation`, a closing that cheers the reader on two or more times ("You've got this", 「ぜひ〜してみてください」); `cold-open`, an opening that piles a bare wish, a word of the previous sentence repeated in quotes, and a teaser ("Sounds simple. It isn't.") before any situation or example; and `unprompted-denial`, many sentences that deny a premise nobody stated ("This isn't about tooling.", 「〜の話ではありません」). diff --git a/text/packages/chaff/rules/inline-triad.yaml b/text/packages/chaff/rules/inline-triad.yaml new file mode 100644 index 000000000..2d5244eda --- /dev/null +++ b/text/packages/chaff/rules/inline-triad.yaml @@ -0,0 +1,63 @@ +id: inline-triad +layer: L2 +status: stable +how_to_find: inline-triad +extra_word_lists: [inline-triad] +languages: [en] +severity: info + +name: + ja: 文の中で三つずつ並べる + en: Threes strung on commas + +why: + ja: 生成された文章は、"the table, the conversation, the feeling of being cared for" のように、短い句を三つ、and も or も入れずにカンマだけでつなぐ形を繰り返します。三つは収まりがよいので、言いたいことが二つでも四つでも三つにされます。一つなら文の調子ですが、重なると中身より型が目立ちます。 + en: 'Generated prose keeps stringing three short phrases on commas with no "and" between them: "the table, the conversation, the feeling of being cared for". Three feels complete, so two things become three and four become three. Once is a turn of phrase; again and again, the pattern shows more than the content.' + +message: + ja: 「{matched}」など、and を入れずに三つ並べた句が 1000 語あたり {density} 個あります({limit} 個まで) + en: '"{matched}" and other threes strung on commas: {density} per 1000 words (limit {limit})' + +how_to_fix: + ja: 本当に三つあるときだけ三つ並べてください。足りない一つを足したのなら消し、残す並びには and を入れてください。 + en: Keep three only where there really are three. Drop the item added to make three, and join the rest with "and". + +# 1000 語あたりの個数。件数で数えると長い文書ほど当たる。 +levels: { strict: 2, normal: 3, relaxed: 5 } + +use_for: [blog, business, technical, legal, docs, academic, literature, speech] + +group: ai-tells +summary: + ja: 短い句を三つ、and を入れずにカンマだけで並べる形が何度も出る所 + en: Three short phrases strung on commas with no "and", again and again + +example: + en: + pad: true + before: | + I started with simple things: scrambled eggs, roasted vegetables, a basic tomato soup. Most dishes start the same way: an onion, some garlic, a little oil. I was chopping, stirring, paying attention. People come for the table, the conversation, the feeling of being cared for. + after: | + I started with scrambled eggs and a basic tomato soup. Most dishes start with an onion and a little oil. I was chopping and paying attention. People come for the conversation and the feeling of being cared for. + +not_flagged: + ja: 英語の文書だけを見ます。短い文書や、こうした並びが 3 つより少ない文書は測りません。and や or でつないだ並び(fast, cheap, and reliable)は人の文書のほうが多く書くので数えません。数字を含む並び(日付、金額、住所)、大文字で始まる名前や肩書きの並び(Jane Roe, Director, GDS)、同じ語の繰り返し(old, old, old)、箇条書きの中、引用符の中の並びは数えません。四つ以上の並びは三つとは数えません。 + en: Only English documents are checked. A short document, or one with fewer than three such runs, is not measured. A list joined with and / or (fast, cheap, and reliable) is not counted, since human documents write those more often. Runs holding a figure (a date, an amount, an address), names and titles that start with a capital (Jane Roe, Director, GDS), one word said again (old, old, old), runs inside a list or inside quotation marks, and runs of four or more are not counted. +level_meaning: + ja: 1000 語あたり三つの並び {limit} 個まで + en: up to {limit} {limit|run of three|runs of three} per 1000 words + +rewrite: + depth: light + en: + direction: In each run of three strung on commas, drop the item that repeats the other two and join the rest with "and". Where there really are three, keep them and add the "and". + pairs: + - before: They come for the table, the conversation, the feeling of being cared for. + after: They come for the conversation and the feeling of being cared for. + - before: I was chopping, stirring, paying attention. + after: I was chopping and paying attention. + keep: + - items that say different things + avoid: + - inventing a fourth item to get away from three + - adding content the document does not have diff --git a/text/packages/chaff/skills/chaff/SKILL.md b/text/packages/chaff/skills/chaff/SKILL.md index 15eee729a..c7ce08879 100644 --- a/text/packages/chaff/skills/chaff/SKILL.md +++ b/text/packages/chaff/skills/chaff/SKILL.md @@ -225,6 +225,7 @@ chaff marks the shapes common in generated text (group "Signs of generated text" `contrast-framing`, `stock-transition`, `announcing-opener`, `colon-lead-in`, `over-general-claim` (English only), `q-fragment-answer` (English only), `assistant-residue`, `email-stock-courtesy` (English emails: stacked "I hope this email finds you well"-type courtesies), `honesty-marker` ("Honestly,", 「正直、」: one is reported in blog posts and business documents), +`inline-triad` (English only: three short phrases strung on commas with no "and", again and again), `closing-exhortation` (a closing that cheers the reader on: 「ぜひ〜してみてください」, "You've got this"), `cold-open` (an opening that piles a bare wish, a quoted echo of the previous sentence and a teaser), `unprompted-denial` (sentences that deny a premise nobody stated: 「〜の話ではありません」, "This isn't about tooling."), and the signals diff --git a/text/packages/chaff/src/ai-score/human-shares.ts b/text/packages/chaff/src/ai-score/human-shares.ts index 36f5449c3..92e2f097e 100644 --- a/text/packages/chaff/src/ai-score/human-shares.ts +++ b/text/packages/chaff/src/ai-score/human-shares.ts @@ -162,6 +162,16 @@ export const HUMAN_SHARES: HumanShares = { technical: { documents: 49, fired: 0 }, }, "honesty-marker": { blog: { documents: 23, fired: 0 }, business: { documents: 80, fired: 0 } }, + "inline-triad": { + academic: { documents: 5, fired: 0 }, + blog: { documents: 9, fired: 0 }, + business: { documents: 41, fired: 0 }, + docs: { documents: 11, fired: 0 }, + legal: { documents: 14, fired: 0 }, + literature: { documents: 9, fired: 0 }, + speech: { documents: 3, fired: 0 }, + technical: { documents: 27, fired: 0 }, + }, "no-em-dash": { academic: { documents: 10, fired: 0 }, blog: { documents: 23, fired: 0 }, diff --git a/text/packages/chaff/src/detectors/inline-triad.ts b/text/packages/chaff/src/detectors/inline-triad.ts new file mode 100644 index 000000000..16a65d9bc --- /dev/null +++ b/text/packages/chaff/src/detectors/inline-triad.ts @@ -0,0 +1,149 @@ +// inline-triad: three short phrases strung on commas with no and / or (the table, the conversation, the feeling of +// being cared for), counted per length of the document. Pure. A list joined by and / or is left out: human prose +// writes those as often as generated prose. The words that open a clause rather than an item, and the words that make +// two items parallel, come from the word list. +import type { Detector, Finding, Lexicon, ProseDocument, Sentence, Span } from "../plugin.ts"; +import { isWithinAny, QUOTATION_MARKS, quotedIn } from "../quoted-span.ts"; +import { rateOver } from "./lexicon.ts"; + +export type TriadWords = { + readonly heads: ReadonlySet; + readonly conjunctions: ReadonlySet; + readonly determiners: ReadonlySet; + readonly suffixes: readonly string[]; +}; +export type Triad = { readonly start: number; readonly end: number; readonly matched: string }; +type Part = { readonly text: string; readonly start: number }; +type Clause = { readonly parts: readonly Part[]; readonly afterColon: boolean }; + +/** The longest a middle item runs, and the last one, which may carry the rest of its clause. */ +const ITEM_MAX_WORDS = 5; +const LAST_ITEM_MAX_WORDS = 7; +/** Fewer than this many in a document, and one turn of phrase decides the density. */ +export const MIN_TRIADS = 3; +const THREE = 3; +const CLAUSE_BREAKS = /[;:—–()[\]"“”]/gu; +const OPENS_ITEMS = new Set([":", "—", "–"]); +const DIGIT = /\d/u; +const LOWER_START = /^\p{Ll}/u; +const CLOSING_MARKS: ReadonlySet = new Set([".", "!", "?", "…"]); + +const withoutClosingMarks = (text: string): string => (CLOSING_MARKS.has(text.at(-1) ?? "") ? withoutClosingMarks(text.slice(0, -1)) : text); + +const wordsIn = (text: string): string[] => + text + .trim() + .split(/\s+/u) + .filter((word) => word !== ""); +const firstWord = (text: string): string => (wordsIn(text)[0] ?? "").toLowerCase().replace(/[^\p{L}']/gu, ""); + +const partsOf = (text: string, start: number): Part[] => { + const pieces = text.split(","); + return pieces.map((piece, at) => ({ text: piece, start: start + pieces.slice(0, at).reduce((sum, before) => sum + before.length + 1, 0) })); +}; + +/** The clauses of a sentence, split at the marks that close a phrase, each with its comma-separated parts. */ +export const clausesOf = (text: string): Clause[] => { + const ends = [...[...text.matchAll(CLAUSE_BREAKS)].map((match) => match.index), text.length]; + return ends.map((end, at) => { + const start = at === 0 ? 0 : (ends[at - 1] ?? 0) + 1; + return { parts: partsOf(text.slice(start, end), start), afterColon: OPENS_ITEMS.has(text.charAt(start - 1)) }; + }); +}; + +/** A short phrase that can be an item: no figure (dates, amounts, addresses), no and / or, not a clause's opening word. */ +const isItem = (text: string, maxWords: number, words: TriadWords): boolean => { + const count = wordsIn(text).length; + return ( + count > 0 && + count <= maxWords && + !DIGIT.test(text) && + !wordsIn(text).some((word) => words.conjunctions.has(word.toLowerCase())) && + !words.heads.has(firstWord(text)) + ); +}; + +/** The items after the opening one start in lower case: a capitalised run is a name, a title or an address (Jane Roe, Director, GDS). */ +const isLaterItem = (text: string, maxWords: number, words: TriadWords): boolean => LOWER_START.test(text.trim()) && isItem(text, maxWords, words); + +/** How many items the run ending at parts[at] and the two after it has: a part too long or opening a clause holds the first item at its end. */ +const itemsBefore = (parts: readonly Part[], at: number, words: TriadWords): number => { + const part = parts[at]; + if (part === undefined || part.text.trim() === "" || DIGIT.test(part.text)) return 0; + return isItem(part.text, ITEM_MAX_WORDS, words) ? 1 + itemsBefore(parts, at - 1, words) : 1; +}; + +const hasSuffix = (word: string, suffixes: readonly string[]): boolean => suffixes.some((suffix) => word.length > suffix.length && word.endsWith(suffix)); + +/** Two items in the same form: the same first word, a determiner each, or the same ending (chopping, stirring). */ +const areParallel = (left: string, right: string, words: TriadWords): boolean => { + const [first, second] = [firstWord(left), firstWord(right)]; + if (first === second) return true; + if (words.determiners.has(first) && words.determiners.has(second)) return true; + return words.suffixes.some((suffix) => hasSuffix(first, [suffix]) && hasSuffix(second, [suffix])); +}; + +/** One word said again (an old, old, old fight; repeat, repeat, repeat): emphasis, not three items. */ +const repeatsOneWord = (opening: string, middle: string, last: string): boolean => { + const word = middle.trim().toLowerCase(); + return [wordsIn(opening).at(-1), wordsIn(last)[0]].some((other) => other !== undefined && withoutClosingMarks(other.toLowerCase()) === word); +}; + +const lastWordsOf = (text: string): string => wordsIn(text).slice(-ITEM_MAX_WORDS).join(" "); + +const triadOf = (clause: Clause, words: TriadWords): Triad | undefined => { + const { parts } = clause; + const [opening, middle, last] = parts.slice(-THREE); + if (parts.length < THREE || opening === undefined || middle === undefined || last === undefined) return undefined; + if (!isLaterItem(last.text, LAST_ITEM_MAX_WORDS, words) || !isLaterItem(middle.text, ITEM_MAX_WORDS, words)) return undefined; + if (repeatsOneWord(opening.text, middle.text, last.text) || itemsBefore(parts, parts.length - THREE, words) !== 1) return undefined; + const listOpened = clause.afterColon && parts.length === THREE && isItem(opening.text, ITEM_MAX_WORDS, words); + if (!listOpened && !areParallel(middle.text, last.text, words)) return undefined; + const first = lastWordsOf(opening.text); + const start = opening.start + opening.text.lastIndexOf(first); + return { start, end: last.start + last.text.trimEnd().length, matched: [first, middle.text.trim(), withoutClosingMarks(last.text.trim())].join(", ") }; +}; + +/** The triads in one sentence's text, with positions in that text. */ +export const inlineTriads = (text: string, words: TriadWords): Triad[] => + clausesOf(text).flatMap((clause) => { + const triad = triadOf(clause, words); + return triad === undefined ? [] : [triad]; + }); + +const patternsIn = (lexicon: Lexicon, group: string): string[] => lexicon.filter((entry) => entry.group === group).map((entry) => entry.pattern.toLowerCase()); + +export const triadWordsOf = (lexicon: Lexicon): TriadWords => ({ + heads: new Set(patternsIn(lexicon, "head")), + conjunctions: new Set(patternsIn(lexicon, "conjunction")), + determiners: new Set(patternsIn(lexicon, "determiner")), + suffixes: patternsIn(lexicon, "suffix"), +}); + +type Hit = { readonly sentence: Sentence; readonly triad: Triad; readonly offset: number }; + +const inList = (doc: ProseDocument, offset: number): boolean => doc.listSpans.some((span) => offset >= span.start && offset < span.end); + +const hitsIn = (doc: ProseDocument, sentence: Sentence, words: TriadWords): Hit[] => { + const quoted: readonly Span[] = quotedIn(sentence, QUOTATION_MARKS); + return inlineTriads(sentence.text, words) + .map((triad) => ({ sentence, triad, offset: sentence.span.start + triad.start })) + .filter((hit) => !inList(doc, hit.offset) && !isWithinAny(quoted, { start: hit.offset, end: sentence.span.start + hit.triad.end })); +}; + +const findingOf = (hit: Hit, count: number, density: number, limit: number): Finding => ({ + rule: "inline-triad", + severity: "info", + line: 0, + column: 0, + quote: hit.sentence.text.trim(), + values: { matched: hit.triad.matched, count, density, limit, offset: hit.offset }, +}); + +/** Every triad once the document holds enough of them, more per 1000 words than the limit. */ +export const inlineTriad: Detector = (doc, options): Finding[] => { + const words = triadWordsOf(doc.lexicons["inline-triad"] ?? []); + const hits = doc.sentences.filter((sentence) => sentence.embeddedLanguage === undefined).flatMap((sentence) => hitsIn(doc, sentence, words)); + const density = hits.length < MIN_TRIADS ? undefined : rateOver(doc, hits.length, options.limit); + return density === undefined ? [] : hits.map((hit) => findingOf(hit, hits.length, density, options.limit)); +}; diff --git a/text/packages/chaff/src/detectors/registry/inline-triad.ts b/text/packages/chaff/src/detectors/registry/inline-triad.ts new file mode 100644 index 000000000..0dff3d873 --- /dev/null +++ b/text/packages/chaff/src/detectors/registry/inline-triad.ts @@ -0,0 +1,4 @@ +import type { Detector } from "../../plugin.ts"; +import { inlineTriad } from "../inline-triad.ts"; + +export const detector: Detector = inlineTriad; diff --git a/text/packages/lang-en/lexicons/inline-triad.yaml b/text/packages/lang-en/lexicons/inline-triad.yaml new file mode 100644 index 000000000..095dd5243 --- /dev/null +++ b/text/packages/lang-en/lexicons/inline-triad.yaml @@ -0,0 +1,71 @@ +# The words inline-triad reads to tell three items strung on commas from a sentence broken by commas. +# head: a word that opens a clause or an aside, not an item ("Well, honestly, it works" holds no items; in "I was +# chopping, stirring, paying attention" the first item is the end of the clause "I was chopping"). +# conjunction: an item holding one of these is part of a list joined with and / or, which the rule leaves out. +# determiner: two items that each open with one of these are in the same form (the table, the conversation). +# suffix: two items whose first words share this ending are in the same form (chopping, stirring). +id: inline-triad +language: en +entries: + - { pattern: i, group: head } + - { pattern: you, group: head } + - { pattern: he, group: head } + - { pattern: she, group: head } + - { pattern: it, group: head } + - { pattern: we, group: head } + - { pattern: they, group: head } + - { pattern: there, group: head } + - { pattern: which, group: head } + - { pattern: who, group: head } + - { pattern: whom, group: head } + - { pattern: whose, group: head } + - { pattern: where, group: head } + - { pattern: when, group: head } + - { pattern: while, group: head } + - { pattern: if, group: head } + - { pattern: because, group: head } + - { pattern: although, group: head } + - { pattern: though, group: head } + - { pattern: since, group: head } + - { pattern: unless, group: head } + - { pattern: until, group: head } + - { pattern: so, group: head } + - { pattern: but, group: head } + - { pattern: yet, group: head } + - { pattern: however, group: head } + - { pattern: then, group: head } + - { pattern: yes, group: head } + - { pattern: well, group: head } + - { pattern: please, group: head } + - { pattern: thanks, group: head } + - { pattern: oh, group: head } + - { pattern: also, group: head } + - { pattern: instead, group: head } + - { pattern: otherwise, group: head } + - { pattern: meanwhile, group: head } + - { pattern: as, group: head } + - { pattern: etc, group: head } + - { pattern: e.g, group: head } + - { pattern: i.e, group: head } + - { pattern: and, group: conjunction } + - { pattern: or, group: conjunction } + - { pattern: nor, group: conjunction } + - { pattern: the, group: determiner } + - { pattern: a, group: determiner } + - { pattern: an, group: determiner } + - { pattern: some, group: determiner } + - { pattern: my, group: determiner } + - { pattern: your, group: determiner } + - { pattern: our, group: determiner } + - { pattern: their, group: determiner } + - { pattern: his, group: determiner } + - { pattern: her, group: determiner } + - { pattern: its, group: determiner } + - { pattern: this, group: determiner } + - { pattern: that, group: determiner } + - { pattern: these, group: determiner } + - { pattern: those, group: determiner } + - { pattern: every, group: determiner } + - { pattern: each, group: determiner } + - { pattern: no, group: determiner } + - { pattern: ing, group: suffix } diff --git a/text/site/src/content/guide/en/ai-sounding-examples.md b/text/site/src/content/guide/en/ai-sounding-examples.md index 390e99f63..2d49ba443 100644 --- a/text/site/src/content/guide/en/ai-sounding-examples.md +++ b/text/site/src/content/guide/en/ai-sounding-examples.md @@ -446,7 +446,7 @@ Wording and shape ✗ Everything comes in threes: 1 in 0 of 23 human-written documents (Blog) ✗ Several signals of generated text together: 6 of its signals (Phrasing common in generated text, Everything comes in threes, Cliched closing, Too many contrast frames, Too many sentences that announce before they say, Bold emphasis scattered through the text); from 2, its strict level, that is one more sign · the other 19 did not show - not compared (no human baseline): Stock email courtesies piled up, Too many sweeping claims about people, Questions answered with a fragment + not compared (no human baseline): Stock email courtesies piled up, Threes strung on commas, Too many sweeping claims about people, Questions answered with a fragment Structure (against human articles) ✗ list items opening with a bold label: 3 higher than 95% of human articles @@ -459,7 +459,7 @@ AI-likeness: low (about as many signs as human-written documents (Blog)) Wording and shape · the other 25 did not show - not compared (no human baseline): Stock email courtesies piled up, Too many sweeping claims about people, Questions answered with a fragment + not compared (no human baseline): Stock email courtesies piled up, Threes strung on commas, Too many sweeping claims about people, Questions answered with a fragment Structure (against human articles) ✗ sections of one or two paragraphs: 100% (mean 1.3 paragraphs) higher than 90% of human articles diff --git a/text/site/src/content/guide/en/ai-sounding.md b/text/site/src/content/guide/en/ai-sounding.md index c913d3751..6e7532856 100644 --- a/text/site/src/content/guide/en/ai-sounding.md +++ b/text/site/src/content/guide/en/ai-sounding.md @@ -143,6 +143,7 @@ Legal documents, manuals, literature and transcripts are written differently, so | `no-em-dash` | Too many em dashes | | `sentence-rhythm` | Sentences that are all about the same length | | `rule-of-three` | Lists that almost all have three items | +| `inline-triad` | Three short phrases strung on commas with no "and", again and again ("the table, the conversation, the feeling of being cared for") (English only) | | `section-length-uniformity` | Every section filled to the same size, which is what a template does | | `paragraph-length-variance` | Paragraphs all of about the same length, as if poured into a mould | | `ai-structure` | An outline past 90% of human articles on several structure measures at once (heading density, headings split into three, bold labels, uniform sections, a closing that restates). In business documents, the textbook proposal outline (Summary, Current Situation, Proposed Solution, Expected Benefits, Costs, Risks and Mitigation…) counts as one more, and one more again when nearly every heading is one of its sections | diff --git a/text/site/src/content/guide/ja/ai-sounding.md b/text/site/src/content/guide/ja/ai-sounding.md index 84870c718..f142e7eed 100644 --- a/text/site/src/content/guide/ja/ai-sounding.md +++ b/text/site/src/content/guide/ja/ai-sounding.md @@ -143,6 +143,7 @@ Claude Code の skill(`npx chaffjs skill`)は、指摘より先に指針を | `no-em-dash` | ダッシュ(——)の多さ | | `sentence-rhythm` | 文の長さが揃いすぎている所 | | `rule-of-three` | 箇条書きがどれも 3 項目の所 | +| `inline-triad` | 「the table, the conversation, the feeling of being cared for」のように、短い句を三つ、and を入れずにカンマだけで並べる形が何度も出る所(英語の文書だけ) | | `section-length-uniformity` | どの節もほぼ同じ長さに埋めてあり、ひな形を埋めたように見える所 | | `paragraph-length-variance` | どの段落もほぼ同じ長さで、型に流しこんだように見える所 | | `ai-structure` | 構成の項目(見出しの多さ、3 つに分けた小見出し、太字の札、節の長さの揃い方、本文を言い直すまとめなど)のいくつもが、人の記事の 9 割を超えた文書。ビジネス文書では、教科書どおりの提案書の見出し(背景、現状と課題、提案内容、期待される効果、費用、リスクと対策…)も 1 項目に数え、見出しのほとんどがその定型ならもう 1 項目に数えます | diff --git a/text/test/fixtures/bench/plants/inline-triad.yaml b/text/test/fixtures/bench/plants/inline-triad.yaml new file mode 100644 index 000000000..23588502b --- /dev/null +++ b/text/test/fixtures/bench/plants/inline-triad.yaml @@ -0,0 +1 @@ +not_planted: a density over the whole document diff --git a/text/test/fixtures/limits/en.md b/text/test/fixtures/limits/en.md index c0d1405a1..ec55c3069 100644 --- a/text/test/fixtures/limits/en.md +++ b/text/test/fixtures/limits/en.md @@ -94,6 +94,8 @@ The day goes like this: Bring the key. +We packed for the trip: maps, ropes, lamps. At camp we were cooking, washing, resting. They came for the view, the quiet, the company. + ## ✅ After Return the key. You've got this. diff --git a/text/test/test_inline_triad.ts b/text/test/test_inline_triad.ts new file mode 100644 index 000000000..53fee967e --- /dev/null +++ b/text/test/test_inline_triad.ts @@ -0,0 +1,105 @@ +import { describe, it } from "node:test"; +import assert from "node:assert/strict"; +import { namedRuleRun } from "./rule-run.ts"; +import { buildDocument } from "../packages/chaff/src/document.ts"; +import { inlineTriads, triadWordsOf } from "../packages/chaff/src/detectors/inline-triad.ts"; +import { adapter as en } from "../packages/lang-en/src/index.ts"; + +// inline-triad: three short phrases strung on commas with no and / or, counted per 1000 words. Every sentence here is self-written. + +const RULE = "inline-triad"; +const WORDS = triadWordsOf(buildDocument("w.md", "# W\n\nA line.\n", en).lexicons[RULE] ?? []); + +const matched = (sentence: string): string[] => inlineTriads(sentence, WORDS).map((triad) => triad.matched); + +/** Plain sentences with no run of three, so a document reaches the length the rule measures. */ +const FILLER = Array.from({ length: 30 }, (_, at) => `Line ${String(at + 1)} says one plain thing about the weekly plan.`).join(" "); + +const findingsOf = (body: string): readonly string[] => namedRuleRun(RULE, `# Notes\n\n${body}\n\n${FILLER}\n`, en, "a.md", "blog/essay").findings; + +const TRIADS = [ + "I started with simple things: scrambled eggs, roasted vegetables, a basic tomato soup.", + "Most dishes start the same way: an onion, some garlic, a little oil.", + "In the evenings I was chopping, stirring, paying attention.", + "People come for the table, the conversation, the feeling of being cared for.", +].join(" "); + +describe("inlineTriads", () => { + it("finds three parallel phrases strung on commas", () => { + assert.deepEqual(matched("They come for the table, the conversation, the feeling of being cared for."), [ + "They come for the table, the conversation, the feeling of being cared for", + ]); + assert.deepEqual(matched("I was chopping, stirring, paying attention."), ["I was chopping, stirring, paying attention"]); + }); + + it("finds three short items opened by a colon, parallel or not", () => { + assert.deepEqual(matched("I started with simple things: scrambled eggs, roasted vegetables, a basic tomato soup."), [ + "scrambled eggs, roasted vegetables, a basic tomato soup", + ]); + }); + + it("gives the position of the run in the sentence", () => { + const sentence = "We packed: maps, ropes, lamps."; + const [triad] = inlineTriads(sentence, WORDS); + assert.ok(triad !== undefined); + assert.equal(sentence.slice(triad.start, triad.end), "maps, ropes, lamps."); + }); + + it("leaves a list joined with and / or", () => { + assert.deepEqual(matched("It was fast, simple, and reliable."), []); + assert.deepEqual(matched("Bring the map, the rope or the lamp."), []); + }); + + it("leaves a run of four or more", () => { + assert.deepEqual(matched("We packed: maps, ropes, lamps, tents."), []); + assert.deepEqual(matched("She bought the bread, the milk, the eggs, the tea."), []); + }); + + it("leaves commas that break a sentence rather than list items", () => { + assert.deepEqual(matched("Well, honestly, it works."), []); + assert.deepEqual(matched("However, in practice, the plan held."), []); + assert.deepEqual(matched("The plan, which we wrote, held up."), []); + }); + + it("leaves names, titles, places and figures", () => { + assert.deepEqual(matched("Signed by Jane Roe, Director, Digital Service."), []); + assert.deepEqual(matched("The office is in Leeds, West Yorkshire, England."), []); + assert.deepEqual(matched("It opened on Monday, 3 March, 2025."), []); + }); + + it("leaves one word said again", () => { + assert.deepEqual(matched("This is an old, old, old fight."), []); + }); + + it("leaves two phrases in different forms without a colon", () => { + assert.deepEqual(matched("After lunch, the team, quite tired, went home."), []); + }); + + it("reads nothing from an empty sentence or one with no comma", () => { + assert.deepEqual(matched(""), []); + assert.deepEqual(matched("No commas here."), []); + assert.deepEqual(matched(", , ,"), []); + }); +}); + +describe("inline-triad", () => { + it("reports every run once a long document holds enough of them", () => { + const findings = findingsOf(TRIADS); + assert.equal(findings.length, 4); + assert.match(findings[0] ?? "", /scrambled eggs, roasted vegetables, a basic tomato soup" and other threes strung on commas: \d+ per 1000 words/u); + }); + + it("does not report fewer than three runs", () => { + assert.deepEqual(findingsOf("In the evenings I was chopping, stirring, paying attention. People come for the table, the conversation, the company."), []); + }); + + it("does not report a run inside a list item or inside quotation marks", () => { + const listed = ["- maps, ropes, lamps", "- the bread, the milk, the eggs", "- chopping, stirring, tasting"].join("\n"); + assert.deepEqual(findingsOf(`${listed}\n\nShe said "the bread, the milk, the eggs" twice.`), []); + }); + + it("does not report a short document", () => { + const short = namedRuleRun(RULE, `# Notes\n\n${TRIADS}\n`, en, "a.md", "blog/essay").findings; + assert.deepEqual(short, []); + }); +}); diff --git a/text/test/test_rule_rewrite.ts b/text/test/test_rule_rewrite.ts index 6aa830b7e..d83488e0c 100644 --- a/text/test/test_rule_rewrite.ts +++ b/text/test/test_rule_rewrite.ts @@ -51,8 +51,8 @@ const LISTS_ITS_CONTEXT: ReadonlySet = new Set([ "contrast-framing", ]); -/** Word lists a rule reads around a finding (便利な's な) or passes over (the very first), not phrases it flags. */ -const UNFLAGGED_LISTS: ReadonlySet = new Set(["very-copula", "very-exception", "negation-word"]); +/** Word lists a rule reads around a finding (便利な's な, the words that pair two items in a run of three) or passes over (the very first), not phrases it flags. */ +const UNFLAGGED_LISTS: ReadonlySet = new Set(["very-copula", "very-exception", "negation-word", "inline-triad"]); const MIN_PAIRS = 2; const MAX_PAIRS = 3;