Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions text/packages/chaff/src/detectors/list-sequence-lead.ts
Original file line number Diff line number Diff line change
@@ -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;
};
6 changes: 3 additions & 3 deletions text/packages/chaff/src/detectors/oxford-comma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions text/test/test_oxford_not_a_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
Loading