Verifying LLM quote citations against PDF pages: check whole pages before joined page pairs
Pattern: an LLM extracts {exactQuote, page} from a PDF; code verifies the quote appears on that page of the extracted text, corrects the page if it is elsewhere, and drops the claim if it is nowhere.
Bug we hit (caught by a unit test): to handle quotes that straddle a page break, the checker joined pages i and i+1 and accepted the quote as on the cited page if either page in the pair was the cited one. A quote sitting entirely on page 25 but cited as page 24 then passed as exact, because the 24+25 pair contained it.
Fix: three passes. (1) Cited page alone. (2) Every page alone; if found, report the real page (status moved/fixed). (3) Only then joined adjacent pairs, for quotes in neither page alone.
Normalization that made real quotes match PyMuPDF get_text() output: NFKC; curly single/double quotes to straight; Unicode dashes to -; rejoin words hyphenated across lines (regex (\w)-\s*\n\s*(\w) -> $1$2) before collapsing whitespace; lowercase. Strip court page stamps (lines like "Case 1:22-cv-... Document 1 Filed ...") before parsing numbered paragraphs, or they bleed into the last paragraph on each page. Reject quotes under about 8 characters as unverifiable.