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
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | |
extract_text_per_page(pdf_path, max_pages=10)
¶
Extract plain text (without positions) for the first max_pages pages.
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
extract_text_with_positions(pdf_path)
¶
Extract text and bounding boxes for words from a PDF using Docling.
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
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
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
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