Keeping texts¶
Why¶
Reading a PDF’s text takes nearly all the time of a reading, and text recognition takes seconds per page. Judging and parsing the text takes milliseconds. A program that reads many sheets keeps each text and reads from it again when a newer parser arrives, without touching the PDFs.
Store a text and read from it¶
extract_pdf_text reads only the text, read_label_images only the
pictograms the label shows as pictures, which the text does not hold.
extraction_to_dict and label_images_to_dict give them in a form to store
as JSON, extraction_from_dict and label_images_from_dict read that back,
and read_extraction gives the reading read_sheet would give for the
file.
import json
from sdb_reader import (
extract_pdf_text,
extraction_from_dict,
extraction_stage,
extraction_to_dict,
label_images_from_dict,
label_images_to_dict,
read_extraction,
read_label_images,
reading_to_dict,
)
stored = extraction_to_dict(extract_pdf_text(path, max_pages=None))
pictures = label_images_to_dict(read_label_images(path))
kept = json.dumps({"text": stored, "pictures": pictures}) # beside your record
# Later, without the PDF:
both = json.loads(kept)
stored = both["text"]
again = read_extraction(
extraction_from_dict(stored), label_images_from_dict(both["pictures"])
)
assert reading_to_dict(again) == reading_to_dict(reading)
Without the pictures read_extraction has only the pictograms the text
names, and the sheet’s drawing_read is false: nothing then says that a
pictogram is missing.
One thing differs from read_sheet:
extract_pdf_textraisesExtractionErrorfor a file it cannot read;read_sheetcatches that and returns a reading witherrors. The error’smessageattribute is theMessagewith the reason.
Like read_sheet it reads all pages unless max_pages says otherwise.
With ocr=False a PDF without a text layer is not handed to text
recognition: the call fails with the reason no-text-layer instead.
from sdb_reader import ExtractionError
try:
extract_pdf_text(path.with_name("missing.pdf"))
except ExtractionError as error:
assert error.message.code == "pdf-not-found"
A stored text holds the text, how it was read (extractor), how many pages
were read (pages_sampled), the warnings as codes and values, and the stage
it comes from:
assert sorted(stored) == ["extractor", "pages_sampled", "stage", "text", "warnings"]
A text that text recognition read, in whole or for single pages, also holds
tesseract: the version of the program that read it. That version is not
part of the stage, because it concerns scans only and differs from machine to
machine. If the same scan must give the same text everywhere, compare it with
the version on the machine that reads next, and read the PDF again where they
differ.
When what you kept is out of date¶
The library reads in three stages, and each has a version, a whole number that only ever rises:
Name |
Rises when |
What to do |
|---|---|---|
|
the same PDF can give another text |
read the PDF again |
|
the same PDF can give other pictures of its label |
look at the PDF again ( |
|
the same text and pictures can give another reading |
read again from what you stored |
Store all three numbers with every reading you keep, and compare them with the library’s when you start.
The text depends on the PDF library too. extraction_stage() names the
reading stage’s version together with the PDF library’s, and a stored text
carries that value as stage. A stored text holds only while its stage is
the current one:
from sdb_reader import EXTRACTION_VERSION, PARSER_VERSION
assert stored["stage"] == extraction_stage()
assert stored["stage"].startswith(f"{EXTRACTION_VERSION}+pymupdf-")
assert isinstance(PARSER_VERSION, int)
The library’s part of the value is the version of the module pymupdf,
whichever distribution installed it (the package depends on pymupdf-lite).
label_images_stage() does the same for the pictures, which pymupdf draws;
stored pictures carry it as stage:
from sdb_reader import LABEL_IMAGES_VERSION, label_images_stage
assert both["pictures"]["stage"] == label_images_stage()
assert label_images_stage().startswith(f"{LABEL_IMAGES_VERSION}+pymupdf-")
A text of another stage is read all the same; whether to use it is yours to
decide by comparing. A text stored by an earlier version of the library can
always be read back: a warning in a form this version does not know is kept
as the message unknown.
CHANGELOG.md says for every release whether a version rose.