pdf¶
academic_doc_generator.core.pdf
¶
PDF processing utilities (Docling + pypdf) with comprehensive type annotations.
extract_annotations_with_positions(pdf_path, ignore_source=True)
¶
Extract annotations (comments/highlights) and their positions using pypdf.
Annotations are categorized for processing: - "llm": Regular comments sent to LLM for rewriting - "quelle": Source-related comments (counted only) - "language": Grammar/spelling comments (counted only) - "ignore": Special markers like "ab hier" (excluded from output)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf_path
|
str
|
Path to the PDF file. |
required |
ignore_source
|
bool
|
Whether to categorize source-related comments as "quelle". If False, they are treated as regular "llm" comments. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
dict[int, list[AnnotationData]]
|
Tuple of (annotations, stats): |
CommentStats
|
|
tuple[dict[int, list[AnnotationData]], CommentStats]
|
|
Example
annotations, stats = extract_annotations_with_positions("thesis.pdf") stats {'quelle': 5, 'language': 3, 'ignore': 1} annotations[0][]['category'] 'llm'
Source code in src/academic_doc_generator/core/pdf.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
extract_text_per_page(pdf_path, max_pages=10)
¶
Extract plain text (without positions) for the first max_pages pages using LiteParse with docling and PyMuPDF fallbacks.
This is faster than extracting word positions and is sufficient for metadata extraction and thesis summarization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf_path
|
str
|
Path to the PDF file. |
required |
max_pages
|
Optional[int]
|
Maximum number of pages to read. If None, all pages are read. Defaults to 10. |
10
|
Returns:
| Type | Description |
|---|---|
dict[int, str]
|
Dictionary mapping 0-based page indices to the full concatenated text |
dict[int, str]
|
of that page. |
Example
text = extract_text_per_page("thesis.pdf", max_pages=2) text[0][:50] 'Introduction This thesis examines the impact of...'
Source code in src/academic_doc_generator/core/pdf.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 | |
extract_text_with_positions(pdf_path)
¶
Extract text and bounding boxes for words from a PDF using LiteParse with docling and PyMuPDF fallbacks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pdf_path
|
str
|
Path to the PDF file. |
required |
Returns:
| Type | Description |
|---|---|
dict[int, list[WordBox]]
|
Dictionary mapping 0-based page indices to a list of words with bounding boxes. |
Example
words = extract_text_with_positions("thesis.pdf") words[0][]
Source code in src/academic_doc_generator/core/pdf.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
find_annotation_context(pages_words, annotations)
¶
Match annotations to the words and paragraphs they reference.
For each annotation, this function: 1. Finds the words that overlap with the annotation's bounding box 2. Extracts the highlighted text from those words 3. Finds the paragraph containing the highlighted text 4. Returns all context information for LLM processing
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pages_words
|
dict[int, list[WordBox]]
|
Words with bounding boxes per page (0-based indices). |
required |
annotations
|
dict[int, list[AnnotationData]]
|
Annotations per page with rects and comments (0-based indices). |
required |
Returns:
| Type | Description |
|---|---|
dict[int, list[AnnotationContext]]
|
Dictionary mapping 1-based page numbers to lists of annotation contexts. |
dict[int, list[AnnotationContext]]
|
Page numbers are 1-based for user display purposes. |
Example
pages_words = {0: [{'text': 'test', 'bbox': (10, 10, 50, 20)}]} annotations = {0: [{'comment': 'Why?', 'rect': [5, 5, 55, 25], ... 'category': 'llm', 'subtype': '/Text', 'quadpoints': None}]} context = find_annotation_context(pages_words, annotations) context[1][0]['highlighted'] 'test'
Source code in src/academic_doc_generator/core/pdf.py
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 | |
get_words_for_annotation_on_page(pages_words, page_index, rect)
¶
Get words that match an annotation rectangle, checking neighboring pages if necessary.
Sometimes annotations appear on the wrong page in the PDF structure. This function checks the specified page first, then the next page (+1), then the previous page (-1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pages_words
|
dict[int, list[WordBox]]
|
Dictionary of pages mapped to word lists. |
required |
page_index
|
int
|
Index of the annotated page (0-based). |
required |
rect
|
tuple[float, float, float, float]
|
Annotation rectangle as (x0, y0, x1, y1). |
required |
Returns:
| Type | Description |
|---|---|
int
|
Tuple of (page_index_used, words) where page_index_used is the page |
list[WordBox]
|
where words were actually found, and words is the list of matching word dicts. |
Example
pages_words = {0: [{'text': 'test', 'bbox': (10, 10, 50, 20)}]} rect = (5, 5, 55, 25) page_idx, words = get_words_for_annotation_on_page(pages_words, 0, rect) page_idx 0 len(words) 1
Source code in src/academic_doc_generator/core/pdf.py
is_footnote_reference(text)
¶
Check if the text is likely just a footnote reference or citation marker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The text to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if it looks like a footnote or citation reference, False otherwise. |
Examples:
>>> is_footnote_reference("[1]")
True
>>> is_footnote_reference("1.")
True
>>> is_footnote_reference("[Mül23]")
True
Source code in src/academic_doc_generator/core/pdf.py
is_quelle_comment(text, max_length=20)
¶
Check if a comment is a source-related comment that should be counted but not rewritten.
Source comments (containing "Quelle" or "source") are counted in statistics but not sent to the LLM for rewriting. They must be short (≤max_length characters) and contain the keyword as a whole word (not part of another word like "Consequent").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
The comment text to check. |
required |
max_length
|
int
|
Maximum length for a comment to be considered a Quelle comment. Defaults to 20 characters. |
20
|
Returns:
| Type | Description |
|---|---|
bool
|
True if this is a source-related comment, False otherwise. |
Examples:
>>> is_quelle_comment("Quelle?")
True
>>> is_quelle_comment("Source missing")
True
>>> is_quelle_comment("Consequent") # Not a whole word match
False
>>> is_quelle_comment("Quelle fehlt hier an dieser Stelle komplett")
False # Too long
Source code in src/academic_doc_generator/core/pdf.py
rect_overlap(word_bbox, annot_bbox)
¶
Check if a word bounding box overlaps with an annotation rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
word_bbox
|
tuple[float, float, float, float]
|
Word bounding box as (x0, y0, x1, y1). |
required |
annot_bbox
|
tuple[float, float, float, float]
|
Annotation bounding box as (x0, y0, x1, y1). |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the bounding boxes overlap, False otherwise. |
Example
rect_overlap((10, 10, 50, 20), (5, 5, 55, 25)) True rect_overlap((10, 10, 50, 20), (100, 100, 150, 120)) False
Source code in src/academic_doc_generator/core/pdf.py
words_overlapping_rect(words, rect, tol=0.5)
¶
Find all words that overlap with a given rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
words
|
list[WordBox]
|
List of word dictionaries with 'text' and 'bbox'. |
required |
rect
|
tuple[float, float, float, float]
|
Annotation rectangle as (x0, y0, x1, y1). |
required |
tol
|
float
|
Tolerance factor for overlap detection in points. Defaults to 0.5. |
0.5
|
Returns:
| Type | Description |
|---|---|
list[WordBox]
|
List of words that overlap with the rectangle. |
Example
words = [{'text': 'Hello', 'bbox': (10, 10, 50, 20)}] rect = (5, 5, 55, 25) overlapping = words_overlapping_rect(words, rect) len(overlapping) 1