INNER CODE UNIT · Python
find_caption_block
gokayfem/awesome-vlm-architectures · scripts/extract_architecture_figures.py:52
def find_caption_block(page: fitz.Page, figure: str) -> tuple[fitz.Rect, str]:
wanted = normalize_figure(figure)
patterns = [
re.compile(rf"(?:Figure|Fig\.)\s*{re.escape(wanted)}\b", re.I),
re.compile(
rf"(?:Figure|Fig\.)\s*{re.escape(wanted.rstrip('abcdefghijklmnopqrstuvwxyz'))}\b",
re.I,
),
]
matches: list[tuple[fitz.Rect, str]] = []
for block in page.get_text("blocks", sort=True):
text = re.sub(r"\s+", " ", block[4]).strip()
if any(pattern.search(text) for pattern in patterns):
matches.append((fitz.Rect(block[:4]), text))
if not matches:
raise ValueError(
f"caption for {figure} not found on PDF page {page.number + 1}"
)