Check an AI's quote against the PDF with pdftotext and grep
Check whether a sentence an AI quoted from a paper appears in the PDF with pdftotext and grep -F. Covers line-break and hyphen misses and labeling mismatches.
Ask an AI to quote a paper word for word and it returns a sentence, but it does not tell you whether the sentence is in the PDF. Convert the PDF to text and search it with grep -F. DOI and bibliographic checks are in the citation verification post; web-page quotes are in the claim-evidence table post. This post covers one PDF.
[1] Download the PDF and convert it to text. The example is "Attention Is All You Need" from arXiv (1706.03762, 15 pages).
$ curl -sL -o paper.pdf https://arxiv.org/pdf/1706.03762$ pdftotext paper.pdf plain.txt$ wc -l plain.txt1312 plain.txt[2] Ask the AI for quotes as tab-separated lines. Forbid text outside the table and tell it not to change a single character.
$ cat prompt.txtFrom the paper "Attention Is All You Need" (Vaswani et al., 2017), pick 5 claims and answer only with one tab-separated line each. Columns: number<TAB>claim<TAB>verbatim English quote from the paper (one line, do not change a single character). Do not use tools; write from memory. No text outside the table.$ claude -p --model sonnet --max-turns 3 --tools "" < prompt.txt > ai.tsv$ cut -f3 ai.tsv > quotes.txt[3] Feed each quote straight to grep -F. It searches for a fixed string, so . or [ in a quote is safe.
$ n=0; while IFS= read -r q; do n=$((n+1)); grep -qF -- "$q" plain.txt && echo "$n found" || echo "$n MISS"; done < quotes.txt1 MISS2 MISS3 MISS4 MISS5 MISSAll five miss. PDF lines break at the page width and a quote is one line, so any sentence that spans two lines is not found. That does not mean the quotes are wrong.
[4] Turn newlines into spaces, squeeze repeated whitespace, and search again.
$ tr '\n' ' ' < plain.txt | tr -s '[:space:]' ' ' > plain.norm$ n=0; while IFS= read -r q; do n=$((n+1)); grep -qF -- "$q" plain.norm && echo "$n found" || echo "$n MISS"; done < quotes.txt1 found2 found3 MISS4 found5 foundQuote 3 is left. Look at the PDF text itself:
$ grep -o 'Our model achieves 28.4 BLEU.\{0,50\}' plain.normOur model achieves 28.4 BLEU on the WMT 2014 Englishto-German translation task, improving overIn the PDF, English- and to-German are split at a line end. Default pdftotext removes end-of-line hyphens and joins the pieces, and the real hyphen in English-to-German disappeared with it. For the same reason sequence-aligned in the body becomes sequencealigned.
[5] Strip hyphens and whitespace from both sides before comparing. -raw keeps end-of-line hyphens, and since the quote gets the same treatment, a hyphen no longer affects the result wherever it falls. The script also unifies straight and curly quotes (' and ’) and ignores case.
#!/bin/bash# usage: quotecheck.sh paper.pdf quotes.txt (quotes.txt: one quote per line)pdf=$1; quotes=$2squash() { sed "s/[’‘]/'/g; s/[“”]/\"/g" | tr -d '[:space:]-'; }pdftotext -raw "$pdf" - | squash > /tmp/qc.$$n=0while IFS= read -r q; do n=$((n+1)) s=$(printf '%s' "$q" | squash) if grep -qiF -- "$s" /tmp/qc.$$; then echo "$n found" else h=$(printf '%s' "${q:0:40}" | squash); t=$(printf '%s' "${q: -40}" | squash) a=no; b=no grep -qiF -- "$h" /tmp/qc.$$ && a=yes; grep -qiF -- "$t" /tmp/qc.$$ && b=yes echo "$n MISS head:$a tail:$b ${q:0:40}" fidone < "$quotes"rm -f /tmp/qc.$$$ bash quotecheck.sh paper.pdf quotes.txt1 found2 found3 found4 found5 found[6] Feed it four deliberately altered quotes and two correct ones to see what a MISS line tells you. head and tail say whether the first 40 and last 40 characters of the quote exist in the PDF.
$ bash quotecheck.sh paper.pdf quotes2.txt1 MISS head:yes tail:no Our model achieves 28.4 BLEU on the WMT 2 found3 MISS head:no tail:no The Transformer reduces training cost by4 found5 MISS head:no tail:yes We trained on the WMT 2014 English-Germa6 found| Line | Test quote | Result |
|---|---|---|
| 1 | Cut mid-sentence and closed with a period (the original continues task,) | head:yes tail:no |
| 2 | Verbatim, with only the first letter capitalized | found (thanks to -i) |
| 3 | An invented sentence | head:no tail:no |
| 5 | standard removed | head:no tail:yes |
| 6 | Google's with a straight ' (the PDF has a curly ’) | found |
Only head matching means the end was altered; only tail means the start was; neither means the sentence is probably not there at all. The flags only point to where to look again. A person makes the call.
Record each quote as one of three states: found, partial (head or tail only), or absent. Do not publish partial or absent quotes as quotes; check that spot in the PDF directly.
A match only proves the sentence is in the PDF. Whether it supports the claim the AI attached to it takes reading the surrounding text.
References: [pdftotext man page](https://manpages.ubuntu.com/manpages/noble/man1/pdftotext.1.html) · GNU grep manual · arXiv 1706.03762