Name extraction rules in natural language processing are hand-written patterns that pick people's names out of text. A rule might say that a capitalised word after "Dr." is a surname, or that any phrase on your customer list is a person. Rules are fast and easy to audit, and they work best on text with a predictable structure, such as forms and email headers.
Free prose is harder. Names collide with ordinary words, and naming conventions change from one language to the next. Most production systems end up pairing a few precise rules with a trained model. Below is how the rules work and where they fail, then how to pair them with a model.
What name extraction rules look like in practice
A name extraction rule is a test applied to a stretch of text: if the words pass, label them as a person. The job belongs to named entity recognition, or NER, the part of NLP that finds and labels named things such as people and places.
Rules come in three families, and real systems usually mix them:
| Rule type | How it works | Example |
|---|---|---|
| Pattern | Matches the shape of the words, such as capitals and titles | "Dr." followed by capitalised words |
| Gazetteer | Looks phrases up in a list of known names | Your customer or employee records |
| Context trigger | Uses nearby words that predict a name | "my name is" or "signed by" |
Gazetteer is a borrowed word. In geography it means a dictionary of place names, and in NLP it means any list of known entities.
In spaCy, a widely used open-source NLP library, the EntityRuler component holds both patterns and gazetteer entries. A phrase pattern is an exact string, and a token pattern describes each word by attributes such as its lowercase form:
import spacy
nlp = spacy.load("en_core_web_sm")
ruler = nlp.add_pipe("entity_ruler", before="ner")
ruler.add_patterns([
# Gazetteer entry: an exact phrase
{"label": "PERSON", "pattern": "Tom Becker"},
# Pattern: a title, then one or more title-case words
{"label": "PERSON", "pattern": [
{"LOWER": {"IN": ["dr", "dr.", "mr", "mr.", "ms", "ms."]}},
{"IS_TITLE": True, "OP": "+"},
]},
])
doc = nlp("Dr. Maria Okafor met Tom Becker on Friday.")
print([(ent.text, ent.label_) for ent in doc.ents])
The before="ner" argument places the rules ahead of spaCy's statistical model, which matters later. Where two matches overlap, the longer wins.
Patterns this tight rarely misfire. Loosen one to "two capitalised words in a row" and it finds a person at the bottom of almost every email: a prolific correspondent named Best Regards.
Natural language processing parameter extraction techniques for names
In many applications the name is a parameter, meaning one value the system needs from a sentence before it can act. "Book a table for Maria Lopez at eight" carries two parameters, a name and a time. Conversational systems call this slot filling.
Parameter extraction suits rules better than open-ended NER, because the system usually knows where to look. Three techniques do most of the work:
- Anchor on the context. In a booking request, the words after "for" or "under the name" are very likely the name.
- Constrain the shape. A name parameter is usually one to four words long and rarely contains a digit.
- Validate against a list. If the name should match an account, check your records and ask a follow-up question when it does not.
Structured documents make rules stronger still. A form field labelled "Full name" or the "From:" line of an email puts the name in a known position. A regular expression anchored on that label is hard for any model to beat on speed and cost.
Where rule-based name extraction breaks
Every rule encodes an assumption about how names look, and every assumption has exceptions. Three kinds cause most of the trouble.
The first is capital letters. English capitalises names, which makes title case a strong signal. German capitalises every noun, so a title-case rule reading German news meets a lot of people named Tisch, the German word for table.
The benchmark numbers show German is harder for everyone. In the CoNLL-2003 shared task, the standard NER benchmark built from Reuters news and the German Frankfurter Rundschau, a majority vote of five top systems scored an F1 of 90.30 on English and 74.17 on German. F1 blends precision, the share of extracted names that are correct, with recall, the share of real names the system found. Weaker capitalisation cues are one reason for the gap.
The second is naming conventions. The W3C guide to personal names around the world documents the exceptions a rule writer has to plan for.
Chinese and Hungarian names, among others, put the family name first. Many people in parts of Southern India and Indonesia have no family name at all. Spanish speakers commonly carry two family names, and particles such as "van der" sit inside a name in lowercase. A rule expecting exactly two title-case words handles none of these.
The third is lookalikes. Many names are also ordinary words.
Jordan is a country and a person, and April is a month and a person. A first-name list will match "Will" in "Will the meeting run late?" Context settles most of these cases, and context is what fixed patterns use worst.
Transformers: state-of-the-art natural language processing for names
A trained NER model learns what names look like from labelled examples, so it can recognise a name it has never seen by reading the words around it.
Training data marks every word with a tag. CoNLL-2003 uses the IOB scheme: I-PER for a word inside a person's name, O for a word outside any entity, and B-PER where a second name starts directly after another. The model learns to predict one tag per token.
Transformer models, the architecture behind modern language models, are now the default for this. Hugging Face describes its open-source library in the paper "HuggingFace's Transformers: State-of-the-art Natural Language Processing", which lists token classification, the task family that includes NER, among its standard model heads. Running a BERT model fine-tuned on CoNLL-2003 takes a few lines:
from transformers import pipeline
ner = pipeline("token-classification", model="dslim/bert-base-NER",
aggregation_strategy="simple")
for ent in ner("Maria Okafor flew from Lagos to meet Tom Becker."):
print(ent["entity_group"], ent["word"])
Large language models can extract names from a plain instruction with no training at all. The 2023 GLiNER paper measured the trade-off on zero-shot NER, meaning entity types and domains the models had not trained on. Its 0.3-billion-parameter encoder averaged an F1 of 60.9 across seven benchmarks, against 47.5 for ChatGPT. Even the 50-million-parameter version scored 52.7.
Those ChatGPT figures come from a 2023 model, so treat the size of the gap as dated. The cost difference is structural: an encoder labels every token in one pass, while an LLM writes its answer one token at a time. An LLM is the colleague who will take on any job you describe, then bill you by the word.
LLM Systems Engineering explains how encoders such as BERT differ from the decoder models behind chat assistants, building up from tokens and embeddings.
Combining name extraction rules with a model
Rules handle the names you already know, and the model handles the ones you do not.
Lists kept their value as machine learning took over. In CoNLL-2003, eleven of the sixteen teams used information beyond the supplied training data, and every one of those eleven included gazetteers.
In spaCy, the order of components decides who wins a disagreement. With the EntityRuler before the ner component, rule matches are fixed and the model predicts around them. Placed after, the rules only add names the model missed, unless you set overwrite_ents=True.
Put an authoritative list, such as your own customer records, first. Put a noisy list, such as common first names, after the model.
Whichever mix you choose, measure it on a few hundred hand-labelled sentences from your own data. Report precision and recall separately, since a new rule usually raises recall and can quietly cost precision. Our guide to the classification report in deep learning shows how both scores are calculated.
Names are personal data under laws such as the GDPR. If the text holds customer records, run extraction where the data already lives. A small encoder model, or a local LLM running on your own machine, keeps every name off third-party servers.
If you hand the long tail to an LLM, fix its output to a JSON list and test the prompt the way you test code. AI Prompt Engineering covers building prompts that hold up as a maintained system.
A checklist for your first name extractor
- Extract from labelled fields and headers with anchored rules before touching free text.
- Add a gazetteer of the names you already hold, with an ID on each entry so spelling variants link to one person.
- Run a pretrained transformer NER model over the free text.
- Label 200 sentences from your own data and measure precision and recall.
- Write a new rule only for an error you have seen at least twice.
Then put Best Regards on the stop list on day one. Whoever they are, they are not a customer.