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
27 changes: 27 additions & 0 deletions packages/core/src/utils/text-splitting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,33 @@ describe("extractFirstSentence", () => {
expect(r.rest).toBe("He waved.");
});

it("does not split at abbreviations preceded by quotes/parens/asterisks", () => {
// Regression from the [\w.]+ tightening: `(?:^|\s)` required start-of-string
// or whitespace immediately before the token, so '"Dr' / '(Mr' / '*Dr'
// never matched the abbreviation list and the first-sentence / TTS
// early-emit path chopped mid-name ('He cited "Dr.'). The old \b regex
// handled these.
const quoted = extractFirstSentence(
'He cited "Dr. Smith" as the source. Next sentence.',
);
expect(quoted.first).toBe('He cited "Dr. Smith" as the source.');
expect(quoted.rest).toBe("Next sentence.");

const paren = extractFirstSentence("(Mr. Jones agreed. Everyone left.)");
expect(paren.first).toBe("(Mr. Jones agreed.");
expect(paren.rest).toBe("Everyone left.)");

const emphasized = extractFirstSentence("*Dr. Smith* arrived. He waved.");
expect(emphasized.first).toBe("*Dr. Smith* arrived.");
expect(emphasized.rest).toBe("He waved.");

const quotedDotted = extractFirstSentence(
'He said "etc." and moved on. Fine.',
);
expect(quotedDotted.first).toBe('He said "etc." and moved on.');
expect(quotedDotted.rest).toBe("Fine.");
});

it("splits normal sentences at the first real boundary", () => {
const r = extractFirstSentence("Hello world. Next one.");
expect(r.first).toBe("Hello world.");
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/utils/text-splitting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ export function extractFirstSentence(text: string): {
// "e.g" — the "e.g"/"i.e" list entries were dead and those got split
// mid-token (the first-sentence / TTS early-emit path chopped "e.g."
// into "e."). Strip a trailing dot before comparing to the list.
const lastWordMatch = preText.match(/(?:^|\s)([\w.]+)$/);
// No prefix anchor: leftmost matching captures the maximal trailing
// [\w.] run, and any other char (space, quote, paren, asterisk, dash)
// or start-of-string delimits it — a (?:^|\s) anchor rejected
// punctuation-preceded abbreviations ('"Dr' / '(Mr') that the
// original \b handled, chopping mid-name.
const lastWordMatch = preText.match(/([\w.]+)$/);

let isAbbreviation = false;
if (lastWordMatch) {
Expand Down
Loading