Guide · Rule-based NER
Rule-based NER, with the rules written in plain English
Updated 25 November 2026, by Jungroo AI Labs, the makers of ReDefiNER
Rule-based NER finds entities with rules that people write: regular expressions, token patterns and lists of names, in tools such as spaCy's EntityRuler, Stanford CoreNLP's RegexNER and Presidio's pattern recognizers. It is exact, fast and easy to audit, but every rule has to be a pattern. ReDefiNER keeps the rules and drops the patterns: it is a 0.8B model that reads rules written in plain English and applies them by meaning. It is not a pattern engine, and it can be wrong. Below, the same rules are written both ways and run on the same sentences. We make ReDefiNER; every output on this page is real.
The short answer
- Use patterns when an entity has a fixed format (emails, ID numbers, card numbers with a checksum), when a list of names covers it, or when you need the same answer every time at almost no cost.
- Use ReDefiNER when the rule depends on meaning, such as “patients, not doctors”, when people write the same thing in many ways, or when the people who own the rules don't write regex.
- Use both: patterns for the formats, ReDefiNER for the rest, in one spaCy or Presidio pipeline.
The same rule, written both ways
Task: find customer order numbers such as #D-55102, but not invoice numbers. As spaCy EntityRuler patterns:
import spacy
nlp = spacy.blank("en")
ruler = nlp.add_pipe("entity_ruler")
ruler.add_patterns([
# spaCy splits "#" off "D-55102", so the pattern needs both forms
{"label": "ORDER_ID",
"pattern": [{"TEXT": "#"}, {"TEXT": {"REGEX": r"^[A-Z]-\d{5}$"}}]},
{"label": "ORDER_ID",
"pattern": [{"TEXT": {"REGEX": r"^#[A-Z]-\d{5}$"}}]},
])
As a ReDefiNER rulebook, one type with a description and one rule:
[TYPE] ORDER_ID [DESCRIPTION] A customer's order reference, such as #D-55102. OR1 Invoice numbers, which start with INV-, are not order references.
from redefiner import Tagger
tagger = Tagger.from_pretrained("jungroo-ai/redefiner-0.8b")
tagger.extract(sentences, rulebook) # one list of spans per sentence
Both on the same four sentences:
| Sentence | EntityRuler patterns | ReDefiNER rulebook (confidence) |
|---|---|---|
| Order #D-55102 shipped on 3 May. | #D-55102 | #D-55102 (0.74) |
| Can you check order D55103? It hasn't arrived. | nothing | D55103 (0.98) |
| My order number is d-55104 and the box was damaged. | nothing | d-55104 (0.97) |
| Invoice INV-7731 covers both orders. | nothing | nothing |
The patterns did exactly what they say and nothing more. Catching D55103 and d-55104 takes two more patterns, and the next customer will type it another way. The rulebook found all three from one example in the description, and its rule kept the invoice number out. The other side: 0.74 is a confidence, not a guarantee, and a model can also label something no pattern would.
A rule no pattern can express
Task: find patients' names, but not the names of staff. To a pattern, both are just names. A rule-based system needs a list of every patient, or context patterns such as “the name after examined”, written for each way a sentence can be phrased. As a rulebook:
[TYPE] PATIENT_NAME [DESCRIPTION] The name of a patient. PA1 Doctors, nurses and other staff are not patients. Never label their names.
| Sentence | Labelled PATIENT_NAME |
|---|---|
| Dr. Anita Rao examined Ravi Kumar on 3 May. | Ravi Kumar |
| Nurse Joseph Mathew took Ravi's blood pressure before Dr. Rao arrived. | Ravi |
| Priya Nair, 34, was referred by her GP, Dr. Samuel Lee. | Priya Nair |
Dr. Anita Rao, Nurse Joseph Mathew and Dr. Samuel Lee were left out, and so was “Dr. Rao”. To change the policy, edit the text: there is no training step.
Where patterns are the better tool
- Checksums. A card-number pattern with a Luhn check, as Presidio's recognizers use, rejects numbers that only look right. ReDefiNER reads text; it cannot compute a checksum.
- Guaranteed coverage. A pattern matches every string of its format, every time. ReDefiNER returns a confidence, can miss a span, and can label something a pattern would not.
- Speed. On the Apple M2 laptop that ran these examples, the EntityRuler took about 0.02 milliseconds per sentence. ReDefiNER took about 0.2 seconds per sentence once the rulebook was loaded, roughly 10,000 times slower, and needs about 2 to 2.6 GB of memory.
- Auditing. A pattern is its own explanation. With ReDefiNER you can read the rule it was given, but not prove that it followed it.
- ReDefiNER's limits. English only. Rules that fight a strong prior, such as labelling a person's name as something other than a person, are mostly ignored. When a type's condition isn't clearly met, it tends to label anyway.
Use both in one pipeline
The spacy-redefiner component runs after spaCy's EntityRuler. With overwrite_ents set to false, the patterns keep their matches and ReDefiNER adds the entities they don't cover:
pip install spacy-redefiner
import spacy
nlp = spacy.blank("en")
ruler = nlp.add_pipe("entity_ruler")
ruler.add_patterns([
{"label": "PAN",
"pattern": [{"TEXT": {"REGEX": r"^[A-Z]{5}\d{4}[A-Z]$"}}]},
{"label": "EMAIL", "pattern": [{"LIKE_EMAIL": True}]},
])
# PATIENT_RULES is the rulebook above
nlp.add_pipe("redefiner",
config={"rulebook": PATIENT_RULES, "overwrite_ents": False})
doc = nlp("Ravi Kumar (PAN ABCDE1234F, ravi.k@example.com) "
"was seen by Dr. Anita Rao.")
print([(ent.text, ent.label_) for ent in doc.ents])
# [('Ravi Kumar', 'PATIENT_NAME'), ('ABCDE1234F', 'PAN'),
# ('ravi.k@example.com', 'EMAIL')]
The PAN and the email came from the patterns; the patient's name came from the rulebook, and Dr. Anita Rao was left out. For personal data, the presidio-redefiner recognizer does the same beside Presidio's own pattern recognizers. PII redaction with ReDefiNER.
Which plain-English rules work
- Descriptions matter. With a one-line description per type, the model scores 7 points higher than with type names alone on our open-NER test sets. Always write one.
- Boundary, exclusion and convention rules work. “The span includes the @ of a Twitter handle” took BroadTwitter from 45.5 to 70.6 F1. “Never label Houston, Texas or Harvey” cut those false positives from 389 to 5 on HarveyNER.
- Rules that fight a strong prior don't. Asking it to label common nouns as entities, or a person's name as something else, mostly has no effect. Pronouns are the exception: listed under a type, they are labelled.
- More: How to write rules that work, with templates to copy.