Papermage. Casting magic over scientific PDFs.
Project description
papermage
Setup
conda create -n papermage python=3.11
conda activate papermage
pip install -e '.[dev,predictors,visualizers]'
(you may need to add/remove quotes depending on your command line shell).
If you're on MacOSX, you'll also want to run:
conda install poppler
Unit testing
python -m pytest
for latest failed test
python -m pytest --lf --no-cov -n0
for specific test name of class name
python -m pytest -k 'TestPDFPlumberParser' --no-cov -n0
Quick start
1. Create a Document for the first time from a PDF
from papermage.recipes import CoreRecipe
recipe = CoreRecipe()
doc = recipe.run("tests/fixtures/papermage.pdf")
2. Understanding the output: the Document
class
What is a Document
? At minimum, it is some text, saved under the .symbols
field, which is just a <str>
. For example:
> doc.symbols
"PaperMage: A Unified Toolkit for Processing, Representing, and\nManipulating Visually-..."
But this library is really useful when you have multiple different ways of segmenting .symbols
. For example, segmenting the paper into Pages, and then each page into Rows:
for page in doc.pages:
print(f'\n=== PAGE: {page.id} ===\n\n')
for row in page.rows:
print(row.text)
...
=== PAGE: 5 ===
4
Vignette: Building an Attributed QA
System for Scientific Papers
How could researchers leverage papermage for
their research? Here, we walk through a user sce-
nario in which a researcher (Lucy) is prototyping
an attributed QA system for science.
System Design.
Drawing inspiration from Ko
...
This shows two nice aspects of this library:
-
Document
provides iterables for different segmentations ofsymbols
. Options include things likepages, tokens, rows, sentences, paragraphs, sections, ...
. Not every Parser will provide every segmentation, though. -
Each one of these segments (in our library, we call them
Entity
objects) is aware of (and can access) other segment types. For example, you can callpage.rows
to get all Rows that intersect a particular Page. Or you can callsent.tokens
to get all Tokens that intersect a particular Sentence. Or you can callsent.rows
to get the Row(s) that intersect a particular Sentence. These indexes are built dynamically when theDocument
is created and each time a newEntity
type is added. In the extreme, as long as those fields are available in the Document, you can write:
for page in doc.pages:
for paragraph in page.paragraphs:
for sent in paragraph.sentences:
for row in sent.rows:
...
You can check which fields are available in a Document via:
> doc.fields
['tokens',
'rows',
'pages',
'words',
'sentences',
'blocks',
'vila_entities',
'titles',
'paragraphs',
'authors',
'abstracts',
'keywords',
'sections',
'lists',
'bibliographies',
'equations',
'algorithms',
'figures',
'tables',
'captions',
'headers',
'footers',
'footnotes',
'symbols',
'images',
'metadata',
'entities',
'relations']
3. Understanding intersection of Entities
Note that Entity
s don't necessarily perfectly nest each other. For example, what happens if you run:
for sent in doc.sentences:
for row in sent.rows:
print([token.text for token in row.tokens])
Tokens that are outside each sentence can still be printed. This is because when we jump from a sentence to its rows, we are looking for all rows that have any overlap with the sentence. Rows can extend beyond sentence boundaries, and as such, can contain tokens outside that sentence.
Here's another example:
for page in doc.pages:
print([sent.text for sent in page.sentences])
Sentences can cross page boundaries. As such, adjacent pages may end up printing the same sentence.
But rows and tokens adhere strictly to page boundaries, and thus will not repeat when printed across pages:
for page in doc.pages:
print([row.text for row in page.rows])
print([token.text for token in page.tokens])
A key aspect of using this library is understanding how these different fields are defined & anticipating how they might interact with each other. We try to make decisions that are intuitive, but we do ask users to experiment with fields to build up familiarity.
4. What's in an Entity
?
Each Entity
object stores information about its contents and position:
-
.spans: List[Span]
, ASpan
is a pointer intoDocument.symbols
(that is,Span(start=0, end=5)
corresponds tosymbols[0:5]
). By default, when you iterate over anEntity
, you iterate over its.spans
. -
.boxes: List[Box]
, ABox
represents a rectangular region on the page. Each span is associated a Box. -
.metadata: Metadata
, A free form dictionary-like object to store extra metadata about thatEntity
. These are usually empty.
5. How can I manually create my own Document
?
A Document
is created by stitching together 3 types of tools: Parsers
, Rasterizers
and Predictors
.
-
Parsers
take a PDF as input and return aDocument
compared of.symbols
and other fields. The example one we use is a wrapper around PDFPlumber - MIT License utility. -
Rasterizers
take a PDF as input and return anImage
per page that is added toDocument.images
. The example one we use is PDF2Image - MIT License. -
Predictors
take aDocument
and apply some operation to compute a new set ofEntity
objects that we can insert into ourDocument
. These are all built in-house and can be either simple heuristics or full machine-learning models.
6. How can I save my Document
?
import json
with open('filename.json', 'w') as f_out:
json.dump(doc.to_json(with_images=True), f_out, indent=4)
will produce something akin to:
{
"symbols": "PaperMage: A Unified Toolkit for Processing, Representing, an...",
"entities": {
"images": [...],
"rows": [...],
"tokens": [...],
"words": [...],
"blocks": [...],
"sentences": [...]
},
"metadata": {...}
}
Note that Images
are serialized to base64
if you include with_images
flag. Otherwise, it's left out of JSON serialization by default.
7. How can I load my Document
?
These can be used to reconstruct a Document
again via:
with open('filename.json') as f_in:
doc_dict = json.load(f_in)
doc = Document.from_json(doc_dict)
Note: A common pattern for adding fields to a document is to load in a previously saved document, run some additional Predictors
on it, and save the result.
See papermage/predictors/README.md
for more information about training custom predictors on your own data.
See papermage/examples/quick_start_demo.ipynb
for a notebook walking through some more usage patterns.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file papermage-0.15.0.tar.gz
.
File metadata
- Download URL: papermage-0.15.0.tar.gz
- Upload date:
- Size: 86.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ee857dff6c9813f3d734993bf9e73d2f85c3ce06497ad1479df3201ae96a1e2 |
|
MD5 | 38ca950da164012a5c359893fe3573b5 |
|
BLAKE2b-256 | bba961f0b731d2e203bb4b6a2bb0226db6ef798c4e8ab177388d6905ef6a4af7 |
File details
Details for the file papermage-0.15.0-py3-none-any.whl
.
File metadata
- Download URL: papermage-0.15.0-py3-none-any.whl
- Upload date:
- Size: 105.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e379aa5f50dd92b581a9595f6938a89997791afe563ccbc69922d01d2239b643 |
|
MD5 | 7dbc0a21dbb39719ae05eef7abb78407 |
|
BLAKE2b-256 | 4ce48431697e19e82da8926b96d2a5bc21786a0646806a6315557eb41df95016 |