Sub topic 6
Detecting names and places with NER
Regular expressions work for identifiers with a fixed shape, such as emails and card numbers. Names and places have no fixed shape. "Jordan" can be a person, a country, or a shoe brand. This chapter covers named-entity recognition (NER), the usual way to find these entities. It assumes you know basic Python, can read the (start, end, label) spans from the earlier detector chapters, and have called an LLM API.
What NER does
NER is a statistical model that reads text and marks stretches of it as entities, each with a type. The model uses context, not spelling. In "Jordan signed the lease" it sees a verb that needs a person as its subject. In "She flew to Jordan" it sees a destination.
Common entity types in the English spaCy models:
| spaCy label | Meaning | Use for redaction |
|---|---|---|
| PERSON | People, including fictional ones | Direct identifier |
| GPE | Countries, cities, states | Quasi-identifier |
| LOC | Mountains, bodies of water, regions | Quasi-identifier |
| FAC | Buildings, airports, highways | Quasi-identifier |
| ORG | Companies, agencies | Depends on context |
Other labels such as DATE, MONEY, and CARDINAL are usually not personal data, so you ignore them.
Running spaCy and producing spans
Install the library and a small English model:
# pip install spacy
# python -m spacy download en_core_web_sm
import spacy
nlp = spacy.load("en_core_web_sm")
LABEL_MAP = {
"PERSON": "PERSON",
"GPE": "LOCATION",
"LOC": "LOCATION",
"FAC": "LOCATION",
"ORG": "ORG",
}
def find_entities(text):
doc = nlp(text)
return [
(ent.start_char, ent.end_char, LABEL_MAP[ent.label_])
for ent in doc.ents
if ent.label_ in LABEL_MAP
]
text = "Maria Lopez moved from Madrid to Berlin to work at Siemens."
for start, end, label in find_entities(text):
print(label, repr(text[start:end]))Three things happen in this code:
ent.start_charandent.end_charare character offsets, the same unit your regex detectors return. Spans from both kinds of detector can therefore go into one list.LABEL_MAPcollapses spaCy's labels into your own label set. Downstream code, such as the placeholder step, then seesLOCATIONand not a mix ofGPEandLOC.- Labels not in the map are dropped, which acts as a whitelist.
On this sentence you should typically see Maria Lopez as PERSON, Madrid and Berlin as LOCATION, and Siemens as ORG. Exact output depends on the model version.
Where NER goes wrong
NER returns guesses, not guarantees. These are the failures you will see most often:
- Missed names. Rare names, names from under-represented languages, and lowercase text such as "email from ana lopez" are missed more often.
- False positives. Product names, project codenames, and capitalized words at the start of a sentence get tagged as people or organizations.
- Boundary errors. The model marks "Lopez" but not "Maria Lopez". A partial redaction leaves the first name visible.
- Context loss. Short inputs, such as a chat message that is only a name, give the model little to work with.
- Model size.
en_core_web_smis fast but less accurate thanen_core_web_lgor a transformer model. The trade-off is latency against recall.
Tuning for your use case
Choose which labels count. A support app may need to hide PERSON and LOCATION but leave ORG alone, because company names rarely identify a customer. A legal app may redact all three.
Many NER libraries expose a confidence score, but spaCy's default pipeline does not give one per entity. Presidio, covered later in this tutorial, wraps NER and adds scores, so you can filter low-confidence hits there.
Run NER on the original text, not on text already modified by regex redaction. Placeholders such as <EMAIL_1> change the sentence the model sees and can lower accuracy. The next chapter shows how to merge spans from several detectors, resolve overlaps, and redact once.
Exercise
Your NER detector tags "Paris" in "Paris Hilton signed the contract" as LOCATION and leaves "Hilton" untagged. Name two problems this causes, and one change to the detection step that helps.
Show answer
First, the text "Hilton" remains visible, so part of the person's name leaks. Second, the label is wrong, so a placeholder like <LOCATION1> Hilton misleads both readers and the model. A change that helps is to use a larger model such as encoreweblg or a transformer model, which handle context better. You can also add a rule-based check for known full names, or measure the error on a labelled test set and adjust.