(doc_id)
| 28 | converter = DocumentConverter(format_options={'pdf': PdfFormatOption(pipeline_options=pipeline_options)}) |
| 29 | |
| 30 | def analyze(doc_id): |
| 31 | url = get_url(doc_id) |
| 32 | if not url: return |
| 33 | try: |
| 34 | doc = converter.convert(url).document |
| 35 | items = list(doc.iterate_items()) |
| 36 | |
| 37 | pic_count = 0 |
| 38 | img_contexts = [] |
| 39 | for i, (element, level) in enumerate(items): |
| 40 | if type(element).__name__ == "PictureItem": |
| 41 | pic_count += 1 |
| 42 | |
| 43 | pre_text = "" |
| 44 | for j in range(i-1, max(-1, i-5), -1): |
| 45 | t = items[j][0] |
| 46 | if hasattr(t, "text") and t.text and len(t.text.strip()) > 5: |
| 47 | pre_text = t.text.strip() |
| 48 | break |
| 49 | |
| 50 | post_text = "" |
| 51 | for j in range(i+1, min(len(items), i+5)): |
| 52 | t = items[j][0] |
| 53 | if hasattr(t, "text") and t.text and len(t.text.strip()) > 5: |
| 54 | post_text = t.text.strip() |
| 55 | break |
| 56 | |
| 57 | img_contexts.append({ |
| 58 | "image": f"{doc_id}-fig{pic_count:02d}.png", |
| 59 | "pre": pre_text, |
| 60 | "post": post_text |
| 61 | }) |
| 62 | |
| 63 | # Save to JSON |
| 64 | with open(REPO_ROOT / f"{doc_id}_img_contexts.json", "w", encoding="utf-8") as f: |
| 65 | json.dump(img_contexts, f, indent=2, ensure_ascii=False) |
| 66 | |
| 67 | except Exception as e: |
| 68 | print(f"Error for {doc_id}: {e}") |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | for d in docs_with_images: |
no test coverage detected