BNF Mets/Alto importers
These importers extend the generic Mets/Alto importer. They handle the OCR newspaper data provided by the BNF, which comes in three formats. Each format has its own canonical classes, and they share an issue-index-based detection module:
BNF-OCR (
no_olr): titles without OLR, made of ALTO pages and an IIIFmanifest.json.MP-OLR (
mp_olr): “Marché Presse” titles with OLR (METS + ALTO).EN-OLR (
en_olr): “Europeana” titles with OLR (METS + ALTO).
See text_preparation/importers/bnf/README.md for the issue indices, the preprocessing
pipeline and the current ingestion status.
BNF-OCR (no OLR) classes
This module contains the definition of BNF OCR-only importer classes.
The classes define newspaper Issues and Pages objects which convert OCR data in the BNF version of the Mets/Alto format to a unified canoncial format. Theses classes are subclasses of generic Mets/Alto importer classes.
- class text_preparation.importers.bnf.no_olr.classes.BnfNewspaperIssue(issue_dir: IssueDir)
Newspaper Issue in BNF format without OLR (Alto).
All functions defined in this child class are specific to parsing BNF OCR-only Alto format.
All OCR-only data is form the new set of API downloaded data, so we know that the format and directory structure is always the same.
- Parameters:
issue_dir (IssueDir) – Identifying information about the issue.
- id
Canonical Issue ID (e.g.
GDL-1900-01-02-a).- Type:
str
- edition
Lower case letter ordering issues of the same day.
- Type:
str
- alias
Newspaper unique alias (identifier or name).
- Type:
str
- path
Path to directory containing the issue’s OCR data.
- Type:
str
- date
Publication date of issue.
- Type:
datetime.date
- issue_data
Issue data according to canonical format.
- Type:
dict[str, Any]
- pages
list of
CanonicalPageinstances from this issue.- Type:
list
- image_properties
metadata allowing to convert region OCR/OLR coordinates to iiif format compliant ones.
- Type:
dict[str, Any]
- ark_id
Issue ARK identifier, for the issue’s pages’ iiif links.
- Type:
int
- issue_uid
Basename of the Mets XML file of this issue.
- Type:
str
- secondary_date
Potential secondary date of issue.
- Type:
datetime.date
- property xml: None
Override the XML property of the parent class since we don’t have METS files.
- class text_preparation.importers.bnf.no_olr.classes.BnfNewspaperPage(_id: str, number: int, filename: str, basedir: str, page_size: tuple[int, int])
Newspaper page in BNF (Mets/Alto) format.
- Parameters:
_id (str) – Canonical page ID.
number (int) – Page number.
filename (str) – Name of the Alto XML page file.
basedir (str) – Base directory where Alto files are located.
- id
Canonical Page ID (e.g.
GDL-1900-01-02-a-p0004).- Type:
str
- number
Page number.
- Type:
int
- page_data
Page data according to canonical format.
- Type:
dict[str, Any]
- issue
Issue this page is from.
- Type:
- filename
Name of the Alto XML page file.
- Type:
str
- basedir
Base directory where Alto files are located.
- Type:
str
- encoding
Encoding of XML file. Defaults to ‘utf-8’.
- Type:
str, optional
- is_gzip
Whether the page’s corresponding file is in .gzip.
- Type:
bool
- ark_link
IIIF Ark identifier for this page.
- Type:
str
- add_issue(issue: MetsAltoCanonicalIssue) None
Add to a page object its parent, i.e. the canonical issue.
This allows each page to preserve contextual information coming from the canonical issue.
- Parameters:
issue (CanonicalIssue) – Canonical issue containing this page.
- parse() None
Process the page XML file and transform into canonical Page format.
Note
This lazy behavior means that the page contents are not processed upon creation of the page object, but only once the
parse()method is called.
- property xml: BeautifulSoup
Read Alto XML file of the page and create a BeautifulSoup object.
This property had to be overridden because this collection presents multiple possible encodings (‘utf-8’ for most, and sometimes “iso-8859-1”)
- Returns:
BeautifulSoup object with Alto XML of the page.
- Return type:
BeautifulSoup
BNF-MP (Marché Presse OLR) classes
This module contains the definition of BNF importer classes for segmented data.
The classes define newspaper Issues and Pages objects which convert OCR and OLR data in the BNF version of the Mets/Alto format to a unified canoncial format. Theses classes are subclasses of generic Mets/Alto importer classes.
- class text_preparation.importers.bnf.mp_olr.classes.BnfMpNewspaperIssue(issue_dir: IssueDir)
Newspaper Issue in BNF “Marché Presse” OLR (Mets/Alto) format.
All functions defined in this child class are specific to parsing BNF’s “Marché Presse” OLR Mets/Alto format.
- Parameters:
issue_dir (IssueDir) – Identifying information about the issue.
- id
Canonical Issue ID (e.g.
GDL-1900-01-02-a).- Type:
str
- edition
Lower case letter ordering issues of the same day.
- Type:
str
- alias
Newspaper unique alias (identifier or name).
- Type:
str
- path
Path to directory containing the issue’s OCR data.
- Type:
str
- date
Publication date of issue.
- Type:
datetime.date
- issue_data
Issue data according to canonical format.
- Type:
dict[str, Any]
- pages
list of
CanonicalPageinstances from this issue.- Type:
list
- image_properties
metadata allowing to convert region OCR/OLR coordinates to iiif format compliant ones.
- Type:
dict[str, Any]
- ark_id
Issue ARK identifier, for the issue’s pages’ iiif links.
- Type:
str
- title_ark_id
Title-level ARK identifier for the newspaper.
- Type:
str
- issue_uid
Basename of the Mets XML file of this issue.
- Type:
str
- secondary_date
Potential secondary date of issue.
- Type:
str
- get_legacy_manifest_info(manifest_path) tuple[dict[int, tuple[int, int]], str]
Read the issue’s legacy manifest.xml (METS) file if present.
Mirrors get_manifest_info() (for the new API’s manifest.json) but for the Impresso I batch’s legacy manifest.xml, which is itself a METS document. Page facsimile dimensions live in <techMD MDTYPE=”NISOIMG”> sections (mix:imageWidth/ mix:imageHeight), each linked to a page’s <file ID=”master.{page_no}”> entry via that file’s ADMID attribute (space-separated list of AMD ids, one of which is the NISOIMG techMD).
Note
Unlike the new manifest.json (which has a “Titre” metadata entry for the media’s variant title), this legacy manifest’s only title-like field (dc:title in DMD.2) is the issue’s own dated title (e.g. “1937-03-05 (Année 0, Numéro 1)”), not a media title variant, so there is no equivalent field to extract here.
- Parameters:
manifest_path (str) – Path to the issue’s manifest.xml file.
- Returns:
- Mapping from page number
to (width, height) in pixels, and an empty title (no equivalent field exists in this format). Empty dict if the file is missing or couldn’t be parsed as expected.
- Return type:
tuple[dict[int, tuple[int, int]], str]
- property xml: BeautifulSoup
Read Mets XML file of the issue and create a BeautifulSoup object.
- Returns:
BeautifulSoup object with Mets XML of the issue.
- Return type:
BeautifulSoup
- class text_preparation.importers.bnf.mp_olr.classes.BnfMpNewspaperPage(_id: str, number: int, filename: str, basedir: str, manifest_dims: tuple[int, int] | None = None)
Newspaper page in BNF “Marché Presse” OLR (Mets/Alto) format.
- Parameters:
_id (str) – Canonical page ID.
number (int) – Page number.
filename (str) – Name of the Alto XML page file.
basedir (str) – Base directory where Alto files are located.
manifest_dims (Optional[tuple[int, int]]) – Facsimile (width, height) of this page as found in the issue’s manifest.json, if any.
- id
Canonical Page ID (e.g.
GDL-1900-01-02-a-p0004).- Type:
str
- number
Page number.
- Type:
int
- page_data
Page data according to canonical format.
- Type:
dict[str, Any]
- issue
Issue this page is from.
- Type:
- filename
Name of the Alto XML page file.
- Type:
str
- basedir
Base directory where Alto files are located.
- Type:
str
- encoding
Encoding of XML file. Defaults to ‘utf-8’.
- Type:
str, optional
- is_gzip
Whether the page’s corresponding file is in .gzip.
- Type:
bool
- page_width
Width in pixels of the page, from the ALTO file.
- Type:
float
- page_height
Height in pixels of the page, from the ALTO file.
- Type:
float
- add_issue(issue: MetsAltoCanonicalIssue) None
Add to a page object its parent, i.e. the canonical issue.
This allows each page to preserve contextual information coming from the canonical issue.
- Parameters:
issue (CanonicalIssue) – Canonical issue containing this page.
- property xml: BeautifulSoup
Read Alto XML file of the page and create a BeautifulSoup object.
Redefined function as for some issues, the pages are in gz format.
- Returns:
BeautifulSoup object with Alto XML of the page.
- Return type:
BeautifulSoup
BNF-EN (Europeana OLR) classes
This module contains the definition of the BNF importer classes for OLR data in the Europeana format.
The classes define newspaper Issues and Pages objects which convert OCR and OLR data in BNF’s “Europeana” version of the Mets/Alto format to a unified canoncial format. Theses classes are subclasses of generic Mets/Alto importer classes.
- class text_preparation.importers.bnf.en_olr.classes.BnfEnNewspaperIssue(issue_dir: IssueDir)
Newspaper Issue in BNF Europeana OLR (Mets/Alto) format.
All functions defined in this child class are specific to parsing BNF-Europeana OLR Mets/Alto format.
- Parameters:
issue_dir (IssueDir) – Identifying information about the issue.
- id
Canonical Issue ID (e.g.
GDL-1900-01-02-a).- Type:
str
- edition
Lower case letter ordering issues of the same day.
- Type:
str
- alias
Newspaper unique alias (identifier or name).
- Type:
str
- path
Path to directory containing the issue’s OCR data.
- Type:
str
- date
Publication date of issue.
- Type:
datetime.date
- issue_data
Issue data according to canonical format.
- Type:
dict[str, Any]
- pages
list of
CanonicalPageinstances from this issue.- Type:
list
- image_properties
metadata allowing to convert region OCR/OLR coordinates to iiif format compliant ones.
- Type:
dict[str, Any]
- ark_id
Issue ARK identifier, for the issue’s pages’ iiif links.
- Type:
str
- title_ark_id
Title-level ARK identifier for the newspaper.
- Type:
str
- secondary_date
Potential secondary date of issue.
- Type:
str
- class text_preparation.importers.bnf.en_olr.classes.BnfEnNewspaperPage(_id: str, number: int, filename: str, basedir: str, manifest_dims: tuple[int, int] | None = None)
Newspaper page in BNF Europeana OLR (Mets/Alto) format.
- Parameters:
_id (str) – Canonical page ID.
number (int) – Page number.
filename (str) – Name of the Alto XML page file.
basedir (str) – Base directory where Alto files are located.
manifest_dims (Optional[tuple[int, int]]) – Facsimile (width, height) of this page as found in the issue’s manifest.json, if any.
- id
Canonical Page ID (e.g.
GDL-1900-01-02-a-p0004).- Type:
str
- number
Page number.
- Type:
int
- page_data
Page data according to canonical format.
- Type:
dict[str, Any]
- issue
Issue this page is from.
- Type:
- filename
Name of the Alto XML page file.
- Type:
str
- basedir
Base directory where Alto files are located.
- Type:
str
- encoding
Encoding of XML file. Defaults to ‘utf-8’.
- Type:
str, optional
- is_gzip
Whether the page’s corresponding file is in .gzip.
- Type:
bool
- page_width
Width in pixels of the page, from the ALTO file.
- Type:
float
- page_height
Height in pixels of the page, from the ALTO file.
- Type:
float
- add_issue(issue: MetsAltoCanonicalIssue) None
Add to a page object its parent, i.e. the canonical issue.
This allows each page to preserve contextual information coming from the canonical issue.
- Parameters:
issue (CanonicalIssue) – Canonical issue containing this page.
BNF Detect functions
This module contains helper functions to find BNF OCR data to import.
- text_preparation.importers.bnf.detect.BnfIssueDir
A light-weight data structure to represent a newspaper issue.
This named tuple contains basic metadata about a newspaper issue. They can then be used to locate the relevant data in the filesystem or to create canonical identifiers for the issue and its pages.
Note
In case of newspaper published multiple times per day, a lowercase letter is used to indicate the edition number: ‘a’ for the first, ‘b’ for the second, etc.
Note
In BNF data, dates can be given in two different formats (separated with - or /). Also, an issue can have two dates, separated by either - or /.
- Parameters:
provider (str) – Provider for this alias, here always “BNF”
alias (str) – Newspaper alias.
date (datetime.date) – Publication date or issue.
edition (str) – Edition of the newspaper issue (‘a’, ‘b’, ‘c’, etc.).
path (str) – Path to the directory containing the issue’s OCR data.
secondary_date (datetime.date) – Secondary publication date or issue.
>>> from datetime import date >>> i = BnfIssueDir( provider='BNF', alias='Marie-Claire', date=datetime.date(1925, 10, 01), edition='a', path="BNF/abendland/1925/10/01/a", ark_id="bpt6k47732053", title_ark="cb343488519", secondary_date = None, )
- text_preparation.importers.bnf.detect.detect_issues(base_dir: str, alias_filter: list[str] | None = None, exclude_list: list[str] | None = None) list[IssueDirectory]
Detect BNF issues to import within the filesystem.
- Parameters:
base_dir (str) – Path to the base directory of newspaper data, this directory should contain directories corresponding to newspaper aliases.
alias_filter (list[str] | None, optional) – Aliases to consider. Defaults to None.
exclude_list (list[str] | None, optional) – Aliases to exclude. Defaults to None.
- Returns:
List of BnfIssueDir instances to import.
- Return type:
list[BnfIssueDir]
- text_preparation.importers.bnf.detect.entry2issue(alias: str, year: str, month: str, entry: dict, base_dir: str, title_ark=None) IssueDirectory
Convert a hierarchical JSON entry into a BnfIssueDir.
- entry example:
{ “day”: “15”, “edition”: “01”, “local_path”: “…_01” }
- text_preparation.importers.bnf.detect.select_issues(base_dir: str, config: dict) list[IssueDirectory] | None
Detect selectively newspaper issues to import.
- Parameters:
base_dir (str) – Path to the base directory of newspaper data, this directory should contain directories corresponding to newspaper aliases.
config (dict) – Configuration dictionary containing ‘aliases’, ‘exclude_aliases’, and ‘year_only’ keys for filtering.
- Returns:
List of BnfIssueDir instances to import.
- Return type:
list[BnfIssueDir] | None
BNF Helper and Parser methods
Set of helper functions for BNF importer
- text_preparation.importers.bnf.helpers.SECTION = 'section'
Content types as defined in BNF Mets flavour. These are the ones we are interested in parsing. The SECTION type should be flattened, and shouldn’t be part of content items, but it is needed to parse what’s inside.
- text_preparation.importers.bnf.helpers.add_div(_dict: dict[str, tuple[str, str]], _type: str, div_id: str, label: str) dict[str, tuple[str, str]]
Adds a div item to the given dictionary (sorted by type).
The types used as keys should be in BNF_CONTENT_TYPES or SECTION.
- Parameters:
_dict (dict[str, tuple[str, str]]) – The dictionary where to add the div
_type (str) – The type of the new div to add.
div_id (str) – The div ID to add.
label (str) – The label of the div to add.
- Returns:
The updated dictionary.
- Return type:
dict[str, tuple[str, str]]
- text_preparation.importers.bnf.helpers.get_dates(date_string: str, separators: list[str]) list[str | None]
Extract date from given string using list of possible separators.
Assumes that the given date string represents exactly 2 dates. Tries to separate them using the given separators, and return when two date were found, otherwise list of None is returned.
- Parameters:
date_string (str) – The date string to separate.
separators (list[str]) – The list of potential separators.
- Returns:
Separated date or pair of None.
- Return type:
list[Optional[str]]
- text_preparation.importers.bnf.helpers.get_journal_name(archive_path: str) str
Return the Journal name from the path of the issue.
It assumes the journal name is one directory above the issue.
- Parameters:
archive_path (str) – Path to the issue’s archive
- Returns:
Extracted journal name in lowercase.
- Return type:
str
- text_preparation.importers.bnf.helpers.get_manifest_info(manifest_path: str) tuple[dict[int, tuple[int, int]], str]
Read the issue’s IIIF presentation manifest.json file if present.
When available, the manifest.json file (fetched from the BNF IIIF presentation API) is the preferred source of the facsimile width/height (in pixels) of each page, matched based on the f{page_number} fragment of each canvas item’s id. Legacy BNF OLR issues (en_olr, mp_olr) currently don’t have this file on disk, in which case callers should fall back to reading each page’s ALTO <Page> tag WIDTH/HEIGHT attributes.
typical item in the manifest: { “id”: “https://openapi.bnf.fr/iiif/presentation/v3/ark:/12148/bpt6k9742578x/f1/canvas”, “type”: “Canvas”, “label”: { “fr”: [“NP”] }, “height”: 6084, “width”: 4584, […]
This function extracts the page dimensions and the newspaper title from the IIIF presentation API metadata.
- Parameters:
issue_path (str) – Path to the directory containing the issue’s data.
- Returns:
- Mapping from page number to
(width, height) in pixels, and extracted title string. Empty if no manifest.json was found in issue_path, or if it couldn’t be parsed as expected.
- Return type:
tuple[dict[int, tuple[int, int]], str]
- text_preparation.importers.bnf.helpers.is_multi_date(date_string: str) bool
Check whether a given date string is composed of more than one date.
This check is based on the assumption that a full date is 10 chars long.
- Parameters:
date_string (str) – Date to check for
- Returns:
True if the string represents multiple dates
- Return type:
bool
- text_preparation.importers.bnf.helpers.parse_date(date_string: str, formats: list[str], separators: list[str]) tuple[~.datetime.date, ~.datetime.date | None]
Parse a date given a list of formats.
The input string can sometimes represent a pair of dates, in which case they are both parsed if possible.
- Parameters:
date_string (str) – Date string to parse.
formats (list[str]) – Possible dates formats.
separators (list[str]) – List of possible date separators.
- Raises:
ValueError – The input date string is too short to be a full date.
ValueError – The string contains two dates that could not be split correctly.
ValueError – The (first) date could not be parsed correctly.
- Returns:
- Parsed date, potentially
parsed pair of dates.
- Return type:
tuple[datetime.date, Optional[datetime.date]]
Utility functions to parse BNF ALTO files.
- text_preparation.importers.bnf.parsers.parse_div_parts(div: Tag) list[dict[str, str | int]]
Parse the parts of a given div element.
Typically, any div of type in BNF_CONTENT_TYPES is composed of child divs. This is what this function parses. Each element of the output contains keys {‘comp_role’, ‘comp_id’, ‘comp_fileid’, ‘comp_page_no’}.
- Parameters:
div (Tag) – Child div to parse.
- Returns:
The list of parts of this Tag.
- Return type:
list[dict[str, str | int]]
- text_preparation.importers.bnf.parsers.parse_embedded_cis(div: Tag, label: str, issue_id: str, parent_id: str | None, counter: int, issue_level_legacy, page_filenames) tuple[list[dict], int]
Parse the div Tags embedded in the given one.
The input div should be of type in BNF_CONTENT_TYPES and should have children of types also in that category. Each child tag represents separate content items, which should thus be processed separately.
- Parameters:
div (Tag) – The parent tag.
label (str) – The label of the parent tag.
issue_id (str) – The ID of the issue.
parent_id (str | None) – The ID of the parent tag (to put into pOf).
counter (int) – Counter for content items.
- Returns:
The embedded CIs and resulting updated counter.
- Return type:
tuple[list[dict], int]
- text_preparation.importers.bnf.parsers.parse_printspace(element: Tag, mappings: dict[str, str]) tuple[list[dict], list[str] | None]
Parse the
<PrintSpace>element of an ALTO XML document for BNF.- Parameters:
element (Tag) – Input XML element (
<PrintSpace>).mappings (Dict[str, str]) – Description of parameter mappings.
- Returns:
- Parsed regions and paragraphs, and
potential notes on issues encountered during the parsing.
- Return type:
tuple[list[dict], list[str] | None]