Skip to content

Instantly share code, notes, and snippets.

@Dirga36
Created July 22, 2026 02:45
Show Gist options
  • Select an option

  • Save Dirga36/7037239d8328b4b40e99898fc94a3de3 to your computer and use it in GitHub Desktop.

Select an option

Save Dirga36/7037239d8328b4b40e99898fc94a3de3 to your computer and use it in GitHub Desktop.
Entities get mapped to a metadata schema.
from transformers import pipeline
# Load a pretrained NER model for entity extraction
ner_pipeline = pipeline(
"ner",
model="dslim/bert-base-NER",
aggregation_strategy="simple"
)
def extract_metadata_entities(ocr_text: str) -> dict:
"""Extract candidate people, places, and organizations
from OCR-derived text for a digital library record."""
entities = ner_pipeline(ocr_text)
metadata = {"persons": [], "locations": [], "organizations": []}
label_map = {
"PER": "persons",
"LOC": "locations",
"ORG": "organizations",
}
for entity in entities:
key = label_map.get(entity["entity_group"])
if key and entity["score"] > 0.85:
metadata[key].append(entity["word"])
# Deduplicate while preserving order
for key in metadata:
metadata[key] = list(dict.fromkeys(metadata[key]))
return metadata
sample_text = (
"Correspondence between Marie Curie and the Sorbonne "
"regarding laboratory funding, Paris, 1911."
)
print(extract_metadata_entities(sample_text))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment