diff --git a/text/packages/chaff/src/detectors/list-sequence-lead.ts b/text/packages/chaff/src/detectors/list-sequence-lead.ts new file mode 100644 index 000000000..9208819e9 --- /dev/null +++ b/text/packages/chaff/src/detectors/list-sequence-lead.ts @@ -0,0 +1,28 @@ +import { isClause, shapeOf } from "./list-item.ts"; +import type { ItemScope } from "./list-sentence.ts"; +import type { TokenRange } from "./token-column.ts"; + +/** The item after and / or without the sequence word that opens it (and then ride / and finally chop). */ +const withoutSequenceLead = (scope: ItemScope, item: TokenRange): TokenRange => { + const lead = scope.sentence.tokens[item.start]?.surface.toLowerCase() ?? ""; + const isSequence = scope.sentence.words.sequence?.has(lead) ?? false; + return item.end - item.start > 1 && isSequence ? { start: item.start + 1, end: item.end } : item; +}; + +/** + * The shape the last item and the item after and / or share, or undefined. A sequence word right after the conjunction + * marks a step, so the item is read by the word past it (We visited Paris, Rome, and then Berlin). Where the sequence + * word matches an adverb that opens the last item, the items are adverbs in a row (primarily by stage, secondarily by + * timebox, and finally by date), unless the words past it take the first item's shape: then the and / or goes on from + * the first item, past an aside (Take a bus to the station, easily spotted from afar, and then ride), and there is no list. + */ +export const sharedShape = (scope: ItemScope, first: TokenRange, last: TokenRange, afterItem: TokenRange): string | undefined => { + const shape = shapeOf(scope, last); + const rest = withoutSequenceLead(scope, afterItem); + // and then we rested: past the sequence word comes a clause of its own, not an item of the list. + const past = rest !== afterItem && isClause(scope, rest) ? undefined : shapeOf(scope, rest); + const asWritten = shapeOf(scope, afterItem); + if (shape === undefined) return undefined; + if (asWritten === shape) return past !== asWritten && past === shapeOf(scope, first) ? undefined : shape; + return past === shape ? shape : undefined; +}; diff --git a/text/packages/chaff/src/detectors/oxford-comma.ts b/text/packages/chaff/src/detectors/oxford-comma.ts index c1cf72b4c..631cbc234 100644 --- a/text/packages/chaff/src/detectors/oxford-comma.ts +++ b/text/packages/chaff/src/detectors/oxford-comma.ts @@ -61,6 +61,7 @@ import { insideSentenceCaseTitle } from "./title-list.ts"; import { insideByline } from "./byline-list.ts"; import { withoutOpeningConjunction } from "./list-opening-conjunction.ts"; import { afterClauseBeforeInfinitives } from "./list-purpose-clause.ts"; +import { sharedShape } from "./list-sequence-lead.ts"; const EMPTY: TokenRange = { start: 0, end: 0 }; @@ -393,9 +394,8 @@ const listAt = (list: ItemList, at: number, walk: Walk): boolean | undefined => if (namesDivision(items) || qualifiesPrevious(items)) return undefined; const last = lastOf(items); if (last !== undefined && opensContrast(scope, last.item)) return undefined; - const shape = last === undefined ? undefined : shapeOf(scope, last.item); - if (sizeOf(items) < 2 || shape === undefined || shape !== shapeOf(scope, afterItem)) return undefined; - return itemsAgree(scope, items, afterItem, shape) ? commaBefore(scope, at) : undefined; + const shape = last === undefined || sizeOf(items) < 2 ? undefined : sharedShape(scope, firstOf(items)?.item ?? last.item, last.item, afterItem); + return shape !== undefined && itemsAgree(scope, items, afterItem, shape) ? commaBefore(scope, at) : undefined; }; const isJudgedConjunction = (token: Token, at: number): boolean => at > 0 && isListConjunction(token); diff --git a/text/test/test_oxford_not_a_list.ts b/text/test/test_oxford_not_a_list.ts index 03697b8fe..ed41b1cd0 100644 --- a/text/test/test_oxford_not_a_list.ts +++ b/text/test/test_oxford_not_a_list.ts @@ -139,6 +139,14 @@ describe("oxford-comma-consistency: what is not a list", () => { assert.equal(reportedWith("I.e., the posters and the cards stay in stock."), false); assert.equal(reportedWith("E.g., posters, cards and calendars sell well."), true); }); + + it("reads the item after and then by the word past then, so an aside opened by an adverb is no item", () => { + assert.equal(reportedWithout("Take a bus to the station, easily spotted from afar, and then ride the lift."), false); + assert.equal(reportedWith("Take a bus to the station, easily spotted from afar, and then ride the lift."), false); + assert.equal(reportedWithout("We visited Paris, Rome, and then Berlin."), true); + assert.equal(reportedWith("We visited Paris, Rome and finally Berlin."), true); + assert.equal(reportedWithout("Items are sorted primarily by stage, secondarily by size, and finally by date."), true); + }); }); describe("excerptSpans", () => {