| 1851 | |
| 1852 | |
| 1853 | def extract_appendix_index( |
| 1854 | pdf_path: Path, |
| 1855 | pdf_coverage: dict[str, Any] | None = None, |
| 1856 | *, |
| 1857 | max_sections: int = 20, |
| 1858 | max_captions: int = 24, |
| 1859 | ) -> dict[str, Any]: |
| 1860 | pdf_coverage = pdf_coverage or {} |
| 1861 | start_page = pdf_coverage.get("appendix_start_page") |
| 1862 | index: dict[str, Any] = { |
| 1863 | "appendix_detected": bool(pdf_coverage.get("appendix_detected")), |
| 1864 | "start_page": start_page, |
| 1865 | "sections": [], |
| 1866 | "figure_captions": [], |
| 1867 | "table_captions": [], |
| 1868 | } |
| 1869 | if not index["appendix_detected"] or not start_page: |
| 1870 | return index |
| 1871 | |
| 1872 | seen_sections = set() |
| 1873 | seen_figure_captions = set() |
| 1874 | seen_table_captions = set() |
| 1875 | for page in extract_appendix_page_texts(pdf_path, int(start_page)): |
| 1876 | page_number = int(page.get("page", 0) or 0) |
| 1877 | text = str(page.get("text", "")) |
| 1878 | for raw_line in text.splitlines(): |
| 1879 | title = appendix_section_title(raw_line) |
| 1880 | marker = normalize_title(title) |
| 1881 | if title and marker not in seen_sections and len(index["sections"]) < max_sections: |
| 1882 | seen_sections.add(marker) |
| 1883 | index["sections"].append({"title": title, "page": page_number}) |
| 1884 | |
| 1885 | for caption in extract_caption_lines(text, "figure"): |
| 1886 | marker = f"{caption.get('id', '').lower()}::{caption.get('caption', '').lower()}" |
| 1887 | if marker in seen_figure_captions or len(index["figure_captions"]) >= max_captions: |
| 1888 | continue |
| 1889 | seen_figure_captions.add(marker) |
| 1890 | index["figure_captions"].append({**caption, "page_hint": f"p.{page_number}"}) |
| 1891 | |
| 1892 | for caption in extract_caption_lines(text, "table"): |
| 1893 | marker = f"{caption.get('id', '').lower()}::{caption.get('caption', '').lower()}" |
| 1894 | if marker in seen_table_captions or len(index["table_captions"]) >= max_captions: |
| 1895 | continue |
| 1896 | seen_table_captions.add(marker) |
| 1897 | index["table_captions"].append({**caption, "page_hint": f"p.{page_number}"}) |
| 1898 | return index |
| 1899 | |
| 1900 | |
| 1901 | def extract_pdf_sections(pdf_path: Path, max_pages: int | None = None) -> dict[str, str]: |